Skip to content

Commit

Permalink
fix cache size display
Browse files Browse the repository at this point in the history
  • Loading branch information
DDeenis committed Apr 23, 2023
1 parent 679340f commit eaf054d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ 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 @@ -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}";
}
}
}

0 comments on commit eaf054d

Please sign in to comment.