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>
|
/// </summary>
|
||||||
public class JsonStrorage<T> where T : new()
|
public class JsonStrorage<T> where T : new()
|
||||||
{
|
{
|
||||||
private readonly JsonSerializerOptions _serializerSettings;
|
|
||||||
protected T _data;
|
protected T _data;
|
||||||
// need a new directory name
|
// need a new directory name
|
||||||
public const string DirectoryName = "Settings";
|
public const string DirectoryName = "Settings";
|
||||||
|
|
@ -20,16 +19,6 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
public string DirectoryPath { get; set; }
|
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()
|
public T Load()
|
||||||
{
|
{
|
||||||
if (File.Exists(FilePath))
|
if (File.Exists(FilePath))
|
||||||
|
|
@ -55,7 +44,7 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
|
_data = JsonSerializer.Deserialize<T>(searlized);
|
||||||
}
|
}
|
||||||
catch (JsonException e)
|
catch (JsonException e)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System.Text.Json.Serialization;
|
||||||
using Flow.Launcher.Infrastructure;
|
using Flow.Launcher.Infrastructure;
|
||||||
using Flow.Launcher.Infrastructure.Storage;
|
using Flow.Launcher.Infrastructure.Storage;
|
||||||
using Flow.Launcher.Plugin;
|
using Flow.Launcher.Plugin;
|
||||||
|
using Flow.Launcher.ViewModel;
|
||||||
|
|
||||||
namespace Flow.Launcher.Storage
|
namespace Flow.Launcher.Storage
|
||||||
{
|
{
|
||||||
|
|
@ -14,72 +15,90 @@ namespace Flow.Launcher.Storage
|
||||||
private const int HASH_INITIAL = 23;
|
private const int HASH_INITIAL = 23;
|
||||||
|
|
||||||
[JsonInclude]
|
[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()
|
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
|
unchecked
|
||||||
{
|
{
|
||||||
// skip the empty space
|
// skip the empty space
|
||||||
// https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast
|
// https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast
|
||||||
// optimization without information lost when int overflow
|
// 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];
|
start = start * HASH_MULTIPLIER + s[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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < result.Title.Length; i++)
|
return start;
|
||||||
{
|
}
|
||||||
char item = result.Title[i];
|
}
|
||||||
hashcode = hashcode * HASH_MULTIPLIER + item;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < result.SubTitle.Length; i++)
|
private static int GenerateResultHashCode(Result result)
|
||||||
{
|
{
|
||||||
char item = result.SubTitle[i];
|
int hashcode = GenerateStaticHashCode(result.Title);
|
||||||
hashcode = hashcode * HASH_MULTIPLIER + item;
|
return GenerateStaticHashCode(result.SubTitle, hashcode);
|
||||||
}
|
}
|
||||||
return 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)
|
public void Add(Result result)
|
||||||
{
|
{
|
||||||
var key = GenerateCustomHashCode(result.OriginQuery, result);
|
TransformOldRecords();
|
||||||
if (records.ContainsKey(key))
|
|
||||||
{
|
|
||||||
records[key]++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
records.Add(key, 1);
|
|
||||||
|
|
||||||
}
|
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)
|
public int GetSelectedCount(Result result)
|
||||||
{
|
{
|
||||||
if (records.TryGetValue(GenerateCustomHashCode(result.OriginQuery, result), out int value))
|
var selectedCount = 0;
|
||||||
{
|
|
||||||
return value;
|
recordsWithQuery.TryGetValue(GenerateQueryAndResultHashCode(result.OriginQuery, result), out int value);
|
||||||
}
|
selectedCount += value * 5;
|
||||||
return 0;
|
|
||||||
|
recordsWithQuery.TryGetValue(GenerateResultHashCode(result), out value);
|
||||||
|
selectedCount += value;
|
||||||
|
|
||||||
|
return selectedCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
private CancellationTokenSource _updateSource;
|
private CancellationTokenSource _updateSource;
|
||||||
private CancellationToken _updateToken;
|
private CancellationToken _updateToken;
|
||||||
private bool _saved;
|
|
||||||
|
|
||||||
private readonly Internationalization _translator = InternationalizationManager.Instance;
|
private readonly Internationalization _translator = InternationalizationManager.Instance;
|
||||||
|
|
||||||
|
|
@ -56,7 +55,6 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
public MainViewModel(Settings settings)
|
public MainViewModel(Settings settings)
|
||||||
{
|
{
|
||||||
_saved = false;
|
|
||||||
_queryTextBeforeLeaveResults = "";
|
_queryTextBeforeLeaveResults = "";
|
||||||
_queryText = "";
|
_queryText = "";
|
||||||
_lastQuery = new Query();
|
_lastQuery = new Query();
|
||||||
|
|
@ -540,7 +538,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
var results = await PluginManager.QueryForPlugin(plugin, query, currentCancellationToken);
|
var results = await PluginManager.QueryForPlugin(plugin, query, currentCancellationToken);
|
||||||
if (currentCancellationToken.IsCancellationRequested || results == null) return;
|
if (currentCancellationToken.IsCancellationRequested || results == null) return;
|
||||||
|
|
||||||
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query, currentCancellationToken)))
|
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query, currentCancellationToken)))
|
||||||
{
|
{
|
||||||
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
|
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
|
||||||
|
|
@ -765,14 +763,9 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
public void Save()
|
public void Save()
|
||||||
{
|
{
|
||||||
if (!_saved)
|
_historyItemsStorage.Save();
|
||||||
{
|
_userSelectedRecordStorage.Save();
|
||||||
_historyItemsStorage.Save();
|
_topMostRecordStorage.Save();
|
||||||
_userSelectedRecordStorage.Save();
|
|
||||||
_topMostRecordStorage.Save();
|
|
||||||
|
|
||||||
_saved = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -813,7 +806,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var priorityScore = metaResults.Metadata.Priority * 150;
|
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