Skip to content

Commit 1b76a2b

Browse files
committed
Use dependency injection for all services
1 parent 32cac76 commit 1b76a2b

File tree

8 files changed

+67
-45
lines changed

8 files changed

+67
-45
lines changed

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<ItemGroup>
5555
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
5656
<PackageReference Include="BitFaster.Caching" Version="2.5.2" />
57+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
5758
<PackageReference Include="Fody" Version="6.5.5">
5859
<PrivateAssets>all</PrivateAssets>
5960
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using JetBrains.Annotations;
77
using Flow.Launcher.Infrastructure.UserSettings;
88
using ToolGood.Words.Pinyin;
9+
using CommunityToolkit.Mvvm.DependencyInjection;
910

1011
namespace Flow.Launcher.Infrastructure
1112
{
@@ -129,7 +130,12 @@ public class PinyinAlphabet : IAlphabet
129130

130131
private Settings _settings;
131132

132-
public void Initialize([NotNull] Settings settings)
133+
public PinyinAlphabet()
134+
{
135+
Initialize(Ioc.Default.GetRequiredService<Settings>());
136+
}
137+
138+
private void Initialize([NotNull] Settings settings)
133139
{
134140
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
135141
}

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Flow.Launcher.Plugin.SharedModels;
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Flow.Launcher.Plugin.SharedModels;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -15,7 +16,7 @@ public class StringMatcher
1516

1617
public StringMatcher(IAlphabet alphabet = null)
1718
{
18-
_alphabet = alphabet;
19+
_alphabet = Ioc.Default.GetRequiredService<IAlphabet>();
1920
}
2021

2122
public static StringMatcher Instance { get; internal set; }

Flow.Launcher/App.xaml.cs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66
using System.Windows;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
78
using Flow.Launcher.Core;
89
using Flow.Launcher.Core.Configuration;
910
using Flow.Launcher.Core.ExternalPlugins.Environments;
@@ -18,23 +19,18 @@
1819
using Flow.Launcher.Infrastructure.UserSettings;
1920
using Flow.Launcher.Plugin;
2021
using Flow.Launcher.ViewModel;
22+
using Microsoft.Extensions.DependencyInjection;
23+
using Microsoft.Extensions.Hosting;
2124
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
2225

2326
namespace Flow.Launcher
2427
{
25-
public partial class App : IDisposable, ISingleInstanceApp, IApp
28+
public partial class App : IDisposable, ISingleInstanceApp
2629
{
27-
public IPublicAPI PublicAPI => API;
2830
public static PublicAPIInstance API { get; private set; }
2931
private const string Unique = "Flow.Launcher_Unique_Application_Mutex";
3032
private static bool _disposed;
3133
private Settings _settings;
32-
private MainViewModel _mainVM;
33-
private SettingWindowViewModel _settingsVM;
34-
private readonly Updater _updater = new Updater(Flow.Launcher.Properties.Settings.Default.GithubRepo);
35-
private readonly Portable _portable = new Portable();
36-
private readonly PinyinAlphabet _alphabet = new PinyinAlphabet();
37-
private StringMatcher _stringMatcher;
3834

3935
[STAThread]
4036
public static void Main()
@@ -53,52 +49,67 @@ private async void OnStartupAsync(object sender, StartupEventArgs e)
5349
{
5450
await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
5551
{
52+
// Initialize settings
5653
var storage = new FlowLauncherJsonStorage<Settings>();
5754
_settings = storage.Load();
5855
_settings.Initialize(storage);
59-
60-
_portable.PreStartCleanUpAfterPortabilityUpdate();
61-
62-
Log.Info(
63-
"|App.OnStartup|Begin Flow Launcher startup ----------------------------------------------------");
56+
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
57+
58+
// Configure the dependency injection container
59+
var host = Host.CreateDefaultBuilder()
60+
.UseContentRoot(AppContext.BaseDirectory)
61+
.ConfigureServices(services => services
62+
.AddSingleton(_ => _settings)
63+
.AddSingleton<Updater>()
64+
.AddSingleton<Portable>()
65+
.AddSingleton<SettingWindowViewModel>()
66+
.AddSingleton<IAlphabet, PinyinAlphabet>()
67+
.AddSingleton<StringMatcher>()
68+
.AddSingleton<IPublicAPI, PublicAPIInstance>()
69+
.AddSingleton<MainViewModel>()
70+
).Build();
71+
Ioc.Default.ConfigureServices(host.Services);
72+
73+
Ioc.Default.GetRequiredService<Updater>().Initialize(Launcher.Properties.Settings.Default.GithubRepo);
74+
75+
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
76+
77+
Log.Info("|App.OnStartup|Begin Flow Launcher startup ----------------------------------------------------");
6478
Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}");
79+
6580
RegisterAppDomainExceptions();
6681
RegisterDispatcherUnhandledException();
6782

6883
var imageLoadertask = ImageLoader.InitializeAsync();
6984

70-
_settingsVM = new SettingWindowViewModel(_settings, _updater, _portable);
71-
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
72-
7385
AbstractPluginEnvironment.PreStartPluginExecutablePathUpdate(_settings);
7486

75-
_alphabet.Initialize(_settings);
76-
_stringMatcher = new StringMatcher(_alphabet);
77-
StringMatcher.Instance = _stringMatcher;
78-
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
87+
var stringMatcher = Ioc.Default.GetRequiredService<StringMatcher>();
88+
StringMatcher.Instance = stringMatcher;
89+
stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
7990

8091
InternationalizationManager.Instance.Settings = _settings;
8192
InternationalizationManager.Instance.ChangeLanguage(_settings.Language);
8293

8394
PluginManager.LoadPlugins(_settings.PluginSettings);
84-
_mainVM = new MainViewModel(_settings);
8595

86-
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
96+
API = Ioc.Default.GetRequiredService<IPublicAPI>() as PublicAPIInstance;
8797

8898
Http.API = API;
8999
Http.Proxy = _settings.Proxy;
90100

91101
await PluginManager.InitializePluginsAsync(API);
92102
await imageLoadertask;
93103

94-
var window = new MainWindow(_settings, _mainVM);
104+
var mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
105+
var window = new MainWindow(_settings, mainVM);
95106

96107
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");
97108

98109
Current.MainWindow = window;
99110
Current.MainWindow.Title = Constant.FlowLauncher;
100111

101-
HotKeyMapper.Initialize(_mainVM);
112+
HotKeyMapper.Initialize(mainVM);
102113

103114
// main windows needs initialized before theme change because of blur settings
104115
ThemeManager.Instance.Settings = _settings;
@@ -147,11 +158,11 @@ private void AutoUpdates()
147158
{
148159
// check update every 5 hours
149160
var timer = new PeriodicTimer(TimeSpan.FromHours(5));
150-
await _updater.UpdateAppAsync(API);
161+
await Ioc.Default.GetRequiredService<Updater>().UpdateAppAsync(API);
151162

152163
while (await timer.WaitForNextTickAsync())
153164
// check updates on startup
154-
await _updater.UpdateAppAsync(API);
165+
await Ioc.Default.GetRequiredService<Updater>().UpdateAppAsync(API);
155166
}
156167
});
157168
}
@@ -194,7 +205,7 @@ public void Dispose()
194205

195206
public void OnSecondAppStarted()
196207
{
197-
_mainVM.Show();
208+
Ioc.Default.GetRequiredService<MainViewModel>().Show();
198209
}
199210
}
200211
}

