Skip to content

Commit dde933a

Browse files
committed
Use api functions instead
1 parent e31f14e commit dde933a

File tree

8 files changed

+79
-64
lines changed

8 files changed

+79
-64
lines changed

Flow.Launcher.Core/Configuration/Portable.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
using Microsoft.Win32;
2-
using Squirrel;
3-
using System;
1+
using System;
42
using System.IO;
3+
using System.Linq;
54
using System.Reflection;
65
using System.Windows;
6+
using CommunityToolkit.Mvvm.DependencyInjection;
77
using Flow.Launcher.Infrastructure;
8-
using Flow.Launcher.Infrastructure.Logger;
98
using Flow.Launcher.Infrastructure.UserSettings;
10-
using Flow.Launcher.Plugin.SharedCommands;
11-
using System.Linq;
12-
using CommunityToolkit.Mvvm.DependencyInjection;
139
using Flow.Launcher.Plugin;
10+
using Flow.Launcher.Plugin.SharedCommands;
11+
using Microsoft.Win32;
12+
using Squirrel;
1413

1514
namespace Flow.Launcher.Core.Configuration
1615
{
1716
public class Portable : IPortable
1817
{
18+
private static readonly string ClassName = nameof(Portable);
19+
1920
private readonly IPublicAPI API = Ioc.Default.GetRequiredService<IPublicAPI>();
2021

2122
/// <summary>
@@ -51,7 +52,7 @@ public void DisablePortableMode()
5152
}
5253
catch (Exception e)
5354
{
54-
Log.Exception("|Portable.DisablePortableMode|Error occurred while disabling portable mode", e);
55+
API.LogException(ClassName, "Error occurred while disabling portable mode", e);
5556
}
5657
}
5758

@@ -75,7 +76,7 @@ public void EnablePortableMode()
7576
}
7677
catch (Exception e)
7778
{
78-
Log.Exception("|Portable.EnablePortableMode|Error occurred while enabling portable mode", e);
79+
API.LogException(ClassName, "Error occurred while enabling portable mode", e);
7980
}
8081
}
8182

Flow.Launcher.Core/ExternalPlugins/CommunityPluginSource.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using Flow.Launcher.Infrastructure.Http;
2-
using Flow.Launcher.Infrastructure.Logger;
3-
using Flow.Launcher.Plugin;
4-
using System;
1+
using System;
52
using System.Collections.Generic;
63
using System.Net;
74
using System.Net.Http;
@@ -11,11 +8,18 @@
118
using System.Text.Json.Serialization;
129
using System.Threading;
1310
using System.Threading.Tasks;
11+
using CommunityToolkit.Mvvm.DependencyInjection;
12+
using Flow.Launcher.Infrastructure.Http;
13+
using Flow.Launcher.Plugin;
1414

