From f3a1bcc01f154f5f2070b127bfd79dc953541975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Wed, 24 Feb 2021 16:03:10 +0800 Subject: [PATCH 1/4] revise access of UserSelectedrecord, Histroy, TopMost setter access to private --- Flow.Launcher/Storage/QueryHistory.cs | 5 +++-- Flow.Launcher/Storage/TopMostRecord.cs | 12 ++---------- Flow.Launcher/Storage/UserSelectedRecord.cs | 11 ++--------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/Flow.Launcher/Storage/QueryHistory.cs b/Flow.Launcher/Storage/QueryHistory.cs index 2b21036059d..a5383b179ab 100644 --- a/Flow.Launcher/Storage/QueryHistory.cs +++ b/Flow.Launcher/Storage/QueryHistory.cs @@ -1,13 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; -using Flow.Launcher.Plugin; +using System.Text.Json.Serialization; namespace Flow.Launcher.Storage { public class History { - public List Items { get; set; } = new List(); + [JsonInclude] + public List Items { get; private set; } = new List(); private int _maxHistory = 300; diff --git a/Flow.Launcher/Storage/TopMostRecord.cs b/Flow.Launcher/Storage/TopMostRecord.cs index c92ef49562a..052c296cdde 100644 --- a/Flow.Launcher/Storage/TopMostRecord.cs +++ b/Flow.Launcher/Storage/TopMostRecord.cs @@ -1,6 +1,4 @@ using System.Collections.Generic; -using System.Linq; -using System.Text.Json; using System.Text.Json.Serialization; using Flow.Launcher.Plugin; @@ -9,14 +7,8 @@ namespace Flow.Launcher.Storage // todo this class is not thread safe.... but used from multiple threads. public class TopMostRecord { - /// - /// You should not directly access this field - /// - /// It is public due to System.Text.Json limitation in version 3.1 - /// - /// - /// TODO: Set it to private - public Dictionary records { get; set; } = new Dictionary(); + [JsonInclude] + public Dictionary records { get; private set; } = new Dictionary(); internal bool IsTopMost(Result result) { diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index bc7a2da73ce..fd3d87fc4c6 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -7,15 +7,8 @@ namespace Flow.Launcher.Storage { public class UserSelectedRecord { - /// - /// You should not directly access this field - /// - /// It is public due to System.Text.Json limitation in version 3.1 - /// - /// - /// TODO: Set it to private - [JsonPropertyName("records")] - public Dictionary records { get; set; } + [JsonInclude] + public Dictionary records { get; private set; } public UserSelectedRecord() { From 2a03bba6668026dc4d6e751925504b788cf2b2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Wed, 24 Feb 2021 16:20:00 +0800 Subject: [PATCH 2/4] add using for JsonDocument in Google and Bing --- .../SuggestionSources/Bing.cs | 29 ++++++++++--------- .../SuggestionSources/Google.cs | 18 ++++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index e505d78cf5d..1a1f10d0c83 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -17,7 +17,6 @@ class Bing : SuggestionSource { public override async Task> Suggestions(string query, CancellationToken token) { - JsonElement json; try { @@ -25,7 +24,20 @@ public override async Task> Suggestions(string query, CancellationT using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false); - json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)).RootElement.GetProperty("AS"); + using var json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)); + var root = json.RootElement.GetProperty("AS"); + + if (root.GetProperty("FullResults").GetInt32() == 0) + return new List(); + + return root.GetProperty("Results") + .EnumerateArray() + .SelectMany(r => r.GetProperty("Suggests") + .EnumerateArray() + .Select(s => s.GetProperty("Txt").GetString())) + .ToList(); + + } catch (TaskCanceledException) @@ -41,18 +53,7 @@ public override async Task> Suggestions(string query, CancellationT { Log.Exception("|Bing.Suggestions|can't parse suggestions", e); return new List(); - } - - if (json.GetProperty("FullResults").GetInt32() == 0) - return new List(); - - return json.GetProperty("Results") - .EnumerateArray() - .SelectMany(r => r.GetProperty("Suggests") - .EnumerateArray() - .Select(s => s.GetProperty("Txt").GetString())) - .ToList(); - + } } public override string ToString() diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index c075f4d5d49..4ca9b2edef1 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -16,15 +16,20 @@ public class Google : SuggestionSource { public override async Task> Suggestions(string query, CancellationToken token) { - JsonDocument json; - try { const string api = "https://www.google.com/complete/search?output=chrome&q="; using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false); - - json = await JsonDocument.ParseAsync(resultStream); + + using var json = await JsonDocument.ParseAsync(resultStream); + + if (json == null) + return new List(); + + var results = json.RootElement.EnumerateArray().ElementAt(1); + + return results.EnumerateArray().Select(o => o.GetString()).ToList(); } catch (TaskCanceledException) @@ -41,11 +46,6 @@ public override async Task> Suggestions(string query, CancellationT Log.Exception("|Google.Suggestions|can't parse suggestions", e); return new List(); } - - var results = json?.RootElement.EnumerateArray().ElementAt(1); - - return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List(); - } public override string ToString() From f9fa3ddd4686b5acd11455e2e47b4e5168137e41 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 25 Feb 2021 14:12:25 +1100 Subject: [PATCH 3/4] version bump WebSearch plugin --- Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json index 6e998674964..996c9b686e2 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json @@ -25,7 +25,7 @@ "Name": "Web Searches", "Description": "Provide the web search ability", "Author": "qianlifeng", - "Version": "1.3.2", + "Version": "1.3.3", "Language": "csharp", "Website": "https://github.com/Flow-Launcher/Flow.Launcher", "ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll", From ee75c6336ef129a23cd605c24be774404e1d9a7f Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 25 Feb 2021 14:18:11 +1100 Subject: [PATCH 4/4] version bump WebSearch plugin after merge --- Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json index 996c9b686e2..e9f237a586e 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json @@ -25,7 +25,7 @@ "Name": "Web Searches", "Description": "Provide the web search ability", "Author": "qianlifeng", - "Version": "1.3.3", + "Version": "1.3.4", "Language": "csharp", "Website": "https://github.com/Flow-Launcher/Flow.Launcher", "ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",