Skip to content

Commit a3964f5

Browse files
committed
Remove instance for Internationalization & Theme
1 parent e022ad6 commit a3964f5

File tree

7 files changed

+25
-52
lines changed

7 files changed

+25
-52
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static async Task InitializePluginsAsync()
204204
}
205205

206206
InternationalizationManager.Instance.AddPluginLanguageDirectories(GetPluginsForInterface<IPluginI18n>());
207-
InternationalizationManager.Instance.ChangeLanguage(InternationalizationManager.Instance.Settings.Language);
207+
InternationalizationManager.Instance.ChangeLanguage(Ioc.Default.GetRequiredService<Settings>().Language);
208208

209209
if (failedPlugins.Any())
210210
{

Flow.Launcher.Core/Resource/Internationalization.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ namespace Flow.Launcher.Core.Resource
1717
{
1818
public class Internationalization
1919
{
20-
public Settings Settings { get; set; }
2120
private const string Folder = "Languages";
2221
private const string DefaultLanguageCode = "en";
2322
private const string DefaultFile = "en.xaml";
2423
private const string Extension = ".xaml";
24+
private readonly Settings _settings;
2525
private readonly List<string> _languageDirectories = new List<string>();
2626
private readonly List<ResourceDictionary> _oldResources = new List<ResourceDictionary>();
2727
private readonly string SystemLanguageCode;
2828

29-
public Internationalization()
29+
public Internationalization(Settings settings)
3030
{
31+
_settings = settings;
3132
AddFlowLauncherLanguageDirectory();
3233
SystemLanguageCode = GetSystemLanguageCodeAtStartup();
3334
}
@@ -142,7 +143,7 @@ private void ChangeLanguage(Language language, bool isSystem)
142143
CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture;
143144

144145
// Raise event after culture is set
145-
Settings.Language = isSystem ? Constant.SystemLanguageCode : language.LanguageCode;
146+
_settings.Language = isSystem ? Constant.SystemLanguageCode : language.LanguageCode;
146147
_ = Task.Run(() =>
147148
{
148149
UpdatePluginMetadataTranslations();
@@ -153,7 +154,7 @@ public bool PromptShouldUsePinyin(string languageCodeToSet)
153154
{
154155
var languageToSet = GetLanguageByLanguageCode(languageCodeToSet);
155156

156-
if (Settings.ShouldUsePinyin)
157+
if (_settings.ShouldUsePinyin)
157158
return false;
158159

159160
if (languageToSet != AvailableLanguages.Chinese && languageToSet != AvailableLanguages.Chinese_TW)
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
1-
namespace Flow.Launcher.Core.Resource
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
3+
namespace Flow.Launcher.Core.Resource
24
{
35
public static class InternationalizationManager
46
{
5-
private static Internationalization instance;
6-
private static object syncObject = new object();
7-
87
public static Internationalization Instance
9-
{
10-
get
11-
{
12-
if (instance == null)
13-
{
14-
lock (syncObject)
15-
{
16-
if (instance == null)
17-
{
18-
instance = new Internationalization();
19-
}
20-
}
21-
}
22-
return instance;
23-
}
24-
}
8+
=> Ioc.Default.GetRequiredService<Internationalization>();
259
}
26-
}
10+
}

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public class Theme
2525

2626
private const int ShadowExtraMargin = 32;
2727

28-
private readonly IPublicAPI API = Ioc.Default.GetRequiredService<IPublicAPI>();
28+
private readonly IPublicAPI API;
29+
private readonly Settings Settings;
2930
private readonly List<string> _themeDirectories = new List<string>();
3031
private ResourceDictionary _oldResource;
3132
private string _oldTheme;
32-
public Settings Settings { get; set; }
3333
private const string Folder = Constant.Themes;
3434
private const string Extension = ".xaml";
3535
private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, Folder);
@@ -39,8 +39,11 @@ public class Theme
3939

4040
private double mainWindowWidth;
4141

42-
public Theme()
42+
public Theme(IPublicAPI publicAPI, Settings settings)
4343
{
44+
API = publicAPI;
45+
Settings = settings;
46+
4447
_themeDirectories.Add(DirectoryPath);
4548
_themeDirectories.Add(UserDirectoryPath);
4649
MakeSureThemeDirectoriesExist();
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
1-
namespace Flow.Launcher.Core.Resource
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
3+
namespace Flow.Launcher.Core.Resource
24
{
35
public class ThemeManager
46
{
5-
private static Theme instance;
6-
private static object syncObject = new object();
7-
87
public static Theme Instance
9-
{
10-
get
11-
{
12-
if (instance == null)
13-
{
14-
lock (syncObject)
15-
{
16-
if (instance == null)
17-
{
18-
instance = new Theme();
19-
}
20-
}
21-
}
22-
return instance;
23-
}
24-
}
8+
=> Ioc.Default.GetRequiredService<Theme>();
259
}
2610
}

Flow.Launcher.Core/Updater.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Flow.Launcher.Plugin;
1818
using System.Text.Json.Serialization;
1919
using System.Threading;
20-
using CommunityToolkit.Mvvm.DependencyInjection;
2120

2221
namespace Flow.Launcher.Core
2322
{

Flow.Launcher/App.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public App()
5050
.AddSingleton<SettingWindowViewModel>()
5151
.AddSingleton<IAlphabet, PinyinAlphabet>()
5252
.AddSingleton<StringMatcher>()
53+
.AddSingleton<Internationalization>()
5354
.AddSingleton<IPublicAPI, PublicAPIInstance>()
5455
.AddSingleton<MainViewModel>()
56+
.AddSingleton<Theme>()
5557
).Build();
5658
Ioc.Default.ConfigureServices(host.Services);
5759

@@ -93,7 +95,7 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
9395

9496
Ioc.Default.GetRequiredService<StringMatcher>().UserSettingSearchPrecision = _settings.QuerySearchPrecision;
9597

96-
InternationalizationManager.Instance.Settings = _settings;
98+
// TODO: Clean InternationalizationManager.Instance and InternationalizationManager.Instance.GetTranslation in future
9799
InternationalizationManager.Instance.ChangeLanguage(_settings.Language);
98100

99101
PluginManager.LoadPlugins(_settings.PluginSettings);
@@ -114,7 +116,7 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
114116
HotKeyMapper.Initialize(mainVM);
115117

116118
// main windows needs initialized before theme change because of blur settings
117-
ThemeManager.Instance.Settings = _settings;
119+
// TODO: Clean ThemeManager.Instance in future
118120
ThemeManager.Instance.ChangeTheme(_settings.Theme);
119121

120122
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

0 commit comments

Comments
 (0)