Skip to content

Commit 8d83849

Browse files
committed
Improve dependency injection in updater & settings view model & settings page
1 parent 3bebb69 commit 8d83849

File tree

9 files changed

+46
-56
lines changed

9 files changed

+46
-56
lines changed

Flow.Launcher.Core/Updater.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public void Initialize(string gitHubRepository)
3434

3535
private SemaphoreSlim UpdateLock { get; } = new SemaphoreSlim(1);
3636

37-
public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
37+
public async Task UpdateAppAsync(bool silentUpdate = true)
3838
{
3939
await UpdateLock.WaitAsync().ConfigureAwait(false);
4040
try
4141
{
4242
if (!silentUpdate)
43-
api.ShowMsg(api.GetTranslation("pleaseWait"),
44-
api.GetTranslation("update_flowlauncher_update_check"));
43+
API.ShowMsg(API.GetTranslation("pleaseWait"),
44+
API.GetTranslation("update_flowlauncher_update_check"));
4545

4646
using var updateManager = await GitHubUpdateManagerAsync(GitHubRepository).ConfigureAwait(false);
4747

@@ -56,13 +56,13 @@ public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
5656
if (newReleaseVersion <= currentVersion)
5757
{
5858
if (!silentUpdate)
59-
API.ShowMsgBox(api.GetTranslation("update_flowlauncher_already_on_latest"));
59+
API.ShowMsgBox(API.GetTranslation("update_flowlauncher_already_on_latest"));
6060
return;
6161
}
6262

6363
if (!silentUpdate)
64-
api.ShowMsg(api.GetTranslation("update_flowlauncher_update_found"),
65-
api.GetTranslation("update_flowlauncher_updating"));
64+
API.ShowMsg(API.GetTranslation("update_flowlauncher_update_found"),
65+
API.GetTranslation("update_flowlauncher_updating"));
6666

6767
await updateManager.DownloadReleases(newUpdateInfo.ReleasesToApply).ConfigureAwait(false);
6868

@@ -73,7 +73,7 @@ public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
7373
var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion.ToString()}\\{DataLocation.PortableFolderName}";
7474
FilesFolders.CopyAll(DataLocation.PortableDataPath, targetDestination, (s) => API.ShowMsgBox(s));
7575
if (!FilesFolders.VerifyBothFolderFilesEqual(DataLocation.PortableDataPath, targetDestination, (s) => API.ShowMsgBox(s)))
76-
API.ShowMsgBox(string.Format(api.GetTranslation("update_flowlauncher_fail_moving_portable_user_profile_data"),
76+
API.ShowMsgBox(string.Format(API.GetTranslation("update_flowlauncher_fail_moving_portable_user_profile_data"),
7777
DataLocation.PortableDataPath,
7878
targetDestination));
7979
}
@@ -86,7 +86,7 @@ public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
8686

8787
Log.Info($"|Updater.UpdateApp|Update success:{newVersionTips}");
8888

89-
if (API.ShowMsgBox(newVersionTips, api.GetTranslation("update_flowlauncher_new_update"), MessageBoxButton.YesNo) == MessageBoxResult.Yes)
89+
if (API.ShowMsgBox(newVersionTips, API.GetTranslation("update_flowlauncher_new_update"), MessageBoxButton.YesNo) == MessageBoxResult.Yes)
9090
{
9191
UpdateManager.RestartApp(Constant.ApplicationFileName);
9292
}
@@ -99,8 +99,8 @@ public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
9999
Log.Exception($"|Updater.UpdateApp|Error Occurred", e);
100100

101101
if (!silentUpdate)
102-
api.ShowMsg(api.GetTranslation("update_flowlauncher_fail"),
103-
api.GetTranslation("update_flowlauncher_check_connection"));
102+
API.ShowMsg(API.GetTranslation("update_flowlauncher_fail"),
103+
API.GetTranslation("update_flowlauncher_check_connection"));
104104
}
105105
finally
106106
{

Flow.Launcher/App.xaml.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
7474

7575
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
7676

77-
Ioc.Default.GetRequiredService<SettingWindowViewModel>().Initialize();
78-
7977
Log.Info("|App.OnStartup|Begin Flow Launcher startup ----------------------------------------------------");
8078
Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}");
8179

@@ -96,6 +94,7 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
9694
PluginManager.LoadPlugins(_settings.PluginSettings);
9795

9896
API = Ioc.Default.GetRequiredService<IPublicAPI>();
97+
((PublicAPIInstance)API).Initialize();
9998

10099
Http.API = API;
101100
Http.Proxy = _settings.Proxy;
@@ -160,11 +159,11 @@ private void AutoUpdates()
160159
{
161160
// check update every 5 hours
162161
var timer = new PeriodicTimer(TimeSpan.FromHours(5));
163-
await Ioc.Default.GetRequiredService<Updater>().UpdateAppAsync(API);
162+
await Ioc.Default.GetRequiredService<Updater>().UpdateAppAsync();
164163

165164
while (await timer.WaitForNextTickAsync())
166165
// check updates on startup
167-
await Ioc.Default.GetRequiredService<Updater>().UpdateAppAsync(API);
166+
await Ioc.Default.GetRequiredService<Updater>().UpdateAppAsync();
168167
}
169168
});
170169
}

