Skip to content

Fix Dependency Injection Issue #3275

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using System.IO;
using System.Linq;
using System.Security.Principal;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Microsoft.Win32;
using Microsoft.Win32.TaskScheduler;

namespace Flow.Launcher.Helper;
namespace Flow.Launcher.Infrastructure;

public class AutoStartup
{
Expand All @@ -32,7 +31,7 @@ public static bool IsEnabled
var path = key?.GetValue(Constant.FlowLauncher) as string;
return path == Constant.ExecutablePath;
}
catch (Exception e)
catch (System.Exception e)
{
Log.Error("AutoStartup", $"Ignoring non-critical registry error (querying if enabled): {e}");
}
Expand All @@ -59,7 +58,7 @@ private static bool CheckLogonTask()

return true;
}
catch (Exception e)
catch (System.Exception e)
{
Log.Error("AutoStartup", $"Failed to check logon task: {e}");
}
Expand Down Expand Up @@ -110,7 +109,7 @@ private static void Disable(bool logonTask)
key?.DeleteValue(Constant.FlowLauncher, false);
}
}
catch (Exception e)
catch (System.Exception e)
{
Log.Error("AutoStartup", $"Failed to disable auto-startup: {e}");
throw;
Expand All @@ -131,7 +130,7 @@ private static void Enable(bool logonTask)
key?.SetValue(Constant.FlowLauncher, $"\"{Constant.ExecutablePath}\"");
}
}
catch (Exception e)
catch (System.Exception e)
{
Log.Error("AutoStartup", $"Failed to enable auto-startup: {e}");
throw;
Expand Down Expand Up @@ -159,7 +158,7 @@ private static bool ScheduleLogonTask()
TaskService.Instance.RootFolder.RegisterTaskDefinition(LogonTaskName, td);
return true;
}
catch (Exception e)
catch (System.Exception e)
{
Log.Error("AutoStartup", $"Failed to schedule logon task: {e}");
return false;
Expand All @@ -174,7 +173,7 @@ private static bool UnscheduleLogonTask()
taskService.RootFolder.DeleteTask(LogonTaskName);
return true;
}
catch (Exception e)
catch (System.Exception e)
{
Log.Error("AutoStartup", $"Failed to unschedule logon task: {e}");
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
Expand Down Expand Up @@ -68,6 +68,7 @@
<PackageReference Include="NLog" Version="4.7.10" />
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.11.0" />
<!--ToolGood.Words.Pinyin v3.0.2.6 results in high memory usage when search with pinyin is enabled-->
<!--Bumping to it or higher needs to test and ensure this is no longer a problem-->
<PackageReference Include="ToolGood.Words.Pinyin" Version="3.0.1.4" />
Expand Down
83 changes: 58 additions & 25 deletions Flow.Launcher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,64 @@ public partial class App : IDisposable, ISingleInstanceApp
public App()
{
// Initialize settings
var storage = new FlowLauncherJsonStorage<Settings>();
_settings = storage.Load();
_settings.SetStorage(storage);
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
try
{
var storage = new FlowLauncherJsonStorage<Settings>();
_settings = storage.Load();
_settings.SetStorage(storage);
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
}
catch (Exception e)
{
ShowErrorMsgBoxAndFailFast("Cannot load setting storage, please check local data directory", e);
return;
}

// Configure the dependency injection container
var host = Host.CreateDefaultBuilder()
.UseContentRoot(AppContext.BaseDirectory)
.ConfigureServices(services => services
.AddSingleton(_ => _settings)
.AddSingleton(sp => new Updater(sp.GetRequiredService<IPublicAPI>(), Launcher.Properties.Settings.Default.GithubRepo))
.AddSingleton<Portable>()
.AddSingleton<SettingWindowViewModel>()
.AddSingleton<IAlphabet, PinyinAlphabet>()
.AddSingleton<StringMatcher>()
.AddSingleton<Internationalization>()
.AddSingleton<IPublicAPI, PublicAPIInstance>()
.AddSingleton<MainViewModel>()
.AddSingleton<Theme>()
).Build();
Ioc.Default.ConfigureServices(host.Services);
try
{
var host = Host.CreateDefaultBuilder()
.UseContentRoot(AppContext.BaseDirectory)
.ConfigureServices(services => services
.AddSingleton(_ => _settings)
.AddSingleton(sp => new Updater(sp.GetRequiredService<IPublicAPI>(), Launcher.Properties.Settings.Default.GithubRepo))
.AddSingleton<Portable>()
.AddSingleton<SettingWindowViewModel>()
.AddSingleton<IAlphabet, PinyinAlphabet>()
.AddSingleton<StringMatcher>()
.AddSingleton<Internationalization>()
.AddSingleton<IPublicAPI, PublicAPIInstance>()
.AddSingleton<MainViewModel>()
.AddSingleton<Theme>()
).Build();
Ioc.Default.ConfigureServices(host.Services);
}
catch (Exception e)
{
ShowErrorMsgBoxAndFailFast("Cannot configure dependency injection container, please open new issue in Flow.Launcher", e);
return;
}

// Initialize the public API and Settings first
API = Ioc.Default.GetRequiredService<IPublicAPI>();
_settings.Initialize();
try
{
API = Ioc.Default.GetRequiredService<IPublicAPI>();
_settings.Initialize();
}
catch (Exception e)
{
ShowErrorMsgBoxAndFailFast("Cannot initialize api and settings, please open new issue in Flow.Launcher", e);
return;
}
}

private static void ShowErrorMsgBoxAndFailFast(string message, Exception e)
{
// Firstly show users the message
MessageBox.Show(e.ToString(), message, MessageBoxButton.OK, MessageBoxImage.Error);

// Flow cannot construct its App instance, so ensure Flow crashes w/ the exception info.
Environment.FailFast(message, e);
}

[STAThread]
Expand Down Expand Up @@ -102,8 +135,8 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
await imageLoadertask;

var mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
((PublicAPIInstance)API).Initialize(mainVM);
var window = new MainWindow(_settings, mainVM);

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

Current.MainWindow = window;
Expand Down Expand Up @@ -132,17 +165,17 @@ private void AutoStartup()
{
// we try to enable auto-startup on first launch, or reenable if it was removed
// but the user still has the setting set
if (_settings.StartFlowLauncherOnSystemStartup && !Helper.AutoStartup.IsEnabled)
if (_settings.StartFlowLauncherOnSystemStartup && !Infrastructure.AutoStartup.IsEnabled)
{
try
{
if (_settings.UseLogonTaskForStartup)
{
Helper.AutoStartup.EnableViaLogonTask();
Infrastructure.AutoStartup.EnableViaLogonTask();
}
else
{
Helper.AutoStartup.EnableViaRegistry();
Infrastructure.AutoStartup.EnableViaRegistry();
}
}
catch (Exception e)
Expand Down
1 change: 0 additions & 1 deletion Flow.Launcher/Flow.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
<PackageReference Include="NHotkey.Wpf" Version="3.0.0" />
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
<PackageReference Include="SemanticVersioning" Version="3.0.0" />
<PackageReference Include="TaskScheduler" Version="2.11.0" />
<PackageReference Include="VirtualizingWrapPanel" Version="2.1.1" />
</ItemGroup>

Expand Down
13 changes: 9 additions & 4 deletions Flow.Launcher/PublicAPIInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ namespace Flow.Launcher
public class PublicAPIInstance : IPublicAPI
{
private readonly Settings _settings;
private readonly MainViewModel _mainVM;
private MainViewModel _mainVM;

#region Constructor
#region Constructor & Initialization

public PublicAPIInstance(Settings settings, MainViewModel mainVM)
public PublicAPIInstance(Settings settings)
{
_settings = settings;
_mainVM = mainVM;
GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
}

// We must initialize mainVM later to avoid dispatcher thread issue
public void Initialize(MainViewModel mainVM)
{
_mainVM = mainVM;
}

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
Expand Down
Loading