Flow.Launcher/Flow.Launcher.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
@@ -83,12 +83,13 @@
8383
</ItemGroup>
8484

8585
<ItemGroup>
86-
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
8786
<PackageReference Include="Fody" Version="6.5.4">
8887
<PrivateAssets>all</PrivateAssets>
8988
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
9089
</PackageReference>
9190
<PackageReference Include="InputSimulator" Version="1.0.4" />
91+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
92+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
9293
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
9394
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
9495
<PrivateAssets>all</PrivateAssets>

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@
2525
using System.Collections.Concurrent;
2626
using System.Diagnostics;
2727
using System.Collections.Specialized;
28-
using Flow.Launcher.Core;
28+
using CommunityToolkit.Mvvm.DependencyInjection;
2929

3030
namespace Flow.Launcher
3131
{
3232
public class PublicAPIInstance : IPublicAPI
3333
{
3434
private readonly SettingWindowViewModel _settingsVM;
3535
private readonly MainViewModel _mainVM;
36-
private readonly PinyinAlphabet _alphabet;
36+
private readonly IAlphabet _alphabet;
3737

3838
#region Constructor
3939

40-
public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, PinyinAlphabet alphabet)
40+
public PublicAPIInstance()
4141
{
42-
_settingsVM = settingsVM;
43-
_mainVM = mainVM;
44-
_alphabet = alphabet;
42+
_settingsVM = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
43+
_mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
44+
_alphabet = Ioc.Default.GetRequiredService<IAlphabet>();
4545
GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback;
4646
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
4747
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.ComponentModel;
2626
using Flow.Launcher.Infrastructure.Image;
2727
using System.Windows.Media;
28+
using CommunityToolkit.Mvvm.DependencyInjection;
2829

2930
namespace Flow.Launcher.ViewModel
3031
{
@@ -58,13 +59,13 @@ public partial class MainViewModel : BaseModel, ISavable
5859

5960
#region Constructor
6061

61-
public MainViewModel(Settings settings)
62+
public MainViewModel()
6263
{
6364
_queryTextBeforeLeaveResults = "";
6465
_queryText = "";
6566
_lastQuery = new Query();
6667

67-
Settings = settings;
68+
Settings = Ioc.Default.GetRequiredService<Settings>();
6869
Settings.PropertyChanged += (_, args) =>
6970
{
7071
switch (args.PropertyName)

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Flow.Launcher.Core;
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Flow.Launcher.Core;
23
using Flow.Launcher.Core.Configuration;
34
using Flow.Launcher.Infrastructure.UserSettings;
45
using Flow.Launcher.Plugin;
@@ -13,11 +14,11 @@ public class SettingWindowViewModel : BaseModel
1314

1415
public Settings Settings { get; }
1516

16-
public SettingWindowViewModel(Settings settings, Updater updater, IPortable portable)
17+
public SettingWindowViewModel()
1718
{
18-
Settings = settings;
19-
Updater = updater;
20-
Portable = portable;
19+
Settings = Ioc.Default.GetRequiredService<Settings>();
20+
Updater = Ioc.Default.GetRequiredService<Updater>();
21+
Portable = Ioc.Default.GetRequiredService<Portable>();
2122
}
2223

2324
public async void UpdateApp()

0 commit comments

Comments
 (0)