Skip to content

Commit

Permalink
persist characters usage between runs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegiacometti committed Feb 22, 2025
1 parent 74214f6 commit 3c15951
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/modules/poweraccent/PowerAccent.Core/Models/UsageInfoData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace PowerAccent.Core.Models
{
public class UsageInfoData
{
public Dictionary<string, uint> CharacterUsageCounters { get; set; } = [];

public Dictionary<string, long> CharacterUsageTimestamp { get; set; } = [];
}
}
12 changes: 10 additions & 2 deletions src/modules/poweraccent/PowerAccent.Core/PowerAccent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ private string[] GetCharacters(LetterKey letterKey)
if (_settingService.SortByUsageFrequency)
{
characters = characters.OrderByDescending(character => _usageInfo.GetUsageFrequency(character))
.ThenByDescending(character => _usageInfo.GetLastUsageTimestamp(character)).
ToArray<string>();
.ThenByDescending(character => _usageInfo.GetLastUsageTimestamp(character))
.ToArray<string>();
}
else if (!_usageInfo.Empty())
{
Expand Down Expand Up @@ -337,6 +337,14 @@ public Position GetToolbarPosition()
return _settingService.Position;
}

public void SaveUsageInfo()
{
if (_settingService.SortByUsageFrequency)
{
_usageInfo.Save();
}
}

public void Dispose()
{
_keyboardListener.UnInitHook();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// See the LICENSE file in the project root for more information.

using System.Text.Json.Serialization;
using PowerAccent.Core.Models;
using PowerAccent.Core.Services;

namespace PowerAccent.Core.SerializationContext;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(SettingsService))]
[JsonSerializable(typeof(UsageInfoData))]
public partial class SourceGenerationContext : JsonSerializerContext
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerAccent.Core.Models;
using PowerAccent.Core.SerializationContext;

namespace PowerAccent.Core.Tools
{
public class CharactersUsageInfo
{
private Dictionary<string, uint> _characterUsageCounters = new Dictionary<string, uint>();
private Dictionary<string, long> _characterUsageTimestamp = new Dictionary<string, long>();
private readonly string _filePath;
private readonly Dictionary<string, uint> _characterUsageCounters;
private readonly Dictionary<string, long> _characterUsageTimestamp;

public CharactersUsageInfo()
{
_filePath = new SettingsUtils().GetSettingsFilePath(PowerAccentSettings.ModuleName, "UsageInfo.json");
var data = GetUsageInfoData();
_characterUsageCounters = data.CharacterUsageCounters;
_characterUsageTimestamp = data.CharacterUsageTimestamp;
}

public bool Empty()
{
Expand All @@ -18,19 +33,18 @@ public void Clear()
{
_characterUsageCounters.Clear();
_characterUsageTimestamp.Clear();
Delete();
}

public uint GetUsageFrequency(string character)
{
_characterUsageCounters.TryGetValue(character, out uint frequency);

return frequency;
}

public long GetLastUsageTimestamp(string character)
{
_characterUsageTimestamp.TryGetValue(character, out long timestamp);

return timestamp;
}

Expand All @@ -47,5 +61,36 @@ public void IncrementUsageFrequency(string character)

_characterUsageTimestamp[character] = DateTimeOffset.Now.ToUnixTimeSeconds();
}

public void Save()
{
var data = new UsageInfoData
{
CharacterUsageCounters = _characterUsageCounters,
CharacterUsageTimestamp = _characterUsageTimestamp,
};

var json = JsonSerializer.Serialize(data, SourceGenerationContext.Default.UsageInfoData);
File.WriteAllText(_filePath, json);
}

public void Delete()
{
if (File.Exists(_filePath))
{
File.Delete(_filePath);
}
}

private UsageInfoData GetUsageInfoData()
{
if (!File.Exists(_filePath))
{
return new UsageInfoData();
}

var json = File.ReadAllText(_filePath);
return JsonSerializer.Deserialize(json, SourceGenerationContext.Default.UsageInfoData);
}
}
}
1 change: 1 addition & 0 deletions src/modules/poweraccent/PowerAccent.UI/Selector.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private void SetWindowPosition()

protected override void OnClosed(EventArgs e)
{
_powerAccent.SaveUsageInfo();
_powerAccent.Dispose();
base.OnClosed(e);
}
Expand Down

0 comments on commit 3c15951

Please sign in to comment.