Flow.Launcher/CustomQueryHotkeySetting.xaml.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@
66
using System.Windows;
77
using System.Windows.Input;
88
using System.Windows.Controls;
9-
using Flow.Launcher.Core;
109

1110
namespace Flow.Launcher
1211
{
1312
public partial class CustomQueryHotkeySetting : Window
1413
{
15-
private SettingWindow _settingWidow;
1614
private bool update;
1715
private CustomPluginHotkey updateCustomHotkey;
1816
public Settings Settings { get; }
1917

20-
public CustomQueryHotkeySetting(SettingWindow settingWidow, Settings settings)
18+
public CustomQueryHotkeySetting(Settings settings)
2119
{
22-
_settingWidow = settingWidow;
2320
Settings = settings;
2421
InitializeComponent();
2522
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,34 @@
2626
using System.Diagnostics;
2727
using System.Collections.Specialized;
2828
using CommunityToolkit.Mvvm.DependencyInjection;
29+
using Flow.Launcher.Core;
2930

3031
namespace Flow.Launcher
3132
{
3233
public class PublicAPIInstance : IPublicAPI
3334
{
3435
private readonly SettingWindowViewModel _settingsVM;
3536
private readonly MainViewModel _mainVM;
36-
private readonly IAlphabet _alphabet;
3737

38-
#region Constructor
38+
private Updater _updater;
39+
40+
#region Constructor & Initialization
3941

4042
public PublicAPIInstance()
4143
{
4244
_settingsVM = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
4345
_mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
44-
_alphabet = Ioc.Default.GetRequiredService<IAlphabet>();
4546
GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback;
4647
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
4748
}
4849

50+
public void Initialize()
51+
{
52+
// We need to initialize Updater not in the constructor because we want to avoid
53+
// recrusive dependency injection
54+
_updater = Ioc.Default.GetRequiredService<Updater>();
55+
}
56+
4957
#endregion
5058

5159
#region Public API
@@ -78,14 +86,14 @@ public void RestartApp()
7886

7987
public event VisibilityChangedEventHandler VisibilityChanged { add => _mainVM.VisibilityChanged += value; remove => _mainVM.VisibilityChanged -= value; }
8088

81-
public void CheckForNewUpdate() => _settingsVM.UpdateApp();
89+
public void CheckForNewUpdate() => _ = _updater.UpdateAppAsync(false);
8290

8391
public void SaveAppAllSettings()
8492
{
8593
PluginManager.Save();
8694
_mainVM.Save();
8795
_settingsVM.Save();
88-
ImageLoader.Save();
96+
_ = ImageLoader.Save();
8997
}
9098

9199
public Task ReloadAllPluginData() => PluginManager.ReloadDataAsync();
@@ -105,7 +113,7 @@ public void OpenSettingDialog()
105113
{
106114
Application.Current.Dispatcher.Invoke(() =>
107115
{
108-
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsVM);
116+
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>();
109117
});
110118
}
111119

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void OpenLogsFolder()
9696
}
9797

9898
[RelayCommand]
99-
private Task UpdateApp() => _updater.UpdateAppAsync(App.API, false);
99+
private Task UpdateApp() => _updater.UpdateAppAsync(false);
100100

101101
private void ClearLogFolder()
102102
{

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private string GetFileFromDialog(string title, string filter = "")
160160

161161
private void UpdateApp()
162162
{
163-
_ = _updater.UpdateAppAsync(App.API, false);
163+
_ = _updater.UpdateAppAsync(false);
164164
}
165165

166166
public bool AutoUpdates

Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Flow.Launcher.Infrastructure.Hotkey;
88
using Flow.Launcher.Infrastructure.UserSettings;
99
using Flow.Launcher.Plugin;
10-
using Flow.Launcher.Core;
1110

1211
namespace Flow.Launcher.SettingPages.ViewModels;
1312

@@ -71,15 +70,15 @@ private void CustomHotkeyEdit()
7170
return;
7271
}
7372

74-
var window = new CustomQueryHotkeySetting(null, Settings);
73+
var window = new CustomQueryHotkeySetting(Settings);
7574
window.UpdateItem(item);
7675
window.ShowDialog();
7776
}
7877

7978
[RelayCommand]
8079
private void CustomHotkeyAdd()
8180
{
82-
new CustomQueryHotkeySetting(null, Settings).ShowDialog();
81+
new CustomQueryHotkeySetting(Settings).ShowDialog();
8382
}
8483

8584
[RelayCommand]

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Windows.Forms;
44
using System.Windows.Input;
55
using System.Windows.Interop;
6+
using CommunityToolkit.Mvvm.DependencyInjection;
67
using Flow.Launcher.Core;
78
using Flow.Launcher.Core.Configuration;
89
using Flow.Launcher.Helper;
@@ -17,16 +18,21 @@ namespace Flow.Launcher;
1718

1819
public partial class SettingWindow
1920
{
21+
private readonly Updater _updater;
22+
private readonly IPortable _portable;
2023
private readonly IPublicAPI _api;
2124
private readonly Settings _settings;
2225
private readonly SettingWindowViewModel _viewModel;
2326

24-
public SettingWindow(IPublicAPI api, SettingWindowViewModel viewModel)
27+
public SettingWindow()
2528
{
29+
var viewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
2630
_settings = viewModel.Settings;
2731
DataContext = viewModel;
2832
_viewModel = viewModel;
29-
_api = api;
33+
_updater = Ioc.Default.GetRequiredService<Updater>();
34+
_portable = Ioc.Default.GetRequiredService<Portable>();
35+
_api = Ioc.Default.GetRequiredService<IPublicAPI>();
3036
InitializePosition();
3137
InitializeComponent();
3238
}
@@ -125,7 +131,7 @@ public void InitializePosition()
125131
WindowState = _settings.SettingWindowState;
126132
}
127133

128-
private bool IsPositionValid(double top, double left)
134+
private static bool IsPositionValid(double top, double left)
129135
{
130136
foreach (var screen in Screen.AllScreens)
131137
{
@@ -145,7 +151,7 @@ private double WindowLeft()
145151
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
146152
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
147153
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
148-
var left = (dip2.X - this.ActualWidth) / 2 + dip1.X;
154+
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
149155
return left;
150156
}
151157

@@ -154,13 +160,13 @@ private double WindowTop()
154160
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
155161
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
156162
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
157-
var top = (dip2.Y - this.ActualHeight) / 2 + dip1.Y - 20;
163+
var top = (dip2.Y - ActualHeight) / 2 + dip1.Y - 20;
158164
return top;
159165
}
160166

161167
private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
162168
{
163-
var paneData = new PaneData(_settings, _viewModel.Updater, _viewModel.Portable);
169+
var paneData = new PaneData(_settings, _updater, _portable);
164170
if (args.IsSettingsSelected)
165171
{
166172
ContentFrame.Navigate(typeof(SettingsPaneGeneral), paneData);

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
using CommunityToolkit.Mvvm.DependencyInjection;
2-
using Flow.Launcher.Core;
3-
using Flow.Launcher.Core.Configuration;
42
using Flow.Launcher.Infrastructure.UserSettings;
53
using Flow.Launcher.Plugin;
64

75
namespace Flow.Launcher.ViewModel;
86

9-
public class SettingWindowViewModel : BaseModel
7+
public partial class SettingWindowViewModel : BaseModel
108
{
11-
public Updater Updater { get; private set; }
12-
13-
public IPortable Portable { get; private set; }
14-
15-
public Settings Settings { get; }
9+
public Settings Settings { get; init; }
1610

1711
public SettingWindowViewModel()
1812
{
1913
Settings = Ioc.Default.GetRequiredService<Settings>();
2014
}
2115

22-
public void Initialize()
23-
{
24-
// We don not initialize Updater and Portable in the constructor because we want to avoid
25-
// recrusive dependency injection
26-
Updater = Ioc.Default.GetRequiredService<Updater>();
27-
Portable = Ioc.Default.GetRequiredService<Portable>();
28-
}
29-
30-
public async void UpdateApp()
31-
{
32-
await Updater.UpdateAppAsync(App.API, false);
33-
}
34-
3516
/// <summary>
3617
/// Save Flow settings. Plugins settings are not included.
3718
/// </summary>

0 commit comments

Comments
 (0)