2024-02-28 01:22:13 +01:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2024-03-02 01:05:44 +01:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2024-02-29 03:09:14 +01:00
|
|
|
|
using System.Collections.ObjectModel;
|
2024-03-02 01:05:44 +01:00
|
|
|
|
using System.Diagnostics;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2024-02-29 03:09:14 +01:00
|
|
|
|
using VDownload.Core.Tasks;
|
|
|
|
|
|
using VDownload.Core.ViewModels.Home.Helpers;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
using VDownload.Models;
|
2024-02-29 03:09:14 +01:00
|
|
|
|
using VDownload.Services.Data.Settings;
|
|
|
|
|
|
using VDownload.Services.UI.StoragePicker;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
using VDownload.Sources.Twitch.Configuration.Models;
|
2024-03-02 02:03:21 +01:00
|
|
|
|
using SimpleToolkit.MVVM;
|
2024-03-02 02:41:42 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-03-03 23:05:32 +01:00
|
|
|
|
using VDownload.Services.Utility.Filename;
|
2024-03-04 00:28:32 +01:00
|
|
|
|
using VDownload.Services.UI.Dialogs;
|
|
|
|
|
|
using VDownload.Services.UI.StringResources;
|
2024-03-04 00:49:15 +01:00
|
|
|
|
using CommunityToolkit.WinUI.Helpers;
|
2024-03-07 22:08:11 +01:00
|
|
|
|
using VDownload.Services.Data.Application;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
namespace VDownload.Core.ViewModels.Home
|
|
|
|
|
|
{
|
2024-03-08 00:07:22 +01:00
|
|
|
|
public partial class HomeVideoCollectionViewModel : ObservableObject
|
2024-02-28 01:22:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
#region SERVICES
|
|
|
|
|
|
|
2024-02-29 03:09:14 +01:00
|
|
|
|
protected readonly IDownloadTaskManager _tasksManager;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
|
2024-02-29 03:09:14 +01:00
|
|
|
|
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-28 01:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region FIELDS
|
|
|
|
|
|
|
2024-03-08 00:07:22 +01:00
|
|
|
|
protected VideoCollection _collection;
|
2024-02-28 01:22:13 +01:00
|
|
|
|
|
2024-03-02 02:03:21 +01:00
|
|
|
|
protected List<VideoViewModel> _removedVideos;
|
2024-03-02 01:05:44 +01:00
|
|
|
|
|
2024-02-28 01:22:13 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PROPERTIES
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected string _name;
|
|
|
|
|
|
|
2024-02-29 03:09:14 +01:00
|
|
|
|
[ObservableProperty]
|
2024-03-02 02:03:21 +01:00
|
|
|
|
protected ObservableDictionary<VideoViewModel, bool> _videos;
|
2024-02-29 03:09:14 +01:00
|
|
|
|
|
2024-03-02 01:05:44 +01:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected int _removedCount;
|
|
|
|
|
|
|
2024-03-02 02:03:21 +01:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected int _hiddenCount;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected bool _isSomethingHidden;
|
|
|
|
|
|
|
2024-03-02 02:41:42 +01:00
|
|
|
|
public string TitleFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _titleFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _titleFilter, value, nameof(TitleFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected string _titleFilter;
|
|
|
|
|
|
|
|
|
|
|
|
public string AuthorFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _authorFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _authorFilter, value, nameof(AuthorFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected string _authorFilter;
|
|
|
|
|
|
|
2024-03-02 19:44:23 +01:00
|
|
|
|
public long MinViewsFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _minViewsFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _minViewsFilter, value, nameof(MinViewsFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected long _minViewsFilter;
|
|
|
|
|
|
|
|
|
|
|
|
public long MaxViewsFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _maxViewsFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _maxViewsFilter, value, nameof(MaxViewsFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected long _maxViewsFilter;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected long _minViews;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected long _maxViews;
|
|
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset MinDateFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _minDateFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _minDateFilter, value, nameof(MinDateFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected DateTimeOffset _minDateFilter;
|
|
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset MaxDateFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _maxDateFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _maxDateFilter, value, nameof(MaxDateFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected DateTimeOffset _maxDateFilter;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected DateTimeOffset _minDate;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected DateTimeOffset _maxDate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TimeSpan MinDurationFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _minDurationFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _minDurationFilter, value, nameof(MinDurationFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected TimeSpan _minDurationFilter;
|
|
|
|
|
|
|
|
|
|
|
|
public TimeSpan MaxDurationFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _maxDurationFilter;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _maxDurationFilter, value, nameof(MaxDurationFilter));
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected TimeSpan _maxDurationFilter;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected TimeSpan _minDuration;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected TimeSpan _maxDuration;
|
|
|
|
|
|
|
2024-02-28 01:22:13 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region CONSTRUCTORS
|
|
|
|
|
|
|
2024-03-08 00:07:22 +01:00
|
|
|
|
public HomeVideoCollectionViewModel(IDownloadTaskManager tasksManager, ISettingsService settingsService, IStoragePickerService storagePickerService, IFilenameService filenameService, IDialogsService dialogsService, IStringResourcesService stringResourcesService, IApplicationDataService applicationDataService)
|
2024-02-29 03:09:14 +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-29 03:09:14 +01:00
|
|
|
|
|
2024-03-02 02:03:21 +01:00
|
|
|
|
_removedVideos = new List<VideoViewModel>();
|
|
|
|
|
|
|
|
|
|
|
|
_videos = new ObservableDictionary<VideoViewModel, bool>();
|
2024-02-28 01:22:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PUBLIC METHODS
|
|
|
|
|
|
|
2024-03-08 00:34:21 +01:00
|
|
|
|
public void LoadCollection(VideoCollection collection)
|
2024-02-28 01:22:13 +01:00
|
|
|
|
{
|
2024-03-08 00:07:22 +01:00
|
|
|
|
_collection = collection;
|
|
|
|
|
|
ParallelQuery<Video> playlistQuery = _collection.AsParallel();
|
2024-02-29 03:09:14 +01:00
|
|
|
|
|
2024-03-02 02:03:21 +01:00
|
|
|
|
_removedVideos.Clear();
|
|
|
|
|
|
|
2024-03-02 02:41:42 +01:00
|
|
|
|
_titleFilter = string.Empty;
|
|
|
|
|
|
_authorFilter = string.Empty;
|
2024-03-02 19:44:23 +01:00
|
|
|
|
|
|
|
|
|
|
IEnumerable<long> views = playlistQuery.Select(x => x.Views);
|
|
|
|
|
|
MinViews = views.Min();
|
|
|
|
|
|
MaxViews = views.Max();
|
|
|
|
|
|
_minViewsFilter = MinViews;
|
|
|
|
|
|
_maxViewsFilter = MaxViews;
|
|
|
|
|
|
|
2024-03-08 00:07:22 +01:00
|
|
|
|
IEnumerable<DateTimeOffset> date = _collection.Select(x => new DateTimeOffset(x.PublishDate));
|
2024-03-02 19:44:23 +01:00
|
|
|
|
MinDate = date.Min();
|
|
|
|
|
|
MaxDate = date.Max();
|
|
|
|
|
|
_minDateFilter = MinDate;
|
|
|
|
|
|
_maxDateFilter = MaxDate;
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerable<TimeSpan> duration = playlistQuery.Select(x => x.Duration);
|
|
|
|
|
|
MinDuration = duration.Min();
|
|
|
|
|
|
MaxDuration = duration.Max();
|
|
|
|
|
|
_minDurationFilter = MinDuration;
|
|
|
|
|
|
_maxDurationFilter = MaxDuration;
|
|
|
|
|
|
|
2024-03-08 00:07:22 +01:00
|
|
|
|
Name = _collection.Name;
|
2024-03-02 02:03:21 +01:00
|
|
|
|
Videos.Clear();
|
2024-03-08 00:07:22 +01:00
|
|
|
|
foreach (Video video in _collection)
|
2024-02-29 03:09:14 +01:00
|
|
|
|
{
|
2024-03-07 22:08:11 +01:00
|
|
|
|
Videos.Add(new VideoViewModel(video, _settingsService, _storagePickerService, _filenameService, _applicationDataService), true);
|
2024-02-29 03:09:14 +01:00
|
|
|
|
}
|
2024-03-02 02:41:42 +01:00
|
|
|
|
UpdateFilter();
|
2024-02-28 01:22:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region COMMANDS
|
|
|
|
|
|
|
2024-03-02 01:05:44 +01:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async Task SelectDirectory()
|
|
|
|
|
|
{
|
|
|
|
|
|
string? newDirectory = await _storagePickerService.OpenDirectory();
|
|
|
|
|
|
if (newDirectory is not null)
|
|
|
|
|
|
{
|
2024-03-02 02:03:21 +01:00
|
|
|
|
foreach (VideoViewModel video in Videos.Keys)
|
2024-03-02 01:05:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
video.DirectoryPath = newDirectory;
|
|
|
|
|
|
}
|
2024-03-07 22:08:11 +01:00
|
|
|
|
|
|
|
|
|
|
_applicationDataService.Data.Common.LastOutputDirectory = newDirectory;
|
|
|
|
|
|
await _applicationDataService.Save();
|
2024-03-02 01:05:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void RemoveVideo(VideoViewModel video)
|
|
|
|
|
|
{
|
2024-03-02 02:03:21 +01:00
|
|
|
|
Videos[video] = false;
|
2024-03-02 01:05:44 +01:00
|
|
|
|
|
2024-03-02 02:03:21 +01:00
|
|
|
|
_removedVideos.Add(video);
|
2024-03-02 01:05:44 +01:00
|
|
|
|
|
2024-03-02 02:41:42 +01:00
|
|
|
|
UpdateFilter();
|
2024-03-02 01:05:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void RestoreRemovedVideos()
|
|
|
|
|
|
{
|
2024-03-02 02:03:21 +01:00
|
|
|
|
foreach(VideoViewModel video in _removedVideos)
|
2024-03-02 01:05:44 +01:00
|
|
|
|
{
|
2024-03-02 02:03:21 +01:00
|
|
|
|
Videos[video] = true;
|
2024-03-02 01:05:44 +01:00
|
|
|
|
}
|
2024-03-02 02:03:21 +01:00
|
|
|
|
_removedVideos.Clear();
|
|
|
|
|
|
|
2024-03-02 02:41:42 +01:00
|
|
|
|
UpdateFilter();
|
2024-03-02 01:05:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2024-03-04 00:28:32 +01:00
|
|
|
|
public async Task CreateTasksAndDownload() => await CreateTasks(true);
|
2024-03-02 01:05:44 +01:00
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2024-03-04 00:28:32 +01:00
|
|
|
|
public async Task CreateTasks() => await CreateTasks(false);
|
2024-03-02 01:05:44 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PRIVATE METHODS
|
2024-02-28 01:22:13 +01:00
|
|
|
|
|
2024-03-04 00:28:32 +01:00
|
|
|
|
protected async Task CreateTasks(bool download)
|
2024-03-02 01:05:44 +01:00
|
|
|
|
{
|
2024-03-10 13:42:41 +01:00
|
|
|
|
if
|
|
|
|
|
|
(
|
|
|
|
|
|
download
|
|
|
|
|
|
&&
|
|
|
|
|
|
_settingsService.Data.Common.Tasks.ShowMeteredConnectionWarnings
|
|
|
|
|
|
&&
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-02 19:44:23 +01:00
|
|
|
|
IEnumerable<VideoViewModel> videos = Videos.Cast<ObservableKeyValuePair<VideoViewModel, bool>>()
|
|
|
|
|
|
.Where(x => x.Value)
|
|
|
|
|
|
.Select(x => x.Key);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (VideoViewModel video in videos)
|
2024-03-02 01:05:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
DownloadTask task = _tasksManager.AddTask(video.Video, video.BuildDownloadOptions());
|
|
|
|
|
|
if (download)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Enqueue();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
CloseRequested?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
}
|
2024-02-28 01:22:13 +01:00
|
|
|
|
|
2024-03-02 02:41:42 +01:00
|
|
|
|
protected void UpdateFilter()
|
2024-03-02 02:03:21 +01:00
|
|
|
|
{
|
2024-03-02 02:41:42 +01:00
|
|
|
|
Regex titleRegex = new Regex(TitleFilter);
|
|
|
|
|
|
Regex authorRegex = new Regex(AuthorFilter);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (ObservableKeyValuePair<VideoViewModel, bool> item in Videos)
|
|
|
|
|
|
{
|
2024-03-02 19:44:23 +01:00
|
|
|
|
VideoViewModel video = item.Key;
|
|
|
|
|
|
bool hide =
|
|
|
|
|
|
(
|
|
|
|
|
|
_removedVideos.Contains(video)
|
|
|
|
|
|
||
|
|
|
|
|
|
!titleRegex.IsMatch(video.Title)
|
|
|
|
|
|
||
|
|
|
|
|
|
!authorRegex.IsMatch(video.Author)
|
|
|
|
|
|
||
|
|
|
|
|
|
MinViewsFilter > video.Views
|
|
|
|
|
|
||
|
|
|
|
|
|
MaxViewsFilter < video.Views
|
|
|
|
|
|
||
|
|
|
|
|
|
MinDateFilter.Date > video.PublishDate.Date
|
|
|
|
|
|
||
|
|
|
|
|
|
MaxDateFilter.Date < video.PublishDate.Date
|
|
|
|
|
|
||
|
|
|
|
|
|
MinDurationFilter > video.Duration
|
|
|
|
|
|
||
|
|
|
|
|
|
MaxDurationFilter < video.Duration
|
|
|
|
|
|
);
|
|
|
|
|
|
item.Value = !hide;
|
2024-03-02 02:41:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-02 02:03:21 +01:00
|
|
|
|
RemovedCount = _removedVideos.Count;
|
|
|
|
|
|
HiddenCount = Videos.Values.Where(x => !x).Count();
|
|
|
|
|
|
IsSomethingHidden = HiddenCount > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-28 01:22:13 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region EVENTS
|
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler CloseRequested;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|