remove some usage of Ioc.GetRequiredService when injection is possible.

This commit is contained in:
Hongtao Zhang 2025-02-23 21:54:08 +08:00
parent ad63b0e7f4
commit f5f0986036
5 changed files with 18 additions and 23 deletions

View file

@ -3,6 +3,7 @@ using Flow.Launcher.Plugin.SharedModels;
using System;
using System.Collections.Generic;
using System.Linq;
using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher.Infrastructure
{
@ -14,15 +15,11 @@ namespace Flow.Launcher.Infrastructure
private readonly IAlphabet _alphabet;
public StringMatcher()
{
_alphabet = Ioc.Default.GetRequiredService<IAlphabet>();
}
// This is a workaround to allow unit tests to set the instance
public StringMatcher(IAlphabet alphabet)
public StringMatcher(IAlphabet alphabet, Settings settings)
{
_alphabet = alphabet;
UserSettingSearchPrecision = settings.QuerySearchPrecision;
}
public static MatchResult FuzzySearch(string query, string stringToCompare)

View file

@ -91,8 +91,6 @@ namespace Flow.Launcher
AbstractPluginEnvironment.PreStartPluginExecutablePathUpdate(_settings);
Ioc.Default.GetRequiredService<StringMatcher>().UserSettingSearchPrecision = _settings.QuerySearchPrecision;
// TODO: Clean InternationalizationManager.Instance and InternationalizationManager.Instance.GetTranslation in future
InternationalizationManager.Instance.ChangeLanguage(_settings.Language);

View file

@ -247,7 +247,7 @@ namespace Flow.Launcher
public void OpenDirectory(string DirectoryPath, string FileNameOrFilePath = null)
{
using var explorer = new Process();
var explorerInfo = _settingsVM.Settings.CustomExplorer;
var explorerInfo = _settingsVM._settings.CustomExplorer;
explorer.StartInfo = new ProcessStartInfo
{
@ -268,7 +268,7 @@ namespace Flow.Launcher
{
if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
{
var browserInfo = _settingsVM.Settings.CustomBrowser;
var browserInfo = _settingsVM._settings.CustomBrowser;
var path = browserInfo.Path == "*" ? "" : browserInfo.Path;

View file

@ -27,7 +27,7 @@ public partial class SettingWindow
public SettingWindow()
{
var viewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
_settings = viewModel.Settings;
_settings = Ioc.Default.GetRequiredService<Settings>();
DataContext = viewModel;
_viewModel = viewModel;
_updater = Ioc.Default.GetRequiredService<Updater>();

View file

@ -6,11 +6,11 @@ namespace Flow.Launcher.ViewModel;
public partial class SettingWindowViewModel : BaseModel
{
public Settings Settings { get; init; }
public readonly Settings _settings;
public SettingWindowViewModel()
public SettingWindowViewModel(Settings settings)
{
Settings = Ioc.Default.GetRequiredService<Settings>();
_settings = settings;
}
/// <summary>
@ -18,30 +18,30 @@ public partial class SettingWindowViewModel : BaseModel
/// </summary>
public void Save()
{
Settings.Save();
_settings.Save();
}
public double SettingWindowWidth
{
get => Settings.SettingWindowWidth;
set => Settings.SettingWindowWidth = value;
get => _settings.SettingWindowWidth;
set => _settings.SettingWindowWidth = value;
}
public double SettingWindowHeight
{
get => Settings.SettingWindowHeight;
set => Settings.SettingWindowHeight = value;
get => _settings.SettingWindowHeight;
set => _settings.SettingWindowHeight = value;
}
public double? SettingWindowTop
{
get => Settings.SettingWindowTop;
set => Settings.SettingWindowTop = value;
get => _settings.SettingWindowTop;
set => _settings.SettingWindowTop = value;
}
public double? SettingWindowLeft
{
get => Settings.SettingWindowLeft;
set => Settings.SettingWindowLeft = value;
get => _settings.SettingWindowLeft;
set => _settings.SettingWindowLeft = value;
}
}