2024-02-14 02:07:22 +01:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2024-03-04 00:49:15 +01:00
|
|
|
|
using CommunityToolkit.WinUI.Helpers;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using VDownload.Core.Tasks;
|
|
|
|
|
|
using VDownload.Models;
|
2024-03-07 22:08:11 +01:00
|
|
|
|
using VDownload.Services.Data.Application;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
using VDownload.Services.Data.Settings;
|
2024-03-04 00:28:32 +01:00
|
|
|
|
using VDownload.Services.UI.Dialogs;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
using VDownload.Services.UI.StoragePicker;
|
2024-03-04 00:28:32 +01:00
|
|
|
|
using VDownload.Services.UI.StringResources;
|
2024-03-03 23:05:32 +01:00
|
|
|
|
using VDownload.Services.Utility.Filename;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
|
|
|
|
|
|
namespace VDownload.Core.ViewModels.Home
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class HomeVideoViewModel : ObservableObject
|
|
|
|
|
|
{
|
|
|
|
|
|
#region SERVICES
|
|
|
|
|
|
|
|
|
|
|
|
protected readonly IDownloadTaskManager _tasksManager;
|
|
|
|
|
|
|
|
|
|
|
|
protected readonly ISettingsService _settingsService;
|
|
|
|
|
|
protected readonly IStoragePickerService _storagePickerService;
|
2024-03-03 23:05:32 +01:00
|
|
|
|
protected readonly IFilenameService _filenameService;
|
2024-03-04 00:28:32 +01:00
|
|
|
|
protected readonly IDialogsService _dialogsService;
|
|
|
|
|
|
protected readonly IStringResourcesService _stringResourcesService;
|
2024-03-07 22:08:11 +01:00
|
|
|
|
protected readonly IApplicationDataService _applicationDataService;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region FIELDS
|
|
|
|
|
|
|
|
|
|
|
|
protected Video _video;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PROPERTIES
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected Uri _thumbnailUrl;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected string _title;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected string _author;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected DateTime _publishDate;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected TimeSpan _duration;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected long _views;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected ObservableCollection<VideoStream> _streams;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected VideoStream _selectedStream;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected MediaType _mediaType;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected TimeSpan _trimStart;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected TimeSpan _trimEnd;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected string _directoryPath;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected string _filename;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected VideoExtension _videoExtension;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected AudioExtension _audioExtension;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region CONSTRUCTORS
|
|
|
|
|
|
|
2024-03-07 22:08:11 +01:00
|
|
|
|
public HomeVideoViewModel(IDownloadTaskManager tasksManager, ISettingsService settingsService, IStoragePickerService storagePickerService, IFilenameService filenameService, IDialogsService dialogsService, IStringResourcesService stringResourcesService, IApplicationDataService applicationDataService)
|
2024-02-14 02:07:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
_tasksManager = tasksManager;
|
|
|
|
|
|
_settingsService = settingsService;
|
|
|
|
|
|
_storagePickerService = storagePickerService;
|
2024-03-03 23:05:32 +01:00
|
|
|
|
_filenameService = filenameService;
|
2024-03-04 00:28:32 +01:00
|
|
|
|
_dialogsService = dialogsService;
|
|
|
|
|
|
_stringResourcesService = stringResourcesService;
|
2024-03-07 22:08:11 +01:00
|
|
|
|
_applicationDataService = applicationDataService;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PUBLIC METHODS
|
|
|
|
|
|
|
2024-02-28 01:22:13 +01:00
|
|
|
|
public async Task LoadVideo(Video video)
|
2024-02-14 02:07:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
await _settingsService.Load();
|
|
|
|
|
|
|
|
|
|
|
|
_video = video;
|
|
|
|
|
|
|
|
|
|
|
|
ThumbnailUrl = video.ThumbnailUrl;
|
|
|
|
|
|
Title = video.Title;
|
|
|
|
|
|
Author = video.Author;
|
|
|
|
|
|
PublishDate = video.PublishDate;
|
|
|
|
|
|
Duration = video.Duration;
|
|
|
|
|
|
Views = video.Views;
|
|
|
|
|
|
|
|
|
|
|
|
Streams = [.. video.Streams];
|
|
|
|
|
|
SelectedStream = Streams[0];
|
2024-02-28 01:22:13 +01:00
|
|
|
|
MediaType = _settingsService.Data.Common.Tasks.DefaultMediaType;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
TrimStart = TimeSpan.Zero;
|
|
|
|
|
|
TrimEnd = Duration;
|
2024-03-03 23:05:32 +01:00
|
|
|
|
Filename = _filenameService.CreateFilename(_settingsService.Data.Common.Tasks.FilenameTemplate, video);
|
2024-02-28 01:22:13 +01:00
|
|
|
|
VideoExtension = _settingsService.Data.Common.Tasks.DefaultVideoExtension;
|
|
|
|
|
|
AudioExtension = _settingsService.Data.Common.Tasks.DefaultAudioExtension;
|
2024-03-07 22:08:11 +01:00
|
|
|
|
|
|
|
|
|
|
if (_settingsService.Data.Common.Tasks.SaveLastOutputDirectory && !string.IsNullOrWhiteSpace(_applicationDataService.Data.Common.LastOutputDirectory))
|
|
|
|
|
|
{
|
|
|
|
|
|
DirectoryPath = _applicationDataService.Data.Common.LastOutputDirectory;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DirectoryPath = _settingsService.Data.Common.Tasks.DefaultOutputDirectory;
|
|
|
|
|
|
}
|
2024-02-14 02:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async Task Browse()
|
|
|
|
|
|
{
|
|
|
|
|
|
string? newDirectory = await _storagePickerService.OpenDirectory();
|
|
|
|
|
|
if (newDirectory is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.DirectoryPath = newDirectory;
|
2024-03-07 22:08:11 +01:00
|
|
|
|
|
|
|
|
|
|
_applicationDataService.Data.Common.LastOutputDirectory = newDirectory;
|
|
|
|
|
|
await _applicationDataService.Save();
|
2024-02-14 02:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2024-03-04 00:28:32 +01:00
|
|
|
|
public async Task CreateTask() => await CreateTask(false);
|
2024-02-14 02:07:22 +01:00
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2024-03-04 00:28:32 +01:00
|
|
|
|
public async Task CreateTaskAndDownload() => await CreateTask(true);
|
2024-02-14 02:07:22 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PRIVATE METHODS
|
|
|
|
|
|
|
2024-03-04 00:28:32 +01:00
|
|
|
|
protected async Task CreateTask(bool download)
|
|
|
|
|
|
{
|
2024-03-04 00:49:15 +01:00
|
|
|
|
if (download && 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);
|
|
|
|
|
|
download = result == DialogResultYesNo.Yes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DownloadTask task = _tasksManager.AddTask(_video, BuildDownloadOptions());
|
|
|
|
|
|
CloseRequested?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
if (download)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Enqueue();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-14 02:07:22 +01:00
|
|
|
|
protected VideoDownloadOptions BuildDownloadOptions()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new VideoDownloadOptions(Duration)
|
|
|
|
|
|
{
|
|
|
|
|
|
MediaType = this.MediaType,
|
|
|
|
|
|
SelectedStream = this.SelectedStream,
|
|
|
|
|
|
TrimStart = this.TrimStart,
|
|
|
|
|
|
TrimEnd = this.TrimEnd,
|
|
|
|
|
|
Directory = this.DirectoryPath,
|
2024-03-03 23:05:32 +01:00
|
|
|
|
Filename = _filenameService.SanitizeFilename(this.Filename),
|
2024-02-14 02:07:22 +01:00
|
|
|
|
Extension = (this.MediaType == MediaType.OnlyAudio ? this.AudioExtension.ToString() : this.VideoExtension.ToString()).ToLower(),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2024-02-28 01:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region EVENTS
|
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler CloseRequested;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2024-02-14 02:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|