Skip to content

Commit 35febd3

Browse files
authored
Merge branch 'dev' into load_image_async
2 parents e6a8fe3 + 67cc1e2 commit 35febd3

File tree

20 files changed

+361
-130
lines changed

20 files changed

+361
-130
lines changed

Flow.Launcher.Core/Configuration/Portable.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Portable : IPortable
2222
/// As at Squirrel.Windows version 1.5.2, UpdateManager needs to be disposed after finish
2323
/// </summary>
2424
/// <returns></returns>
25-
private UpdateManager NewUpdateManager()
25+
private static UpdateManager NewUpdateManager()
2626
{
2727
var applicationFolderName = Constant.ApplicationDirectory
2828
.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None)
@@ -81,20 +81,16 @@ public void EnablePortableMode()
8181

8282
public void RemoveShortcuts()
8383
{
84-
using (var portabilityUpdater = NewUpdateManager())
85-
{
86-
portabilityUpdater.RemoveShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.StartMenu);
87-
portabilityUpdater.RemoveShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Desktop);
88-
portabilityUpdater.RemoveShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Startup);
89-
}
84+
using var portabilityUpdater = NewUpdateManager();
85+
portabilityUpdater.RemoveShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.StartMenu);
86+
portabilityUpdater.RemoveShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Desktop);
87+
portabilityUpdater.RemoveShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Startup);
9088
}
9189

9290
public void RemoveUninstallerEntry()
9391
{
94-
using (var portabilityUpdater = NewUpdateManager())
95-
{
96-
portabilityUpdater.RemoveUninstallerRegistryEntry();
97-
}
92+
using var portabilityUpdater = NewUpdateManager();
93+
portabilityUpdater.RemoveUninstallerRegistryEntry();
9894
}
9995

10096
public void MoveUserDataFolder(string fromLocation, string toLocation)
@@ -110,12 +106,10 @@ public void VerifyUserDataAfterMove(string fromLocation, string toLocation)
110106

111107
public void CreateShortcuts()
112108
{
113-
using (var portabilityUpdater = NewUpdateManager())
114-
{
115-
portabilityUpdater.CreateShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.StartMenu, false);
116-
portabilityUpdater.CreateShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Desktop, false);
117-
portabilityUpdater.CreateShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Startup, false);
118-
}
109+
using var portabilityUpdater = NewUpdateManager();
110+
portabilityUpdater.CreateShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.StartMenu, false);
111+
portabilityUpdater.CreateShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Desktop, false);
112+
portabilityUpdater.CreateShortcutsForExecutable(Constant.ApplicationFileName, ShortcutLocation.Startup, false);
119113
}
120114

121115
public void CreateUninstallerEntry()
@@ -129,18 +123,14 @@ public void CreateUninstallerEntry()
129123
subKey2.SetValue("DisplayIcon", Path.Combine(Constant.ApplicationDirectory, "app.ico"), RegistryValueKind.String);
130124
}
131125

132-
using (var portabilityUpdater = NewUpdateManager())
133-
{
134-
_ = portabilityUpdater.CreateUninstallerRegistryEntry();
135-
}
126+
using var portabilityUpdater = NewUpdateManager();
127+
_ = portabilityUpdater.CreateUninstallerRegistryEntry();
136128
}
137129

138-
internal void IndicateDeletion(string filePathTodelete)
130+
private static void IndicateDeletion(string filePathTodelete)
139131
{
140132
var deleteFilePath = Path.Combine(filePathTodelete, DataLocation.DeletionIndicatorFile);
141-
using (var _ = File.CreateText(deleteFilePath))
142-
{
143-
}
133+
using var _ = File.CreateText(deleteFilePath);
144134
}
145135

146136
///<summary>

Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class JsonRPCPluginSettings
2323
protected ConcurrentDictionary<string, object?> Settings { get; set; } = null!;
2424
public required IPublicAPI API { get; init; }
2525

26+
private static readonly string ClassName = nameof(JsonRPCPluginSettings);
27+
2628
private JsonStorage<ConcurrentDictionary<string, object?>> _storage = null!;
2729

2830
private static readonly Thickness SettingPanelMargin = (Thickness)Application.Current.FindResource("SettingPanelMargin");
@@ -122,12 +124,26 @@ public void UpdateSettings(IReadOnlyDictionary<string, object> settings)
122124

123125
public async Task SaveAsync()
124126
{
125-
await _storage.SaveAsync();
127+
try
128+
{
129+
await _storage.SaveAsync();
130+
}
131+
catch (System.Exception e)
132+
{
133+
API.LogException(ClassName, $"Failed to save plugin settings to path: {SettingPath}", e);
134+
}
126135
}
127136

128137
public void Save()
129138
{
130-
_storage.Save();
139+
try
140+
{
141+
_storage.Save();
142+
}
143+
catch (System.Exception e)
144+
{
145+
API.LogException(ClassName, $"Failed to save plugin settings to path: {SettingPath}", e);
146+
}
131147
}
132148

