Crunchy-Downloader/CRD/ViewModels/MainWindowViewModel.cs

75 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-05-05 16:03:07 +00:00
using System;
using System.IO;
2024-05-05 16:03:07 +00:00
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Avalonia;
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;
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;
[ObservableProperty]
private bool _finishedLoading = false;
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();
CleanUpOldUpdater();
2024-05-04 15:35:32 +00:00
}
private void CleanUpOldUpdater(){
string backupFilePath = Path.Combine(Directory.GetCurrentDirectory(), "Updater.exe.bak");
2024-05-05 16:03:07 +00:00
if (File.Exists(backupFilePath)){
try{
File.Delete(backupFilePath);
Console.WriteLine($"Deleted old updater file: {backupFilePath}");
} catch (Exception ex){
Console.Error.WriteLine($"Failed to delete old updater file: {ex.Message}");
}
} else{
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();
CrunchyrollManager.Instance.InitOptions();
if (CrunchyrollManager.Instance.CrunOptions.AccentColor != null){
_faTheme.CustomAccentColor = Color.Parse(CrunchyrollManager.Instance.CrunOptions.AccentColor);
2024-05-04 15:35:32 +00:00
}
if (CrunchyrollManager.Instance.CrunOptions.Theme == "System"){
2024-05-04 15:35:32 +00:00
_faTheme.PreferSystemTheme = true;
} 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;
}
await CrunchyrollManager.Instance.Init();
FinishedLoading = true;
2024-05-04 15:35:32 +00:00
}
}