Skip to content

Commit

Permalink
重构版本选择页面
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Oct 2, 2024
1 parent d423e93 commit cc8f9fd
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 441 deletions.
2 changes: 1 addition & 1 deletion WonderLab/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ private static void ConfigureView(IServiceCollection services) {
}

private static void ConfigureServices(IServiceCollection services) {
services.AddTransient<GameService>();
services.AddTransient<BackendService>();
services.AddTransient<DownloadService>();

services.AddSingleton<GameService>();
services.AddSingleton<TaskService>();
services.AddSingleton<WrapService>();
services.AddSingleton<SkinService>();
Expand Down
5 changes: 5 additions & 0 deletions WonderLab/Classes/Datas/MessageData/ActiveGameEntryMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using WonderLab.Classes.Datas.ViewData;

namespace WonderLab.Classes.Datas.MessageData;

public sealed record ActiveGameEntryMessage(GameViewData Data);
3 changes: 3 additions & 0 deletions WonderLab/Classes/Datas/MessageData/GameManagerMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace WonderLab.Classes.Datas.MessageData;

public sealed record GameManagerMessage();
2 changes: 1 addition & 1 deletion WonderLab/Services/Game/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public GameService(SettingService settingService, ILogger<GameService> logger) {
}
}

private void Initialize() {
public void Initialize() {
_logger.LogInformation("开始初始化游戏实例服务");
GameResolver = new GameResolver(_settingService?.Data?.ActiveGameFolder ?? "C:\\Users\\w\\Desktop\\temp\\.minecraft");
RefreshGameViewEntry();
Expand Down
3 changes: 2 additions & 1 deletion WonderLab/Services/SettingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ private void ViewDataInitialize() {
_windowService.SetBackground(_settingData.BackgroundIndex);
}

public override async Task StopAsync(CancellationToken cancellationToken) {
public override Task StopAsync(CancellationToken cancellationToken) {
Save();
return Task.CompletedTask;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
Expand Down
21 changes: 14 additions & 7 deletions WonderLab/ViewModels/Pages/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using WonderLab.ViewModels.Dialogs;
using System;
using System.Threading;
using WonderLab.Classes.Datas.MessageData;

namespace WonderLab.ViewModels.Pages;

Expand All @@ -28,6 +29,7 @@ public sealed partial class HomePageViewModel : ViewModelBase {
private readonly AccountService _accountService;
private readonly SettingService _settingService;
private readonly NotificationService _notificationService;
private readonly WeakReferenceMessenger _weakReferenceMessenger;

[ObservableProperty] private bool _isGameEmpty;
[ObservableProperty] private GameViewData _activeGameEntry;
Expand All @@ -41,7 +43,8 @@ public HomePageViewModel(
DialogService dialogService,
AccountService accountService,
SettingService settingService,
NotificationService notificationService) {
NotificationService notificationService,
WeakReferenceMessenger weakReferenceMessenger) {
_gameService = gameService;
_taskService = taskService;
_dialogService = dialogService;
Expand All @@ -50,13 +53,12 @@ public HomePageViewModel(
_settingService = settingService;
_notificationService = notificationService;

GameEntries = _gameService.GameEntries.ToObservableList();
IsGameEmpty = GameEntries.Count == 0;

RunBackgroundWork(async() => {
await Task.Delay(250);
ActiveGameEntry = _gameService.ActiveGameEntry;
_weakReferenceMessenger = weakReferenceMessenger;
_weakReferenceMessenger.Register<ActiveGameEntryMessage>(this, (_, args) => {
ActiveGameEntry = args.Data;
});

ActiveGameEntry = gameService.ActiveGameEntry;
}

partial void OnActiveGameEntryChanged(GameViewData value) {
Expand Down Expand Up @@ -85,4 +87,9 @@ private async void Launch() {

await _launchService.LaunchWithDisplayTaskAsync(_accountService.AccountViewData.Account);
}

[RelayCommand]
private void OpenGameManager() {
_weakReferenceMessenger.Send(new GameManagerMessage());
}
}
15 changes: 9 additions & 6 deletions WonderLab/ViewModels/Pages/Setting/DetailSettingPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ namespace WonderLab.ViewModels.Pages.Setting;

public sealed partial class DetailSettingPageViewModel : ViewModelBase {
private readonly ThemeService _themeService;
private readonly DialogService _dialogService;
private readonly WindowService _windowService;
private readonly SettingService _settingService;
private readonly LanguageService _languageService;
private readonly NotificationService _notificationService;
private readonly DialogService _dialogService;
private readonly WeakReferenceMessenger _weakReferenceMessenger;

[ObservableProperty] private bool _isImage = false;
[ObservableProperty] private bool _isDebugMode = false;
Expand All @@ -40,16 +41,18 @@ public sealed partial class DetailSettingPageViewModel : ViewModelBase {
public DetailSettingPageViewModel(
ThemeService themeService,
WindowService windowService,
DialogService dialogService,
SettingService settingService,
LanguageService languageService,
NotificationService notificationService,
DialogService dialogService) {
WeakReferenceMessenger weakReferenceMessenger) {
_themeService = themeService;
_windowService = windowService;
_settingService = settingService;
_languageService = languageService;
_notificationService = notificationService;
_dialogService = dialogService;
_weakReferenceMessenger = weakReferenceMessenger;

BlurRadius = _settingService.Data.BlurRadius;
ThemeIndex = _settingService.Data.ThemeIndex;
Expand Down Expand Up @@ -105,19 +108,19 @@ protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
break;
case nameof(IsAlignCenter):
_settingService.Data.IsAlignCenter = IsAlignCenter;
WeakReferenceMessenger.Default.Send(new AlignCenterChangeMessage(IsAlignCenter));
_weakReferenceMessenger.Send(new AlignCenterChangeMessage(IsAlignCenter));
break;
case nameof(IsEnableBlur):
_settingService.Data.IsEnableBlur = IsEnableBlur;
WeakReferenceMessenger.Default.Send(new BlurEnableMessage(IsEnableBlur));
_weakReferenceMessenger.Send(new BlurEnableMessage(IsEnableBlur));
break;
case nameof(ParallaxMode):
_settingService.Data.ParallaxMode = ParallaxMode;
WeakReferenceMessenger.Default.Send(new ParallaxModeChangeMessage(ParallaxMode));
_weakReferenceMessenger.Send(new ParallaxModeChangeMessage(ParallaxMode));
break;
case nameof(BlurRadius):
_settingService.Data.BlurRadius = BlurRadius;
WeakReferenceMessenger.Default.Send(new BlurRadiusChangeMessage(BlurRadius));
_weakReferenceMessenger.Send(new BlurRadiusChangeMessage(BlurRadius));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
using System.IO;
using WonderLab.Extensions;
using Microsoft.Extensions.Logging;
using WonderLab.Services.Game;

namespace WonderLab.ViewModels.Pages.Setting;

public sealed partial class LaunchSettingPageViewModel : ViewModelBase {
private readonly SettingData _data;
private readonly JavaFetcher _javaFetcher;
private readonly GameService _gameService;
private readonly DialogService _dialogService;
private readonly SettingService _settingService;
private readonly ILogger<LaunchSettingPageViewModel> _logger;
Expand All @@ -42,9 +44,11 @@ public sealed partial class LaunchSettingPageViewModel : ViewModelBase {

public LaunchSettingPageViewModel(
JavaFetcher javaFetcher,
GameService gameService,
DialogService dialogService,
SettingService settingService,
ILogger<LaunchSettingPageViewModel> logger) {
_gameService = gameService;
_dialogService = dialogService;
_settingService = settingService;

Expand Down Expand Up @@ -146,6 +150,7 @@ protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
break;
case nameof(ActiveGameFolder):
_data.ActiveGameFolder = ActiveGameFolder;
_gameService.Initialize();
break;
case nameof(IsFullScreen):
_data.IsFullScreen = IsFullScreen;
Expand Down
69 changes: 49 additions & 20 deletions WonderLab/ViewModels/Windows/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
using WonderLab.ViewModels.Pages;
using CommunityToolkit.Mvvm.Input;
using WonderLab.Classes.Interfaces;
using WonderLab.Services.Navigation;
using Avalonia.Controls;
using Avalonia.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using WonderLab.ViewModels.Pages.Navigation;
using System.Collections.ObjectModel;
using WonderLab.Services;
using WonderLab.Services.UI;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using WonderLab.Classes.Datas.MessageData;
using WonderLab.Classes.Datas.ViewData;
using WonderLab.Classes.Enums;
using Avalonia.Controls;
using Avalonia.Input;
using WonderLab.Classes.Interfaces;
using WonderLab.Extensions;
using WonderLab.Services;
using WonderLab.Services.Game;
using WonderLab.Services.Navigation;
using WonderLab.Services.UI;
using WonderLab.ViewModels.Dialogs;
using System.Linq;
using WonderLab.ViewModels.Pages;
using WonderLab.ViewModels.Pages.Navigation;
using WonderLab.Views.Controls;
using WonderLab.Classes.Datas.ViewData;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace WonderLab.ViewModels.Windows;

public sealed partial class MainWindowViewModel : ViewModelBase {
private GameService _gameService;
private readonly TaskService _taskService;
private readonly DialogService _dialogService;
private readonly SettingService _settingService;
private readonly LanguageService _languageService;
private readonly NotificationService _notificationService;
private readonly WeakReferenceMessenger _weakReferenceMessenger;

public readonly HostNavigationService _navigationService;

Expand All @@ -34,12 +40,15 @@ public sealed partial class MainWindowViewModel : ViewModelBase {

[ObservableProperty] private object _activePage;
[ObservableProperty] private ParallaxMode _parallaxMode;
[ObservableProperty] private GameViewData _activeGameEntry;

[ObservableProperty] private bool _isEnableBlur;
[ObservableProperty] private bool _isAlignCenter;
[ObservableProperty] private bool _isOpenGamePanel;
[ObservableProperty] private bool _isOpenTaskListPanel;
[ObservableProperty] private bool _isOpenBackgroundPanel;

[ObservableProperty] private ReadOnlyObservableCollection<GameViewData> _gameEntries;
[ObservableProperty] private ReadOnlyObservableCollection<TaskViewData> _tasks;
[ObservableProperty] private ReadOnlyObservableCollection<INotification> _notifications;

Expand All @@ -51,18 +60,29 @@ public MainWindowViewModel(
SettingService settingService,
LanguageService languageService,
HostNavigationService navigationService,
NotificationService notificationService) {
NotificationService notificationService,
WeakReferenceMessenger weakReferenceMessenger) {
_taskService = taskService;
_dialogService = dialogService;
_settingService = settingService;
_languageService = languageService;
_navigationService = navigationService;
_notificationService = notificationService;
_weakReferenceMessenger = weakReferenceMessenger;

weakReferenceMessenger.Register<BlurEnableMessage>(this, BlurEnableValueHandle);
weakReferenceMessenger.Register<BlurRadiusChangeMessage>(this, BlurRadiusChangeHandle);
weakReferenceMessenger.Register<AlignCenterChangeMessage>(this, AlignCenterChangeHandle);
weakReferenceMessenger.Register<ParallaxModeChangeMessage>(this, ParallaxModeChangeHandle);

WeakReferenceMessenger.Default.Register<BlurEnableMessage>(this, BlurEnableValueHandle);
WeakReferenceMessenger.Default.Register<BlurRadiusChangeMessage>(this, BlurRadiusChangeHandle);
WeakReferenceMessenger.Default.Register<AlignCenterChangeMessage>(this, AlignCenterChangeHandle);
WeakReferenceMessenger.Default.Register<ParallaxModeChangeMessage>(this, ParallaxModeChangeHandle);
weakReferenceMessenger.Register<SettingDataChangedMessage>(this, (_, _) => {
_gameService = App.GetService<GameService>();
});

weakReferenceMessenger.Register<GameManagerMessage>(this, (_, _) => {
IsOpenGamePanel = !IsOpenGamePanel;
GameEntries = _gameService.GameEntries;
});
}

[RelayCommand]
Expand Down Expand Up @@ -117,9 +137,7 @@ private void ParallaxModeChangeHandle(object obj, ParallaxModeChangeMessage para
};
}

public void OnLoaded(object sender, object args) {
//_taskService.QueueJob(new InitTask(_languageService, _settingService, _dialogService, _notificationService));

public void OnLoaded() {
Tasks = new(_taskService.DisplayTasks);
Notifications = new(_notificationService.Notifications);

Expand All @@ -132,6 +150,8 @@ public void OnLoaded(object sender, object args) {
};

HomePage = _navigationService.NavigationToHome();

GameEntries = _gameService.GameEntries;
}

public void OnDrop(object sender, DragEventArgs args) {
Expand All @@ -150,4 +170,13 @@ public void OnDragEnter(object sender, DragEventArgs args) {
public void OnDragLeave(object sender, DragEventArgs args) {
_dialogService.CloseContentDialog();
}

protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
base.OnPropertyChanged(e);

if (e.PropertyName is nameof(ActiveGameEntry)) {
_gameService.ActivateGameViewEntry(ActiveGameEntry);
_weakReferenceMessenger.Send(new ActiveGameEntryMessage(ActiveGameEntry));
}
}
}
Loading

0 comments on commit cc8f9fd

Please sign in to comment.