Skip to content

Commit

Permalink
[Infra] Dialog services (#297)
Browse files Browse the repository at this point in the history
* Add dialog services to DI

* Add dialog types and view models to DI

* Rename IDialogActivationService methods

* Refactor AddVmArgumentDialog

* Refactor AuthenticationWizardDialog

* Refactor DeleteInstanceDialog

* Refactor DownloadResourceDialog

* Refactor ExceptionDialog

* Refactor SwitchAccountDialog

* Refactor UploadSkinDialog
  • Loading branch information
gaviny82 authored Dec 22, 2024
1 parent 2b43c06 commit cf24697
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Natsurainko.FluentLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static void ShowErrorMessage(string errorMessage)
{
try
{
await new ExceptionDialog(errorMessage).ShowAsync();
await new ExceptionDialog(errorMessage) { XamlRoot = MainWindow.XamlRoot }.ShowAsync();
}
catch
{
Expand Down
13 changes: 13 additions & 0 deletions Natsurainko.FluentLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@

#endregion

#region Configure WinUI dialogs

var dialogs = builder.Dialogs;

dialogs.WithDialog<Views.Common.AddVmArgumentDialog, ViewModels.Common.AddVmArgumentDialogViewModel>("AddVmArgumentDialog");
dialogs.WithDialog<Views.Common.AuthenticationWizardDialog, ViewModels.Common.AuthenticationWizardDialogViewModel>("AuthenticationWizardDialog");
dialogs.WithDialog<Views.Common.DeleteInstanceDialog, ViewModels.Common.DeleteInstanceDialogViewModel>("DeleteInstanceDialog");
dialogs.WithDialog<Views.Common.DownloadResourceDialog, ViewModels.Common.DownloadResourceDialogViewModel>("DownloadResourceDialog");
dialogs.WithDialog<Views.Common.SwitchAccountDialog, ViewModels.Common.SwitchAccountDialogViewModel>("SwitchAccountDialog");
dialogs.WithDialog<Views.Common.UploadSkinDialog, ViewModels.Common.UploadSkinDialogViewModel>("UploadSkinDialog");

#endregion

#region Services

var services = builder.Services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using Microsoft.UI.Xaml.Controls;
using Natsurainko.FluentLauncher.Utils;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Natsurainko.FluentLauncher.ViewModels.Common;

internal partial class AddVmArgumentDialogViewModel : ObservableObject
internal partial class AddVmArgumentDialogViewModel : ObservableObject, IDialogParameterAware
{
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(AddCommand))]
public partial string? Argument { get; set; }

private readonly Action<string> _addAction;
private Action<string> _addAction = null!;
private ContentDialog _dialog = null!;

public AddVmArgumentDialogViewModel(Action<string> addAction)
public AddVmArgumentDialogViewModel() { }

void IDialogParameterAware.HandleParameter(object param)
{
_addAction = addAction;
_addAction = (Action<string>)param;
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using FluentLauncher.Infra.UI.Navigation;
using Microsoft.UI.Xaml.Controls;
using Natsurainko.FluentLauncher.Services.Launch;
Expand All @@ -13,12 +14,12 @@

namespace Natsurainko.FluentLauncher.ViewModels.Common;

internal partial class DeleteInstanceDialogViewModel : ObservableObject
internal partial class DeleteInstanceDialogViewModel : ObservableObject, IDialogParameterAware
{
private readonly MinecraftInstance _minecraftInstance;
private readonly INavigationService _navigationService;
private readonly NotificationService _notificationService;
private readonly GameService _gameService;
private MinecraftInstance _minecraftInstance = null!;

private ContentDialog _dialog = null!; // Set in LoadEvent

Expand All @@ -28,17 +29,20 @@ internal partial class DeleteInstanceDialogViewModel : ObservableObject
public partial bool DeleteCoreSettings { get; set; } = true;

public DeleteInstanceDialogViewModel(
MinecraftInstance minecraftInstance,
INavigationService navigationService,
NotificationService notificationService,
GameService gameService)
{
_minecraftInstance = minecraftInstance;
_navigationService = navigationService;
_notificationService = notificationService;
_gameService = gameService;
}

void IDialogParameterAware.HandleParameter(object param)
{
_minecraftInstance = (MinecraftInstance)param;
}

[RelayCommand]
public void LoadEvent(object args)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using FluentLauncher.Infra.UI.Navigation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand All @@ -22,31 +23,44 @@
#nullable disable
namespace Natsurainko.FluentLauncher.ViewModels.Common;

internal partial class DownloadResourceDialogViewModel : ObservableObject
internal partial class DownloadResourceDialogViewModel : ObservableObject, IDialogParameterAware
{
private ContentDialog _dialog;
private GameResourceFile[] ResourceFileItems;

private readonly object _resource;
private readonly INavigationService _navigationService;
private readonly CurseForgeClient _curseForgeClient;
private readonly ModrinthClient _modrinthClient;

private readonly CurseForgeClient _curseForgeClient = App.GetService<CurseForgeClient>();
private readonly ModrinthClient _modrinthClient = App.GetService<ModrinthClient>();
private readonly GameService _gameService;
private readonly DownloadService _downloadService;
private readonly NotificationService _notificationService;

private readonly GameService _gameService = App.GetService<GameService>();
private readonly DownloadService _downloadService = App.GetService<DownloadService>();
private readonly NotificationService _notificationService = App.GetService<NotificationService>();
private object _resource = null!;

public MinecraftInstance MinecraftInstance { get; private set; }

public DownloadResourceDialogViewModel(object resource, INavigationService navigationService)
public DownloadResourceDialogViewModel(
CurseForgeClient curseForgeClient,
ModrinthClient modrinthClient,
GameService gameService,
DownloadService downloadService,
NotificationService notificaitonService)
{
_resource = resource;
_navigationService = navigationService;
_curseForgeClient = curseForgeClient;
_modrinthClient = modrinthClient;

_gameService = gameService;
_downloadService = downloadService;
_notificationService = notificaitonService;

MinecraftInstance = _gameService.ActiveGame;
}

void IDialogParameterAware.HandleParameter(object param)
{
(_resource, _) = ((object, INavigationService))param;
}

protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Win32;
using Natsurainko.FluentLauncher.Services.Network;
Expand All @@ -14,16 +15,18 @@
#nullable disable
namespace Natsurainko.FluentLauncher.ViewModels.Common;

public partial class UploadSkinDialogViewModel : ObservableObject
public partial class UploadSkinDialogViewModel : ObservableObject, IDialogParameterAware
{
private readonly Account _account;
private Account _account = null!;
private ContentDialog _dialog;

private readonly NotificationService _notificationService = App.GetService<NotificationService>();

public UploadSkinDialogViewModel(Account account)
public UploadSkinDialogViewModel() { }

void IDialogParameterAware.HandleParameter(object param)
{
_account = account;
_account = (Account)param;
}

[ObservableProperty]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using FluentLauncher.Infra.UI.Navigation;
using Microsoft.UI.Xaml.Controls;
using Natsurainko.FluentLauncher.Models.Launch;
using Natsurainko.FluentLauncher.Services.Accounts;
using Natsurainko.FluentLauncher.Utils.Extensions;
Expand Down Expand Up @@ -33,9 +35,12 @@ internal partial class ConfigViewModel : ObservableObject, INavigationAware
[ObservableProperty]
public partial Account TargetedAccount { get; set; }

public ConfigViewModel(AccountService accountService)
private readonly IDialogActivationService<ContentDialogResult> _dialogs;

public ConfigViewModel(AccountService accountService, IDialogActivationService<ContentDialogResult> dialogs)
{
Accounts = accountService.Accounts;
_dialogs = dialogs;
}

void INavigationAware.OnNavigatedTo(object parameter)
Expand Down Expand Up @@ -79,11 +84,7 @@ private void LoadTargetedAccount()
[RelayCommand]
public async Task AddArgument()
{
await new AddVmArgumentDialog()
{
DataContext = new AddVmArgumentDialogViewModel(VmArguments.Add)
}.ShowAsync();

await _dialogs.ShowAsync("AddVmArgumentDialog", (object)VmArguments.Add);
InstanceConfig.VmParameters = [.. VmArguments];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using FluentLauncher.Infra.UI.Navigation;
using Microsoft.UI.Xaml.Controls;
using Natsurainko.FluentLauncher.Models.Launch;
using Natsurainko.FluentLauncher.Services.Launch;
using Natsurainko.FluentLauncher.Services.UI;
Expand All @@ -24,6 +26,7 @@ internal partial class DefaultViewModel : ObservableObject, INavigationAware
private readonly GameService _gameService;
private readonly NotificationService _notificationService;
private readonly QuickLaunchService _quickLaunchService;
private readonly IDialogActivationService<ContentDialogResult> _dialogs;

private JumpList jumpList;

Expand All @@ -36,12 +39,14 @@ public DefaultViewModel(
GameService gameService,
INavigationService navigationService,
NotificationService notificationService,
QuickLaunchService quickLaunchService)
QuickLaunchService quickLaunchService,
IDialogActivationService<ContentDialogResult> dialogs)
{
_gameService = gameService;
_navigationService = navigationService;
_notificationService = notificationService;
_quickLaunchService = quickLaunchService;
_dialogs = dialogs;
}

[ObservableProperty]
Expand Down Expand Up @@ -102,10 +107,7 @@ private void InstanceConfig_PropertyChanged(object sender, PropertyChangedEventA
public async Task OpenVersionFolder() => await Launcher.LaunchFolderPathAsync(MinecraftInstance.GetGameDirectory());

[RelayCommand]
public async Task DeleteGame() => await new DeleteInstanceDialog()
{
DataContext = new DeleteInstanceDialogViewModel(MinecraftInstance, _navigationService, _notificationService, _gameService)
}.ShowAsync();
public async Task DeleteGame() => await _dialogs.ShowAsync("DeleteInstanceDialog", MinecraftInstance);

protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.UI.Dialogs;
using FluentLauncher.Infra.UI.Navigation;
using Microsoft.UI.Xaml.Controls;
using Natsurainko.FluentLauncher.Models.UI;
using Natsurainko.FluentLauncher.Services.Launch;
using Natsurainko.FluentLauncher.Services.Network;
Expand Down Expand Up @@ -28,6 +30,7 @@ internal partial class DefaultViewModel : ObservableObject, INavigationAware
private readonly CacheInterfaceService _cacheInterfaceService;
private readonly SearchProviderService _searchProviderService;
private readonly NotificationService _notificationService;
private readonly IDialogActivationService<ContentDialogResult> _dialogs;

private readonly CurseForgeClient _curseForgeClient;
private readonly ModrinthClient _modrinthClient;
Expand All @@ -39,13 +42,15 @@ public DefaultViewModel(
SearchProviderService searchProviderService,
NotificationService notificationService,
CurseForgeClient curseForgeClient,
ModrinthClient modrinthClient)
ModrinthClient modrinthClient,
IDialogActivationService<ContentDialogResult> dialogs)
{
_navigationService = navigationService;
_gameService = gameService;
_cacheInterfaceService = cacheInterfaceService;
_searchProviderService = searchProviderService;
_notificationService = notificationService;
_dialogs = dialogs;

_curseForgeClient = curseForgeClient;
_modrinthClient = modrinthClient;
Expand Down Expand Up @@ -231,7 +236,7 @@ void ParseModrinthTask(Task<IEnumerable<ModrinthResource>> task)
void SearchMoreModrinth() => _navigationService.NavigateTo("Download/Search", new SearchOptions { ResourceSource = 2 });

[RelayCommand]
async Task DownloadResource(object resource) => await new DownloadResourceDialog() { DataContext = new DownloadResourceDialogViewModel(resource, _navigationService) }.ShowAsync();
async Task DownloadResource(object resource) => await _dialogs.ShowAsync("DownloadResourceDialog", (resource, _navigationService));

[RelayCommand]
void ResourceDetails(object resource) => _navigationService.NavigateTo("Download/Details", resource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.UI.Controls;
using FluentLauncher.Infra.UI.Dialogs;
using FluentLauncher.Infra.UI.Navigation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand All @@ -21,18 +22,21 @@ internal partial class DetailsViewModel : ObservableObject, INavigationAware
private object _resource;

private readonly INavigationService _navigationService;
private readonly IDialogActivationService<ContentDialogResult> _dialogs;

private readonly CurseForgeClient _curseForgeClient;
private readonly ModrinthClient _modrinthClient;

public DetailsViewModel(
INavigationService navigationService,
CurseForgeClient curseForgeClient,
ModrinthClient modrinthClient)
ModrinthClient modrinthClient,
IDialogActivationService<ContentDialogResult> dialogs)
{
_navigationService = navigationService;
_curseForgeClient = curseForgeClient;
_modrinthClient = modrinthClient;
_dialogs = dialogs;
}

[ObservableProperty]
Expand Down Expand Up @@ -100,7 +104,7 @@ void ParseResource()
}

[RelayCommand]
async Task DownloadResource() => await new DownloadResourceDialog() { DataContext = new DownloadResourceDialogViewModel(_resource, _navigationService) }.ShowAsync();
async Task DownloadResource() => await _dialogs.ShowAsync("DownloadResourceDialog", (_resource, _navigationService));

[RelayCommand]
public async Task MarkdownTextBlockLoadedEvent(object args)
Expand Down
Loading

0 comments on commit cf24697

Please sign in to comment.