Files

162 lines
4.3 KiB
C#
Raw Permalink Normal View History

using CommunityToolkit.Mvvm.ComponentModel;
2024-03-09 02:56:56 +01:00
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.Helpers;
2024-03-08 20:48:31 +01:00
using System;
using System.Collections.Generic;
2024-03-09 02:56:56 +01:00
using System.Collections.ObjectModel;
using System.Diagnostics;
2024-03-08 20:48:31 +01:00
using System.Linq;
using System.Net.Http;
2024-03-08 20:48:31 +01:00
using System.Text;
using System.Threading.Tasks;
2024-03-15 12:54:21 +01:00
using VDownload.Core.Strings;
2024-03-09 02:56:56 +01:00
using VDownload.Core.ViewModels.Subscriptions.Helpers;
using VDownload.Models;
using VDownload.Services.Data.Subscriptions;
using VDownload.Sources;
using VDownload.Sources.Common;
2024-03-08 20:48:31 +01:00
namespace VDownload.Core.ViewModels.Subscriptions
{
2024-03-09 02:56:56 +01:00
public partial class SubscriptionsViewModel : ObservableObject
2024-03-08 20:48:31 +01:00
{
2024-03-09 02:56:56 +01:00
#region SERVICES
protected readonly ISearchService _searchService;
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
2024-03-15 12:54:21 +01:00
public SubscriptionsViewModel(ISearchService searchService, ISubscriptionsDataService subscriptionsDataService)
2024-03-09 02:56:56 +01:00
{
_searchService = searchService;
_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()
{
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
2024-03-15 12:54:21 +01:00
ShowError(StringResourcesManager.SubscriptionsView.Get("NoInternetConnectionError"));
return;
}
2024-03-09 02:56:56 +01:00
Loading = true;
Playlist playlist;
try
{
playlist = await _searchService.SearchPlaylist(Url, 0);
}
catch (MediaSearchException ex)
{
2024-03-15 12:54:21 +01:00
ShowError(StringResourcesManager.Search.Get(ex.StringCode));
return;
}
catch (Exception ex) when (ex is TaskCanceledException || ex is HttpRequestException)
{
2024-03-15 12:54:21 +01:00
ShowError(StringResourcesManager.Search.Get("SearchTimeout"));
2024-03-09 02:56:56 +01:00
return;
}
if (_subscriptionsDataService.Data.Any(x => x.Source == playlist.Source && x.Name == playlist.Name))
{
2024-03-15 12:54:21 +01:00
ShowError(StringResourcesManager.SubscriptionsView.Get("DuplicateError"));
2024-03-09 02:56:56 +01:00
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
#region PRIVATE METHODS
protected void ShowError(string message)
{
Error = message;
Loading = false;
}
#endregion
2024-03-08 20:48:31 +01:00
}
}