home, subscriptions, authentication no internet connection errors added, twitch login button bug fixed, cancel all button added

This commit is contained in:
2024-03-10 00:35:11 +01:00
Unverified
parent 66e6ed3b4e
commit d73ce6a05b
12 changed files with 230 additions and 83 deletions

View File

@@ -4,6 +4,7 @@ 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.Tasks;
@@ -163,46 +164,57 @@ namespace VDownload.Core.ViewModels.Home
[RelayCommand]
public async Task LoadFromSubscription()
{
MainContent = _downloadsView;
OptionBarLoading = false;
OptionBarMessage = null;
SearchButtonClicked();
if (OptionBarLoadSubscriptionButtonChecked)
{
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
ShowError(ErrorNoInternetConnection());
OptionBarLoadSubscriptionButtonChecked = false;
return;
}
OptionBarContent = OptionBarContentType.None;
OptionBarVideoSearchButtonChecked = false;
OptionBarPlaylistSearchButtonChecked = false;
OptionBarSearchNotPending = false;
OptionBarLoading = true;
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
StartSearch();
SubscriptionsVideoList subList = new SubscriptionsVideoList { Name = _stringResourcesService.CommonResources.Get("SubscriptionVideoListName") };
List<Task> tasks = new List<Task>();
foreach (Subscription sub in _subscriptionsDataService.Data.ToArray())
try
{
tasks.Add(Task.Run(async () =>
foreach (Subscription sub in _subscriptionsDataService.Data.ToArray())
{
Playlist playlist;
try
tasks.Add(Task.Run(async () =>
{
playlist = await _searchService.SearchPlaylist(sub.Url.OriginalString, 0);
}
catch (MediaSearchException)
{
return;
}
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);
}
}));
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();
}
catch (Exception ex) when (ex is TaskCanceledException || ex is HttpRequestException)
{
ShowError(ErrorSearchTimeout());
return;
}
await Task.WhenAll(tasks);
await _subscriptionsDataService.Save();
if (subList.Count > 0)
{
@@ -224,10 +236,7 @@ namespace VDownload.Core.ViewModels.Home
[RelayCommand]
public void VideoSearchShow()
{
OptionBarSearchNotPending = true;
OptionBarLoading = false;
OptionBarMessage = null;
MainContent = _downloadsView;
SearchButtonClicked();
if (OptionBarContent != OptionBarContentType.VideoSearch)
{
@@ -244,10 +253,7 @@ namespace VDownload.Core.ViewModels.Home
[RelayCommand]
public void PlaylistSearchShow()
{
OptionBarSearchNotPending = true;
OptionBarLoading = false;
OptionBarMessage = null;
MainContent = _downloadsView;
SearchButtonClicked();
if (OptionBarContent != OptionBarContentType.PlaylistSearch)
{
@@ -264,9 +270,13 @@ namespace VDownload.Core.ViewModels.Home
[RelayCommand]
public async Task VideoSearchStart()
{
OptionBarSearchNotPending = false;
OptionBarLoading = true;
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
ShowError(ErrorNoInternetConnection());
return;
}
StartSearch();
Video video;
try
@@ -275,10 +285,12 @@ namespace VDownload.Core.ViewModels.Home
}
catch (MediaSearchException ex)
{
OptionBarError = _stringResourcesService.SearchResources.Get(ex.StringCode);
OptionBarSearchNotPending = true;
OptionBarLoading = false;
OptionBarMessage = null;
ShowError(_stringResourcesService.SearchResources.Get(ex.StringCode));
return;
}
catch (Exception ex) when (ex is TaskCanceledException || ex is HttpRequestException)
{
ShowError(ErrorSearchTimeout());
return;
}
@@ -294,9 +306,13 @@ namespace VDownload.Core.ViewModels.Home
[RelayCommand]
public async Task PlaylistSearchStart()
{
OptionBarSearchNotPending = false;
OptionBarLoading = true;
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
ShowError(ErrorNoInternetConnection());
return;
}
StartSearch();
Playlist playlist;
try
@@ -305,10 +321,12 @@ namespace VDownload.Core.ViewModels.Home
}
catch (MediaSearchException ex)
{
OptionBarError = _stringResourcesService.SearchResources.Get(ex.StringCode);
OptionBarSearchNotPending = true;
OptionBarLoading = false;
OptionBarMessage = null;
ShowError(_stringResourcesService.SearchResources.Get(ex.StringCode));
return;
}
catch (Exception ex) when (ex is TaskCanceledException || ex is HttpRequestException)
{
ShowError(ErrorSearchTimeout());
return;
}
@@ -324,6 +342,12 @@ namespace VDownload.Core.ViewModels.Home
[RelayCommand]
public async Task Download()
{
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
ShowError(ErrorNoInternetConnection());
return;
}
if (_downloadTaskManager.Tasks.Count > 0 && NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection)
{
string title = _stringResourcesService.CommonResources.Get("StartAtMeteredConnectionDialogTitle");
@@ -341,6 +365,17 @@ namespace VDownload.Core.ViewModels.Home
}
}
[RelayCommand]
public async Task Cancel()
{
List<Task> tasks = new List<Task>();
foreach (DownloadTask task in _downloadTaskManager.Tasks)
{
tasks.Add(task.Cancel());
}
await Task.WhenAll(tasks);
}
[RelayCommand]
public void CloseError()
{
@@ -354,7 +389,34 @@ namespace VDownload.Core.ViewModels.Home
#region PRIVATE METHODS
private async void BackToDownload_EventHandler(object sender, EventArgs e) => await Navigation();
protected void ShowError(string message)
{
OptionBarError = message;
OptionBarSearchNotPending = true;
OptionBarLoading = false;
OptionBarMessage = null;
}
protected void SearchButtonClicked()
{
OptionBarSearchNotPending = true;
OptionBarLoading = false;
OptionBarMessage = null;
MainContent = _downloadsView;
}
protected void StartSearch()
{
OptionBarSearchNotPending = false;
OptionBarLoading = true;
OptionBarMessage = _stringResourcesService.HomeViewResources.Get("OptionBarMessageLoading");
}
protected async void BackToDownload_EventHandler(object sender, EventArgs e) => await Navigation();
protected string ErrorNoInternetConnection() => _stringResourcesService.HomeViewResources.Get("ErrorInfoBarNoInternetConnection");
protected string ErrorSearchTimeout() => _stringResourcesService.SearchResources.Get("SearchTimeout");
#endregion
}