Skip to content

Commit 4a7915b

Browse files
committed
Use api fuctions forr main project
1 parent b13ab3b commit 4a7915b

File tree

13 files changed

+67
-56
lines changed

13 files changed

+67
-56
lines changed

Flow.Launcher.Core/Resource/Internationalization.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ public class Internationalization
1818
{
1919
private static readonly string ClassName = nameof(Internationalization);
2020

21+
// We should not initialize API in static constructor because it will create another API instance
22+
private static IPublicAPI api = null;
23+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
24+
2125
private const string Folder = "Languages";
2226
private const string DefaultLanguageCode = "en";
2327
private const string DefaultFile = "en.xaml";
2428
private const string Extension = ".xaml";
2529
private readonly Settings _settings;
26-
private readonly IPublicAPI _api;
2730
private readonly List<string> _languageDirectories = new();
2831
private readonly List<ResourceDictionary> _oldResources = new();
2932
private readonly string SystemLanguageCode;
3033

3134
public Internationalization(Settings settings)
3235
{
3336
_settings = settings;
34-
_api = Ioc.Default.GetRequiredService<IPublicAPI>();
3537
AddFlowLauncherLanguageDirectory();
3638
SystemLanguageCode = GetSystemLanguageCodeAtStartup();
3739
}
@@ -83,7 +85,7 @@ private void AddPluginLanguageDirectories()
8385
}
8486
else
8587
{
86-
_api.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
88+
API.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
8789
}
8890
}
8991

@@ -147,13 +149,13 @@ public void ChangeLanguage(string languageCode)
147149
_settings.Language = isSystem ? Constant.SystemLanguageCode : language.LanguageCode;
148150
}
149151

150-
private Language GetLanguageByLanguageCode(string languageCode)
152+
private static Language GetLanguageByLanguageCode(string languageCode)
151153
{
152154
var lowercase = languageCode.ToLower();
153155
var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase);
154156
if (language == null)
155157
{
156-
_api.LogError(ClassName, $"Language code can't be found <{languageCode}>");
158+
API.LogError(ClassName, $"Language code can't be found <{languageCode}>");
157159
return AvailableLanguages.English;
158160
}
159161
else
@@ -242,7 +244,7 @@ public List<Language> LoadAvailableLanguages()
242244
return list;
243245
}
244246

245-
public string GetTranslation(string key)
247+
public static string GetTranslation(string key)
246248
{
247249
var translation = Application.Current.TryFindResource(key);
248250
if (translation is string)
@@ -251,7 +253,7 @@ public string GetTranslation(string key)
251253
}
252254
else
253255
{
254-
_api.LogError(ClassName, $"No Translation for key {key}");
256+
API.LogError(ClassName, $"No Translation for key {key}");
255257
return $"No Translation for key {key}";
256258
}
257259
}
@@ -269,12 +271,12 @@ private void UpdatePluginMetadataTranslations()
269271
}
270272
catch (Exception e)
271273
{
272-
_api.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
274+
API.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
273275
}
274276
}
275277
}
276278

277-
private string LanguageFile(string folder, string language)
279+
private static string LanguageFile(string folder, string language)
278280
{
279281
if (Directory.Exists(folder))
280282
{
@@ -285,15 +287,15 @@ private string LanguageFile(string folder, string language)
285287
}
286288
else
287289
{
288-
_api.LogError(ClassName, $"Language path can't be found <{path}>");
290+
API.LogError(ClassName, $"Language path can't be found <{path}>");
289291
var english = Path.Combine(folder, DefaultFile);
290292
if (File.Exists(english))
291293
{
292294
return english;
293295
}
294296
else
295297
{
296-
_api.LogError(ClassName, $"Default English Language path can't be found <{path}>");
298+
API.LogError(ClassName, $"Default English Language path can't be found <{path}>");
297299
return string.Empty;
298300
}
299301
}

Flow.Launcher/App.xaml.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
148148

149149
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
150150

151-
Log.Info("|App.OnStartup|Begin Flow Launcher startup ----------------------------------------------------");
152-
Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}");
151+
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");
152+
API.LogInfo(ClassName, "Runtime info:{ErrorReporting.RuntimeInfo()}");
153153

154154
RegisterAppDomainExceptions();
155155
RegisterDispatcherUnhandledException();
@@ -176,7 +176,7 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
176176

177177
_mainWindow = new MainWindow();
178178

179-
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");
179+
API.LogInfo(ClassName, "Dependencies Info:{ErrorReporting.DependenciesInfo()}");
180180

181181
Current.MainWindow = _mainWindow;
182182
Current.MainWindow.Title = Constant.FlowLauncher;
@@ -192,7 +192,7 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
192192
AutoUpdates();
193193

194194
API.SaveAppAllSettings();
195-
Log.Info("|App.OnStartup|End Flow Launcher startup ----------------------------------------------------");
195+
API.LogInfo(ClassName, "End Flow Launcher startup ----------------------------------------------------");
196196
});
197197
}
198198

