Skip to content

Commit 0682e9b

Browse files
committed
Improve code quality for public api
1 parent b9418f1 commit 0682e9b

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public static class PluginsManifest
2222
private static DateTime lastFetchedAt = DateTime.MinValue;
2323
private static readonly TimeSpan fetchTimeout = TimeSpan.FromMinutes(2);
2424

25+
// We should not initialize API in static constructor because it will create another API instance
26+
private static IPublicAPI api = null;
27+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
28+
2529
public static List<UserPlugin> UserPlugins { get; private set; }
2630

2731
public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default)
@@ -46,7 +50,7 @@ public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = fals
4650
}
4751
catch (Exception e)
4852
{
49-
Ioc.Default.GetRequiredService<IPublicAPI>().LogException(ClassName, "Http request failed", e);
53+
API.LogException(ClassName, "Http request failed", e);
5054
}
5155
finally
5256
{

Flow.Launcher.Core/Plugin/PluginsLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source
126126

127127
_ = Task.Run(() =>
128128
{
129-
Ioc.Default.GetRequiredService<IPublicAPI>().ShowMsgBox($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
129+
API.ShowMsgBox($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
130130
$"{errorPluginString}{Environment.NewLine}{Environment.NewLine}" +
131131
$"Please refer to the logs for more information", "",
132132
MessageBoxButton.OK, MessageBoxImage.Warning);

Flow.Launcher.Core/Resource/Internationalization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public bool PromptShouldUsePinyin(string languageCodeToSet)
214214
// "Do you want to search with pinyin?"
215215
string text = languageToSet == AvailableLanguages.Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?" ;
216216

217-
if (Ioc.Default.GetRequiredService<IPublicAPI>().ShowMsgBox(text, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No)
217+
if (API.ShowMsgBox(text, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No)
218218
return false;
219219

220220
return true;

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public static class Http
2020

2121
private static readonly HttpClient client = new();
2222

23+
// We should not initialize API in static constructor because it will create another API instance
24+
private static IPublicAPI api = null;
25+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
26+
2327
static Http()
2428
{
2529
// need to be added so it would work on a win10 machine
@@ -78,7 +82,7 @@ var userName when string.IsNullOrEmpty(userName) =>
7882
}
7983
catch (UriFormatException e)
8084
{
81-
Ioc.Default.GetRequiredService<IPublicAPI>().ShowMsg("Please try again", "Unable to parse Http Proxy");
85+
API.ShowMsg("Please try again", "Unable to parse Http Proxy");
8286
Log.Exception(ClassName, "Unable to parse Uri", e);
8387
}
8488
}

0 commit comments

Comments
 (0)