-
-
Notifications
You must be signed in to change notification settings - Fork 455
Bring Legacy records together #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| 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 @@ public class UserSelectedRecord | |
| 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>(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure whether this name is great......but we cannot use records as name since the legacy one is using it. |
||
| } | ||
|
|
||
| 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++) | ||
| { | ||
| 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; | ||
| } | ||
|
|
||
| for (int i = 0; i < result.Title.Length; i++) | ||
| for (int i = 0; i < s.Length; i++) | ||
| { | ||
| char item = result.Title[i]; | ||
| hashcode = hashcode * HASH_MULTIPLIER + item; | ||
| start = start * HASH_MULTIPLIER + s[i]; | ||
| } | ||
|
|
||
| for (int i = 0; i < result.SubTitle.Length; i++) | ||
| return start; | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
| { | ||
| char item = result.SubTitle[i]; | ||
| hashcode = hashcode * HASH_MULTIPLIER + item; | ||
| recordsWithQuery.TryAdd(GenerateStaticHashCode(pair.Key), pair.Value); | ||
| } | ||
| return hashcode; | ||
|
|
||
| } | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,6 @@ public class MainViewModel : BaseModel, ISavable | |
|
|
||
| private CancellationTokenSource _updateSource; | ||
| private CancellationToken _updateToken; | ||
| private bool _saved; | ||
|
|
||
| private readonly Internationalization _translator = InternationalizationManager.Instance; | ||
|
|
||
|
|
@@ -56,7 +55,6 @@ public class MainViewModel : BaseModel, ISavable | |
|
|
||
| public MainViewModel(Settings settings) | ||
| { | ||
| _saved = false; | ||
| _queryTextBeforeLeaveResults = ""; | ||
| _queryText = ""; | ||
| _lastQuery = new Query(); | ||
|
|
@@ -540,7 +538,7 @@ async Task QueryTask(PluginPair plugin) | |
|
|
||
| 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 @@ private void ToggleFlowLauncher() | |
|
|
||
| public void Save() | ||
| { | ||
| if (!_saved) | ||
| { | ||
| _historyItemsStorage.Save(); | ||
| _userSelectedRecordStorage.Save(); | ||
| _topMostRecordStorage.Save(); | ||
|
|
||
| _saved = true; | ||
| } | ||
| _historyItemsStorage.Save(); | ||
| _userSelectedRecordStorage.Save(); | ||
| _topMostRecordStorage.Save(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remove this because it will prevent flow to continue save the records once user hit "Save Settings" |
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -813,7 +806,7 @@ public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates) | |
| else | ||
| { | ||
| var priorityScore = metaResults.Metadata.Priority * 150; | ||
| result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore; | ||
| result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore; | ||
|
Comment on lines
-816
to
+809
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This *5 has been changed to UserSelectedRecords. Old one will get 1 score everytime selected |
||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used to remove it once transformed