Skip to content

Commit 7319133

Browse files
committed
add backwards compatibility with Everything plugin
1 parent e6db8b8 commit 7319133

File tree

8 files changed

+130
-8
lines changed

8 files changed

+130
-8
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ public static void Save()
4848
{
4949
foreach (var plugin in AllPlugins)
5050
{
51-
var savable = plugin.Plugin as ISavable;
51+
var savable = plugin.Plugin as ISettingsSavable;
5252
savable?.Save();
5353
}
54+
55+
// Everything plugin
56+
var savableEverything = AllPlugins.FirstOrDefault(x => x.Metadata.ID == "D2D2C23B084D411DB66FE0C79D6C2A6E")?.Plugin as ISavable;
57+
savableEverything?.Save();
58+
5459
API.SavePluginSettings();
5560
}
5661

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Flow.Launcher.Infrastructure.Storage
2+
{
3+
/// <summary>
4+
/// Save plugin settings/cache,
5+
/// todo should be merged into a abstract class intead of seperate interface
6+
/// </summary>
7+
public interface ISavable
8+
{
9+
void Save();
10+
}
11+
}

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,94 @@ public void Save()
8787
File.WriteAllText(FilePath, serialized);
8888
}
8989
}
90+
91+
public class JsonStrorage<T> where T : new()
92+
{
93+
private readonly JsonSerializerOptions _serializerSettings;
94+
private T _data;
95+
// need a new directory name
96+
public const string DirectoryName = "Settings";
97+
public const string FileSuffix = ".json";
98+
public string FilePath { get; set; }
99+
public string DirectoryPath { get; set; }
100+
101+
102+
internal JsonStrorage()
103+
{
104+
// use property initialization instead of DefaultValueAttribute
105+
// easier and flexible for default value of object
106+
_serializerSettings = new JsonSerializerOptions
107+
{
108+
IgnoreNullValues = false
109+
};
110+
}
111+
112+
public T Load()
113+
{
114+
if (File.Exists(FilePath))
115+
{
116+
var searlized = File.ReadAllText(FilePath);
117+
if (!string.IsNullOrWhiteSpace(searlized))
118+
{
119+
Deserialize(searlized);
120+
}
121+
else
122+
{
123+
LoadDefault();
124+
}
125+
}
126+
else
127+
{
128+
LoadDefault();
129+
}
130+
return _data.NonNull();
131+
}
132+
133+
private void Deserialize(string searlized)
134+
{
135+
try
136+
{
137+
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
138+
}
139+
catch (JsonException e)
140+
{
141+
LoadDefault();
142+
Log.Exception($"|JsonStrorage.Deserialize|Deserialize error for json <{FilePath}>", e);
143+
}
144+
145+
if (_data == null)
146+
{
147+
LoadDefault();
148+
}
149+
}
150+
151+
private void LoadDefault()
152+
{
153+
if (File.Exists(FilePath))
154+
{
155+
BackupOriginFile();
156+
}
157+
158+
_data = new T();
159+
Save();
160+
}
161+
162+
private void BackupOriginFile()
163+
{
164+
var timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff", CultureInfo.CurrentUICulture);
165+
var directory = Path.GetDirectoryName(FilePath).NonNull();
166+
var originName = Path.GetFileNameWithoutExtension(FilePath);
167+
var backupName = $"{originName}-{timestamp}{FileSuffix}";
168+
var backupPath = Path.Combine(directory, backupName);
169+
File.Copy(FilePath, backupPath, true);
170+
// todo give user notification for the backup process
171+
}
172+
173+
public void Save()
174+
{
175+
string serialized = JsonSerializer.Serialize(_data, new JsonSerializerOptions() { WriteIndented = true });
176+
177+
File.WriteAllText(FilePath, serialized);
178+
}
179+
}
90180
}

Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace Flow.Launcher.Infrastructure.Storage
55
{
6-
public class PluginJsonStorage<T> :JsonStorage<T> where T : new()
6+
public class PluginJsonSettingStorage<T> :JsonStorage<T> where T : new()
77
{
8-
public PluginJsonStorage()
8+
public PluginJsonSettingStorage()
99
{
1010
// C# related, add python related below
1111
var dataType = typeof(T);
@@ -16,9 +16,25 @@ public PluginJsonStorage()
1616
FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
1717
}
1818

19-
public PluginJsonStorage(T data) : this()
19+
public PluginJsonSettingStorage(T data) : this()
2020
{
2121
_data = data;
2222
}
2323
}
24+
25+
public class PluginJsonStorage<T> : JsonStrorage<T> where T : new()
26+
{
27+
public PluginJsonStorage()
28+
{
29+
// C# releated, add python releated below
30+
var dataType = typeof(T);
31+
var assemblyName = typeof(T).Assembly.GetName().Name;
32+
DirectoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Plugins, assemblyName);
33+
Helper.ValidateDirectory(DirectoryPath);
34+
35+
FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
36+
}
37+
}
38+
2439
}
40+

Flow.Launcher.Plugin/Interfaces/ISavable.cs renamed to Flow.Launcher.Plugin/Interfaces/ISettingsSavable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/// Otherwise if LoadSettingJsonStorage or SaveSettingJsonStorage has been callded,
66
/// plugin settings will be automatically saved (see Flow.Launcher/PublicAPIInstance.SavePluginSettings) by Flow
77
/// </summary>
8-
public interface ISavable
8+
public interface ISettingsSavable
99
{
1010
void Save();
1111
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void SavePluginSettings()
177177
public void SaveJsonStorage<T>(T settings) where T : new()
178178
{
179179
var type = typeof(T);
180-
_pluginJsonStorages[type] = new PluginJsonStorage<T>(settings);
180+
_pluginJsonStorages[type] = new PluginJsonSettingStorage<T>(settings);
181181

182182
((PluginJsonStorage<T>) _pluginJsonStorages[type]).Save();
183183
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace Flow.Launcher.ViewModel
2525
{
26-
public class MainViewModel : BaseModel, ISavable
26+
public class MainViewModel : BaseModel, ISettingsSavable
2727
{
2828
#region Private Fields
2929

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Flow.Launcher.Plugin.Program
1515
{
16-
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, ISavable, IAsyncReloadable
16+
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, ISettingsSavable, IAsyncReloadable
1717
{
1818
internal static Win32[] _win32s { get; set; }
1919
internal static UWP.Application[] _uwps { get; set; }

0 commit comments

Comments
 (0)