Crunchy-Downloader/CRD/Views/MainWindow.axaml.cs

176 lines
6.2 KiB
C#
Raw Normal View History

2024-05-04 15:35:32 +00:00
using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
2024-05-05 16:03:07 +00:00
using CRD.Utils.Updater;
2024-05-04 15:35:32 +00:00
using CRD.ViewModels;
using CRD.Views.Utils;
using FluentAvalonia.Core;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Windowing;
using ReactiveUI;
namespace CRD.Views;
public partial class MainWindow : AppWindow{
private Stack<object> navigationStack = new Stack<object>();
2024-05-05 16:03:07 +00:00
#region Singelton
private static MainWindow? _instance;
private static readonly object Padlock = new();
public static MainWindow Instance{
get{
if (_instance == null){
lock (Padlock){
if (_instance == null){
_instance = new MainWindow();
}
}
}
return _instance;
}
}
#endregion
2024-05-05 16:03:07 +00:00
private object selectedNavVieItem;
2024-05-04 15:35:32 +00:00
public MainWindow(){
AvaloniaXamlLoader.Load(this);
2024-05-04 15:35:32 +00:00
InitializeComponent();
2024-05-05 16:03:07 +00:00
2024-05-04 15:35:32 +00:00
TitleBar.ExtendsContentIntoTitleBar = true;
TitleBar.TitleBarHitTestType = TitleBarHitTestType.Complex;
//select first element as default
var nv = this.FindControl<NavigationView>("NavView");
nv.SelectedItem = nv.MenuItems.ElementAt(0);
2024-05-05 16:03:07 +00:00
selectedNavVieItem = nv.SelectedItem;
2024-05-04 15:35:32 +00:00
MessageBus.Current.Listen<NavigationMessage>()
.Subscribe(message => {
if (message.Refresh){
navigationStack.Pop();
var viewModel = Activator.CreateInstance(message.ViewModelType);
if (viewModel is SeriesPageViewModel){
((SeriesPageViewModel)viewModel).SetStorageProvider(StorageProvider);
}
2024-05-04 15:35:32 +00:00
navigationStack.Push(viewModel);
nv.Content = viewModel;
} else if (!message.Back && message.ViewModelType != null){
var viewModel = Activator.CreateInstance(message.ViewModelType);
if (viewModel is SeriesPageViewModel){
((SeriesPageViewModel)viewModel).SetStorageProvider(StorageProvider);
}
2024-05-04 15:35:32 +00:00
navigationStack.Push(viewModel);
nv.Content = viewModel;
} else{
navigationStack.Pop();
var viewModel = navigationStack.Peek();
nv.Content = viewModel;
}
});
MessageBus.Current.Listen<ToastMessage>()
.Subscribe(message => ShowToast(message.Message, message.Type, message.Seconds));
}
public async void ShowError(string message){
var dialog = new ContentDialog(){
Title = "Error",
Content = message,
CloseButtonText = "Close"
};
2024-05-05 16:03:07 +00:00
_ = await dialog.ShowAsync();
2024-05-04 15:35:32 +00:00
}
2024-05-04 15:35:32 +00:00
public void ShowToast(string message, ToastType type, int durationInSeconds = 5){
this.FindControl<ToastNotification>("Toast").Show(message, type, durationInSeconds);
}
2024-05-04 15:35:32 +00:00
private void NavView_SelectionChanged(object? sender, NavigationViewSelectionChangedEventArgs e){
if (sender is NavigationView navView){
var selectedItem = navView.SelectedItem as NavigationViewItem;
if (selectedItem != null){
switch (selectedItem.Tag){
case "DownloadQueue":
2024-05-05 16:03:07 +00:00
navView.Content = Activator.CreateInstance(typeof(DownloadsPageViewModel));
selectedNavVieItem = selectedItem;
2024-05-04 15:35:32 +00:00
break;
case "AddDownload":
2024-05-05 16:03:07 +00:00
navView.Content = Activator.CreateInstance(typeof(AddDownloadPageViewModel));
selectedNavVieItem = selectedItem;
2024-05-04 15:35:32 +00:00
break;
case "Calendar":
2024-05-05 16:03:07 +00:00
navView.Content = Activator.CreateInstance(typeof(CalendarPageViewModel));
selectedNavVieItem = selectedItem;
2024-05-04 15:35:32 +00:00
break;
case "History":
2024-05-05 16:03:07 +00:00
navView.Content = Activator.CreateInstance(typeof(HistoryPageViewModel));
2024-05-04 15:35:32 +00:00
navigationStack.Clear();
2024-05-05 16:03:07 +00:00
navigationStack.Push(navView.Content);
selectedNavVieItem = selectedItem;
2024-05-04 15:35:32 +00:00
break;
case "Account":
2024-05-05 16:03:07 +00:00
navView.Content = Activator.CreateInstance(typeof(AccountPageViewModel));
selectedNavVieItem = selectedItem;
2024-05-04 15:35:32 +00:00
break;
case "Settings":
var viewModel = (SettingsPageViewModel)Activator.CreateInstance(typeof(SettingsPageViewModel));
viewModel.SetStorageProvider(StorageProvider);
navView.Content = viewModel;
2024-05-05 16:03:07 +00:00
selectedNavVieItem = selectedItem;
break;
case "UpdateAvailable":
Updater.Instance.DownloadAndUpdateAsync();
ShowUpdateDialog();
2024-05-04 15:35:32 +00:00
break;
default:
2024-05-05 16:03:07 +00:00
// (sender as NavigationView).Content = Activator.CreateInstance(typeof(DownloadsPageViewModel));
2024-05-04 15:35:32 +00:00
break;
}
}
}
}
2024-05-05 16:03:07 +00:00
public async void ShowUpdateDialog(){
var dialog = new ContentDialog(){
Title = "Updating",
// CloseButtonText = "Close"
};
2024-05-05 16:03:07 +00:00
var viewModel = new ContentDialogUpdateViewModel(dialog);
dialog.Content = new ContentDialogUpdateView(){
DataContext = viewModel
};
_ = await dialog.ShowAsync();
}
2024-05-04 15:35:32 +00:00
}
public class ToastMessage(string message, ToastType type, int i){
public string? Message{ get; set; } = message;
public int Seconds{ get; set; } = i;
public ToastType Type{ get; set; } = type;
}
public class NavigationMessage{
public Type? ViewModelType{ get; }
public bool Back{ get; }
public bool Refresh{ get; }
public NavigationMessage(Type? viewModelType, bool back, bool refresh){
ViewModelType = viewModelType;
Back = back;
Refresh = refresh;
}
}