Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions Flow.Launcher.Infrastructure/Storage/JsonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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))
Expand All @@ -55,7 +44,7 @@ private void Deserialize(string searlized)
{
try
{
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
_data = JsonSerializer.Deserialize<T>(searlized);
}
catch (JsonException e)
{
Expand Down
97 changes: 58 additions & 39 deletions Flow.Launcher/Storage/UserSelectedRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)]
Copy link
Member Author

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

public Dictionary<string, int> records { get; private set; }


public UserSelectedRecord()
{
records = new Dictionary<int, int>();
recordsWithQuery = new Dictionary<int, int>();
Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
}
}
17 changes: 5 additions & 12 deletions Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class MainViewModel : BaseModel, ISavable

private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;
private bool _saved;

private readonly Internationalization _translator = InternationalizationManager.Instance;

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

public MainViewModel(Settings settings)
{
_saved = false;
_queryTextBeforeLeaveResults = "";
_queryText = "";
_lastQuery = new Query();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Copy link
Member Author

Choose a reason for hiding this comment

The 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>
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The 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

}
}
}
Expand Down