Skip to content

Commit ce6ebd6

Browse files
committed
feat: add updater
1 parent c47f653 commit ce6ebd6

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Main.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
using Microsoft.PowerToys.Settings.UI.Library;
88
using System.Windows.Controls;
99
using System.Linq;
10+
using Community.PowerToys.Run.Plugin.Update;
11+
using Wox.Infrastructure.Storage;
12+
using Wox.Plugin.Logger;
1013

1114
namespace Community.PowerToys.Run.Plugin.Its_MyPic
1215
{
13-
public class Main : IPlugin, IDelayedExecutionPlugin, IContextMenu, ISettingProvider, IPluginI18n
16+
public class Main : IPlugin, IDelayedExecutionPlugin, IContextMenu, ISettingProvider, IPluginI18n, ISavable
1417
{
1518
public static string PluginID => "048FCB4CE3034FD9ACD9486F9FAB1F9E";
1619
public string Name => "Its-MyPic";
@@ -30,6 +33,13 @@ public class Main : IPlugin, IDelayedExecutionPlugin, IContextMenu, ISettingProv
3033

3134
private readonly Data DB = new();
3235

36+
private PluginJsonStorage<SampleSettings> Storage { get; set; }
37+
public void Save() => Storage.Save();
38+
39+
private SampleSettings Settings { get; set; }
40+
41+
private PluginUpdateHandler Updater { get; set; }
42+
private PluginInitContext Context { get; set; }
3343

3444
public List<Result> Query(Query query)
3545
{
@@ -38,12 +48,34 @@ public List<Result> Query(Query query)
3848
public List<Result> Query(Query query, bool delayedExecution)
3949
{
4050
var search = query.Search.ToLower();
41-
var results = DB.GetMatchedSubtitleDatas(search, delayedExecution);
51+
List<Result> results;
52+
if (Updater.IsUpdateAvailable())
53+
{
54+
Log.Info("Update available", GetType());
55+
results = Updater.GetResults();
56+
}
57+
else
58+
{
59+
results = DB.GetMatchedSubtitleDatas(search, delayedExecution);
60+
}
4261
return results;
4362
}
4463
public void Init(PluginInitContext context)
4564
{
4665
CopyImage = false;
66+
Context = context;
67+
68+
Storage = new PluginJsonStorage<SampleSettings>();
69+
Settings = Storage.Load();
70+
71+
Log.Info(context.CurrentPluginMetadata.Version, GetType());
72+
73+
Updater = new PluginUpdateHandler(Settings.Update);
74+
Updater.UpdateInstalling += OnUpdateInstalling;
75+
Updater.UpdateInstalled += OnUpdateInstalled;
76+
Updater.UpdateSkipped += OnUpdateSkipped;
77+
78+
Updater.Init(Context);
4779
}
4880

4981
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
@@ -86,5 +118,24 @@ public string GetTranslatedPluginDescription()
86118
{
87119
throw new System.NotImplementedException();
88120
}
121+
122+
123+
private void OnUpdateInstalling(object sender, PluginUpdateEventArgs e)
124+
{
125+
Log.Info("UpdateInstalling: " + e.Version, GetType());
126+
}
127+
128+
private void OnUpdateInstalled(object sender, PluginUpdateEventArgs e)
129+
{
130+
Log.Info("UpdateInstalled: " + e.Version, GetType());
131+
Context!.API.ShowNotification($"{Name} {e.Version}", "Update installed");
132+
}
133+
134+
private void OnUpdateSkipped(object sender, PluginUpdateEventArgs e)
135+
{
136+
Log.Info("UpdateSkipped: " + e.Version, GetType());
137+
Save();
138+
Context?.API.ChangeQuery(Context.CurrentPluginMetadata.ActionKeyword, true);
139+
}
89140
}
90141
}

Updater.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Community.PowerToys.Run.Plugin.Update;
2+
using Microsoft.PowerToys.Settings.UI.Library;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Community.PowerToys.Run.Plugin.Its_MyPic
10+
{
11+
public class SampleSettings
12+
{
13+
public PluginUpdateSettings Update { get; set; } = new PluginUpdateSettings { ResultScore = 100 };
14+
15+
internal IEnumerable<PluginAdditionalOption> GetAdditionalOptions() => Update.GetAdditionalOptions();
16+
17+
internal void SetAdditionalOptions(IEnumerable<PluginAdditionalOption> additionalOptions) => Update.SetAdditionalOptions(additionalOptions);
18+
}
19+
}

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"ExecuteFileName": "Community.PowerToys.Run.Plugin.Its-MyPic.dll",
1111
"IcoPathDark": "Images\\its_mypic.dark.png",
1212
"IcoPathLight": "Images\\its_mypic.light.png",
13-
"DynamicLoading": false
13+
"DynamicLoading": true
1414
}

0 commit comments

Comments
 (0)