4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
6
using System . Windows ;
7
+ using CommunityToolkit . Mvvm . DependencyInjection ;
7
8
using Flow . Launcher . Core ;
8
9
using Flow . Launcher . Core . Configuration ;
9
10
using Flow . Launcher . Core . ExternalPlugins . Environments ;
18
19
using Flow . Launcher . Infrastructure . UserSettings ;
19
20
using Flow . Launcher . Plugin ;
20
21
using Flow . Launcher . ViewModel ;
22
+ using Microsoft . Extensions . DependencyInjection ;
23
+ using Microsoft . Extensions . Hosting ;
21
24
using Stopwatch = Flow . Launcher . Infrastructure . Stopwatch ;
22
25
23
26
namespace Flow . Launcher
24
27
{
25
- public partial class App : IDisposable , ISingleInstanceApp , IApp
28
+ public partial class App : IDisposable , ISingleInstanceApp
26
29
{
27
- public IPublicAPI PublicAPI => API ;
28
30
public static PublicAPIInstance API { get ; private set ; }
29
31
private const string Unique = "Flow.Launcher_Unique_Application_Mutex" ;
30
32
private static bool _disposed ;
31
33
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 ;
38
34
39
35
[ STAThread ]
40
36
public static void Main ( )
@@ -53,52 +49,67 @@ private async void OnStartupAsync(object sender, StartupEventArgs e)
53
49
{
54
50
await Stopwatch . NormalAsync ( "|App.OnStartup|Startup cost" , async ( ) =>
55
51
{
52
+ // Initialize settings
56
53
var storage = new FlowLauncherJsonStorage < Settings > ( ) ;
57
54
_settings = storage . Load ( ) ;
58
55
_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 ----------------------------------------------------" ) ;
64
78
Log . Info ( $ "|App.OnStartup|Runtime info:{ ErrorReporting . RuntimeInfo ( ) } ") ;
79
+
65
80
RegisterAppDomainExceptions ( ) ;
66
81
RegisterDispatcherUnhandledException ( ) ;
67
82
68
83
var imageLoadertask = ImageLoader . InitializeAsync ( ) ;
69
84
70
- _settingsVM = new SettingWindowViewModel ( _settings , _updater , _portable ) ;
71
- _settings . WMPInstalled = WindowsMediaPlayerHelper . IsWindowsMediaPlayerInstalled ( ) ;
72
-
73
85
AbstractPluginEnvironment . PreStartPluginExecutablePathUpdate ( _settings ) ;
74
86
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 ;
79
90
80
91
InternationalizationManager . Instance . Settings = _settings ;
81
92
InternationalizationManager . Instance . ChangeLanguage ( _settings . Language ) ;
82
93
83
94
PluginManager . LoadPlugins ( _settings . PluginSettings ) ;
84
- _mainVM = new MainViewModel ( _settings ) ;
85
95
86
- API = new PublicAPIInstance ( _settingsVM , _mainVM , _alphabet ) ;
96
+ API = Ioc . Default . GetRequiredService < IPublicAPI > ( ) as PublicAPIInstance ;
87
97
88
98
Http . API = API ;
89
99
Http . Proxy = _settings . Proxy ;
90
100
91
101
await PluginManager . InitializePluginsAsync ( API ) ;
92
102
await imageLoadertask ;
93
103
94
- var window = new MainWindow ( _settings , _mainVM ) ;
104
+ var mainVM = Ioc . Default . GetRequiredService < MainViewModel > ( ) ;
105
+ var window = new MainWindow ( _settings , mainVM ) ;
95
106
96
107
Log . Info ( $ "|App.OnStartup|Dependencies Info:{ ErrorReporting . DependenciesInfo ( ) } ") ;
97
108
98
109
Current . MainWindow = window ;
99
110
Current . MainWindow . Title = Constant . FlowLauncher ;
100
111
101
- HotKeyMapper . Initialize ( _mainVM ) ;
112
+ HotKeyMapper . Initialize ( mainVM ) ;
102
113
103
114
// main windows needs initialized before theme change because of blur settings
104
115
ThemeManager . Instance . Settings = _settings ;
@@ -147,11 +158,11 @@ private void AutoUpdates()
147
158
{
148
159
// check update every 5 hours
149
160
var timer = new PeriodicTimer ( TimeSpan . FromHours ( 5 ) ) ;
150
- await _updater . UpdateAppAsync ( API ) ;
161
+ await Ioc . Default . GetRequiredService < Updater > ( ) . UpdateAppAsync ( API ) ;
151
162
152
163
while ( await timer . WaitForNextTickAsync ( ) )
153
164
// check updates on startup
154
- await _updater . UpdateAppAsync ( API ) ;
165
+ await Ioc . Default . GetRequiredService < Updater > ( ) . UpdateAppAsync ( API ) ;
155
166
}
156
167
} ) ;
157
168
}
@@ -194,7 +205,7 @@ public void Dispose()
194
205
195
206
public void OnSecondAppStarted ( )
196
207
{
197
- _mainVM . Show ( ) ;
208
+ Ioc . Default . GetRequiredService < MainViewModel > ( ) . Show ( ) ;
198
209
}
199
210
}
200
211
}
0 commit comments