Files
VDownload/VDownload.Core/VDownload.Core.ViewModels/Home/HomeViewModel.cs

362 lines
12 KiB
C#
Raw Normal View History

2024-02-13 02:59:40 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.Helpers;
2024-02-13 02:59:40 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-02-28 01:22:13 +01:00
using VDownload.Core.Tasks;
using VDownload.Models;
using VDownload.Services.Data.Configuration;
using VDownload.Services.Data.Settings;
2024-03-09 20:15:19 +01:00
using VDownload.Services.Data.Subscriptions;
2024-03-04 00:28:32 +01:00
using VDownload.Services.UI.Dialogs;
using VDownload.Services.UI.StringResources;
using VDownload.Sources;
using VDownload.Sources.Common;
2024-03-04 00:28:32 +01:00
using VDownload.Sources.Twitch.Configuration.Models;
namespace VDownload.Core.ViewModels.Home
2024-02-13 02:59:40 +01:00
{
public partial class HomeViewModel : ObservableObject
{
#region ENUMS
public enum OptionBarContentType
{
None,
VideoSearch,
PlaylistSearch
}
public enum MainContentType
{
Downloads,
Video
}
#endregion
#region SERVICES
protected readonly IConfigurationService _configurationService;
protected readonly ISettingsService _settingsService;
protected readonly IStringResourcesService _stringResourcesService;
protected readonly ISearchService _searchService;
2024-03-09 20:15:19 +01:00
protected readonly ISubscriptionsDataService _subscriptionsDataService;
2024-03-04 00:28:32 +01:00
protected readonly IDialogsService _dialogsService;
2024-02-13 02:59:40 +01:00
2024-02-28 01:22:13 +01:00
protected readonly IDownloadTaskManager _downloadTaskManager;
protected readonly HomeVideoViewModel _videoViewModel;
2024-03-08 00:34:21 +01:00
protected readonly HomeVideoCollectionViewModel _videoCollectionViewModel;
2024-02-13 02:59:40 +01:00
#endregion
2024-02-13 02:59:40 +01:00
#region FIELDS
2024-02-13 02:59:40 +01:00
protected readonly Type _downloadsView = typeof(HomeDownloadsViewModel);
protected readonly Type _videoView = typeof(HomeVideoViewModel);
2024-03-08 00:34:21 +01:00
protected readonly Type _videoCollectionView = typeof(HomeVideoCollectionViewModel);
2024-02-13 02:59:40 +01:00
#endregion
2024-02-13 02:59:40 +01:00
#region PROPERTIES
2024-02-13 02:59:40 +01:00
[ObservableProperty]
private Type _mainContent;
2024-02-13 02:59:40 +01:00
2024-03-09 21:53:30 +01:00
[ObservableProperty]
private string _optionBarError;
[ObservableProperty]
private bool _optionBarIsErrorOpened;
2024-02-13 02:59:40 +01:00
[ObservableProperty]
private OptionBarContentType _optionBarContent;
[ObservableProperty]
private string _optionBarMessage;
[ObservableProperty]
2024-03-09 21:53:30 +01:00
private bool _optionBarLoading;
2024-02-13 02:59:40 +01:00
2024-03-09 20:15:19 +01:00
[ObservableProperty]
private bool _optionBarLoadSubscriptionButtonChecked;
2024-02-13 02:59:40 +01:00
[ObservableProperty]
private bool _optionBarVideoSearchButtonChecked;
[ObservableProperty]
private bool _optionBarPlaylistSearchButtonChecked;
[ObservableProperty]
private bool _optionBarSearchNotPending;
[ObservableProperty]
private string _optionBarVideoSearchTBValue;
[ObservableProperty]
private string _optionBarPlaylistSearchTBValue;
[ObservableProperty]
private int _optionBarPlaylistSearchNBValue;
#endregion
#region CONSTRUCTORS
2024-03-09 20:15:19 +01:00
public HomeViewModel(IConfigurationService configurationService, ISettingsService settingsService, IStringResourcesService stringResourcesService, ISearchService searchService, ISubscriptionsDataService subscriptionsDataService, IDialogsService dialogsService, IDownloadTaskManager downloadTaskManager, HomeVideoViewModel videoViewModel, HomeVideoCollectionViewModel videoCollectionViewModel)
2024-02-13 02:59:40 +01:00
{
_configurationService = configurationService;
_settingsService = settingsService;
_stringResourcesService = stringResourcesService;
2024-02-13 02:59:40 +01:00
_searchService = searchService;
2024-03-09 20:15:19 +01:00
_subscriptionsDataService = subscriptionsDataService;
2024-03-04 00:28:32 +01:00
_dialogsService = dialogsService;
2024-02-13 02:59:40 +01:00
2024-02-28 01:22:13 +01:00
_downloadTaskManager = downloadTaskManager;
_videoViewModel = videoViewModel;
2024-02-28 01:22:13 +01:00
_videoViewModel.CloseRequested += BackToDownload_EventHandler;
2024-03-08 00:34:21 +01:00
_videoCollectionViewModel = videoCollectionViewModel;
_videoCollectionViewModel.CloseRequested += BackToDownload_EventHandler;
2024-02-13 02:59:40 +01:00
}
#endregion
#region COMMANDS
[RelayCommand]
public async Task Navigation()
2024-02-13 02:59:40 +01:00
{
await _settingsService.Load();
MainContent = _downloadsView;
2024-02-13 02:59:40 +01:00
OptionBarContent = OptionBarContentType.None;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-02-13 02:59:40 +01:00
OptionBarMessage = null;
2024-03-09 21:53:30 +01:00
OptionBarError = null;
OptionBarIsErrorOpened = true;
2024-03-09 20:15:19 +01:00
OptionBarLoadSubscriptionButtonChecked = false;
2024-02-13 02:59:40 +01:00
OptionBarVideoSearchButtonChecked = false;
OptionBarPlaylistSearchButtonChecked = false;
OptionBarSearchNotPending = true;
OptionBarVideoSearchTBValue = string.Empty;
OptionBarPlaylistSearchNBValue = _settingsService.Data.Common.Searching.MaxNumberOfVideosToGetFromPlaylist;
2024-02-13 02:59:40 +01:00
OptionBarPlaylistSearchTBValue = string.Empty;
}
[RelayCommand]
2024-03-09 20:15:19 +01:00
public async Task LoadFromSubscription()
2024-02-13 02:59:40 +01:00
{
MainContent = _downloadsView;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-03-09 20:15:19 +01:00
OptionBarMessage = null;
2024-02-13 02:59:40 +01:00
2024-03-09 20:15:19 +01:00
if (OptionBarLoadSubscriptionButtonChecked)
{
OptionBarContent = OptionBarContentType.None;
OptionBarVideoSearchButtonChecked = false;
OptionBarPlaylistSearchButtonChecked = false;
2024-02-13 02:59:40 +01:00
2024-03-09 20:15:19 +01:00
OptionBarSearchNotPending = false;
2024-03-09 21:53:30 +01:00
OptionBarLoading = true;
2024-03-09 20:15:19 +01:00
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
SubscriptionsVideoList subList = new SubscriptionsVideoList { Name = _stringResourcesService.CommonResources.Get("SubscriptionVideoListName") };
List<Task> tasks = new List<Task>();
foreach (Subscription sub in _subscriptionsDataService.Data.ToArray())
{
tasks.Add(Task.Run(async () =>
{
Playlist playlist;
try
{
playlist = await _searchService.SearchPlaylist(sub.Url.OriginalString, 0);
}
catch (MediaSearchException)
{
return;
}
IEnumerable<Video> newIds = playlist.Where(x => !sub.VideoIds.Contains(x.Id));
subList.AddRange(newIds);
foreach (Video video in newIds)
{
sub.VideoIds.Add(video.Id);
}
}));
}
await Task.WhenAll(tasks);
await _subscriptionsDataService.Save();
if (subList.Count > 0)
{
OptionBarMessage = $"{_stringResourcesService.HomeViewResources.Get("OptionBarMessageVideosFound")} {subList.Count}";
_videoCollectionViewModel.LoadCollection(subList);
MainContent = _videoCollectionView;
}
else
{
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageVideosNotFound");
}
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-03-09 20:15:19 +01:00
}
2024-02-13 02:59:40 +01:00
}
[RelayCommand]
public void VideoSearchShow()
{
2024-03-03 19:00:38 +01:00
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-03-03 19:00:38 +01:00
OptionBarMessage = null;
MainContent = _downloadsView;
2024-02-13 02:59:40 +01:00
if (OptionBarContent != OptionBarContentType.VideoSearch)
{
OptionBarContent = OptionBarContentType.VideoSearch;
OptionBarPlaylistSearchButtonChecked = false;
2024-03-09 20:15:19 +01:00
OptionBarLoadSubscriptionButtonChecked = false;
2024-02-13 02:59:40 +01:00
}
else
{
OptionBarContent = OptionBarContentType.None;
}
}
[RelayCommand]
public void PlaylistSearchShow()
{
2024-03-03 19:00:38 +01:00
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-03-03 19:00:38 +01:00
OptionBarMessage = null;
MainContent = _downloadsView;
2024-02-13 02:59:40 +01:00
if (OptionBarContent != OptionBarContentType.PlaylistSearch)
{
OptionBarContent = OptionBarContentType.PlaylistSearch;
OptionBarVideoSearchButtonChecked = false;
2024-03-09 20:15:19 +01:00
OptionBarLoadSubscriptionButtonChecked = false;
2024-02-13 02:59:40 +01:00
}
else
{
OptionBarContent = OptionBarContentType.None;
}
}
[RelayCommand]
public async Task VideoSearchStart()
{
OptionBarSearchNotPending = false;
2024-03-09 21:53:30 +01:00
OptionBarLoading = true;
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
2024-02-13 02:59:40 +01:00
Video video;
try
{
video = await _searchService.SearchVideo(OptionBarVideoSearchTBValue);
}
catch (MediaSearchException ex)
{
2024-03-09 21:53:30 +01:00
OptionBarError = _stringResourcesService.SearchResources.Get(ex.StringCode);
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
OptionBarMessage = null;
2024-02-13 02:59:40 +01:00
return;
}
2024-02-28 01:22:13 +01:00
await _videoViewModel.LoadVideo(video);
2024-02-13 02:59:40 +01:00
MainContent = _videoView;
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-02-13 02:59:40 +01:00
OptionBarMessage = null;
}
[RelayCommand]
public async Task PlaylistSearchStart()
{
OptionBarSearchNotPending = false;
2024-03-09 21:53:30 +01:00
OptionBarLoading = true;
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
2024-02-13 02:59:40 +01:00
Playlist playlist;
try
{
playlist = await _searchService.SearchPlaylist(OptionBarPlaylistSearchTBValue, OptionBarPlaylistSearchNBValue);
}
catch (MediaSearchException ex)
{
2024-03-09 21:53:30 +01:00
OptionBarError = _stringResourcesService.SearchResources.Get(ex.StringCode);
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
OptionBarMessage = null;
2024-02-13 02:59:40 +01:00
return;
}
2024-03-08 00:34:21 +01:00
_videoCollectionViewModel.LoadCollection(playlist);
2024-02-28 01:22:13 +01:00
2024-03-08 00:34:21 +01:00
MainContent = _videoCollectionView;
2024-02-28 01:22:13 +01:00
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
2024-03-09 21:53:30 +01:00
OptionBarLoading = false;
2024-02-13 02:59:40 +01:00
OptionBarMessage = null;
}
[RelayCommand]
2024-03-04 00:28:32 +01:00
public async Task Download()
2024-02-13 02:59:40 +01:00
{
if (_downloadTaskManager.Tasks.Count > 0 && NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection)
2024-03-04 00:28:32 +01:00
{
string title = _stringResourcesService.CommonResources.Get("StartAtMeteredConnectionDialogTitle");
string message = _stringResourcesService.CommonResources.Get("StartAtMeteredConnectionDialogMessage");
DialogResultYesNo result = await _dialogsService.ShowYesNo(title, message);
if (result == DialogResultYesNo.No)
{
return;
}
}
2024-02-28 01:22:13 +01:00
foreach (DownloadTask task in _downloadTaskManager.Tasks)
{
task.Enqueue();
}
2024-02-13 02:59:40 +01:00
}
2024-03-09 21:53:30 +01:00
[RelayCommand]
public void CloseError()
{
OptionBarError = null;
OptionBarIsErrorOpened = true;
}
2024-02-13 02:59:40 +01:00
#endregion
#region PRIVATE METHODS
2024-02-13 02:59:40 +01:00
2024-02-28 01:22:13 +01:00
private async void BackToDownload_EventHandler(object sender, EventArgs e) => await Navigation();
2024-02-13 02:59:40 +01:00
#endregion
}
}