2024-05-05 16:03:07 +00:00
|
|
|
|
using System;
|
2024-05-11 11:08:24 +00:00
|
|
|
|
using System.IO;
|
2024-05-05 16:03:07 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Avalonia;
|
2024-08-20 16:40:45 +00:00
|
|
|
|
using Avalonia.Controls.Chrome;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
using Avalonia.Styling;
|
2024-05-05 16:03:07 +00:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using CRD.Downloader;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
using CRD.Downloader.Crunchyroll;
|
2024-05-05 16:03:07 +00:00
|
|
|
|
using CRD.Utils.Updater;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
using FluentAvalonia.Styling;
|
2024-05-05 16:03:07 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
namespace CRD.ViewModels;
|
|
|
|
|
|
|
|
|
|
public partial class MainWindowViewModel : ViewModelBase{
|
|
|
|
|
private readonly FluentAvaloniaTheme _faTheme;
|
|
|
|
|
|
2024-05-05 16:03:07 +00:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _updateAvailable = true;
|
2024-08-09 21:16:13 +00:00
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _finishedLoading = false;
|
2024-07-22 19:11:13 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
public MainWindowViewModel(){
|
|
|
|
|
_faTheme = App.Current.Styles[0] as FluentAvaloniaTheme;
|
2024-05-05 16:03:07 +00:00
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
Init();
|
2024-05-11 11:08:24 +00:00
|
|
|
|
|
|
|
|
|
CleanUpOldUpdater();
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 19:11:13 +00:00
|
|
|
|
private void CleanUpOldUpdater(){
|
2024-05-11 11:08:24 +00:00
|
|
|
|
string backupFilePath = Path.Combine(Directory.GetCurrentDirectory(), "Updater.exe.bak");
|
2024-05-05 16:03:07 +00:00
|
|
|
|
|
2024-07-22 19:11:13 +00:00
|
|
|
|
if (File.Exists(backupFilePath)){
|
|
|
|
|
try{
|
2024-05-11 11:08:24 +00:00
|
|
|
|
File.Delete(backupFilePath);
|
|
|
|
|
Console.WriteLine($"Deleted old updater file: {backupFilePath}");
|
2024-07-22 19:11:13 +00:00
|
|
|
|
} catch (Exception ex){
|
2024-06-19 00:16:02 +00:00
|
|
|
|
Console.Error.WriteLine($"Failed to delete old updater file: {ex.Message}");
|
2024-05-11 11:08:24 +00:00
|
|
|
|
}
|
2024-07-22 19:11:13 +00:00
|
|
|
|
} else{
|
2024-05-11 11:08:24 +00:00
|
|
|
|
Console.WriteLine("No old updater file found to delete.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-05 16:03:07 +00:00
|
|
|
|
|
|
|
|
|
|
2024-05-04 15:35:32 +00:00
|
|
|
|
public async void Init(){
|
2024-05-05 16:03:07 +00:00
|
|
|
|
UpdateAvailable = await Updater.Instance.CheckForUpdatesAsync();
|
2024-07-22 19:11:13 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
CrunchyrollManager.Instance.InitOptions();
|
2024-07-16 23:52:46 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
if (CrunchyrollManager.Instance.CrunOptions.AccentColor != null){
|
|
|
|
|
_faTheme.CustomAccentColor = Color.Parse(CrunchyrollManager.Instance.CrunOptions.AccentColor);
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
2024-08-20 16:40:45 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
if (CrunchyrollManager.Instance.CrunOptions.Theme == "System"){
|
2024-05-04 15:35:32 +00:00
|
|
|
|
_faTheme.PreferSystemTheme = true;
|
2024-07-21 09:30:20 +00:00
|
|
|
|
} else if (CrunchyrollManager.Instance.CrunOptions.Theme == "Dark"){
|
2024-05-04 15:35:32 +00:00
|
|
|
|
_faTheme.PreferSystemTheme = false;
|
|
|
|
|
Application.Current.RequestedThemeVariant = ThemeVariant.Dark;
|
|
|
|
|
} else{
|
|
|
|
|
_faTheme.PreferSystemTheme = false;
|
|
|
|
|
Application.Current.RequestedThemeVariant = ThemeVariant.Light;
|
|
|
|
|
}
|
2024-07-22 19:11:13 +00:00
|
|
|
|
|
2024-07-21 09:30:20 +00:00
|
|
|
|
await CrunchyrollManager.Instance.Init();
|
2024-08-09 21:16:13 +00:00
|
|
|
|
|
|
|
|
|
FinishedLoading = true;
|
2024-05-04 15:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|