add score bumping with query and do hashcode customized

This commit is contained in:
弘韬 张 2021-04-01 18:02:27 +08:00
parent b5e54a76b0
commit 362504e223

View file

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Plugin;
@ -8,16 +10,42 @@ namespace Flow.Launcher.Storage
public class UserSelectedRecord
{
[JsonInclude]
public Dictionary<string, int> records { get; private set; }
public Dictionary<int, int> records { get; private set; }
public UserSelectedRecord()
{
records = new Dictionary<string, int>();
records = new Dictionary<int, int>();
}
private static int GenerateCustomHashCode(Query query, Result result)
{
int hashcode = 23;
unchecked
{
// skip the empty space
foreach (var item in query.ActionKeyword.Concat(query.Search))
{
hashcode = hashcode * 31 + item;
}
foreach (var item in result.Title)
{
hashcode = hashcode * 31 + item;
}
foreach (var item in result.SubTitle)
{
hashcode = hashcode * 31 + item;
}
return hashcode;
}
}
public void Add(Result result)
{
var key = result.ToString();
var key = GenerateCustomHashCode(result.OriginQuery, result);
if (records.ContainsKey(key))
{
records[key]++;
@ -31,7 +59,7 @@ namespace Flow.Launcher.Storage
public int GetSelectedCount(Result result)
{
if (records.TryGetValue(result.ToString(), out int value))
if (records.TryGetValue(GenerateCustomHashCode(result.OriginQuery, result), out int value))
{
return value;
}