Skip to content

Use dependency injection instead of navigation parameter #3311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Windows.Navigation;
using System.Windows.Navigation;
using Flow.Launcher.Core;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.Infrastructure.UserSettings;
using CommunityToolkit.Mvvm.DependencyInjection;

namespace Flow.Launcher.SettingPages.Views;

Expand All @@ -12,8 +14,8 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater })
throw new ArgumentException("Settings are required for SettingsPaneAbout.");
var settings = Ioc.Default.GetRequiredService<Settings>();
var updater = Ioc.Default.GetRequiredService<Updater>();
_viewModel = new SettingsPaneAboutViewModel(settings, updater);
DataContext = _viewModel;
InitializeComponent();
Expand Down
11 changes: 7 additions & 4 deletions Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Windows.Navigation;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.SettingPages.ViewModels;

Expand All @@ -13,8 +15,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: {} updater, Portable: {} portable })
throw new ArgumentException("Settings, Updater and Portable are required for SettingsPaneGeneral.");
var settings = Ioc.Default.GetRequiredService<Settings>();
var updater = Ioc.Default.GetRequiredService<Updater>();
var portable = Ioc.Default.GetRequiredService<Portable>();
_viewModel = new SettingsPaneGeneralViewModel(settings, updater, portable);
DataContext = _viewModel;
InitializeComponent();
Expand Down
8 changes: 4 additions & 4 deletions Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Windows.Navigation;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.Infrastructure.UserSettings;

namespace Flow.Launcher.SettingPages.Views;

Expand All @@ -12,8 +13,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings })
throw new ArgumentException("Settings are required for SettingsPaneHotkey.");
var settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = new SettingsPaneHotkeyViewModel(settings);
DataContext = _viewModel;
InitializeComponent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.ComponentModel;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.ViewModel;
using Flow.Launcher.Infrastructure.UserSettings;

namespace Flow.Launcher.SettingPages.Views;

Expand All @@ -16,8 +17,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings })
throw new ArgumentException($"Settings are required for {nameof(SettingsPanePluginStore)}.");
var settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = new SettingsPanePluginStoreViewModel();
DataContext = _viewModel;
InitializeComponent();
Expand Down
8 changes: 4 additions & 4 deletions Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Windows.Input;
using System.Windows.Input;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.Infrastructure.UserSettings;

namespace Flow.Launcher.SettingPages.Views;

Expand All @@ -13,8 +14,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings })
throw new ArgumentException("Settings are required for SettingsPaneHotkey.");
var settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = new SettingsPanePluginsViewModel(settings);
DataContext = _viewModel;
InitializeComponent();
Expand Down
10 changes: 6 additions & 4 deletions Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Windows.Navigation;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.Infrastructure.UserSettings;

namespace Flow.Launcher.SettingPages.Views;

Expand All @@ -12,8 +14,8 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater })
throw new ArgumentException($"Settings are required for {nameof(SettingsPaneProxy)}.");
var settings = Ioc.Default.GetRequiredService<Settings>();
var updater = Ioc.Default.GetRequiredService<Updater>();
_viewModel = new SettingsPaneProxyViewModel(settings, updater);
DataContext = _viewModel;
InitializeComponent();
Expand Down
10 changes: 4 additions & 6 deletions Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.SettingPages.ViewModels;
using Page = ModernWpf.Controls.Page;
using Flow.Launcher.Infrastructure.UserSettings;

namespace Flow.Launcher.SettingPages.Views;

Expand All @@ -16,8 +15,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!IsInitialized)
{
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings })
throw new ArgumentException($"Settings are required for {nameof(SettingsPaneTheme)}.");
var settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = new SettingsPaneThemeViewModel(settings);
DataContext = _viewModel;
InitializeComponent();
Expand Down
13 changes: 2 additions & 11 deletions Flow.Launcher/SettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Windows.Input;
using System.Windows.Interop;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
Expand All @@ -18,8 +16,6 @@ namespace Flow.Launcher;

public partial class SettingWindow
{
private readonly Updater _updater;
private readonly IPortable _portable;
private readonly IPublicAPI _api;
private readonly Settings _settings;
private readonly SettingWindowViewModel _viewModel;
Expand All @@ -30,8 +26,6 @@ public SettingWindow()
_settings = Ioc.Default.GetRequiredService<Settings>();
DataContext = viewModel;
_viewModel = viewModel;
_updater = Ioc.Default.GetRequiredService<Updater>();
_portable = Ioc.Default.GetRequiredService<Portable>();
_api = Ioc.Default.GetRequiredService<IPublicAPI>();
InitializePosition();
InitializeComponent();
Expand Down Expand Up @@ -166,10 +160,9 @@ private double WindowTop()

private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
var paneData = new PaneData(_settings, _updater, _portable);
if (args.IsSettingsSelected)
{
ContentFrame.Navigate(typeof(SettingsPaneGeneral), paneData);
ContentFrame.Navigate(typeof(SettingsPaneGeneral));
}
else
{
Expand All @@ -191,7 +184,7 @@ private void NavigationView_SelectionChanged(NavigationView sender, NavigationVi
nameof(About) => typeof(SettingsPaneAbout),
_ => typeof(SettingsPaneGeneral)
};
ContentFrame.Navigate(pageType, paneData);
ContentFrame.Navigate(pageType);
}
}

Expand All @@ -211,6 +204,4 @@ private void ContentFrame_Loaded(object sender, RoutedEventArgs e)
{
NavView.SelectedItem ??= NavView.MenuItems[0]; /* Set First Page */
}

public record PaneData(Settings Settings, Updater Updater, IPortable Portable);
}
Loading