133149
public bool NeedCreateSettingPanel()

Flow.Launcher.Core/Updater.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
using System.Net.Http;
55
using System.Net.Sockets;
66
using System.Linq;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
using System.Threading;
710
using System.Threading.Tasks;
811
using System.Windows;
9-
using JetBrains.Annotations;
10-
using Squirrel;
12+
using CommunityToolkit.Mvvm.DependencyInjection;
1113
using Flow.Launcher.Core.Resource;
1214
using Flow.Launcher.Plugin.SharedCommands;
1315
using Flow.Launcher.Infrastructure;
1416
using Flow.Launcher.Infrastructure.Http;
1517
using Flow.Launcher.Infrastructure.Logger;
1618
using Flow.Launcher.Infrastructure.UserSettings;
1719
using Flow.Launcher.Plugin;
18-
using System.Text.Json.Serialization;
19-
using System.Threading;
20+
using JetBrains.Annotations;
21+
using Squirrel;
2022

2123
namespace Flow.Launcher.Core
2224
{
@@ -51,7 +53,7 @@ public async Task UpdateAppAsync(bool silentUpdate = true)
5153
var newReleaseVersion = Version.Parse(newUpdateInfo.FutureReleaseEntry.Version.ToString());
5254
var currentVersion = Version.Parse(Constant.Version);
5355

54-
Log.Info($"|Updater.UpdateApp|Future Release <{newUpdateInfo.FutureReleaseEntry.Formatted()}>");
56+
Log.Info($"|Updater.UpdateApp|Future Release <{Formatted(newUpdateInfo.FutureReleaseEntry)}>");
5557

5658
if (newReleaseVersion <= currentVersion)
5759
{
@@ -70,7 +72,7 @@ public async Task UpdateAppAsync(bool silentUpdate = true)
7072

7173
if (DataLocation.PortableDataLocationInUse())
7274
{
73-
var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion.ToString()}\\{DataLocation.PortableFolderName}";
75+
var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion}\\{DataLocation.PortableFolderName}";
7476
FilesFolders.CopyAll(DataLocation.PortableDataPath, targetDestination, (s) => _api.ShowMsgBox(s));
7577
if (!FilesFolders.VerifyBothFolderFilesEqual(DataLocation.PortableDataPath, targetDestination, (s) => _api.ShowMsgBox(s)))
7678
_api.ShowMsgBox(string.Format(_api.GetTranslation("update_flowlauncher_fail_moving_portable_user_profile_data"),
@@ -122,7 +124,7 @@ private class GithubRelease
122124
}
123125

124126
// https://github.com/Squirrel/Squirrel.Windows/blob/master/src/Squirrel/UpdateManager.Factory.cs
125-
private async Task<UpdateManager> GitHubUpdateManagerAsync(string repository)
127+
private static async Task<UpdateManager> GitHubUpdateManagerAsync(string repository)
126128
{
127129
var uri = new Uri(repository);
128130
var api = $"https://api.github.com/repos{uri.AbsolutePath}/releases";
@@ -144,12 +146,22 @@ private async Task<UpdateManager> GitHubUpdateManagerAsync(string repository)
144146
return manager;
145147
}
146148

147-
public string NewVersionTips(string version)
149+
private static string NewVersionTips(string version)
148150
{
149-
var translator = InternationalizationManager.Instance;
151+
var translator = Ioc.Default.GetRequiredService<Internationalization>();
150152
var tips = string.Format(translator.GetTranslation("newVersionTips"), version);
151153

152154
return tips;
153155
}
156+
157+
private static string Formatted<T>(T t)
158+
{
159+
var formatted = JsonSerializer.Serialize(t, new JsonSerializerOptions
160+
{
161+
WriteIndented = true
162+
});
163+
164+
return formatted;
165+
}
154166
}
155167
}
Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
#nullable enable
22

33
using System;
4-
using System.IO;
5-
using System.Text.Json;
6-
using System.Text.Json.Serialization;
74