1515
namespace Flow.Launcher.Core.ExternalPlugins
1616
{
1717
public record CommunityPluginSource(string ManifestFileUrl)
1818
{
19+
// We should not initialize API in static constructor because it will create another API instance
20+
private static IPublicAPI api = null;
21+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
22+
1923
private static readonly string ClassName = nameof(CommunityPluginSource);
2024

2125
private string latestEtag = "";
@@ -37,7 +41,7 @@ public record CommunityPluginSource(string ManifestFileUrl)
3741
/// </remarks>
3842
public async Task<List<UserPlugin>> FetchAsync(CancellationToken token)
3943
{
40-
Log.Info(ClassName, $"Loading plugins from {ManifestFileUrl}");
44+
API.LogInfo(ClassName, $"Loading plugins from {ManifestFileUrl}");
4145

4246
var request = new HttpRequestMessage(HttpMethod.Get, ManifestFileUrl);
4347

@@ -55,30 +59,29 @@ public async Task<List<UserPlugin>> FetchAsync(CancellationToken token)
5559
.ConfigureAwait(false);
5660
latestEtag = response.Headers.ETag?.Tag;
5761

58-
Log.Info(ClassName, $"Loaded {plugins.Count} plugins from {ManifestFileUrl}");
62+
API.LogInfo(ClassName, $"Loaded {plugins.Count} plugins from {ManifestFileUrl}");
5963
return plugins;
6064
}
6165
else if (response.StatusCode == HttpStatusCode.NotModified)
6266
{
63-
Log.Info(ClassName, $"Resource {ManifestFileUrl} has not been modified.");
67+
API.LogInfo(ClassName, $"Resource {ManifestFileUrl} has not been modified.");
6468
return plugins;
6569
}
6670
else
6771
{
68-
Log.Warn(ClassName,
69-
$"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
72+
API.LogWarn(ClassName, $"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
7073
return plugins;
7174
}
7275
}
7376
catch (Exception e)
7477
{
7578
if (e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException)
7679
{
77-
Log.Exception(ClassName, $"Check your connection and proxy settings to {ManifestFileUrl}.", e);
80+
API.LogException(ClassName, $"Check your connection and proxy settings to {ManifestFileUrl}.", e);
7881
}
7982
else
8083
{
81-
Log.Exception(ClassName, "Error Occurred", e);
84+
API.LogException(ClassName, "Error Occurred", e);
8285
}
8386
return plugins;
8487
}

Flow.Launcher.Core/ExternalPlugins/Environments/AbstractPluginEnvironment.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Windows;
66
using System.Windows.Forms;
77
using CommunityToolkit.Mvvm.DependencyInjection;
8-
using Flow.Launcher.Infrastructure.Logger;
98
using Flow.Launcher.Infrastructure.UserSettings;
109
using Flow.Launcher.Plugin;
1110
using Flow.Launcher.Plugin.SharedCommands;
@@ -14,6 +13,8 @@ namespace Flow.Launcher.Core.ExternalPlugins.Environments
1413
{
1514
public abstract class AbstractPluginEnvironment
1615
{
16+
private static readonly string ClassName = nameof(AbstractPluginEnvironment);
17+
1718
protected readonly IPublicAPI API = Ioc.Default.GetRequiredService<IPublicAPI>();
1819

1920
internal abstract string Language { get; }
@@ -120,7 +121,7 @@ internal IEnumerable<PluginPair> Setup()
120121
else
121122
{
122123
API.ShowMsgBox(string.Format(API.GetTranslation("runtimePluginUnableToSetExecutablePath"), Language));
123-
Log.Error("PluginsLoader",
124+
API.LogError(ClassName,
124125
$"Not able to successfully set {EnvName} path, setting's plugin executable path variable is still an empty string.",
125126
$"{Language}Environment");
126127

Flow.Launcher.Core/Plugin/PluginConfig.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
using System.Linq;
44
using System.IO;
55
using Flow.Launcher.Infrastructure;
6-
using Flow.Launcher.Infrastructure.Logger;
76
using Flow.Launcher.Plugin;
87
using System.Text.Json;
8+
using CommunityToolkit.Mvvm.DependencyInjection;
99

1010
namespace Flow.Launcher.Core.Plugin
1111
{
1212
internal abstract class PluginConfig
1313
{
14+
private static readonly string ClassName = nameof(PluginConfig);
15+
16+
// We should not initialize API in static constructor because it will create another API instance
17+
private static IPublicAPI api = null;
18+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
19+
1420
/// <summary>
1521
/// Parse plugin metadata in the given directories
1622
/// </summary>
@@ -32,7 +38,7 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
3238
}
3339
catch (Exception e)
3440
{
35-
Log.Exception($"|PluginConfig.ParsePLuginConfigs|Can't delete <{directory}>", e);
41+
API.LogException(ClassName, $"Can't delete <{directory}>", e);
3642
}
3743
}
3844
else
@@ -49,11 +55,11 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
4955

5056
duplicateList
5157
.ForEach(
52-
x => Log.Warn("PluginConfig",
53-
string.Format("Duplicate plugin name: {0}, id: {1}, version: {2} " +
54-
"not loaded due to version not the highest of the duplicates",
55-
x.Name, x.ID, x.Version),
56-
"GetUniqueLatestPluginMetadata"));
58+
x => API.LogWarn(ClassName,
59+
string.Format("Duplicate plugin name: {0}, id: {1}, version: {2} " +
60+
"not loaded due to version not the highest of the duplicates",
61+
x.Name, x.ID, x.Version),
62+
"GetUniqueLatestPluginMetadata"));
5763

5864
return uniqueList;
5965
}
@@ -101,7 +107,7 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
101107
string configPath = Path.Combine(pluginDirectory, Constant.PluginMetadataFileName);
102108
if (!File.Exists(configPath))
103109
{
104-
Log.Error($"|PluginConfig.GetPluginMetadata|Didn't find config file <{configPath}>");
110+
API.LogError(ClassName, $"Didn't find config file <{configPath}>");
105111
return null;
106112
}
107113

@@ -117,19 +123,19 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
117123
}
118124
catch (Exception e)
119125
{
120-
Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e);
126+
API.LogException(ClassName, $"Invalid json for config <{configPath}>", e);
121127
return null;
122128
}
123129

124130
if (!AllowedLanguage.IsAllowed(metadata.Language))
125131
{
126-
Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>");
132+
API.LogError(ClassName, $"Invalid language <{metadata.Language}> for config <{configPath}>");
127133
return null;
128134
}
129135

130136
if (!File.Exists(metadata.ExecuteFilePath))
131137
{
132-
Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
138+
API.LogError(ClassName, $"Execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
133139
return null;
134140
}
135141

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using CommunityToolkit.Mvvm.DependencyInjection;
1010
using Flow.Launcher.Core.ExternalPlugins;
1111
using Flow.Launcher.Infrastructure;
12-
using Flow.Launcher.Infrastructure.Logger;
1312
using Flow.Launcher.Infrastructure.UserSettings;
1413
using Flow.Launcher.Plugin;
1514
using Flow.Launcher.Plugin.SharedCommands;
@@ -214,12 +213,12 @@ public static async Task InitializePluginsAsync()
214213
() => pair.Plugin.InitAsync(new PluginInitContext(pair.Metadata, API)));
215214

216215
pair.Metadata.InitTime += milliseconds;
217-
Log.Info(
218-
$"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
216+
API.LogInfo(ClassName,
217+
$"Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
219218
}
220219
catch (Exception e)
221220
{
222-
Log.Exception(ClassName, $"Fail to Init plugin: {pair.Metadata.Name}", e);
221+
API.LogException(ClassName, $"Fail to Init plugin: {pair.Metadata.Name}", e);
223222
pair.Metadata.Disabled = true;
224223
failedPlugins.Enqueue(pair);
225224
}
@@ -370,8 +369,8 @@ public static List<Result> GetContextMenusForPlugin(Result result)
370369
}
371370
catch (Exception e)
372371
{
373-
Log.Exception(
374-
$"|PluginManager.GetContextMenusForPlugin|Can't load context menus for plugin <{pluginPair.Metadata.Name}>",
372+
API.LogException(ClassName,
373+
$"Can't load context menus for plugin <{pluginPair.Metadata.Name}>",
375374
e);
376375
}
377376
}
@@ -563,7 +562,7 @@ internal static void InstallPlugin(UserPlugin plugin, string zipFilePath, bool c
563562
}
564563
catch (Exception e)
565564
{
566-
Log.Exception($"|PluginManager.InstallPlugin|Failed to delete temp folder {tempFolderPluginPath}", e);
565+
API.LogException(ClassName, $"Failed to delete temp folder {tempFolderPluginPath}", e);
567566
}
568567

569568
if (checkModified)
@@ -608,7 +607,7 @@ internal static async Task UninstallPluginAsync(PluginMetadata plugin, bool remo
608607
}
609608
catch (Exception e)
610609
{
611-
Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin settings folder for {plugin.Name}", e);
610+
API.LogException(ClassName, $"Failed to delete plugin settings folder for {plugin.Name}", e);
612611
API.ShowMsg(API.GetTranslation("failedToRemovePluginSettingsTitle"),
613612
string.Format(API.GetTranslation("failedToRemovePluginSettingsMessage"), plugin.Name));
614613
}
@@ -624,7 +623,7 @@ internal static async Task UninstallPluginAsync(PluginMetadata plugin, bool remo
624623
}
625624
catch (Exception e)
626625
{
627-
Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin cache folder for {plugin.Name}", e);
626+
API.LogException(ClassName, $"Failed to delete plugin cache folder for {plugin.Name}", e);
628627
API.ShowMsg(API.GetTranslation("failedToRemovePluginCacheTitle"),
629628
string.Format(API.GetTranslation("failedToRemovePluginCacheMessage"), plugin.Name));
630629
}

Flow.Launcher.Core/Resource/Internationalization.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Windows;
77
using Flow.Launcher.Core.Plugin;
88
using Flow.Launcher.Infrastructure;
9-
using Flow.Launcher.Infrastructure.Logger;
109
using Flow.Launcher.Infrastructure.UserSettings;
1110
using Flow.Launcher.Plugin;
1211
using System.Globalization;
@@ -17,18 +16,22 @@ namespace Flow.Launcher.Core.Resource
1716
{
1817
public class Internationalization
1918
{
19+
private static readonly string ClassName = nameof(Internationalization);
20+
2021
private const string Folder = "Languages";
2122
private const string DefaultLanguageCode = "en";
2223
private const string DefaultFile = "en.xaml";
2324
private const string Extension = ".xaml";
2425
private readonly Settings _settings;
26+
private readonly IPublicAPI _api;
2527
private readonly List<string> _languageDirectories = new();
2628
private readonly List<ResourceDictionary> _oldResources = new();
2729
private readonly string SystemLanguageCode;
2830

2931
public Internationalization(Settings settings)
3032
{
3133
_settings = settings;
34+
_api = Ioc.Default.GetRequiredService<IPublicAPI>();
3235
AddFlowLauncherLanguageDirectory();
3336
SystemLanguageCode = GetSystemLanguageCodeAtStartup();
3437
}
@@ -80,7 +83,7 @@ private void AddPluginLanguageDirectories()
8083
}
8184
else
8285
{
83-
Log.Error($"|Internationalization.AddPluginLanguageDirectories|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
86+
_api.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
8487
}
8588
}
8689

@@ -144,13 +147,13 @@ public void ChangeLanguage(string languageCode)
144147
_settings.Language = isSystem ? Constant.SystemLanguageCode : language.LanguageCode;
145148
}
146149

147-
private static Language GetLanguageByLanguageCode(string languageCode)
150+
private Language GetLanguageByLanguageCode(string languageCode)
148151
{
149152
var lowercase = languageCode.ToLower();
150153
var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase);
151154
if (language == null)
152155
{
153-
Log.Error($"|Internationalization.GetLanguageByLanguageCode|Language code can't be found <{languageCode}>");
156+
_api.LogError(ClassName, $"Language code can't be found <{languageCode}>");
154157
return AvailableLanguages.English;
155158
}
156159
else
@@ -239,7 +242,7 @@ public List<Language> LoadAvailableLanguages()
239242
return list;
240243
}
241244

242-
public static string GetTranslation(string key)
245+
public string GetTranslation(string key)
243246
{
244247
var translation = Application.Current.TryFindResource(key);
245248
if (translation is string)
@@ -248,7 +251,7 @@ public static string GetTranslation(string key)
248251
}
249252
else
250253
{
251-
Log.Error($"|Internationalization.GetTranslation|No Translation for key {key}");
254+
_api.LogError(ClassName, $"No Translation for key {key}");
252255
return $"No Translation for key {key}";
253256
}
254257
}
@@ -266,12 +269,12 @@ private void UpdatePluginMetadataTranslations()
266269
}
267270
catch (Exception e)
268271
{
269-
Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Failed for <{p.Metadata.Name}>", e);
272+
_api.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
270273
}
271274
}
272275
}
273276

274-
private static string LanguageFile(string folder, string language)
277+
private string LanguageFile(string folder, string language)
275278
{
276279
if (Directory.Exists(folder))
277280
{
@@ -282,15 +285,15 @@ private static string LanguageFile(string folder, string language)
282285
}
283286
else
284287
{
285-
Log.Error($"|Internationalization.LanguageFile|Language path can't be found <{path}>");
288+
_api.LogError(ClassName, $"Language path can't be found <{path}>");
286289
var english = Path.Combine(folder, DefaultFile);
287290
if (File.Exists(english))
288291
{
289292
return english;
290293
}
291294
else
292295
{
293-
Log.Error($"|Internationalization.LanguageFile|Default English Language path can't be found <{path}>");
296+
_api.LogError(ClassName, $"Default English Language path can't be found <{path}>");
294297
return string.Empty;
295298
}
296299
}

0 commit comments

Comments
 (0)