@@ -250,19 +250,19 @@ private void RegisterExitEvents()
250250
{
251251
AppDomain.CurrentDomain.ProcessExit += (s, e) =>
252252
{
253-
Log.Info("|App.RegisterExitEvents|Process Exit");
253+
API.LogInfo(ClassName, "Process Exit");
254254
Dispose();
255255
};
256256

257257
Current.Exit += (s, e) =>
258258
{
259-
Log.Info("|App.RegisterExitEvents|Application Exit");
259+
API.LogInfo(ClassName, "Application Exit");
260260
Dispose();
261261
};
262262

263263
Current.SessionEnding += (s, e) =>
264264
{
265-
Log.Info("|App.RegisterExitEvents|Session Ending");
265+
API.LogInfo(ClassName, "Session Ending");
266266
Dispose();
267267
};
268268
}
@@ -326,7 +326,7 @@ protected virtual void Dispose(bool disposing)
326326

327327
API.StopwatchLogInfo(ClassName, "Dispose cost", () =>
328328
{
329-
Log.Info("|App.Dispose|Begin Flow Launcher dispose ----------------------------------------------------");
329+
API.LogInfo(ClassName, "Begin Flow Launcher dispose ----------------------------------------------------");
330330

331331
if (disposing)
332332
{
@@ -336,7 +336,7 @@ protected virtual void Dispose(bool disposing)
336336
_mainVM?.Dispose();
337337
}
338338

339-
Log.Info("|App.Dispose|End Flow Launcher dispose ----------------------------------------------------");
339+
API.LogInfo(ClassName, "End Flow Launcher dispose ----------------------------------------------------");
340340
});
341341
}
342342

Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
using System.Windows.Controls;
55
using System.Windows.Data;
66
using System.Windows.Media;
7-
using Flow.Launcher.Infrastructure.Logger;
87
using Flow.Launcher.ViewModel;
98

109
namespace Flow.Launcher.Converters;
1110

1211
public class QuerySuggestionBoxConverter : IMultiValueConverter
1312
{
13+
private static readonly string ClassName = nameof(QuerySuggestionBoxConverter);
14+
1415
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
1516
{
1617
// values[0] is TextBox: The textbox displaying the autocomplete suggestion
@@ -64,7 +65,7 @@ values[2] is not string queryText ||
6465
}
6566
catch (Exception e)
6667
{
67-
Log.Exception(nameof(QuerySuggestionBoxConverter), "fail to convert text for suggestion box", e);
68+
App.API.LogException(ClassName, "fail to convert text for suggestion box", e);
6869
return string.Empty;
6970
}
7071
}

Flow.Launcher/Helper/AutoStartup.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Flow.Launcher.Helper;
1111

1212
public class AutoStartup
1313
{
14+
private static readonly string ClassName = nameof(AutoStartup);
15+
1416
private const string StartupPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
1517
private const string LogonTaskName = $"{Constant.FlowLauncher} Startup";
1618
private const string LogonTaskDesc = $"{Constant.FlowLauncher} Auto Startup";
@@ -34,7 +36,7 @@ public static bool IsEnabled
3436
}
3537
catch (Exception e)
3638
{
37-
Log.Error("AutoStartup", $"Ignoring non-critical registry error (querying if enabled): {e}");
39+
App.API.LogError(ClassName, $"Ignoring non-critical registry error (querying if enabled): {e}");
3840
}
3941

4042
return false;
@@ -61,7 +63,7 @@ private static bool CheckLogonTask()
6163
}
6264
catch (Exception e)
6365
{
64-
Log.Error("AutoStartup", $"Failed to check logon task: {e}");
66+
App.API.LogError(ClassName, $"Failed to check logon task: {e}");
6567
}
6668
}
6769

@@ -112,7 +114,7 @@ private static void Disable(bool logonTask)
112114
}
113115
catch (Exception e)
114116
{
115-
Log.Error("AutoStartup", $"Failed to disable auto-startup: {e}");
117+
App.API.LogError(ClassName, $"Failed to disable auto-startup: {e}");
116118
throw;
117119
}
118120
}
@@ -133,7 +135,7 @@ private static void Enable(bool logonTask)
133135
}
134136
catch (Exception e)
135137
{
136-
Log.Error("AutoStartup", $"Failed to enable auto-startup: {e}");
138+
App.API.LogError(ClassName, $"Failed to enable auto-startup: {e}");
137139
throw;
138140
}
139141
}
@@ -161,7 +163,7 @@ private static bool ScheduleLogonTask()
161163
}
162164
catch (Exception e)
163165
{
164-
Log.Error("AutoStartup", $"Failed to schedule logon task: {e}");
166+
App.API.LogError(ClassName, $"Failed to schedule logon task: {e}");
165167
return false;
166168
}
167169
}
@@ -176,7 +178,7 @@ private static bool UnscheduleLogonTask()
176178
}
177179
catch (Exception e)
178180
{
179-
Log.Error("AutoStartup", $"Failed to unschedule logon task: {e}");
181+
App.API.LogError(ClassName, $"Failed to unschedule logon task: {e}");
180182
return false;
181183
}
182184
}

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Flow.Launcher.Helper;
1212

