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

98 lines
3.3 KiB
C#
Raw Permalink Normal View History

2020-12-06 10:10:22 +00:00
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Plugin.PluginsManager.ViewModels;
using Flow.Launcher.Plugin.PluginsManager.Views;
2020-12-06 08:58:27 +00:00
using System.Collections.Generic;
using System.Linq;
2020-12-06 10:10:22 +00:00
using System.Windows.Controls;
using Flow.Launcher.Infrastructure;
using System;
using System.Threading.Tasks;
2021-01-02 14:21:45 +00:00
using System.Threading;
using System.Windows;
2020-12-06 08:58:27 +00:00
namespace Flow.Launcher.Plugin.PluginsManager
{
public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, IAsyncReloadable
2020-12-06 08:58:27 +00:00
{
internal PluginInitContext Context { get; set; }
2020-12-06 10:10:22 +00:00
internal Settings Settings;
private SettingsViewModel viewModel;
private IContextMenu contextMenu;
internal PluginsManager pluginManager;
2020-12-29 10:33:53 +00:00
private DateTime lastUpdateTime = DateTime.MinValue;
2020-12-06 10:10:22 +00:00
public Control CreateSettingPanel()
{
return new PluginsManagerSettings(viewModel);
}
public Task InitAsync(PluginInitContext context)
2020-12-06 10:10:22 +00:00
{
Context = context;
Settings = context.API.LoadSettingJsonStorage<Settings>();
viewModel = new SettingsViewModel(context, Settings);
contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings);
2021-02-03 02:23:38 +00:00
_ = pluginManager.UpdateManifest().ContinueWith(_ =>
{
lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion);
return Task.CompletedTask;
2020-12-06 10:10:22 +00:00
}
public List<Result> LoadContextMenus(Result selectedResult)
{
return contextMenu.LoadContextMenus(selectedResult);
}
2021-01-02 14:21:45 +00:00
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
2020-12-06 10:10:22 +00:00
{
var search = query.Search.ToLower();
if (string.IsNullOrWhiteSpace(search))
return pluginManager.GetDefaultHotKeys();
2020-12-06 20:40:42 +00:00
if ((DateTime.Now - lastUpdateTime).TotalHours > 12) // 12 hours
{
_ = pluginManager.UpdateManifest().ContinueWith(t =>
{
2021-01-29 04:17:17 +00:00
lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
return search switch
{
2021-01-17 10:42:01 +00:00
var s when s.StartsWith(Settings.HotKeyInstall) => await pluginManager.RequestInstallOrUpdate(s, token),
var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s),
var s when s.StartsWith(Settings.HotkeyUpdate) => await pluginManager.RequestUpdate(s, token),
_ => pluginManager.GetDefaultHotKeys().Where(hotkey =>
{
hotkey.Score = StringMatcher.FuzzySearch(search, hotkey.Title).Score;
return hotkey.Score > 0;
}).ToList()
};
2020-12-06 10:10:22 +00:00
}
public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("plugin_pluginsmanager_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return Context.API.GetTranslation("plugin_pluginsmanager_plugin_description");
}
2021-01-02 14:21:45 +00:00
public async Task ReloadDataAsync()
{
2021-01-02 14:21:45 +00:00
await pluginManager.UpdateManifest();
2021-01-02 07:06:44 +00:00
lastUpdateTime = DateTime.Now;
}
2020-12-06 08:58:27 +00:00
}
}