Update PluginManifest if last update time is 12 hours ago

This commit is contained in:
弘韬 张 2020-12-28 23:06:47 +08:00
parent deaac1c7e4
commit 5a0358718c
3 changed files with 25 additions and 9 deletions

View file

@ -6,6 +6,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Windows.Controls; using System.Windows.Controls;
using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure;
using System;
using System.Threading.Tasks;
namespace Flow.Launcher.Plugin.PluginsManager namespace Flow.Launcher.Plugin.PluginsManager
{ {
@ -21,6 +23,8 @@ namespace Flow.Launcher.Plugin.PluginsManager
internal PluginsManager pluginManager; internal PluginsManager pluginManager;
internal DateTime _lastUpdateTime;
public Control CreateSettingPanel() public Control CreateSettingPanel()
{ {
return new PluginsManagerSettings(viewModel); return new PluginsManagerSettings(viewModel);
@ -33,6 +37,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
Settings = viewModel.Settings; Settings = viewModel.Settings;
contextMenu = new ContextMenu(Context, Settings); contextMenu = new ContextMenu(Context, Settings);
pluginManager = new PluginsManager(Context, Settings); pluginManager = new PluginsManager(Context, Settings);
_lastUpdateTime = DateTime.Now;
} }
public List<Result> LoadContextMenus(Result selectedResult) public List<Result> LoadContextMenus(Result selectedResult)
@ -43,10 +48,19 @@ namespace Flow.Launcher.Plugin.PluginsManager
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
var search = query.Search.ToLower(); var search = query.Search.ToLower();
if (string.IsNullOrWhiteSpace(search)) if (string.IsNullOrWhiteSpace(search))
return Settings.HotKeys; return Settings.HotKeys;
if ((DateTime.Now - _lastUpdateTime).TotalSeconds > 43200) // 12 hours
{
Task.Run(async () =>
{
await pluginManager.UpdateManifest();
_lastUpdateTime = DateTime.Now;
});
}
return search switch return search switch
{ {
var s when s.StartsWith(Settings.HotKeyInstall) => pluginManager.RequestInstallOrUpdate(s), var s when s.StartsWith(Settings.HotKeyInstall) => pluginManager.RequestInstallOrUpdate(s),

View file

@ -12,19 +12,15 @@ namespace Flow.Launcher.Plugin.PluginsManager.Models
internal List<UserPlugin> UserPlugins { get; private set; } internal List<UserPlugin> UserPlugins { get; private set; }
internal PluginsManifest() internal PluginsManifest()
{ {
DownloadManifest(); Task.Run(() => DownloadManifest()).Wait();
} }
private void DownloadManifest() internal async Task DownloadManifest()
{ {
var json = string.Empty; var json = string.Empty;
try try
{ {
var t = Task.Run( json = await Http.Get("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json");
async () =>
json = await Http.Get("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json"));
t.Wait();
UserPlugins = JsonConvert.DeserializeObject<List<UserPlugin>>(json); UserPlugins = JsonConvert.DeserializeObject<List<UserPlugin>>(json);
} }

View file

@ -7,13 +7,14 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace Flow.Launcher.Plugin.PluginsManager namespace Flow.Launcher.Plugin.PluginsManager
{ {
internal class PluginsManager internal class PluginsManager
{ {
private readonly PluginsManifest pluginsManifest; private PluginsManifest pluginsManifest;
private PluginInitContext Context { get; set; } private PluginInitContext Context { get; set; }
@ -44,6 +45,11 @@ namespace Flow.Launcher.Plugin.PluginsManager
Settings = settings; Settings = settings;
} }
internal async Task UpdateManifest()
{
await pluginsManifest.DownloadManifest();
}
internal void InstallOrUpdate(UserPlugin plugin) internal void InstallOrUpdate(UserPlugin plugin)
{ {
if (PluginExists(plugin.ID)) if (PluginExists(plugin.ID))