mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #416 from taooceros/BringOldSelectedRecordBack
Bring Legacy records together
This commit is contained in:
commit
baf961ef3a
3 changed files with 65 additions and 64 deletions
|
|
@ -11,7 +11,6 @@ namespace Flow.Launcher.Infrastructure.Storage
|
|||
/// </summary>
|
||||
public class JsonStrorage<T> where T : new()
|
||||
{
|
||||
private readonly JsonSerializerOptions _serializerSettings;
|
||||
protected T _data;
|
||||
// need a new directory name
|
||||
public const string DirectoryName = "Settings";
|
||||
|
|
@ -20,16 +19,6 @@ namespace Flow.Launcher.Infrastructure.Storage
|
|||
public string DirectoryPath { get; set; }
|
||||
|
||||
|
||||
internal JsonStrorage()
|
||||
{
|
||||
// use property initialization instead of DefaultValueAttribute
|
||||
// easier and flexible for default value of object
|
||||
_serializerSettings = new JsonSerializerOptions
|
||||
{
|
||||
IgnoreNullValues = false
|
||||
};
|
||||
}
|
||||
|
||||
public T Load()
|
||||
{
|
||||
if (File.Exists(FilePath))
|
||||
|
|
@ -55,7 +44,7 @@ namespace Flow.Launcher.Infrastructure.Storage
|
|||
{
|
||||
try
|
||||
{
|
||||
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
|
||||
_data = JsonSerializer.Deserialize<T>(searlized);
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using System.Text.Json.Serialization;
|
|||
using Flow.Launcher.Infrastructure;
|
||||
using Flow.Launcher.Infrastructure.Storage;
|
||||
using Flow.Launcher.Plugin;
|
||||
using Flow.Launcher.ViewModel;
|
||||
|
||||
namespace Flow.Launcher.Storage
|
||||
{
|
||||
|
|
@ -14,72 +15,90 @@ namespace Flow.Launcher.Storage
|
|||
private const int HASH_INITIAL = 23;
|
||||
|
||||
[JsonInclude]
|
||||
public Dictionary<int, int> records { get; private set; }
|
||||
public Dictionary<int, int> recordsWithQuery { get; private set; }
|
||||
|
||||
[JsonInclude, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public Dictionary<string, int> records { get; private set; }
|
||||
|
||||
|
||||
public UserSelectedRecord()
|
||||
{
|
||||
records = new Dictionary<int, int>();
|
||||
recordsWithQuery = new Dictionary<int, int>();
|
||||
}
|
||||
|
||||
private static int GenerateCustomHashCode(Query query, Result result)
|
||||
private static int GenerateStaticHashCode(string s, int start = HASH_INITIAL)
|
||||
{
|
||||
int hashcode = HASH_INITIAL;
|
||||
|
||||
unchecked
|
||||
{
|
||||
// skip the empty space
|
||||
// https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast
|
||||
// optimization without information lost when int overflow
|
||||
|
||||
for (int i = 0; i < query.ActionKeyword.Length; i++)
|
||||
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
char item = query.ActionKeyword[i];
|
||||
hashcode = hashcode * HASH_MULTIPLIER + item;
|
||||
}
|
||||
|
||||
for (int i = 0; i < query.Search.Length; i++)
|
||||
{
|
||||
char item = query.Search[i];
|
||||
hashcode = hashcode * HASH_MULTIPLIER + item;
|
||||
start = start * HASH_MULTIPLIER + s[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < result.Title.Length; i++)
|
||||
{
|
||||
char item = result.Title[i];
|
||||
hashcode = hashcode * HASH_MULTIPLIER + item;
|
||||
}
|
||||
return start;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < result.SubTitle.Length; i++)
|
||||
{
|
||||
char item = result.SubTitle[i];
|
||||
hashcode = hashcode * HASH_MULTIPLIER + item;
|
||||
}
|
||||
return hashcode;
|
||||
private static int GenerateResultHashCode(Result result)
|
||||
{
|
||||
int hashcode = GenerateStaticHashCode(result.Title);
|
||||
return GenerateStaticHashCode(result.SubTitle, hashcode);
|
||||
}
|
||||
|
||||
private static int GenerateQueryAndResultHashCode(Query query, Result result)
|
||||
{
|
||||
int hashcode = GenerateStaticHashCode(query.ActionKeyword);
|
||||
hashcode = GenerateStaticHashCode(query.Search, hashcode);
|
||||
hashcode = GenerateStaticHashCode(result.Title, hashcode);
|
||||
hashcode = GenerateStaticHashCode(result.SubTitle, hashcode);
|
||||
|
||||
return hashcode;
|
||||
}
|
||||
|
||||
private void TransformOldRecords()
|
||||
{
|
||||
if (records != null)
|
||||
{
|
||||
var localRecords = records;
|
||||
records = null;
|
||||
|
||||
foreach (var pair in localRecords)
|
||||
{
|
||||
recordsWithQuery.TryAdd(GenerateStaticHashCode(pair.Key), pair.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(Result result)
|
||||
{
|
||||
var key = GenerateCustomHashCode(result.OriginQuery, result);
|
||||
if (records.ContainsKey(key))
|
||||
{
|
||||
records[key]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
records.Add(key, 1);
|
||||
TransformOldRecords();
|
||||
|
||||
}
|
||||
var keyWithQuery = GenerateQueryAndResultHashCode(result.OriginQuery, result);
|
||||
|
||||
if (!recordsWithQuery.TryAdd(keyWithQuery, 1))
|
||||
recordsWithQuery[keyWithQuery]++;
|
||||
|
||||
var keyWithoutQuery = GenerateResultHashCode(result);
|
||||
|
||||
if (!recordsWithQuery.TryAdd(keyWithoutQuery, 1))
|
||||
recordsWithQuery[keyWithoutQuery]++;
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
{
|
||||
if (records.TryGetValue(GenerateCustomHashCode(result.OriginQuery, result), out int value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return 0;
|
||||
var selectedCount = 0;
|
||||
|
||||
recordsWithQuery.TryGetValue(GenerateQueryAndResultHashCode(result.OriginQuery, result), out int value);
|
||||
selectedCount += value * 5;
|
||||
|
||||
recordsWithQuery.TryGetValue(GenerateResultHashCode(result), out value);
|
||||
selectedCount += value;
|
||||
|
||||
return selectedCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ namespace Flow.Launcher.ViewModel
|
|||
|
||||
private CancellationTokenSource _updateSource;
|
||||
private CancellationToken _updateToken;
|
||||
private bool _saved;
|
||||
|
||||
private readonly Internationalization _translator = InternationalizationManager.Instance;
|
||||
|
||||
|
|
@ -56,7 +55,6 @@ namespace Flow.Launcher.ViewModel
|
|||
|
||||
public MainViewModel(Settings settings)
|
||||
{
|
||||
_saved = false;
|
||||
_queryTextBeforeLeaveResults = "";
|
||||
_queryText = "";
|
||||
_lastQuery = new Query();
|
||||
|
|
@ -540,7 +538,7 @@ namespace Flow.Launcher.ViewModel
|
|||
|
||||
var results = await PluginManager.QueryForPlugin(plugin, query, currentCancellationToken);
|
||||
if (currentCancellationToken.IsCancellationRequested || results == null) return;
|
||||
|
||||
|
||||
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query, currentCancellationToken)))
|
||||
{
|
||||
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
|
||||
|
|
@ -765,14 +763,9 @@ namespace Flow.Launcher.ViewModel
|
|||
|
||||
public void Save()
|
||||
{
|
||||
if (!_saved)
|
||||
{
|
||||
_historyItemsStorage.Save();
|
||||
_userSelectedRecordStorage.Save();
|
||||
_topMostRecordStorage.Save();
|
||||
|
||||
_saved = true;
|
||||
}
|
||||
_historyItemsStorage.Save();
|
||||
_userSelectedRecordStorage.Save();
|
||||
_topMostRecordStorage.Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -813,7 +806,7 @@ namespace Flow.Launcher.ViewModel
|
|||
else
|
||||
{
|
||||
var priorityScore = metaResults.Metadata.Priority * 150;
|
||||
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore;
|
||||
result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue