2024-05-04 15:35:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Avalonia.Media.Imaging;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
using CRD.Downloader;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
using CRD.Downloader.Crunchyroll;
|
2024-07-22 19:11:13 +00:00
|
|
|
|
using CRD.Utils;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using CRD.Utils.Structs;
|
|
|
|
|
|
|
|
|
|
namespace CRD.ViewModels;
|
|
|
|
|
|
|
|
|
|
public partial class DownloadsPageViewModel : ViewModelBase{
|
|
|
|
|
public ObservableCollection<DownloadItemModel> Items{ get; }
|
|
|
|
|
|
2024-06-24 20:29:13 +00:00
|
|
|
|
[ObservableProperty]
|
2024-07-22 19:11:13 +00:00
|
|
|
|
private bool _autoDownload;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
2024-06-24 20:29:13 +00:00
|
|
|
|
[ObservableProperty]
|
2024-07-22 19:11:13 +00:00
|
|
|
|
private bool _removeFinished;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
public DownloadsPageViewModel(){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
QueueManager.Instance.UpdateDownloadListItems();
|
|
|
|
|
Items = QueueManager.Instance.DownloadItemModels;
|
|
|
|
|
AutoDownload = CrunchyrollManager.Instance.CrunOptions.AutoDownload;
|
|
|
|
|
RemoveFinished = CrunchyrollManager.Instance.CrunOptions.RemoveFinishedDownload;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
2024-08-09 21:16:13 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
partial void OnAutoDownloadChanged(bool value){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.AutoDownload = value;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
if (value){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
QueueManager.Instance.UpdateDownloadListItems();
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
2024-08-09 21:16:13 +00:00
|
|
|
|
|
2024-07-22 19:11:13 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 20:29:13 +00:00
|
|
|
|
partial void OnRemoveFinishedChanged(bool value){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.RemoveFinishedDownload = value;
|
2024-07-22 19:11:13 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
2024-06-24 20:29:13 +00:00
|
|
|
|
}
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class DownloadItemModel : INotifyPropertyChanged{
|
|
|
|
|
public string ImageUrl{ get; set; }
|
|
|
|
|
public Bitmap? ImageBitmap{ get; set; }
|
|
|
|
|
public string Title{ get; set; }
|
|
|
|
|
|
|
|
|
|
public bool isDownloading{ get; set; }
|
|
|
|
|
public bool Done{ get; set; }
|
|
|
|
|
public bool Paused{ get; set; }
|
|
|
|
|
|
|
|
|
|
public double Percent{ get; set; }
|
|
|
|
|
public string Time{ get; set; }
|
|
|
|
|
public string DoingWhat{ get; set; }
|
|
|
|
|
public string DownloadSpeed{ get; set; }
|
|
|
|
|
public string InfoText{ get; set; }
|
|
|
|
|
|
|
|
|
|
public CrunchyEpMeta epMeta{ get; set; }
|
|
|
|
|
|
2024-06-24 20:29:13 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
public bool Error{ get; set; }
|
|
|
|
|
|
|
|
|
|
public DownloadItemModel(CrunchyEpMeta epMetaF){
|
|
|
|
|
epMeta = epMetaF;
|
|
|
|
|
|
|
|
|
|
ImageUrl = epMeta.Image;
|
2024-08-09 21:16:13 +00:00
|
|
|
|
Title = epMeta.SeriesTitle + (!string.IsNullOrEmpty(epMeta.Season) ? " - S" + epMeta.Season + "E" + (epMeta.EpisodeNumber != string.Empty ? epMeta.EpisodeNumber : epMeta.AbsolutEpisodeNumberE) : "") + " - " +
|
|
|
|
|
epMeta.EpisodeTitle;
|
2024-06-24 20:29:13 +00:00
|
|
|
|
isDownloading = epMeta.DownloadProgress.IsDownloading || Done;
|
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
Done = epMeta.DownloadProgress.Done;
|
|
|
|
|
Percent = epMeta.DownloadProgress.Percent;
|
2024-07-06 18:48:58 +00:00
|
|
|
|
Time = "Estimated Time: " + TimeSpan.FromSeconds(epMeta.DownloadProgress.Time).ToString(@"hh\:mm\:ss");
|
2024-05-04 15:35:32 +00:00
|
|
|
|
DownloadSpeed = $"{epMeta.DownloadProgress.DownloadSpeed / 1000000.0:F2}Mb/s";
|
|
|
|
|
Paused = epMeta.Paused || !isDownloading && !epMeta.Paused;
|
2024-06-24 20:29:13 +00:00
|
|
|
|
DoingWhat = epMeta.Paused ? "Paused" : Done ? "Done" : epMeta.DownloadProgress.Doing != string.Empty ? epMeta.DownloadProgress.Doing : "Waiting";
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
2024-05-24 22:46:11 +00:00
|
|
|
|
if (epMeta.Data != null) InfoText = GetDubString() + " - " + GetSubtitleString();
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
Error = epMeta.DownloadProgress.Error;
|
|
|
|
|
}
|
2024-06-24 20:29:13 +00:00
|
|
|
|
|
2024-05-24 22:46:11 +00:00
|
|
|
|
private string GetDubString(){
|
2024-08-09 21:16:13 +00:00
|
|
|
|
if (epMeta.SelectedDubs == null || epMeta.SelectedDubs.Count < 1){
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return epMeta.SelectedDubs.Aggregate("Dub: ", (current, crunOptionsDlDub) => current + (crunOptionsDlDub + " "));
|
2024-05-24 22:46:11 +00:00
|
|
|
|
}
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
private string GetSubtitleString(){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
var hardSubs = CrunchyrollManager.Instance.CrunOptions.Hslang != "none" ? "Hardsub: " : "";
|
2024-05-04 15:35:32 +00:00
|
|
|
|
if (hardSubs != string.Empty){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
var locale = Languages.Locale2language(CrunchyrollManager.Instance.CrunOptions.Hslang).CrLocale;
|
|
|
|
|
if (epMeta.AvailableSubs != null && epMeta.AvailableSubs.Contains(locale)){
|
|
|
|
|
hardSubs += locale + " ";
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
return hardSubs;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-09 21:16:13 +00:00
|
|
|
|
if (epMeta.DownloadSubs.Count < 1){
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
var softSubs = "Softsub: ";
|
|
|
|
|
|
2024-07-16 23:52:46 +00:00
|
|
|
|
if (epMeta.DownloadSubs.Contains("all")){
|
2024-06-15 13:55:39 +00:00
|
|
|
|
if (epMeta.AvailableSubs != null){
|
2024-08-09 21:16:13 +00:00
|
|
|
|
return epMeta.AvailableSubs.Aggregate(softSubs, (current, epMetaAvailableSub) => current + (epMetaAvailableSub + " "));
|
2024-06-15 13:55:39 +00:00
|
|
|
|
}
|
2024-06-24 20:29:13 +00:00
|
|
|
|
}
|
2024-05-24 22:46:11 +00:00
|
|
|
|
|
2024-07-16 23:52:46 +00:00
|
|
|
|
foreach (var crunOptionsDlSub in epMeta.DownloadSubs){
|
2024-05-24 22:46:11 +00:00
|
|
|
|
if (epMeta.AvailableSubs != null && epMeta.AvailableSubs.Contains(crunOptionsDlSub)){
|
|
|
|
|
softSubs += crunOptionsDlSub + " ";
|
|
|
|
|
}
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return softSubs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Refresh(){
|
2024-06-24 20:29:13 +00:00
|
|
|
|
isDownloading = epMeta.DownloadProgress.IsDownloading || Done;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
Done = epMeta.DownloadProgress.Done;
|
|
|
|
|
Percent = epMeta.DownloadProgress.Percent;
|
2024-07-06 18:48:58 +00:00
|
|
|
|
Time = "Estimated Time: " + TimeSpan.FromSeconds(epMeta.DownloadProgress.Time).ToString(@"hh\:mm\:ss");
|
2024-05-04 15:35:32 +00:00
|
|
|
|
DownloadSpeed = $"{epMeta.DownloadProgress.DownloadSpeed / 1000000.0:F2}Mb/s";
|
|
|
|
|
|
|
|
|
|
Paused = epMeta.Paused || !isDownloading && !epMeta.Paused;
|
2024-06-24 20:29:13 +00:00
|
|
|
|
DoingWhat = epMeta.Paused ? "Paused" : Done ? "Done" : epMeta.DownloadProgress.Doing != string.Empty ? epMeta.DownloadProgress.Doing : "Waiting";
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
2024-05-24 22:46:11 +00:00
|
|
|
|
if (epMeta.Data != null) InfoText = GetDubString() + " - " + GetSubtitleString();
|
2024-06-24 20:29:13 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
Error = epMeta.DownloadProgress.Error;
|
2024-06-24 20:29:13 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
if (PropertyChanged != null){
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(isDownloading)));
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Percent)));
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Time)));
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DownloadSpeed)));
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DoingWhat)));
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Error)));
|
2024-05-24 22:46:11 +00:00
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InfoText)));
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
public void ToggleIsDownloading(){
|
|
|
|
|
if (isDownloading){
|
|
|
|
|
//StopDownload();
|
|
|
|
|
epMeta.Paused = !epMeta.Paused;
|
|
|
|
|
|
|
|
|
|
Paused = epMeta.Paused || !isDownloading && !epMeta.Paused;
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Paused)));
|
|
|
|
|
} else{
|
|
|
|
|
if (epMeta.Paused){
|
|
|
|
|
epMeta.Paused = false;
|
|
|
|
|
Paused = epMeta.Paused || !isDownloading && !epMeta.Paused;
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Paused)));
|
|
|
|
|
} else{
|
|
|
|
|
StartDownload();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-24 20:29:13 +00:00
|
|
|
|
|
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
if (PropertyChanged != null){
|
|
|
|
|
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("isDownloading"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void StartDownload(){
|
|
|
|
|
if (!isDownloading){
|
|
|
|
|
isDownloading = true;
|
|
|
|
|
epMeta.DownloadProgress.IsDownloading = true;
|
|
|
|
|
Paused = !epMeta.Paused && !isDownloading || epMeta.Paused;
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Paused)));
|
2024-07-21 09:30:20 +00:00
|
|
|
|
await CrunchyrollManager.Instance.DownloadEpisode(epMeta, CrunchyrollManager.Instance.CrunOptions);
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
public void RemoveFromQueue(){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyEpMeta? downloadItem = QueueManager.Instance.Queue.FirstOrDefault(e => e.Equals(epMeta)) ?? null;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
if (downloadItem != null){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
QueueManager.Instance.Queue.Remove(downloadItem);
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task LoadImage(){
|
|
|
|
|
try{
|
|
|
|
|
using (var client = new HttpClient()){
|
|
|
|
|
var response = await client.GetAsync(ImageUrl);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
using (var stream = await response.Content.ReadAsStreamAsync()){
|
|
|
|
|
ImageBitmap = new Bitmap(stream);
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ImageBitmap)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex){
|
|
|
|
|
// Handle exceptions
|
2024-06-19 00:16:02 +00:00
|
|
|
|
Console.Error.WriteLine("Failed to load image: " + ex.Message);
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|