85
namespace Flow.Launcher.Infrastructure
96
{
107
public static class Helper
118
{
12-
static Helper()
13-
{
14-
jsonFormattedSerializerOptions.Converters.Add(new JsonStringEnumConverter());
15-
}
16-
179
/// <summary>
1810
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
1911
/// </summary>
@@ -36,55 +28,5 @@ public static void RequireNonNull<T>(this T obj)
3628
throw new NullReferenceException();
3729
}
3830
}
39-
40-
public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
41-
{
42-
if (!Directory.Exists(dataDirectory))
43-
{
44-
Directory.CreateDirectory(dataDirectory);
45-
}
46-
47-
foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
48-
{
49-
var data = Path.GetFileName(bundledDataPath);
50-
var dataPath = Path.Combine(dataDirectory, data.NonNull());
51-
if (!File.Exists(dataPath))
52-
{
53-
File.Copy(bundledDataPath, dataPath);
54-
}
55-
else
56-
{
57-
var time1 = new FileInfo(bundledDataPath).LastWriteTimeUtc;
58-
var time2 = new FileInfo(dataPath).LastWriteTimeUtc;
59-
if (time1 != time2)
60-
{
61-
File.Copy(bundledDataPath, dataPath, true);
62-
}
63-
}
64-
}
65-
}
66-
67-
public static void ValidateDirectory(string path)
68-
{
69-
if (!Directory.Exists(path))
70-
{
71-
Directory.CreateDirectory(path);
72-
}
73-
}
74-
75-
private static readonly JsonSerializerOptions jsonFormattedSerializerOptions = new JsonSerializerOptions
76-
{
77-
WriteIndented = true
78-
};
79-
80-
public static string Formatted<T>(this T t)
81-
{
82-
var formatted = JsonSerializer.Serialize(t, new JsonSerializerOptions
83-
{
84-
WriteIndented = true
85-
});
86-
87-
return formatted;
88-
}
8931
}
9032
}

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ await Stopwatch.NormalAsync("|ImageLoader.Initialize|Preload images cost", async
6161
});
6262
}
6363

64-
public static async Task Save()
64+
public static async Task SaveAsync()
6565
{
6666
await storageLock.WaitAsync();
6767

@@ -71,12 +71,22 @@ await _storage.SaveAsync(ImageCache.EnumerateEntries()
7171
.Select(x => x.Key)
7272
.ToList());
7373
}
74+
catch (System.Exception e)
75+
{
76+
Log.Exception($"|ImageLoader.SaveAsync|Failed to save image cache to file", e);
77+
}
7478
finally
7579
{
7680
storageLock.Release();
7781
}
7882
}
7983

84+
public static async Task WaitSaveAsync()
85+
{
86+
await storageLock.WaitAsync();
87+
storageLock.Release();
88+
}
89+
8090
private static async Task<List<(string, bool)>> LoadStorageToConcurrentDictionaryAsync()
8191
{
8292
await storageLock.WaitAsync();

Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using Flow.Launcher.Infrastructure.Logger;
44
using Flow.Launcher.Infrastructure.UserSettings;
5+
using Flow.Launcher.Plugin.SharedCommands;
56
using MemoryPack;
67

78
namespace Flow.Launcher.Infrastructure.Storage
@@ -21,7 +22,7 @@ public class BinaryStorage<T>
2122
public BinaryStorage(string filename, string directoryPath = null)
2223
{
2324
directoryPath ??= DataLocation.CacheDirectory;
24-
Helper.ValidateDirectory(directoryPath);
25+
FilesFolders.ValidateDirectory(directoryPath);
2526

2627
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
2728
}
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
11
using System.IO;
2+
using System.Threading.Tasks;
3+
using CommunityToolkit.Mvvm.DependencyInjection;
24
using Flow.Launcher.Infrastructure.UserSettings;
5+
using Flow.Launcher.Plugin;
6+
using Flow.Launcher.Plugin.SharedCommands;
37

48
namespace Flow.Launcher.Infrastructure.Storage
59
{
610
public class FlowLauncherJsonStorage<T> : JsonStorage<T> where T : new()
711
{
12+
private static readonly string ClassName = "FlowLauncherJsonStorage";
13+
14+
// We should not initialize API in static constructor because it will create another API instance
15+
private static IPublicAPI api = null;
16+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
17+
818
public FlowLauncherJsonStorage()
919
{
1020
var directoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName);
11-
Helper.ValidateDirectory(directoryPath);
21+
FilesFolders.ValidateDirectory(directoryPath);
1222

1323
var filename = typeof(T).Name;
1424
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
1525
}
26+
27+
public new void Save()
28+
{
29+
try
30+
{
31+
base.Save();
32+
}
33+
catch (System.Exception e)
34+
{
35+
API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
36+
}
37+
}
38+
39+
public new async Task SaveAsync()
40+
{
41+
try
42+
{
43+
await base.SaveAsync();
44+
}
45+
catch (System.Exception e)
46+
{
47+
API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
48+
}
49+
}
1650
}
17-
}
51+
}

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.Json;
66
using System.Threading.Tasks;
77
using Flow.Launcher.Infrastructure.Logger;
8+
using Flow.Launcher.Plugin.SharedCommands;
89

910
namespace Flow.Launcher.Infrastructure.Storage
1011
{
@@ -37,7 +38,7 @@ public JsonStorage(string filePath)
3738
FilePath = filePath;
3839
DirectoryPath = Path.GetDirectoryName(filePath) ?? throw new ArgumentException("Invalid file path");
3940

40-
Helper.ValidateDirectory(DirectoryPath);
41+
FilesFolders.ValidateDirectory(DirectoryPath);
4142
}
4243

4344
public async Task<T> LoadAsync()

0 commit comments

Comments
 (0)