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

126 lines
3.7 KiB
C#
Raw Normal View History

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-03-15 12:54:21 +01:00
using VDownload.Core.Strings;
using VDownload.Core.Tasks;
2024-03-10 13:42:41 +01:00
using VDownload.Services.Data.Settings;
2024-03-04 00:28:32 +01:00
using VDownload.Services.UI.Dialogs;
namespace VDownload.Core.ViewModels.Home
{
public partial class HomeDownloadsViewModel : ObservableObject
{
#region SERVICES
protected readonly IDownloadTaskManager _tasksManager;
2024-03-04 00:28:32 +01:00
protected readonly IDialogsService _dialogsService;
2024-03-10 13:42:41 +01:00
protected readonly ISettingsService _settingsService;
2024-03-04 00:28:32 +01:00
#endregion
#region PROPERTIES
public ReadOnlyObservableCollection<DownloadTask> Tasks => _tasksManager.Tasks;
[ObservableProperty]
private bool _taskListIsEmpty;
#endregion
#region CONSTRUCTORS
2024-03-15 12:54:21 +01:00
public HomeDownloadsViewModel(IDownloadTaskManager tasksManager, IDialogsService dialogsService, ISettingsService settingsService)
{
_tasksManager = tasksManager;
_tasksManager.TaskCollectionChanged += Tasks_CollectionChanged;
2024-03-04 00:28:32 +01:00
_dialogsService = dialogsService;
2024-03-10 13:42:41 +01:00
_settingsService = settingsService;
2024-03-04 00:28:32 +01:00
_taskListIsEmpty = _tasksManager.Tasks.Count == 0;
}
#endregion
#region PUBLIC METHODS
[RelayCommand]
public async Task StartCancelTask(DownloadTask task)
{
DownloadTaskStatus[] idleStatuses =
[
DownloadTaskStatus.Idle,
DownloadTaskStatus.EndedUnsuccessfully,
DownloadTaskStatus.EndedSuccessfully,
DownloadTaskStatus.EndedCancelled
];
if (idleStatuses.Contains(task.Status))
{
2024-03-10 01:52:23 +01:00
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
2024-03-15 12:54:21 +01:00
string title = StringResourcesManager.HomeDownloadsView.Get("DialogErrorTitle");
string message = StringResourcesManager.HomeDownloadsView.Get("DialogErrorMessageNoInternetConnection");
2024-03-10 01:52:23 +01:00
await _dialogsService.ShowOk(title, message);
return;
}
2024-03-04 00:28:32 +01:00
bool continueEnqueue = true;
2024-03-10 13:42:41 +01:00
if
(
_settingsService.Data.Common.Tasks.ShowMeteredConnectionWarnings
&&
NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection
)
2024-03-04 00:28:32 +01:00
{
2024-03-15 12:54:21 +01:00
string title = StringResourcesManager.Common.Get("StartAtMeteredConnectionDialogTitle");
string message = StringResourcesManager.Common.Get("StartAtMeteredConnectionDialogMessage");
2024-03-04 00:28:32 +01:00
DialogResultYesNo result = await _dialogsService.ShowYesNo(title, message);
continueEnqueue = result == DialogResultYesNo.Yes;
}
if (continueEnqueue)
{
task.Enqueue();
}
}
else
{
await task.Cancel();
}
}
[RelayCommand]
public async Task RemoveTask(DownloadTask task)
{
await task.Cancel();
_tasksManager.RemoveTask(task);
}
#endregion
#region PRIVATE METHODS
private void Tasks_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
TaskListIsEmpty = Tasks.Count == 0;
}
#endregion
}
}