Skip to content

Commit 0e366a6

Browse files
committed
Use PublicApi.Instance instead of private one
1 parent 7350c1d commit 0e366a6

File tree

13 files changed

+140
-208
lines changed

13 files changed

+140
-208
lines changed

Flow.Launcher.Core/Configuration/Portable.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Windows;
6-
using CommunityToolkit.Mvvm.DependencyInjection;
76
using Flow.Launcher.Infrastructure;
87
using Flow.Launcher.Infrastructure.UserSettings;
9-
using Flow.Launcher.Plugin;
108
using Flow.Launcher.Plugin.SharedCommands;
119
using Microsoft.Win32;
1210
using Squirrel;
@@ -17,8 +15,6 @@ public class Portable : IPortable
1715
{
1816
private static readonly string ClassName = nameof(Portable);
1917

20-
private readonly IPublicAPI API = Ioc.Default.GetRequiredService<IPublicAPI>();
21-
2218
/// <summary>
2319
/// As at Squirrel.Windows version 1.5.2, UpdateManager needs to be disposed after finish
2420
/// </summary>
@@ -45,13 +41,13 @@ public void DisablePortableMode()
4541
#endif
4642
IndicateDeletion(DataLocation.PortableDataPath);
4743

48-
API.ShowMsgBox(Localize.restartToDisablePortableMode());
44+
PublicApi.Instance.ShowMsgBox(Localize.restartToDisablePortableMode());
4945

5046
UpdateManager.RestartApp(Constant.ApplicationFileName);
5147
}
5248
catch (Exception e)
5349
{
54-
API.LogException(ClassName, "Error occurred while disabling portable mode", e);
50+
PublicApi.Instance.LogException(ClassName, "Error occurred while disabling portable mode", e);
5551
}
5652
}
5753

@@ -68,13 +64,13 @@ public void EnablePortableMode()
6864
#endif
6965
IndicateDeletion(DataLocation.RoamingDataPath);
7066

71-
API.ShowMsgBox(Localize.restartToEnablePortableMode());
67+
PublicApi.Instance.ShowMsgBox(Localize.restartToEnablePortableMode());
7268

7369
UpdateManager.RestartApp(Constant.ApplicationFileName);
7470
}
7571
catch (Exception e)
7672
{
77-
API.LogException(ClassName, "Error occurred while enabling portable mode", e);
73+
PublicApi.Instance.LogException(ClassName, "Error occurred while enabling portable mode", e);
7874
}
7975
}
8076

@@ -94,13 +90,13 @@ public void RemoveUninstallerEntry()
9490

9591
public void MoveUserDataFolder(string fromLocation, string toLocation)
9692
{
97-
FilesFolders.CopyAll(fromLocation, toLocation, (s) => API.ShowMsgBox(s));
93+
FilesFolders.CopyAll(fromLocation, toLocation, (s) => PublicApi.Instance.ShowMsgBox(s));
9894
VerifyUserDataAfterMove(fromLocation, toLocation);
9995
}
10096

10197
public void VerifyUserDataAfterMove(string fromLocation, string toLocation)
10298
{
103-
FilesFolders.VerifyBothFolderFilesEqual(fromLocation, toLocation, (s) => API.ShowMsgBox(s));
99+
FilesFolders.VerifyBothFolderFilesEqual(fromLocation, toLocation, (s) => PublicApi.Instance.ShowMsgBox(s));
104100
}
105101

