subscription view finished

This commit is contained in:
2024-03-09 02:56:56 +01:00
Unverified
parent 70fdc10f50
commit 38475a96ad
17 changed files with 439 additions and 31 deletions

View File

@@ -117,7 +117,22 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="DuplicateError" xml:space="preserve">
<value>Playlist has been already added</value>
</data>
<data name="ErrorInfoBar.Title" xml:space="preserve">
<value>Error</value>
</data>
<data name="Header.Text" xml:space="preserve">
<value>Subscriptions</value>
</data>
<data name="PlaylistSearchButton.Content" xml:space="preserve">
<value>Add</value>
</data>
<data name="PlaylistUrlTextBox.PlaceholderText" xml:space="preserve">
<value>Playlist URL</value>
</data>
<data name="RemovePlaylistButton.Content" xml:space="preserve">
<value>Remove</value>
</data>
</root>

View File

@@ -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
}
}

View File

@@ -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<PlaylistViewModel> _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<PlaylistViewModel>();
_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
}
}

View File

@@ -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">
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="Loaded">
<ic:InvokeCommandAction Command="{Binding NavigationCommand}"/>
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
<Grid>
<TextBlock Text="AAAAAAAAAA"/>
<Grid Padding="20"
RowSpacing="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Uid="/VDownload.Core.Strings/SubscriptionsViewResources/Header"
Grid.Row="0"
FontSize="28"
FontWeight="SemiBold"/>
<ScrollViewer Grid.Row="1">
<ItemsControl ItemsSource="{Binding Playlists}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ctuc:StaggeredPanel ColumnSpacing="10"
DesiredColumnWidth="120"
RowSpacing="10"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid RowSpacing="10"
Padding="10"
Background="{ThemeResource PanelBackgroundColor}"
CornerRadius="10">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Grid.Row="0">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding="{Binding Source, Converter={StaticResource ObjectToStringConverter}}"
Value="Twitch">
<ic:ChangePropertyAction PropertyName="Source"
Value="{StaticResource ImageSourcesTwitch}"/>
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
</Image>
<TextBlock Grid.Row="1"
Text="{Binding Name}"
HorizontalAlignment="Center"/>
<HyperlinkButton x:Uid="/VDownload.Core.Strings/SubscriptionsViewResources/RemovePlaylistButton"
Grid.Row="2"
Content="Remove"
HorizontalAlignment="Center"
Command="{Binding DataContext.RemovePlaylistCommand, ElementName=Root}"
CommandParameter="{Binding}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<ctuc:SwitchPresenter Grid.Row="2"
Value="{Binding Error, Converter={StaticResource IsNotNullConverter}}">
<ctuc:Case Value="False">
<Grid Background="{ThemeResource PanelBackgroundColor}"
Padding="10"
ColumnSpacing="10"
CornerRadius="10">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox x:Uid="/VDownload.Core.Strings/SubscriptionsViewResources/PlaylistUrlTextBox"
Grid.Column="0"
Text="{Binding Url, Mode=TwoWay}"/>
<ctuc:SwitchPresenter Grid.Column="1"
Value="{Binding Loading, Converter={StaticResource ObjectToStringConverter}}">
<ctuc:Case Value="True">
<ProgressRing IsIndeterminate="True"
Width="20"
Height="20"/>
</ctuc:Case>
<ctuc:Case Value="False">
<Button x:Uid="/VDownload.Core.Strings/SubscriptionsViewResources/PlaylistSearchButton"
Command="{Binding AddCommand}"/>
</ctuc:Case>
</ctuc:SwitchPresenter>
</Grid>
</ctuc:Case>
<ctuc:Case Value="True">
<InfoBar x:Uid="/VDownload.Core.Strings/SubscriptionsViewResources/ErrorInfoBar"
Severity="Error"
IsOpen="{Binding IsErrorOpened, Mode=TwoWay}"
Message="{Binding Error}"
CloseButtonCommand="{Binding CloseErrorCommand}"/>
</ctuc:Case>
</ctuc:SwitchPresenter>
</Grid>
</Page>

View File

@@ -12,6 +12,7 @@ namespace VDownload.Models
public required string Description { get; set; }
public required Uri Url { get; set; }
public Source Source { get; protected set; }
#endregion
}

View File

@@ -8,7 +8,6 @@ namespace VDownload.Models
{
public enum Source
{
Twitch,
Subscriptions
Twitch
}
}

View File

@@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VDownload.Models
{
public class SubscriptionList : VideoCollection
{
#region CONSTRUCTORS
public SubscriptionList()
{
Source = Source.Subscriptions;
}
#endregion
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VDownload.Models
{
public class SubscriptionsVideoList : VideoCollection
{
}
}

View File

@@ -11,7 +11,6 @@ namespace VDownload.Models
#region PROPERTIES
public required string Name { get; init; }
public Source Source { get; protected set; }
#endregion
}

View File

@@ -0,0 +1,32 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VDownload.Models;
namespace VDownload.Services.Data.Subscriptions
{
public class Subscription
{
#region PROPERTIES
[JsonProperty("guid")]
public Guid Guid { get; private set; } = Guid.NewGuid();
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("source")]
public Source Source { get; set; }
[JsonProperty("url")]
public Uri Url { get; set; }
[JsonProperty("video_ids")]
public ICollection<string> VideoIds { get; private set; } = new List<string>();
#endregion
}
}

View File

