From eaf054d76f9a2c78e542b77c2fc996e778506137 Mon Sep 17 00:00:00 2001 From: DDeenis Date: Sun, 23 Apr 2023 18:12:07 +0100 Subject: [PATCH 1/2] fix cache size display --- .../Services/MystatAPICachingService.cs | 26 +++++++++++-------- .../SettingsSections/CacheSettings.xaml.cs | 18 ++++++++++++- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/MystatDesktopWpf/Services/MystatAPICachingService.cs b/MystatDesktopWpf/Services/MystatAPICachingService.cs index 74898de..adf27b7 100644 --- a/MystatDesktopWpf/Services/MystatAPICachingService.cs +++ b/MystatDesktopWpf/Services/MystatAPICachingService.cs @@ -10,7 +10,7 @@ namespace MystatDesktopWpf.Services { - internal static class MystatAPICachingService + internal static class MystatAPICachingService { private static readonly MystatAPIClient api; private static string? userCachePath; @@ -111,28 +111,32 @@ public async static Task ClearCacheAsync() }); } - public async static Task GetCacheSize() + public async static Task GetCacheSize() { return await Task.Run(() => { - int size = 0; + long sizeInBytes = 0; + try { - foreach (var dir in Directory.GetDirectories(rootCachePath)) + string[] fileNames = Directory.GetFiles(rootCachePath, "*.*", SearchOption.AllDirectories); + + foreach (string name in fileNames) { - foreach (var file in Directory.GetFiles(dir)) - { - size += file.Length; - } + FileInfo info = new FileInfo(name); + sizeInBytes += info.Length; } + + return sizeInBytes; } - catch (Exception) {} - return size; + catch (Exception) { } + + return sizeInBytes; }); } - private static void CreateUserCacheDir() + private static void CreateUserCacheDir() { Directory.CreateDirectory(userCachePath); } diff --git a/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs b/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs index ce7ad05..17b9bbd 100644 --- a/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs +++ b/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs @@ -21,6 +21,8 @@ namespace MystatDesktopWpf.UserControls.SettingsSections /// public partial class CacheSettings : UserControl { + static string[] cacheSizeLabels = new string[] { "B", "KB", "MB", "GB" }; + public CacheSettings() { InitializeComponent(); @@ -48,7 +50,21 @@ private async void ClearCacheButton_Click(object sender, RoutedEventArgs e) public async void UpdateCacheSize() { CacheSizeTextBlock.SetResourceReference(TextBlock.TextProperty, "m_Calculating"); - CacheSizeTextBlock.Text = Math.Round(await MystatAPICachingService.GetCacheSize() / 1024.0, 2) + " KB"; + + long cacheSizeInBytes = await MystatAPICachingService.GetCacheSize(); + + + double cacheSize = cacheSizeInBytes; + string label = cacheSizeLabels[0]; + + int i = 0; + while (cacheSize >= 1024) + { + cacheSize = Math.Round(cacheSize / 1024.0, 2, MidpointRounding.ToZero); + label = cacheSizeLabels[++i]; + } + + CacheSizeTextBlock.Text = $"{cacheSize} {label}"; } } } From 4ce20d0393caf8413e24b15ed8e0e956f5e8d3a0 Mon Sep 17 00:00:00 2001 From: DDeenis Date: Sun, 23 Apr 2023 18:45:27 +0100 Subject: [PATCH 2/2] move size formatting to separate service and add translations --- MystatDesktopWpf/Languages/lang.en-US.xaml | 6 ++++ MystatDesktopWpf/Languages/lang.ru-RU.xaml | 6 ++++ MystatDesktopWpf/Languages/lang.uk-UA.xaml | 6 ++++ .../Services/SizeFormatterService.cs | 28 +++++++++++++++++++ .../SettingsSections/CacheSettings.xaml.cs | 16 +---------- 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 MystatDesktopWpf/Services/SizeFormatterService.cs diff --git a/MystatDesktopWpf/Languages/lang.en-US.xaml b/MystatDesktopWpf/Languages/lang.en-US.xaml index c38dc0f..08666ec 100644 --- a/MystatDesktopWpf/Languages/lang.en-US.xaml +++ b/MystatDesktopWpf/Languages/lang.en-US.xaml @@ -190,4 +190,10 @@ Windows settings Run on Windows startup + + + B + KB + MB + GB diff --git a/MystatDesktopWpf/Languages/lang.ru-RU.xaml b/MystatDesktopWpf/Languages/lang.ru-RU.xaml index 38fec0e..5a8ae99 100644 --- a/MystatDesktopWpf/Languages/lang.ru-RU.xaml +++ b/MystatDesktopWpf/Languages/lang.ru-RU.xaml @@ -189,4 +189,10 @@ Настройки Windows Запускать при старте Windows + + + Б + КБ + МБ + ГБ diff --git a/MystatDesktopWpf/Languages/lang.uk-UA.xaml b/MystatDesktopWpf/Languages/lang.uk-UA.xaml index f303c2f..4533510 100644 --- a/MystatDesktopWpf/Languages/lang.uk-UA.xaml +++ b/MystatDesktopWpf/Languages/lang.uk-UA.xaml @@ -189,4 +189,10 @@ Налаштування Windows Запускати під час старту Windows + + + Б + КБ + МБ + ГБ diff --git a/MystatDesktopWpf/Services/SizeFormatterService.cs b/MystatDesktopWpf/Services/SizeFormatterService.cs new file mode 100644 index 0000000..34badcc --- /dev/null +++ b/MystatDesktopWpf/Services/SizeFormatterService.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MystatDesktopWpf.Services +{ + internal static class SizeFormatterService + { + static string[] sizeLabelKeys = new string[] { "size-b", "size-kb", "size-mb", "size-gb" }; + + public static string Format(long sizeInBytes) + { + double cacheSize = sizeInBytes; + string label = sizeLabelKeys[0]; + + int i = 0; + while (cacheSize >= 1024) + { + cacheSize = Math.Round(cacheSize / 1024.0, 2, MidpointRounding.ToZero); + label = sizeLabelKeys[++i]; + } + + return $"{cacheSize} {App.Current.FindResource(label)}"; + } + } +} diff --git a/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs b/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs index 17b9bbd..b249f58 100644 --- a/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs +++ b/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs @@ -21,8 +21,6 @@ namespace MystatDesktopWpf.UserControls.SettingsSections /// public partial class CacheSettings : UserControl { - static string[] cacheSizeLabels = new string[] { "B", "KB", "MB", "GB" }; - public CacheSettings() { InitializeComponent(); @@ -52,19 +50,7 @@ public async void UpdateCacheSize() CacheSizeTextBlock.SetResourceReference(TextBlock.TextProperty, "m_Calculating"); long cacheSizeInBytes = await MystatAPICachingService.GetCacheSize(); - - - double cacheSize = cacheSizeInBytes; - string label = cacheSizeLabels[0]; - - int i = 0; - while (cacheSize >= 1024) - { - cacheSize = Math.Round(cacheSize / 1024.0, 2, MidpointRounding.ToZero); - label = cacheSizeLabels[++i]; - } - - CacheSizeTextBlock.Text = $"{cacheSize} {label}"; + CacheSizeTextBlock.Text = SizeFormatterService.Format(cacheSizeInBytes); } } }