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

280 lines
8.2 KiB
C#
Raw Normal View History

2024-02-13 02:59:40 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
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;
using VDownload.Services.UI.StringResources;
using VDownload.Sources;
using VDownload.Sources.Common;
namespace VDownload.Core.ViewModels.Home
2024-02-13 02:59:40 +01:00
{
public partial class HomeViewModel : ObservableObject
{
#region ENUMS
2024-03-03 19:00:38 +01:00
public enum OptionBarMessageIconType
{
None,
ProgressRing,
Error
}
2024-02-13 02:59:40 +01:00
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-02-13 02:59:40 +01:00
2024-02-28 01:22:13 +01:00
protected readonly IDownloadTaskManager _downloadTaskManager;
protected readonly HomeVideoViewModel _videoViewModel;
2024-02-28 01:22:13 +01:00
protected readonly HomePlaylistViewModel _playlistViewModel;
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-02-28 01:22:13 +01:00
protected readonly Type _playlistView = typeof(HomePlaylistViewModel);
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
[ObservableProperty]
private OptionBarContentType _optionBarContent;
[ObservableProperty]
private string _optionBarMessage;
[ObservableProperty]
2024-03-03 19:00:38 +01:00
private OptionBarMessageIconType _optionBarMessageIcon;
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-02-28 01:22:13 +01:00
public HomeViewModel(IConfigurationService configurationService, ISettingsService settingsService, IStringResourcesService stringResourcesService, ISearchService searchService, IDownloadTaskManager downloadTaskManager, HomeVideoViewModel videoViewModel, HomePlaylistViewModel playlistViewModel)
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-02-28 01:22:13 +01:00
_downloadTaskManager = downloadTaskManager;
_videoViewModel = videoViewModel;
2024-02-28 01:22:13 +01:00
_videoViewModel.CloseRequested += BackToDownload_EventHandler;
_playlistViewModel = playlistViewModel;
_playlistViewModel.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-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.None;
2024-02-13 02:59:40 +01:00
OptionBarMessage = null;
OptionBarVideoSearchButtonChecked = false;
OptionBarPlaylistSearchButtonChecked = false;
OptionBarSearchNotPending = true;
OptionBarVideoSearchTBValue = string.Empty;
OptionBarPlaylistSearchNBValue = _settingsService.Data.Common.MaxNumberOfVideosToGetFromPlaylist;
2024-02-13 02:59:40 +01:00
OptionBarPlaylistSearchTBValue = string.Empty;
}
[RelayCommand]
public void LoadFromSubscription()
{
MainContent = _downloadsView;
2024-02-13 02:59:40 +01:00
OptionBarContent = OptionBarContentType.None;
OptionBarVideoSearchButtonChecked = false;
OptionBarPlaylistSearchButtonChecked = false;
OptionBarSearchNotPending = false;
//TODO: Load videos
}
[RelayCommand]
public void VideoSearchShow()
{
2024-03-03 19:00:38 +01:00
OptionBarSearchNotPending = true;
OptionBarMessageIcon = OptionBarMessageIconType.None;
OptionBarMessage = null;
MainContent = _downloadsView;
2024-02-13 02:59:40 +01:00
if (OptionBarContent != OptionBarContentType.VideoSearch)
{
OptionBarContent = OptionBarContentType.VideoSearch;
OptionBarPlaylistSearchButtonChecked = false;
}
else
{
OptionBarContent = OptionBarContentType.None;
}
}
[RelayCommand]
public void PlaylistSearchShow()
{
2024-03-03 19:00:38 +01:00
OptionBarSearchNotPending = true;
OptionBarMessageIcon = OptionBarMessageIconType.None;
OptionBarMessage = null;
MainContent = _downloadsView;
2024-02-13 02:59:40 +01:00
if (OptionBarContent != OptionBarContentType.PlaylistSearch)
{
OptionBarContent = OptionBarContentType.PlaylistSearch;
OptionBarVideoSearchButtonChecked = false;
}
else
{
OptionBarContent = OptionBarContentType.None;
}
}
[RelayCommand]
public async Task VideoSearchStart()
{
OptionBarSearchNotPending = false;
2024-03-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.ProgressRing;
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-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.Error;
OptionBarMessage = _stringResourcesService.SearchResources.Get(ex.StringCode);
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
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-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.None;
2024-02-13 02:59:40 +01:00
OptionBarMessage = null;
}
[RelayCommand]
public async Task PlaylistSearchStart()
{
OptionBarSearchNotPending = false;
2024-03-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.ProgressRing;
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-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.Error;
OptionBarMessage = _stringResourcesService.SearchResources.Get(ex.StringCode);
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
return;
}
_playlistViewModel.LoadPlaylist(playlist);
2024-02-28 01:22:13 +01:00
MainContent = _playlistView;
2024-02-13 02:59:40 +01:00
OptionBarSearchNotPending = true;
2024-03-03 19:00:38 +01:00
OptionBarMessageIcon = OptionBarMessageIconType.None;
2024-02-13 02:59:40 +01:00
OptionBarMessage = null;
}
[RelayCommand]
public void Download()
{
2024-02-28 01:22:13 +01:00
foreach (DownloadTask task in _downloadTaskManager.Tasks)
{
task.Enqueue();
}
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
}
}