Skip to content

Commit 2f161b6

Browse files
authored
Merge pull request #368 from Flow-Launcher/JsonRevise
revise access of UserSelectedrecord, Histroy, TopMost setter access to private
2 parents 890f114 + 7660405 commit 2f161b6

File tree

6 files changed

+31
-44
lines changed

6 files changed

+31
-44
lines changed

Flow.Launcher/Storage/QueryHistory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Flow.Launcher.Plugin;
4+
using System.Text.Json.Serialization;
55

66
namespace Flow.Launcher.Storage
77
{
88
public class History
99
{
10-
public List<HistoryItem> Items { get; set; } = new List<HistoryItem>();
10+
[JsonInclude]
11+
public List<HistoryItem> Items { get; private set; } = new List<HistoryItem>();
1112

1213
private int _maxHistory = 300;
1314

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Text.Json;
42
using System.Text.Json.Serialization;
53
using Flow.Launcher.Plugin;
64

@@ -9,14 +7,8 @@ namespace Flow.Launcher.Storage
97
// todo this class is not thread safe.... but used from multiple threads.
108
public class TopMostRecord
119
{
12-
/// <summary>
13-
/// You should not directly access this field
14-
/// <para>
15-
/// It is public due to System.Text.Json limitation in version 3.1
16-
/// </para>
17-
/// </summary>
18-
/// TODO: Set it to private
19-
public Dictionary<string, Record> records { get; set; } = new Dictionary<string, Record>();
10+
[JsonInclude]
11+
public Dictionary<string, Record> records { get; private set; } = new Dictionary<string, Record>();
2012

2113
internal bool IsTopMost(Result result)
2214
{

Flow.Launcher/Storage/UserSelectedRecord.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@ namespace Flow.Launcher.Storage
77
{
88
public class UserSelectedRecord
99
{
10-
/// <summary>
11-
/// You should not directly access this field
12-
/// <para>
13-
/// It is public due to System.Text.Json limitation in version 3.1
14-
/// </para>
15-
/// </summary>
16-
/// TODO: Set it to private
17-
[JsonPropertyName("records")]
18-
public Dictionary<string, int> records { get; set; }
10+
[JsonInclude]
11+
public Dictionary<string, int> records { get; private set; }
1912

2013
public UserSelectedRecord()
2114
{

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ class Bing : SuggestionSource
1717
{
1818
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
1919
{
20-
JsonElement json;
2120

2221
try
2322
{
2423
const string api = "https://api.bing.com/qsonhs.aspx?q=";
2524

2625
using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false);
2726

28-
json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)).RootElement.GetProperty("AS");
27+
using var json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token));
28+
var root = json.RootElement.GetProperty("AS");
29+
30+
if (root.GetProperty("FullResults").GetInt32() == 0)
31+
return new List<string>();
32+
33+
return root.GetProperty("Results")
34+
.EnumerateArray()
35+
.SelectMany(r => r.GetProperty("Suggests")
36+
.EnumerateArray()
37+
.Select(s => s.GetProperty("Txt").GetString()))
38+
.ToList();
39+
40+
2941

3042
}
3143
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
@@ -41,18 +53,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
4153
{
4254
Log.Exception("|Bing.Suggestions|can't parse suggestions", e);
4355
return new List<string>();
44-
}
45-
46-
if (json.GetProperty("FullResults").GetInt32() == 0)
47-
return new List<string>();
48-
49-
return json.GetProperty("Results")
50-
.EnumerateArray()
51-
.SelectMany(r => r.GetProperty("Suggests")
52-
.EnumerateArray()
53-
.Select(s => s.GetProperty("Txt").GetString()))
54-
.ToList();
55-
56+
}
5657
}
5758

5859
public override string ToString()

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ public class Google : SuggestionSource
1616
{
1717
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
1818
{
19-
JsonDocument json;
20-
2119
try
2220
{
2321
const string api = "https://www.google.com/complete/search?output=chrome&q=";
2422

2523
using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false);
2624

27-
json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
25+
using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
26+
27+
if (json == null)
28+
return new List<string>();
29+
30+
var results = json.RootElement.EnumerateArray().ElementAt(1);
31+
32+
return results.EnumerateArray().Select(o => o.GetString()).ToList();
2833

2934
}
3035
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
@@ -41,11 +46,6 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
4146
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
4247
return new List<string>();
4348
}
44-
45-
var results = json?.RootElement.EnumerateArray().ElementAt(1);
46-
47-
return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List<string>();
48-
4949
}
5050

5151
public override string ToString()

Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Name": "Web Searches",
2626
"Description": "Provide the web search ability",
2727
"Author": "qianlifeng",
28-
"Version": "1.3.3",
28+
"Version": "1.3.4",
2929
"Language": "csharp",
3030
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
3131
"ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",

0 commit comments

Comments
 (0)