Skip to content

Commit

Permalink
Merge pull request #68 from BeloMaximka/fix-cache-size
Browse files Browse the repository at this point in the history
fix cache size display
  • Loading branch information
BeloMaximka authored Apr 24, 2023
2 parents 679340f + 4ce20d0 commit a880bc7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 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>
26 changes: 15 additions & 11 deletions MystatDesktopWpf/Services/MystatAPICachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace MystatDesktopWpf.Services
{
internal static class MystatAPICachingService
internal static class MystatAPICachingService
{
private static readonly MystatAPIClient api;
private static string? userCachePath;
Expand Down Expand Up @@ -111,28 +111,32 @@ public async static Task<bool> ClearCacheAsync()
});
}

public async static Task<int> GetCacheSize()
public async static Task<long> 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);
}
Expand Down
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 @@ -48,7 +48,9 @@ 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();
CacheSizeTextBlock.Text = SizeFormatterService.Format(cacheSizeInBytes);
}
}
}

0 comments on commit a880bc7

Please sign in to comment.