2024-05-04 15:35:32 +00:00
|
|
|
|
using System;
|
2024-07-12 02:35:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2024-07-26 00:18:39 +00:00
|
|
|
|
using System.IO;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using System.Linq;
|
2024-07-26 00:18:39 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Avalonia;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using Avalonia.Controls;
|
2024-07-26 00:18:39 +00:00
|
|
|
|
using Avalonia.Media.Imaging;
|
|
|
|
|
using Avalonia.Platform;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
using CRD.Downloader;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
using CRD.Downloader.Crunchyroll;
|
2024-06-21 19:35:51 +00:00
|
|
|
|
using CRD.Utils;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using CRD.Utils.Structs;
|
|
|
|
|
using DynamicData;
|
|
|
|
|
|
|
|
|
|
namespace CRD.ViewModels;
|
|
|
|
|
|
|
|
|
|
public partial class CalendarPageViewModel : ViewModelBase{
|
|
|
|
|
public ObservableCollection<CalendarDay> CalendarDays{ get; set; }
|
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
|
2024-07-12 02:35:33 +00:00
|
|
|
|
[ObservableProperty]
|
2024-07-12 13:53:44 +00:00
|
|
|
|
private bool _showLoading;
|
2024-07-12 02:35:33 +00:00
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2024-07-12 13:53:44 +00:00
|
|
|
|
private bool _customCalendar;
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _filterByAirDate;
|
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _hideDubs;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
public ObservableCollection<ComboBoxItem> CalendarDubFilter{ get; } = new(){
|
|
|
|
|
new ComboBoxItem(){ Content = "none" },
|
|
|
|
|
};
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private ComboBoxItem? _currentCalendarDubFilter;
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
public ObservableCollection<ComboBoxItem> CalendarLanguage{ get; } = new(){
|
|
|
|
|
new ComboBoxItem(){ Content = "en-us" },
|
|
|
|
|
new ComboBoxItem(){ Content = "es" },
|
|
|
|
|
new ComboBoxItem(){ Content = "es-es" },
|
|
|
|
|
new ComboBoxItem(){ Content = "pt-br" },
|
|
|
|
|
new ComboBoxItem(){ Content = "pt-pt" },
|
|
|
|
|
new ComboBoxItem(){ Content = "fr" },
|
|
|
|
|
new ComboBoxItem(){ Content = "de" },
|
|
|
|
|
new ComboBoxItem(){ Content = "ar" },
|
|
|
|
|
new ComboBoxItem(){ Content = "it" },
|
|
|
|
|
new ComboBoxItem(){ Content = "ru" },
|
|
|
|
|
new ComboBoxItem(){ Content = "hi" },
|
|
|
|
|
};
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private ComboBoxItem? _currentCalendarLanguage;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
private CalendarWeek? currentWeek;
|
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
private bool loading = true;
|
2024-07-26 00:18:39 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
public CalendarPageViewModel(){
|
2024-07-26 00:18:39 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
CalendarDays = new ObservableCollection<CalendarDay>();
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
foreach (var languageItem in Languages.languages){
|
|
|
|
|
CalendarDubFilter.Add(new ComboBoxItem{ Content = languageItem.CrLocale });
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CustomCalendar = CrunchyrollManager.Instance.CrunOptions.CustomCalendar;
|
|
|
|
|
HideDubs = CrunchyrollManager.Instance.CrunOptions.CalendarHideDubs;
|
|
|
|
|
FilterByAirDate = CrunchyrollManager.Instance.CrunOptions.CalendarFilterByAirDate;
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
ComboBoxItem? dubfilter = CalendarDubFilter.FirstOrDefault(a => a.Content != null && (string)a.Content == CrunchyrollManager.Instance.CrunOptions.CalendarDubFilter) ?? null;
|
2024-07-12 13:53:44 +00:00
|
|
|
|
CurrentCalendarDubFilter = dubfilter ?? CalendarDubFilter[0];
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CurrentCalendarLanguage = CalendarLanguage.FirstOrDefault(a => a.Content != null && (string)a.Content == CrunchyrollManager.Instance.CrunOptions.SelectedCalendarLanguage) ?? CalendarLanguage[0];
|
2024-07-12 13:53:44 +00:00
|
|
|
|
loading = false;
|
|
|
|
|
LoadCalendar(GetThisWeeksMondayDate(), false);
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
2024-07-26 00:18:39 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
private string GetThisWeeksMondayDate(){
|
|
|
|
|
// Get today's date
|
|
|
|
|
DateTime today = DateTime.Today;
|
|
|
|
|
|
|
|
|
|
// Calculate the number of days to subtract to get to Monday
|
|
|
|
|
// DayOfWeek.Monday is 1, so if today is Monday, subtract 0 days, if it's Tuesday subtract 1 day, etc.
|
|
|
|
|
int daysToSubtract = (int)today.DayOfWeek - (int)DayOfWeek.Monday;
|
|
|
|
|
|
|
|
|
|
// If today is Sunday (0), it will subtract -1, which we need to adjust to 6 to go back to the previous Monday
|
|
|
|
|
if (daysToSubtract < 0){
|
|
|
|
|
daysToSubtract += 7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the date of the most recent Monday
|
|
|
|
|
DateTime monday = today.AddDays(-daysToSubtract);
|
|
|
|
|
|
|
|
|
|
// Format and print the date
|
|
|
|
|
string formattedDate = monday.ToString("yyyy-MM-dd");
|
|
|
|
|
|
|
|
|
|
return formattedDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void LoadCalendar(string mondayDate, bool forceUpdate){
|
|
|
|
|
ShowLoading = true;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
|
|
|
|
|
CalendarWeek week;
|
|
|
|
|
|
|
|
|
|
if (CustomCalendar){
|
|
|
|
|
week = await CalendarManager.Instance.BuildCustomCalendar(forceUpdate);
|
|
|
|
|
} else{
|
|
|
|
|
week = await CalendarManager.Instance.GetCalendarForDate(mondayDate, forceUpdate);
|
|
|
|
|
if (currentWeek != null && currentWeek == week){
|
|
|
|
|
ShowLoading = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
2024-07-12 02:35:33 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
currentWeek = week;
|
|
|
|
|
CalendarDays.Clear();
|
|
|
|
|
CalendarDays.AddRange(week.CalendarDays);
|
|
|
|
|
RaisePropertyChanged(nameof(CalendarDays));
|
|
|
|
|
ShowLoading = false;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
if (CustomCalendar){
|
|
|
|
|
foreach (var calendarDay in CalendarDays){
|
|
|
|
|
foreach (var calendarDayCalendarEpisode in calendarDay.CalendarEpisodes){
|
|
|
|
|
if (calendarDayCalendarEpisode.ImageBitmap == null){
|
|
|
|
|
calendarDayCalendarEpisode.LoadImage();
|
|
|
|
|
}
|
2024-07-12 13:53:44 +00:00
|
|
|
|
}
|
2024-07-21 09:30:20 +00:00
|
|
|
|
}
|
|
|
|
|
} else{
|
|
|
|
|
foreach (var calendarDay in CalendarDays){
|
|
|
|
|
var episodesCopy = new List<CalendarEpisode>(calendarDay.CalendarEpisodes);
|
|
|
|
|
foreach (var calendarDayCalendarEpisode in episodesCopy){
|
|
|
|
|
if (calendarDayCalendarEpisode.SeasonName != null && HideDubs && calendarDayCalendarEpisode.SeasonName.EndsWith("Dub)")){
|
|
|
|
|
calendarDay.CalendarEpisodes.Remove(calendarDayCalendarEpisode);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
if (calendarDayCalendarEpisode.ImageBitmap == null){
|
|
|
|
|
calendarDayCalendarEpisode.LoadImage();
|
|
|
|
|
}
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string NextMonday(DateTime currentMonday){
|
|
|
|
|
DateTime nextMonday = currentMonday.AddDays(7);
|
|
|
|
|
return nextMonday.ToString("yyyy-MM-dd");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string PreviousMonday(DateTime currentMonday){
|
|
|
|
|
DateTime nextMonday = currentMonday.AddDays(-7);
|
|
|
|
|
return nextMonday.ToString("yyyy-MM-dd");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
public void Refresh(){
|
2024-07-12 13:53:44 +00:00
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
string mondayDate;
|
|
|
|
|
|
|
|
|
|
if (currentWeek is{ FirstDayOfWeekString: not null }){
|
|
|
|
|
mondayDate = currentWeek.FirstDayOfWeekString;
|
|
|
|
|
} else{
|
|
|
|
|
mondayDate = GetThisWeeksMondayDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadCalendar(mondayDate, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
public void PrevWeek(){
|
2024-07-12 13:53:44 +00:00
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
string mondayDate;
|
|
|
|
|
|
|
|
|
|
if (currentWeek is{ FirstDayOfWeek: not null }){
|
|
|
|
|
mondayDate = PreviousMonday((DateTime)currentWeek.FirstDayOfWeek);
|
|
|
|
|
} else{
|
|
|
|
|
mondayDate = GetThisWeeksMondayDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadCalendar(mondayDate, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
public void NextWeek(){
|
2024-07-12 13:53:44 +00:00
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
string mondayDate;
|
|
|
|
|
|
|
|
|
|
if (currentWeek is{ FirstDayOfWeek: not null }){
|
|
|
|
|
mondayDate = NextMonday((DateTime)currentWeek.FirstDayOfWeek);
|
|
|
|
|
} else{
|
|
|
|
|
mondayDate = GetThisWeeksMondayDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadCalendar(mondayDate, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
partial void OnCurrentCalendarLanguageChanged(ComboBoxItem? value){
|
2024-07-12 13:53:44 +00:00
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
if (value?.Content != null){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.SelectedCalendarLanguage = value.Content.ToString();
|
2024-05-04 15:35:32 +00:00
|
|
|
|
Refresh();
|
2024-06-21 19:35:51 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-12 02:35:33 +00:00
|
|
|
|
|
|
|
|
|
partial void OnCustomCalendarChanged(bool value){
|
2024-07-12 13:53:44 +00:00
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.CustomCalendar = value;
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
LoadCalendar(GetThisWeeksMondayDate(), true);
|
2024-07-26 00:18:39 +00:00
|
|
|
|
|
2024-07-12 13:53:44 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
partial void OnHideDubsChanged(bool value){
|
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
2024-07-12 02:35:33 +00:00
|
|
|
|
}
|
2024-07-13 11:26:11 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.CalendarHideDubs = value;
|
2024-07-12 13:53:44 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
|
|
|
|
}
|
2024-07-21 09:30:20 +00:00
|
|
|
|
|
2024-07-13 11:26:11 +00:00
|
|
|
|
partial void OnFilterByAirDateChanged(bool value){
|
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.CalendarFilterByAirDate = value;
|
2024-07-13 11:26:11 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
|
|
|
|
}
|
2024-07-12 13:53:44 +00:00
|
|
|
|
|
|
|
|
|
partial void OnCurrentCalendarDubFilterChanged(ComboBoxItem? value){
|
|
|
|
|
if (loading){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(value?.Content + "")){
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.CrunOptions.CalendarDubFilter = value?.Content + "";
|
2024-07-12 13:53:44 +00:00
|
|
|
|
CfgManager.WriteSettingsToFile();
|
|
|
|
|
}
|
2024-07-12 02:35:33 +00:00
|
|
|
|
}
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|