using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.WinUI.Helpers; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using VDownload.Core.Strings; using VDownload.Core.Tasks; using VDownload.Models; using VDownload.Services.Data.Configuration; using VDownload.Services.Data.Settings; using VDownload.Services.Data.Subscriptions; using VDownload.Services.UI.Dialogs; using VDownload.Sources; using VDownload.Sources.Common; using VDownload.Sources.Twitch.Configuration.Models; namespace VDownload.Core.ViewModels.Home { 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 ISearchService _searchService; protected readonly ISubscriptionsDataService _subscriptionsDataService; protected readonly IDialogsService _dialogsService; protected readonly IDownloadTaskManager _downloadTaskManager; protected readonly HomeVideoViewModel _videoViewModel; protected readonly HomeVideoCollectionViewModel _videoCollectionViewModel; #endregion #region FIELDS protected readonly Type _downloadsView = typeof(HomeDownloadsViewModel); protected readonly Type _videoView = typeof(HomeVideoViewModel); protected readonly Type _videoCollectionView = typeof(HomeVideoCollectionViewModel); #endregion #region PROPERTIES [ObservableProperty] private Type _mainContent; [ObservableProperty] private string _optionBarError; [ObservableProperty] private bool _optionBarIsErrorOpened; [ObservableProperty] private OptionBarContentType _optionBarContent; [ObservableProperty] private string _optionBarMessage; [ObservableProperty] private bool _optionBarLoading; [ObservableProperty] private bool _optionBarLoadSubscriptionButtonChecked; [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 public HomeViewModel(IConfigurationService configurationService, ISettingsService settingsService, ISearchService searchService, ISubscriptionsDataService subscriptionsDataService, IDialogsService dialogsService, IDownloadTaskManager downloadTaskManager, HomeVideoViewModel videoViewModel, HomeVideoCollectionViewModel videoCollectionViewModel) { _configurationService = configurationService; _settingsService = settingsService; _searchService = searchService; _subscriptionsDataService = subscriptionsDataService; _dialogsService = dialogsService; _downloadTaskManager = downloadTaskManager; _videoViewModel = videoViewModel; _videoViewModel.CloseRequested += BackToDownload_EventHandler; _videoCollectionViewModel = videoCollectionViewModel; _videoCollectionViewModel.CloseRequested += BackToDownload_EventHandler; } #endregion #region COMMANDS [RelayCommand] public async Task Navigation() { await _settingsService.Load(); MainContent = _downloadsView; OptionBarContent = OptionBarContentType.None; OptionBarLoading = false; OptionBarMessage = null; OptionBarError = null; OptionBarIsErrorOpened = true; OptionBarLoadSubscriptionButtonChecked = false; OptionBarVideoSearchButtonChecked = false; OptionBarPlaylistSearchButtonChecked = false; OptionBarSearchNotPending = true; OptionBarVideoSearchTBValue = string.Empty; OptionBarPlaylistSearchNBValue = _settingsService.Data.Common.Searching.MaxNumberOfVideosToGetFromPlaylist; OptionBarPlaylistSearchTBValue = string.Empty; } [RelayCommand] public async Task LoadFromSubscription() { SearchButtonClicked(); if (OptionBarLoadSubscriptionButtonChecked) { if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable) { ShowError(ErrorNoInternetConnection()); OptionBarLoadSubscriptionButtonChecked = false; return; } OptionBarContent = OptionBarContentType.None; OptionBarVideoSearchButtonChecked = false; OptionBarPlaylistSearchButtonChecked = false; StartSearch(); SubscriptionsVideoList subList = new SubscriptionsVideoList { Name = StringResourcesManager.Common.Get("SubscriptionVideoListName") }; List tasks = new List(); try { 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