1313
internal static class HotKeyMapper
1414
{
15+
private static readonly string ClassName = nameof(HotKeyMapper);
16+
1517
private static Settings _settings;
1618
private static MainViewModel _mainViewModel;
1719

@@ -51,7 +53,7 @@ private static void SetWithChefKeys(string hotkeyStr)
5153
}
5254
catch (Exception e)
5355
{
54-
Log.Error(
56+
App.API.LogError(ClassName,
5557
string.Format("|HotkeyMapper.SetWithChefKeys|Error registering hotkey: {0} \nStackTrace:{1}",
5658
e.Message,
5759
e.StackTrace));
@@ -76,7 +78,7 @@ internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs>
7678
}
7779
catch (Exception e)
7880
{
79-
Log.Error(
81+
App.API.LogError(ClassName,
8082
string.Format("|HotkeyMapper.SetHotkey|Error registering hotkey {2}: {0} \nStackTrace:{1}",
8183
e.Message,
8284
e.StackTrace,
@@ -102,7 +104,7 @@ internal static void RemoveHotkey(string hotkeyStr)
102104
}
103105
catch (Exception e)
104106
{
105-
Log.Error(
107+
App.API.LogError(ClassName,
106108
string.Format("|HotkeyMapper.RemoveHotkey|Error removing hotkey: {0} \nStackTrace:{1}",
107109
e.Message,
108110
e.StackTrace));

Flow.Launcher/MessageBoxEx.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
using System.Windows;
55
using System.Windows.Input;
66
using Flow.Launcher.Infrastructure;
7-
using Flow.Launcher.Infrastructure.Image;
8-
using Flow.Launcher.Infrastructure.Logger;
97

108
namespace Flow.Launcher
119
{
1210
public partial class MessageBoxEx : Window
1311
{
12+
private static readonly string ClassName = nameof(MessageBoxEx);
13+
1414
private static MessageBoxEx msgBox;
1515
private static MessageBoxResult _result = MessageBoxResult.None;
1616

@@ -59,7 +59,7 @@ public static MessageBoxResult Show(
5959
}
6060
catch (Exception e)
6161
{
62-
Log.Error($"|MessageBoxEx.Show|An error occurred: {e.Message}");
62+
App.API.LogError(ClassName, $"An error occurred: {e.Message}");
6363
msgBox = null;
6464
return MessageBoxResult.None;
6565
}

Flow.Launcher/Msg.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Windows.Input;
66
using System.Windows.Media.Animation;
77
using Flow.Launcher.Infrastructure;
8-
using Flow.Launcher.Infrastructure.Image;
98

109
namespace Flow.Launcher
1110
{

Flow.Launcher/Notification.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Flow.Launcher.Infrastructure;
2-
using Flow.Launcher.Infrastructure.Logger;
32
using Microsoft.Toolkit.Uwp.Notifications;
43
using System;
54
using System.IO;
@@ -9,6 +8,8 @@ namespace Flow.Launcher
98
{
109
internal static class Notification
1110
{
11+
private static readonly string ClassName = nameof(Notification);
12+
1213
internal static bool legacy = !Win32Helper.IsNotificationSupported();
1314

1415
internal static void Uninstall()
@@ -51,12 +52,12 @@ private static void ShowInternal(string title, string subTitle, string iconPath
5152
{
5253
// Temporary fix for the Windows 11 notification issue
5354
// Possibly from 22621.1413 or 22621.1485, judging by post time of #2024
54-
Log.Exception("Flow.Launcher.Notification|Notification InvalidOperationException Error", e);
55+
App.API.LogException(ClassName, "Notification InvalidOperationException Error", e);
5556
LegacyShow(title, subTitle, iconPath);
5657
}
5758
catch (Exception e)
5859
{
59-
Log.Exception("Flow.Launcher.Notification|Notification Error", e);
60+
App.API.LogException(ClassName, "Notification Error", e);
6061
LegacyShow(title, subTitle, iconPath);
6162
}
6263
}

Flow.Launcher/ProgressBoxEx.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using System.Threading.Tasks;
33
using System.Windows;
44
using System.Windows.Input;
5-
using Flow.Launcher.Infrastructure.Logger;
65

76
namespace Flow.Launcher
87
{
98
public partial class ProgressBoxEx : Window
109
{
10+
private static readonly string ClassName = nameof(ProgressBoxEx);
11+
1112
private readonly Action _cancelProgress;
1213

1314
private ProgressBoxEx(Action cancelProgress)
@@ -47,7 +48,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
4748
}
4849
catch (Exception e)
4950
{
50-
Log.Error($"|ProgressBoxEx.Show|An error occurred: {e.Message}");
51+
App.API.LogError(ClassName, $"An error occurred: {e.Message}");
5152

5253
await reportProgressAsync(null).ConfigureAwait(false);
5354
}

0 commit comments

Comments
 (0)