Skip to content

Commit baf961e

Browse files
authored
Merge pull request #416 from taooceros/BringOldSelectedRecordBack
Bring Legacy records together
2 parents 68e3d07 + d2fb26b commit baf961e

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace Flow.Launcher.Infrastructure.Storage
1111
/// </summary>
1212
public class JsonStrorage<T> where T : new()
1313
{
14-
private readonly JsonSerializerOptions _serializerSettings;
1514
protected T _data;
1615
// need a new directory name
1716
public const string DirectoryName = "Settings";
@@ -20,16 +19,6 @@ namespace Flow.Launcher.Infrastructure.Storage
2019
public string DirectoryPath { get; set; }
2120

2221

23-
internal JsonStrorage()
24-
{
25-
// use property initialization instead of DefaultValueAttribute
26-
// easier and flexible for default value of object
27-
_serializerSettings = new JsonSerializerOptions
28-
{
29-
IgnoreNullValues = false
30-
};
31-
}
32-
3322
public T Load()
3423
{
3524
if (File.Exists(FilePath))
@@ -55,7 +44,7 @@ private void Deserialize(string searlized)
5544
{
5645
try
5746
{
58-
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
47+
_data = JsonSerializer.Deserialize<T>(searlized);
5948
}
6049
catch (JsonException e)
6150
{

Flow.Launcher/Storage/UserSelectedRecord.cs

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Flow.Launcher.Infrastructure;
66
using Flow.Launcher.Infrastructure.Storage;
77
using Flow.Launcher.Plugin;
8+
using Flow.Launcher.ViewModel;
89

910
namespace Flow.Launcher.Storage
1011
{
@@ -14,72 +15,90 @@ public class UserSelectedRecord
1415
private const int HASH_INITIAL = 23;
1516

1617
[JsonInclude]
17-
public Dictionary<int, int> records { get; private set; }
18+
public Dictionary<int, int> recordsWithQuery { get; private set; }
19+
20+
[JsonInclude, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
21+
public Dictionary<string, int> records { get; private set; }
22+
1823

1924
public UserSelectedRecord()
2025
{
21-
records = new Dictionary<int, int>();
26+
recordsWithQuery = new Dictionary<int, int>();
2227
}
2328

24-
private static int GenerateCustomHashCode(Query query, Result result)
29+
private static int GenerateStaticHashCode(string s, int start = HASH_INITIAL)
2530
{
26-
int hashcode = HASH_INITIAL;
27-
2831
unchecked
2932
{
3033
// skip the empty space
3134
// https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast
3235
// optimization without information lost when int overflow
33-
34-
for (int i = 0; i < query.ActionKeyword.Length; i++)
35-
{
36-
char item = query.ActionKeyword[i];
37-
hashcode = hashcode * HASH_MULTIPLIER + item;
38-
}
39-
40-
for (int i = 0; i < query.Search.Length; i++)
41-
{
42-
char item = query.Search[i];
43-
hashcode = hashcode * HASH_MULTIPLIER + item;
44-
}
4536

46-
for (int i = 0; i < result.Title.Length; i++)
37+
for (int i = 0; i < s.Length; i++)
4738
{
48-
char item = result.Title[i];
49-
hashcode = hashcode * HASH_MULTIPLIER + item;
39+
start = start * HASH_MULTIPLIER + s[i];
5040
}
5141

52-
for (int i = 0; i < result.SubTitle.Length; i++)
42+
return start;
43+
}
44+
}
45+
46+
private static int GenerateResultHashCode(Result result)
47+
{
48+
int hashcode = GenerateStaticHashCode(result.Title);
49+
return GenerateStaticHashCode(result.SubTitle, hashcode);
50+
}
51+
52+
private static int GenerateQueryAndResultHashCode(Query query, Result result)
53+
{
54+
int hashcode = GenerateStaticHashCode(query.ActionKeyword);
55+
hashcode = GenerateStaticHashCode(query.Search, hashcode);
56+
hashcode = GenerateStaticHashCode(result.Title, hashcode);
57+
hashcode = GenerateStaticHashCode(result.SubTitle, hashcode);
58+
59+
return hashcode;
60+
}
61+
62+
private void TransformOldRecords()
63+
{
64+
if (records != null)
65+
{
66+
var localRecords = records;
67+
records = null;
68+
69+
foreach (var pair in localRecords)
5370
{
54-
char item = result.SubTitle[i];
55-
hashcode = hashcode * HASH_MULTIPLIER + item;
71+
recordsWithQuery.TryAdd(GenerateStaticHashCode(pair.Key), pair.Value);
5672
}
57-
return hashcode;
58-
5973
}
6074
}
6175

6276
public void Add(Result result)
6377
{
64-
var key = GenerateCustomHashCode(result.OriginQuery, result);
65-
if (records.ContainsKey(key))
66-
{
67-
records[key]++;
68-
}
69-
else
70-
{
71-
records.Add(key, 1);
78+
TransformOldRecords();
7279

73-
}
80+
var keyWithQuery = GenerateQueryAndResultHashCode(result.OriginQuery, result);
81+
82+
if (!recordsWithQuery.TryAdd(keyWithQuery, 1))
83+
recordsWithQuery[keyWithQuery]++;
84+
85+
var keyWithoutQuery = GenerateResultHashCode(result);
86+
87+
if (!recordsWithQuery.TryAdd(keyWithoutQuery, 1))
88+
recordsWithQuery[keyWithoutQuery]++;
7489
}
7590

7691
public int GetSelectedCount(Result result)
7792
{
78-
if (records.TryGetValue(GenerateCustomHashCode(result.OriginQuery, result), out int value))
79-
{
80-
return value;
81-
}
82-
return 0;
93+
var selectedCount = 0;
94+
95+
recordsWithQuery.TryGetValue(GenerateQueryAndResultHashCode(result.OriginQuery, result), out int value);
96+
selectedCount += value * 5;
97+
98+
recordsWithQuery.TryGetValue(GenerateResultHashCode(result), out value);
99+
selectedCount += value;
100+
101+
return selectedCount;
83102
}
84103
}
85104
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class MainViewModel : BaseModel, ISavable
4343

4444
private CancellationTokenSource _updateSource;
4545
private CancellationToken _updateToken;
46-
private bool _saved;
4746

4847
private readonly Internationalization _translator = InternationalizationManager.Instance;
4948

@@ -56,7 +55,6 @@ public class MainViewModel : BaseModel, ISavable
5655

5756
public MainViewModel(Settings settings)
5857
{
59-
_saved = false;
6058
_queryTextBeforeLeaveResults = "";
6159
_queryText = "";
6260
_lastQuery = new Query();
@@ -540,7 +538,7 @@ async Task QueryTask(PluginPair plugin)
540538

541539
var results = await PluginManager.QueryForPlugin(plugin, query, currentCancellationToken);
542540
if (currentCancellationToken.IsCancellationRequested || results == null) return;
543-
541+
544542
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query, currentCancellationToken)))
545543
{
546544
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
@@ -765,14 +763,9 @@ private void ToggleFlowLauncher()
765763

766764
public void Save()
767765
{
768-
if (!_saved)
769-
{
770-
_historyItemsStorage.Save();
771-
_userSelectedRecordStorage.Save();
772-
_topMostRecordStorage.Save();
773-
774-
_saved = true;
775-
}
766+
_historyItemsStorage.Save();
767+
_userSelectedRecordStorage.Save();
768+
_topMostRecordStorage.Save();
776769
}
777770

778771
/// <summary>
@@ -813,7 +806,7 @@ public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
813806
else
814807
{
815808
var priorityScore = metaResults.Metadata.Priority * 150;
816-
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore;
809+
result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore;
817810
}
818811
}
819812
}

0 commit comments

Comments
 (0)