Merge pull request #368 from Flow-Launcher/JsonRevise

revise access of UserSelectedrecord, Histroy, TopMost setter access to private
This commit is contained in:
Jeremy Wu 2021-02-25 20:34:33 +11:00 committed by GitHub
commit 2f161b6150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 44 deletions

View file

@ -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<HistoryItem> Items { get; set; } = new List<HistoryItem>();
[JsonInclude]
public List<HistoryItem> Items { get; private set; } = new List<HistoryItem>();
private int _maxHistory = 300;

View file

@ -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
{
/// <summary>
/// You should not directly access this field
/// <para>
/// It is public due to System.Text.Json limitation in version 3.1
/// </para>
/// </summary>
/// TODO: Set it to private
public Dictionary<string, Record> records { get; set; } = new Dictionary<string, Record>();
[JsonInclude]
public Dictionary<string, Record> records { get; private set; } = new Dictionary<string, Record>();
internal bool IsTopMost(Result result)
{

View file

@ -7,15 +7,8 @@ namespace Flow.Launcher.Storage
{
public class UserSelectedRecord
{
/// <summary>
/// You should not directly access this field
/// <para>
/// It is public due to System.Text.Json limitation in version 3.1
/// </para>
/// </summary>
/// TODO: Set it to private
[JsonPropertyName("records")]
public Dictionary<string, int> records { get; set; }
[JsonInclude]
public Dictionary<string, int> records { get; private set; }
public UserSelectedRecord()
{

View file

@ -17,7 +17,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
{
JsonElement json;
try
{
@ -25,7 +24,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
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<string>();
return root.GetProperty("Results")
.EnumerateArray()
.SelectMany(r => r.GetProperty("Suggests")
.EnumerateArray()
.Select(s => s.GetProperty("Txt").GetString()))
.ToList();
}
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
@ -41,18 +53,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
Log.Exception("|Bing.Suggestions|can't parse suggestions", e);
return new List<string>();
}
if (json.GetProperty("FullResults").GetInt32() == 0)
return new List<string>();
return json.GetProperty("Results")
.EnumerateArray()
.SelectMany(r => r.GetProperty("Suggests")
.EnumerateArray()
.Select(s => s.GetProperty("Txt").GetString()))
.ToList();
}
}
public override string ToString()

View file

@ -16,15 +16,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public override async Task<List<string>> 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, cancellationToken: token);
using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
if (json == null)
return new List<string>();
var results = json.RootElement.EnumerateArray().ElementAt(1);
return results.EnumerateArray().Select(o => o.GetString()).ToList();
}
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
@ -41,11 +46,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
return new List<string>();
}
var results = json?.RootElement.EnumerateArray().ElementAt(1);
return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List<string>();
}
public override string ToString()

View file

@ -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",