-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindowViewModel.cs
107 lines (94 loc) · 3.37 KB
/
MainWindowViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aminos.BiliLive.Models;
using Aminos.BiliLive.ViewModels;
using CommunityToolkit.Mvvm.Input;
using Avalonia.Controls;
using SukiUI;
using Aminos.BiliLive.Services;
using CommunityToolkit.Mvvm.Messaging;
using Tmds.DBus.Protocol;
using Aminos.BiliLive.Utils;
using SukiUI.Dialogs;
using SukiUI.Toasts;
namespace Aminos.BiliLive
{
public partial class MainWindowViewModel : ObservableObject
{
public AvaloniaList<ViewModelBase> Pages { get; }
[ObservableProperty]
private ViewModelBase? _currentPage;
[ObservableProperty]
private bool _loading;
private readonly ConfigService _configService;
private readonly IServiceProvider _serviceProvider;
public ISukiToastManager ToastManager { get; }
public ISukiDialogManager DialogManager { get; }
public MainWindowViewModel(
ConfigService configService,
IServiceProvider serviceProvider,
ISukiToastManager toastManager,
ISukiDialogManager dialogManager)
{
_configService = configService;
_serviceProvider = serviceProvider;
ToastManager = toastManager;
DialogManager = dialogManager;
Pages = new AvaloniaList<ViewModelBase>();
MinimalEventBus.Global.Subscribe(MinimalEventBus.EventName.ChangeMenu, HandleChangeMenu);
MinimalEventBus.Global.Subscribe(MinimalEventBus.EventName.ReloadMenu, HandleReloadMenu);
}
~MainWindowViewModel()
{
MinimalEventBus.Global.Unsubscribe(MinimalEventBus.EventName.ChangeMenu, HandleChangeMenu);
MinimalEventBus.Global.Unsubscribe(MinimalEventBus.EventName.ReloadMenu, HandleReloadMenu);
}
public IRelayCommand OnViewShowCommand => new RelayCommand(OnViewShow);
private async void OnViewShow()
{
Loading = true;
await _configService.LoadAsync();
await Task.Delay(500);
var vms = _serviceProvider.GetServices<ViewModelBase>();
Loading = false;
Pages.AddRange(vms.OrderBy(o => o.Index));
}
private void HandleChangeMenu(MinimalEventArg eventArg)
{
Loading = true;
if (eventArg.Data is string modelName)
{
var find = Pages.FirstOrDefault(o => o.ModelName == modelName);
if (find != null)
{
CurrentPage = find;
}
}
Loading = false;
}
private async void HandleReloadMenu(MinimalEventArg eventArg)
{
Loading = true;
Pages.Clear();
await Task.Delay(500);
var vms = _serviceProvider.GetServices<ViewModelBase>();
Pages.AddRange(vms.OrderBy(o => o.Index));
Loading = false;
}
[RelayCommand]
public async Task ChangeLightDarkCommand()
{
await _configService.ChangeBaseThemeAsync();
}
[RelayCommand]
public async Task ChangeThemeColorCommand()
{
await _configService.ChangeThemeColorAsync();
}
}
}