@@ -1,12 +1,97 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VDownload.Services.Data.Configuration;
namespace VDownload.Services.Data.Subscriptions
{
public class SubscriptionsDataService
public interface ISubscriptionsDataService
{
#region PROPERTIES
ICollection<Subscription> Data { get; }
#endregion
#region METHODS
Task Load();
Task Save();
#endregion
}
public class SubscriptionsDataService : ISubscriptionsDataService
{
#region SERVICES
protected readonly IConfigurationService _configurationService;
#endregion
#region FIELDS
protected readonly string _filePath;
#endregion
#region PROPERTIES
public ICollection<Subscription> Data { get; private set; }
#endregion
#region CONSTRUCTORS
public SubscriptionsDataService(IConfigurationService configurationService)
{
_configurationService = configurationService;
string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appdataDirectoryName = _configurationService.Common.Path.Appdata.DirectoryName;
string appdataAuthenticationFilename = _configurationService.Common.Path.Appdata.SubscriptionsFile;
_filePath = Path.Combine(appdataPath, appdataDirectoryName, appdataAuthenticationFilename);
}
#endregion
#region PUBLIC METHODS
public async Task Load()
{
if (File.Exists(_filePath))
{
string content = await File.ReadAllTextAsync(_filePath);
Data = JsonConvert.DeserializeObject<ICollection<Subscription>>(content);
}
else
{
Data = new List<Subscription>();
}
}
public async Task Save()
{
Directory.CreateDirectory(Path.GetDirectoryName(_filePath));
string content = JsonConvert.SerializeObject(Data);
await File.WriteAllTextAsync(_filePath, content);
}
#endregion
}
}

View File

@@ -6,4 +6,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\VDownload.Models\VDownload.Models.csproj" />
<ProjectReference Include="..\VDownload.Services.Data.Configuration\VDownload.Services.Data.Configuration.csproj" />
</ItemGroup>
</Project>

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\..\VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Configuration\VDownload.Services.Data.Configuration.csproj" />
<ProjectReference Include="..\..\VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Subscriptions\VDownload.Services.Data.Subscriptions.csproj" />
<ProjectReference Include="..\VDownload.Sources.Common\VDownload.Sources.Common.csproj" />
<ProjectReference Include="..\VDownload.Sources.Twitch\VDownload.Sources.Twitch\VDownload.Sources.Twitch.csproj" />
</ItemGroup>

View File

@@ -23,6 +23,7 @@ using VDownload.Services.Data.Authentication;
using VDownload.Services.Data.Configuration;
using VDownload.Services.Data.Configuration.Models;
using VDownload.Services.Data.Settings;
using VDownload.Services.Data.Subscriptions;
using VDownload.Services.UI.Dialogs;
using VDownload.Services.UI.DictionaryResources;
using VDownload.Services.UI.Notifications;
@@ -110,6 +111,7 @@ namespace VDownload
services.AddSingleton<IAuthenticationDataService, AuthenticationDataService>();
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<IApplicationDataService, ApplicationDataService>();
services.AddSingleton<ISubscriptionsDataService, SubscriptionsDataService>();
}
protected void BuildUIServices(IServiceCollection services)
@@ -189,7 +191,8 @@ namespace VDownload
IApplicationDataService applicationDataService = _serviceProvider.GetService<IApplicationDataService>();
ISettingsService settingsService = _serviceProvider.GetService<ISettingsService>();
IAuthenticationDataService authenticationDataService = _serviceProvider.GetService<IAuthenticationDataService>();
await Task.WhenAll(applicationDataService.Load(), settingsService.Load(), authenticationDataService.Load());
ISubscriptionsDataService subscriptionsDataService = _serviceProvider.GetService<ISubscriptionsDataService>();
await Task.WhenAll(applicationDataService.Load(), settingsService.Load(), authenticationDataService.Load(), subscriptionsDataService.Load());
}
protected void AssignStaticProperties()

View File

@@ -6,10 +6,12 @@
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="ViewBackgroundColor" Color="#F5F5F5"/>
<SolidColorBrush x:Key="PanelBackgroundColor" Color="#F5F5F5"/>
<SolidColorBrush x:Key="OptionBarBackgroundColor" Color="#F5F5F5"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="ViewBackgroundColor" Color="#242424"/>
<SolidColorBrush x:Key="PanelBackgroundColor" Color="#2F2F2F"/>
<SolidColorBrush x:Key="OptionBarBackgroundColor" Color="#1B1B1B"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

View File

@@ -13,4 +13,7 @@
<ct:StringFormatConverter x:Key="StringFormatConverter"/>
<ct:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<ct:BoolNegationConverter x:Key="BoolNegationConverter"/>
<ct:EmptyObjectToObjectConverter x:Key="IsNotNullConverter"
EmptyValue="False"
NotEmptyValue="True"/>
</ResourceDictionary>

View File

@@ -192,6 +192,7 @@
<ProjectReference Include="..\VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Authentication\VDownload.Services.Data.Authentication.csproj" />
<ProjectReference Include="..\VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Configuration\VDownload.Services.Data.Configuration.csproj" />
<ProjectReference Include="..\VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Settings\VDownload.Services.Data.Settings.csproj" />
<ProjectReference Include="..\VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Subscriptions\VDownload.Services.Data.Subscriptions.csproj" />
<ProjectReference Include="..\VDownload.Services\VDownload.Services.UI\VDownload.Services.UI.Dialogs\VDownload.Services.UI.Dialogs.csproj" />
<ProjectReference Include="..\VDownload.Services\VDownload.Services.UI\VDownload.Services.UI.DictionaryResources\VDownload.Services.UI.DictionaryResources.csproj" />
<ProjectReference Include="..\VDownload.Services\VDownload.Services.UI\VDownload.Services.UI.Notifications\VDownload.Services.UI.Notifications.csproj" />