106102
public void CreateShortcuts()
@@ -150,12 +146,12 @@ public void PreStartCleanUpAfterPortabilityUpdate()
150146
// delete it and prompt the user to pick the portable data location
151147
if (File.Exists(roamingDataDeleteFilePath))
152148
{
153-
FilesFolders.RemoveFolderIfExists(roamingDataDir, (s) => API.ShowMsgBox(s));
149+
FilesFolders.RemoveFolderIfExists(roamingDataDir, (s) => PublicApi.Instance.ShowMsgBox(s));
154150

155-
if (API.ShowMsgBox(Localize.moveToDifferentLocation(),
151+
if (PublicApi.Instance.ShowMsgBox(Localize.moveToDifferentLocation(),
156152
string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
157153
{
158-
FilesFolders.OpenPath(Constant.RootDirectory, (s) => API.ShowMsgBox(s));
154+
FilesFolders.OpenPath(Constant.RootDirectory, (s) => PublicApi.Instance.ShowMsgBox(s));
159155

160156
Environment.Exit(0);
161157
}
@@ -164,9 +160,9 @@ public void PreStartCleanUpAfterPortabilityUpdate()
164160
// delete it and notify the user about it.
165161
else if (File.Exists(portableDataDeleteFilePath))
166162
{
167-
FilesFolders.RemoveFolderIfExists(portableDataDir, (s) => API.ShowMsgBox(s));
163+
FilesFolders.RemoveFolderIfExists(portableDataDir, (s) => PublicApi.Instance.ShowMsgBox(s));
168164

169-
API.ShowMsgBox(Localize.shortcutsUninstallerCreated());
165+
PublicApi.Instance.ShowMsgBox(Localize.shortcutsUninstallerCreated());
170166
}
171167
}
172168

@@ -177,7 +173,7 @@ public bool CanUpdatePortability()
177173

178174
if (roamingLocationExists && portableLocationExists)
179175
{
180-
API.ShowMsgBox(Localize.userDataDuplicated(DataLocation.PortableDataPath, DataLocation.RoamingDataPath, Environment.NewLine));
176+
PublicApi.Instance.ShowMsgBox(Localize.userDataDuplicated(DataLocation.PortableDataPath, DataLocation.RoamingDataPath, Environment.NewLine));
181177

182178
return false;
183179
}

Flow.Launcher.Core/ExternalPlugins/CommunityPluginSource.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text.Json.Serialization;
99
using System.Threading;
1010
using System.Threading.Tasks;
11-
using CommunityToolkit.Mvvm.DependencyInjection;
1211
using Flow.Launcher.Infrastructure.Http;
1312
using Flow.Launcher.Plugin;
1413

@@ -18,13 +17,9 @@ public record CommunityPluginSource(string ManifestFileUrl)
1817
{
1918
private static readonly string ClassName = nameof(CommunityPluginSource);
2019

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-
2520
private string latestEtag = "";
2621

27-
private List<UserPlugin> plugins = new();
22+
private List<UserPlugin> plugins = [];
2823

2924
private static readonly JsonSerializerOptions PluginStoreItemSerializationOption = new()
3025
{
@@ -41,7 +36,7 @@ public record CommunityPluginSource(string ManifestFileUrl)
4136
/// </remarks>
4237
public async Task<List<UserPlugin>> FetchAsync(CancellationToken token)
4338
{
44-
API.LogInfo(ClassName, $"Loading plugins from {ManifestFileUrl}");
39+
PublicApi.Instance.LogInfo(ClassName, $"Loading plugins from {ManifestFileUrl}");
4540

4641
var request = new HttpRequestMessage(HttpMethod.Get, ManifestFileUrl);
4742

@@ -59,40 +54,40 @@ public async Task<List<UserPlugin>> FetchAsync(CancellationToken token)
5954
.ConfigureAwait(false);
6055
latestEtag = response.Headers.ETag?.Tag;
6156

62-
API.LogInfo(ClassName, $"Loaded {plugins.Count} plugins from {ManifestFileUrl}");
57+
PublicApi.Instance.LogInfo(ClassName, $"Loaded {plugins.Count} plugins from {ManifestFileUrl}");
6358
return plugins;
6459
}
6560
else if (response.StatusCode == HttpStatusCode.NotModified)
6661
{
67-
API.LogInfo(ClassName, $"Resource {ManifestFileUrl} has not been modified.");
62+
PublicApi.Instance.LogInfo(ClassName, $"Resource {ManifestFileUrl} has not been modified.");
6863
return plugins;
6964
}
7065
else
7166
{
72-
API.LogWarn(ClassName, $"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
67+
PublicApi.Instance.LogWarn(ClassName, $"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
7368
return null;
7469
}
7570
}
7671
catch (OperationCanceledException) when (token.IsCancellationRequested)
7772
{
78-
API.LogDebug(ClassName, $"Fetching from {ManifestFileUrl} was cancelled by caller.");
73+
PublicApi.Instance.LogDebug(ClassName, $"Fetching from {ManifestFileUrl} was cancelled by caller.");
7974
return null;
8075
}
8176
catch (TaskCanceledException)
8277
{
8378
// Likely an HttpClient timeout or external cancellation not requested by our token
84-
API.LogWarn(ClassName, $"Fetching from {ManifestFileUrl} timed out.");
79+
PublicApi.Instance.LogWarn(ClassName, $"Fetching from {ManifestFileUrl} timed out.");
8580
return null;
8681
}
8782
catch (Exception e)
8883
{
8984
if (e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException)
9085
{
91-
API.LogException(ClassName, $"Check your connection and proxy settings to {ManifestFileUrl}.", e);
86+
PublicApi.Instance.LogException(ClassName, $"Check your connection and proxy settings to {ManifestFileUrl}.", e);
9287
}
9388
else
9489
{
95-
API.LogException(ClassName, "Error Occurred", e);
90+
PublicApi.Instance.LogException(ClassName, "Error Occurred", e);
9691
}
9792
return null;
9893
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Windows;
66
using System.Windows.Forms;
7-
using CommunityToolkit.Mvvm.DependencyInjection;
87
using Flow.Launcher.Infrastructure.UserSettings;
98
using Flow.Launcher.Plugin;
109
using Flow.Launcher.Plugin.SharedCommands;
@@ -15,8 +14,6 @@ public abstract class AbstractPluginEnvironment
1514
{
1615
private static readonly string ClassName = nameof(AbstractPluginEnvironment);
1716

18-
protected readonly IPublicAPI API = Ioc.Default.GetRequiredService<IPublicAPI>();
19-
2017
internal abstract string Language { get; }
2118

2219
internal abstract string EnvName { get; }
@@ -59,7 +56,7 @@ internal IEnumerable<PluginPair> Setup()
5956
}
6057

6158
var noRuntimeMessage = Localize.runtimePluginInstalledChooseRuntimePrompt(Language, EnvName, Environment.NewLine);
62-
if (API.ShowMsgBox(noRuntimeMessage, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No)
59+
if (PublicApi.Instance.ShowMsgBox(noRuntimeMessage, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No)
6360
{
6461
var msg = Localize.runtimePluginChooseRuntimeExecutable(EnvName);
6562

@@ -77,7 +74,7 @@ internal IEnumerable<PluginPair> Setup()
7774
// Let users select valid path or choose to download
7875
while (string.IsNullOrEmpty(selectedFile))
7976
{
80-
if (API.ShowMsgBox(forceDownloadMessage, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
77+
if (PublicApi.Instance.ShowMsgBox(forceDownloadMessage, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
8178
{
8279
// Continue select file
8380
selectedFile = GetFileFromDialog(msg, FileDialogFilter);
@@ -110,8 +107,8 @@ internal IEnumerable<PluginPair> Setup()
110107
}
111108
else
112109
{
113-
API.ShowMsgBox(Localize.runtimePluginUnableToSetExecutablePath(Language));
114-
API.LogError(ClassName,
110+
PublicApi.Instance.ShowMsgBox(Localize.runtimePluginUnableToSetExecutablePath(Language));
111+
PublicApi.Instance.LogError(ClassName,
115112
$"Not able to successfully set {EnvName} path, setting's plugin executable path variable is still an empty string.",
116113
$"{Language}Environment");
117114

@@ -125,7 +122,7 @@ private void EnsureLatestInstalled(string expectedPath, string currentPath, stri
125122
{
126123
if (expectedPath == currentPath) return;
127124

128-
FilesFolders.RemoveFolderIfExists(installedDirPath, (s) => API.ShowMsgBox(s));
125+
FilesFolders.RemoveFolderIfExists(installedDirPath, (s) => PublicApi.Instance.ShowMsgBox(s));
129126

130127
InstallEnvironment();
131128
}

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Threading;
44
using System.Threading.Tasks;
5-
using CommunityToolkit.Mvvm.DependencyInjection;
65
using Flow.Launcher.Plugin;
76
using Flow.Launcher.Infrastructure;
87

@@ -23,10 +22,6 @@ public static class PluginsManifest
2322
private static DateTime lastFetchedAt = DateTime.MinValue;
2423
private static readonly TimeSpan fetchTimeout = TimeSpan.FromMinutes(2);
2524

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

3227
public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default)
@@ -61,7 +56,7 @@ public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = fals
6156
}
6257
catch (Exception e)
6358
{
64-
API.LogException(ClassName, "Http request failed", e);
59+
PublicApi.Instance.LogException(ClassName, "Http request failed", e);
6560
}
6661
finally
6762
{
@@ -83,12 +78,12 @@ private static bool IsMinimumAppVersionSatisfied(UserPlugin plugin, SemanticVers
8378
}
8479
catch (Exception e)
8580
{
86-
API.LogException(ClassName, $"Failed to parse the minimum app version {plugin.MinimumAppVersion} for plugin {plugin.Name}. "
81+
PublicApi.Instance.LogException(ClassName, $"Failed to parse the minimum app version {plugin.MinimumAppVersion} for plugin {plugin.Name}. "
8782
+ "Plugin excluded from manifest", e);
8883
return false;
8984
}
9085

91-
API.LogInfo(ClassName, $"Plugin {plugin.Name} requires minimum Flow Launcher version {plugin.MinimumAppVersion}, "
86+
PublicApi.Instance.LogInfo(ClassName, $"Plugin {plugin.Name} requires minimum Flow Launcher version {plugin.MinimumAppVersion}, "
9287
+ $"but current version is {Constant.Version}. Plugin excluded from manifest.");
9388

9489
return false;

Flow.Launcher.Core/Plugin/PluginConfig.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
using Flow.Launcher.Infrastructure;
66
using Flow.Launcher.Plugin;
77
using System.Text.Json;
8-
using CommunityToolkit.Mvvm.DependencyInjection;
98

109
namespace Flow.Launcher.Core.Plugin
1110
{
1211
internal abstract class PluginConfig
1312
{
1413
private static readonly string ClassName = nameof(PluginConfig);
1514

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-
2015
/// <summary>
2116
/// Parse plugin metadata in the given directories
2217
/// </summary>
@@ -38,7 +33,7 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
3833
}
3934
catch (Exception e)
4035
{
41-
API.LogException(ClassName, $"Can't delete <{directory}>", e);
36+
PublicApi.Instance.LogException(ClassName, $"Can't delete <{directory}>", e);
4237
}
4338
}
4439
else
@@ -55,7 +50,7 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
5550

5651
duplicateList
5752
.ForEach(
58-
x => API.LogWarn(ClassName,
53+
x => PublicApi.Instance.LogWarn(ClassName,
5954
string.Format("Duplicate plugin name: {0}, id: {1}, version: {2} " +
6055
"not loaded due to version not the highest of the duplicates",
6156
x.Name, x.ID, x.Version),
@@ -107,7 +102,7 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
107102
string configPath = Path.Combine(pluginDirectory, Constant.PluginMetadataFileName);
108103
if (!File.Exists(configPath))
109104
{
110-
API.LogError(ClassName, $"Didn't find config file <{configPath}>");
105+
PublicApi.Instance.LogError(ClassName, $"Didn't find config file <{configPath}>");
111106
return null;
112107
}
113108

@@ -123,19 +118,19 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
123118
}
124119
catch (Exception e)
125120
{
126-
API.LogException(ClassName, $"Invalid json for config <{configPath}>", e);
121+
PublicApi.Instance.LogException(ClassName, $"Invalid json for config <{configPath}>", e);
127122
return null;
128123
}
129124

130125
if (!AllowedLanguage.IsAllowed(metadata.Language))
131126
{
132-
API.LogError(ClassName, $"Invalid language <{metadata.Language}> for config <{configPath}>");
127+
PublicApi.Instance.LogError(ClassName, $"Invalid language <{metadata.Language}> for config <{configPath}>");
133128
return null;
134129
}
135130

136131
if (!File.Exists(metadata.ExecuteFilePath))
137132
{
138-
API.LogError(ClassName, $"Execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
133+
PublicApi.Instance.LogError(ClassName, $"Execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
139134
return null;
140135
}
141136

0 commit comments

Comments
 (0)