From 38475a96adb4b1382d80fe2f2cc5ec98dd6f59f1 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sat, 9 Mar 2024 02:56:56 +0100 Subject: [PATCH] subscription view finished --- .../en-US/SubscriptionsViewResources.resw | 15 ++ .../Helpers/PlaylistViewModel.cs | 39 ++++++ .../Subscriptions/SubscriptionsViewModel.cs | 132 +++++++++++++++++- .../Subscriptions/SubscriptionsView.xaml | 105 +++++++++++++- VDownload.Models/Playlist.cs | 1 + VDownload.Models/Source.cs | 3 +- VDownload.Models/SubscriptionList.cs | 20 --- VDownload.Models/SubscriptionsVideoList.cs | 12 ++ VDownload.Models/VideoCollection.cs | 1 - .../Subscription.cs | 32 +++++ .../SubscriptionsDataService.cs | 89 +++++++++++- ...ownload.Services.Data.Subscriptions.csproj | 9 ++ .../VDownload.Sources.csproj | 1 + VDownload/App.xaml.cs | 5 +- VDownload/Dictionaries/Colors.xaml | 2 + VDownload/Dictionaries/Converters.xaml | 3 + VDownload/VDownload.csproj | 1 + 17 files changed, 439 insertions(+), 31 deletions(-) create mode 100644 VDownload.Core/VDownload.Core.ViewModels/Subscriptions/Helpers/PlaylistViewModel.cs delete mode 100644 VDownload.Models/SubscriptionList.cs create mode 100644 VDownload.Models/SubscriptionsVideoList.cs create mode 100644 VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/Subscription.cs diff --git a/VDownload.Core/VDownload.Core.Strings/Strings/en-US/SubscriptionsViewResources.resw b/VDownload.Core/VDownload.Core.Strings/Strings/en-US/SubscriptionsViewResources.resw index 8590552..1e97e5b 100644 --- a/VDownload.Core/VDownload.Core.Strings/Strings/en-US/SubscriptionsViewResources.resw +++ b/VDownload.Core/VDownload.Core.Strings/Strings/en-US/SubscriptionsViewResources.resw @@ -117,7 +117,22 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Playlist has been already added + + + Error + Subscriptions + + Add + + + Playlist URL + + + Remove + \ No newline at end of file diff --git a/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/Helpers/PlaylistViewModel.cs b/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/Helpers/PlaylistViewModel.cs new file mode 100644 index 0000000..8f77732 --- /dev/null +++ b/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/Helpers/PlaylistViewModel.cs @@ -0,0 +1,39 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VDownload.Models; + +namespace VDownload.Core.ViewModels.Subscriptions.Helpers +{ + public partial class PlaylistViewModel : ObservableObject + { + #region PROPERTIES + + [ObservableProperty] + protected Guid _guid; + + [ObservableProperty] + protected string _name; + + [ObservableProperty] + protected Source _source; + + #endregion + + + + #region CONSTRUCTORS + + public PlaylistViewModel(string name, Source source, Guid guid) + { + _name = name; + _source = source; + _guid = guid; + } + + #endregion + } +} diff --git a/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/SubscriptionsViewModel.cs b/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/SubscriptionsViewModel.cs index 663b143..921b770 100644 --- a/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/SubscriptionsViewModel.cs +++ b/VDownload.Core/VDownload.Core.ViewModels/Subscriptions/SubscriptionsViewModel.cs @@ -1,13 +1,141 @@ -using CommunityToolkit.Mvvm.ComponentModel; +using ABI.System; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; +using VDownload.Core.ViewModels.Subscriptions.Helpers; +using VDownload.Models; +using VDownload.Services.Data.Subscriptions; +using VDownload.Services.UI.StringResources; +using VDownload.Sources; +using VDownload.Sources.Common; namespace VDownload.Core.ViewModels.Subscriptions { - public class SubscriptionsViewModel : ObservableObject + public partial class SubscriptionsViewModel : ObservableObject { + #region SERVICES + + protected readonly ISearchService _searchService; + protected readonly IStringResourcesService _stringResourcesService; + protected readonly ISubscriptionsDataService _subscriptionsDataService; + + #endregion + + + + #region PROPERTIES + + [ObservableProperty] + protected ObservableCollection _playlists; + + [ObservableProperty] + protected string _url; + + [ObservableProperty] + protected bool _loading; + + [ObservableProperty] + protected string _error; + + [ObservableProperty] + protected bool _isErrorOpened; + + #endregion + + + + #region CONSTRUCTORS + + public SubscriptionsViewModel(ISearchService searchService, IStringResourcesService stringResourcesService, ISubscriptionsDataService subscriptionsDataService) + { + _searchService = searchService; + _stringResourcesService = stringResourcesService; + _subscriptionsDataService = subscriptionsDataService; + + _playlists = new ObservableCollection(); + _loading = false; + _isErrorOpened = true; + _error = null; + } + + #endregion + + + + #region COMMANDS + + [RelayCommand] + public void Navigation() + { + Playlists.Clear(); + foreach (Subscription sub in _subscriptionsDataService.Data) + { + Playlists.Add(new PlaylistViewModel(sub.Name, sub.Source, sub.Guid)); + } + } + + [RelayCommand] + public async Task RemovePlaylist(PlaylistViewModel playlist) + { + Playlists.Remove(playlist); + + Subscription sub = _subscriptionsDataService.Data.FirstOrDefault(x => x.Guid == playlist.Guid); + _subscriptionsDataService.Data.Remove(sub); + await _subscriptionsDataService.Save(); + } + + [RelayCommand] + public async Task Add() + { + Loading = true; + + Playlist playlist; + try + { + playlist = await _searchService.SearchPlaylist(Url, 0); + } + catch (MediaSearchException ex) + { + Error = _stringResourcesService.SearchResources.Get(ex.StringCode); + Loading = false; + return; + } + + + if (_subscriptionsDataService.Data.Any(x => x.Source == playlist.Source && x.Name == playlist.Name)) + { + Error = _stringResourcesService.SubscriptionsViewResources.Get("DuplicateError"); + Loading = false; + return; + } + + Subscription subscription = new Subscription + { + Name = playlist.Name, + Source = playlist.Source, + Url = playlist.Url, + }; + playlist.ForEach(x => subscription.VideoIds.Add(x.Id)); + + _subscriptionsDataService.Data.Add(subscription); + await _subscriptionsDataService.Save(); + Playlists.Add(new PlaylistViewModel(subscription.Name, subscription.Source, subscription.Guid)); + + Loading = false; + } + + [RelayCommand] + public void CloseError() + { + Error = null; + IsErrorOpened = true; + } + + #endregion } } diff --git a/VDownload.Core/VDownload.Core.Views/Subscriptions/SubscriptionsView.xaml b/VDownload.Core/VDownload.Core.Views/Subscriptions/SubscriptionsView.xaml index 386316e..2827059 100644 --- a/VDownload.Core/VDownload.Core.Views/Subscriptions/SubscriptionsView.xaml +++ b/VDownload.Core/VDownload.Core.Views/Subscriptions/SubscriptionsView.xaml @@ -6,10 +6,109 @@ xmlns:local="using:VDownload.Core.Views.Subscriptions" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:ctuc="using:CommunityToolkit.WinUI.UI.Controls" + xmlns:ctc="using:CommunityToolkit.WinUI.Controls" + xmlns:ct="using:CommunityToolkit.WinUI" + xmlns:i="using:Microsoft.Xaml.Interactivity" + xmlns:ic="using:Microsoft.Xaml.Interactions.Core" mc:Ignorable="d" - Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> + Background="{ThemeResource ViewBackgroundColor}" + x:Name="Root"> + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +