From 3acaf253768dba4ef4ced2e372201c194eda590d Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Mon, 12 Dec 2022 17:25:03 +0700 Subject: [PATCH] fix plugins ui crashes --- Torch.API/WebAPI/Plugin/LegacyPluginQuery.cs | 6 +- Torch.Server/Views/PluginBrowser.xaml.cs | 58 ++++++++++---------- Torch.Server/Views/PluginsControl.xaml.cs | 2 +- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Torch.API/WebAPI/Plugin/LegacyPluginQuery.cs b/Torch.API/WebAPI/Plugin/LegacyPluginQuery.cs index fd2a544..d569cd4 100644 --- a/Torch.API/WebAPI/Plugin/LegacyPluginQuery.cs +++ b/Torch.API/WebAPI/Plugin/LegacyPluginQuery.cs @@ -11,7 +11,7 @@ namespace Torch.API.WebAPI.Plugin; public class LegacyPluginQuery : IPluginQuery { - private const string BASE_URL = "https://torchapi.com/api/plugins/"; + private const string BASE_URL = "https://torchapi.com/"; private readonly HttpClient _client; private static readonly Logger Log = LogManager.GetCurrentClassLogger(); @@ -27,12 +27,12 @@ public class LegacyPluginQuery : IPluginQuery public async Task QueryAll() { - return await _client.GetFromJsonAsync("/", CancellationToken.None); + return await _client.GetFromJsonAsync("/api/plugins/", CancellationToken.None); } public async Task QueryOne(Guid guid) { - using var res = await _client.GetAsync($"/search/{guid}"); + using var res = await _client.GetAsync($"/api/plugins/search/{guid}"); if (!res.IsSuccessStatusCode) return null; return await res.Content.ReadFromJsonAsync(); diff --git a/Torch.Server/Views/PluginBrowser.xaml.cs b/Torch.Server/Views/PluginBrowser.xaml.cs index 2e06bc4..894e970 100644 --- a/Torch.Server/Views/PluginBrowser.xaml.cs +++ b/Torch.Server/Views/PluginBrowser.xaml.cs @@ -22,6 +22,7 @@ using Torch.Collections; using Torch.Server.Annotations; using Torch.Managers; using Torch.API.Managers; +using Torch.API.Plugins; using Torch.API.WebAPI.Plugin; namespace Torch.Server.Views @@ -56,39 +57,38 @@ namespace Torch.Server.Views InitializeComponent(); var installedPlugins = pluginManager.Plugins; - BindingOperations.EnableCollectionSynchronization(Plugins, _syncLock); - Task.Run(async () => - { - try - { - var res = await LegacyPluginQuery.Instance.QueryAll(); - foreach (var item in res.Plugins.OrderBy(i => i.Name)) { - lock (_syncLock) - { - var pluginItem = item with - { - Description = item.Description.Replace("<", "<").Replace(">", ">"), - Installed = installedPlugins.Keys.Contains(item.Id) - }; - Plugins.Add(pluginItem); - PluginsSource.Add(pluginItem); - } - } - - Dispatcher.Invoke(() => PluginsList.SelectedIndex = 0); - CurrentDescription = "Please select a plugin..."; - } - catch (Exception e) - { - MessageBox.Show(e.ToString(), "An Error Occurred", MessageBoxButton.OK, MessageBoxImage.Error); - Close(); - throw; - } - }); + LoadAsync(installedPlugins); MarkdownFlow.CommandBindings.Add(new CommandBinding(NavigationCommands.GoToPage, (sender, e) => OpenUri((string)e.Parameter))); } + private async void LoadAsync(IReadOnlyDictionary installedPlugins) + { + try + { + var res = await LegacyPluginQuery.Instance.QueryAll(); + foreach (var item in res.Plugins.OrderBy(i => i.Name)) + { + var pluginItem = item with + { + Description = item.Description?.Replace("<", "<").Replace(">", ">") ?? string.Empty, + Installed = installedPlugins.Keys.Contains(item.Id) + }; + Plugins.Add(pluginItem); + PluginsSource.Add(pluginItem); + } + + PluginsList.SelectedIndex = 0; + CurrentDescription = "Please select a plugin..."; + } + catch (Exception e) + { + MessageBox.Show(e.ToString(), "An Error Occurred", MessageBoxButton.OK, MessageBoxImage.Error); + Close(); + throw; + } + } + public static bool IsValidUri(string uri) { if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute)) diff --git a/Torch.Server/Views/PluginsControl.xaml.cs b/Torch.Server/Views/PluginsControl.xaml.cs index 356febe..d4f291a 100644 --- a/Torch.Server/Views/PluginsControl.xaml.cs +++ b/Torch.Server/Views/PluginsControl.xaml.cs @@ -71,7 +71,7 @@ namespace Torch.Server.Views private void OpenFolder_OnClick(object sender, RoutedEventArgs e) { if (_plugins?.PluginDir != null) - Process.Start(_plugins.PluginDir); + Process.Start("explorer", _plugins.PluginDir); } private void BrowsPlugins_OnClick(object sender, RoutedEventArgs e)