PowerToys/Wox/Storage/TopMostRecord.cs

59 lines
1.6 KiB
C#
Raw Normal View History

2015-10-31 07:17:34 +08:00
using System.Collections.Generic;
2015-02-05 23:29:41 +08:00
using System.Linq;
2019-12-04 05:02:24 +08:00
using Newtonsoft.Json;
2016-01-07 05:34:42 +08:00
using Wox.Plugin;
2015-02-05 23:29:41 +08:00
namespace Wox.Storage
{
2019-12-04 05:02:24 +08:00
// todo this class is not thread safe.... but used from multiple threads.
public class TopMostRecord
2015-02-05 23:29:41 +08:00
{
2019-12-04 05:02:24 +08:00
[JsonProperty]
private Dictionary<string, Record> records = new Dictionary<string, Record>();
2015-02-05 23:29:41 +08:00
2016-01-07 05:34:42 +08:00
internal bool IsTopMost(Result result)
2015-02-05 23:29:41 +08:00
{
2019-12-04 05:02:24 +08:00
if (records.Count == 0)
{
return false;
}
// since this dictionary should be very small (or empty) going over it should be pretty fast.
2015-02-05 23:29:41 +08:00
return records.Any(o => o.Value.Title == result.Title
2019-12-04 05:02:24 +08:00
&& o.Value.SubTitle == result.SubTitle
&& o.Value.PluginID == result.PluginID
&& o.Key == result.OriginQuery.RawQuery);
2015-02-05 23:29:41 +08:00
}
2016-01-07 05:34:42 +08:00
internal void Remove(Result result)
2015-02-05 23:29:41 +08:00
{
2019-12-04 05:02:24 +08:00
records.Remove(result.OriginQuery.RawQuery);
2015-02-05 23:29:41 +08:00
}
2016-01-07 05:34:42 +08:00
internal void AddOrUpdate(Result result)
2015-02-05 23:29:41 +08:00
{
2019-12-04 05:02:24 +08:00
var record = new Record
2015-02-05 23:29:41 +08:00
{
2019-12-04 05:02:24 +08:00
PluginID = result.PluginID,
Title = result.Title,
SubTitle = result.SubTitle
};
records[result.OriginQuery.RawQuery] = record;
}
public void Load(Dictionary<string, Record> dictionary)
{
records = dictionary;
2015-02-05 23:29:41 +08:00
}
}
public class Record
2015-02-05 23:29:41 +08:00
{
public string Title { get; set; }
public string SubTitle { get; set; }
public string PluginID { get; set; }
}
}