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

106 lines
3.8 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);
_manifestUpdateTask = pluginManager
.UpdateManifestAsync(true)
.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);
}
private Task _manifestUpdateTask = Task.CompletedTask;
2020-12-06 10:10:22 +00:00
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;
if (string.IsNullOrWhiteSpace(search))
return pluginManager.GetDefaultHotKeys();
2020-12-06 20:40:42 +00:00
if ((DateTime.Now - lastUpdateTime).TotalHours > 12 && _manifestUpdateTask.IsCompleted) // 12 hours
{
_manifestUpdateTask = pluginManager.UpdateManifestAsync().ContinueWith(t =>
{
2021-01-29 04:17:17 +00:00
lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
return search switch
{
//search could be url, no need ToLower() when passed in
var s when s.StartsWith(Settings.HotKeyInstall, StringComparison.OrdinalIgnoreCase)
=> await pluginManager.RequestInstallOrUpdate(search, token),
var s when s.StartsWith(Settings.HotkeyUninstall, StringComparison.OrdinalIgnoreCase)
=> pluginManager.RequestUninstall(search),
var s when s.StartsWith(Settings.HotkeyUpdate, StringComparison.OrdinalIgnoreCase)
=> await pluginManager.RequestUpdate(search, 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()
{
await pluginManager.UpdateManifestAsync();
2021-01-02 07:06:44 +00:00
lastUpdateTime = DateTime.Now;
}
2020-12-06 08:58:27 +00:00
}
}