Skip to content

Commit

Permalink
move size formatting to separate service and add translations
Browse files Browse the repository at this point in the history
  • Loading branch information
DDeenis committed Apr 23, 2023
1 parent eaf054d commit 4ce20d0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
6 changes: 6 additions & 0 deletions MystatDesktopWpf/Languages/lang.en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,10 @@
<!-- Windows settings -->
<v:String x:Key="m_WindowsSettings">Windows settings</v:String>
<v:String x:Key="m_RunOnWindowsStartup">Run on Windows startup</v:String>

<!-- sizes -->
<v:String x:Key="size-b">B</v:String>
<v:String x:Key="size-kb">KB</v:String>
<v:String x:Key="size-mb">MB</v:String>
<v:String x:Key="size-gb">GB</v:String>
</ResourceDictionary>
6 changes: 6 additions & 0 deletions MystatDesktopWpf/Languages/lang.ru-RU.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,10 @@
<!-- Windows settings -->
<v:String x:Key="m_WindowsSettings">Настройки Windows</v:String>
<v:String x:Key="m_RunOnWindowsStartup">Запускать при старте Windows</v:String>

<!-- sizes -->
<v:String x:Key="size-b">Б</v:String>
<v:String x:Key="size-kb">КБ</v:String>
<v:String x:Key="size-mb">МБ</v:String>
<v:String x:Key="size-gb">ГБ</v:String>
</ResourceDictionary>
6 changes: 6 additions & 0 deletions MystatDesktopWpf/Languages/lang.uk-UA.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,10 @@
<!-- Windows settings -->
<v:String x:Key="m_WindowsSettings">Налаштування Windows</v:String>
<v:String x:Key="m_RunOnWindowsStartup">Запускати під час старту Windows</v:String>

<!-- sizes -->
<v:String x:Key="size-b">Б</v:String>
<v:String x:Key="size-kb">КБ</v:String>
<v:String x:Key="size-mb">МБ</v:String>
<v:String x:Key="size-gb">ГБ</v:String>
</ResourceDictionary>
28 changes: 28 additions & 0 deletions MystatDesktopWpf/Services/SizeFormatterService.cs
Original file line number Diff line number Diff line change
@@ -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)}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace MystatDesktopWpf.UserControls.SettingsSections
/// </summary>
public partial class CacheSettings : UserControl
{
static string[] cacheSizeLabels = new string[] { "B", "KB", "MB", "GB" };

public CacheSettings()
{
InitializeComponent();
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 4ce20d0

Please sign in to comment.