Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Flow.Launcher.Infrastructure/Storage/JsonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Flow.Launcher.Infrastructure.Storage
/// <summary>
/// Serialize object using json format.
/// </summary>
public class JsonStrorage<T>
public class JsonStrorage<T> where T : new()
{
private readonly JsonSerializerOptions _serializerSettings;
private T _data;
Expand Down Expand Up @@ -76,7 +76,7 @@ private void LoadDefault()
BackupOriginFile();
}

_data = JsonSerializer.Deserialize<T>("{}", _serializerSettings);
_data = new T();
Save();
}

Expand Down
26 changes: 19 additions & 7 deletions Flow.Launcher/Storage/TopMostRecord.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Flow.Launcher.Plugin;

namespace Flow.Launcher.Storage
{
// todo this class is not thread safe.... but used from multiple threads.
public class TopMostRecord
{
private Dictionary<string, Record> records = new Dictionary<string, Record>();
/// <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>();

internal bool IsTopMost(Result result)
{
if (records.Count == 0)
if (records.Count == 0 || !records.ContainsKey(result.OriginQuery.RawQuery))
{
return false;
}

// since this dictionary should be very small (or empty) going over it should be pretty fast.
return records.Any(o => o.Value.Title == result.Title
&& o.Value.SubTitle == result.SubTitle
&& o.Value.PluginID == result.PluginID
&& o.Key == result.OriginQuery.RawQuery);
// since this dictionary should be very small (or empty) going over it should be pretty fast.
return records[result.OriginQuery.RawQuery].Equals(result);
}

internal void Remove(Result result)
Expand Down Expand Up @@ -53,5 +58,12 @@ public class Record
public string Title { get; set; }
public string SubTitle { get; set; }
public string PluginID { get; set; }

public bool Equals(Result r)
{
return Title == r.Title
&& SubTitle == r.SubTitle
&& PluginID == r.PluginID;
}
}
}
20 changes: 17 additions & 3 deletions Flow.Launcher/Storage/UserSelectedRecord.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Plugin;

namespace Flow.Launcher.Storage
{
public class UserSelectedRecord
{
private Dictionary<string, int> records = new Dictionary<string, int>();
/// <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; }

public UserSelectedRecord()
{
records = new Dictionary<string, int>();
}

public void Add(Result result)
{
var key = result.ToString();
if (records.TryGetValue(key, out int value))
if (records.ContainsKey(key))
{
records[key] = value + 1;
records[key]++;
}
else
{
Expand Down