Commit df6b771a authored by Axelander's avatar Axelander

Added folder size for the track cache to the SettingsPage (issue #4)

parent 7c92a91e
......@@ -107,13 +107,22 @@
</StackPanel.Resources>
<ToggleSwitch Header="Clear Selection after each Action" IsOn="{Binding IsSelectionCleared, Mode=TwoWay}" />
<ToggleSwitch Header="Scrobble music on last.fm" IsOn="{Binding IsScrobbleActivated, Mode=TwoWay}" />
<Button Content="Clear Track Cache">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="ClearTrackCacheClick" TargetObject="{Binding Mode=OneWay}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
<StackPanel Orientation="Horizontal">
<Button Content="Clear Track Cache">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="ClearTrackCacheClick" TargetObject="{Binding Mode=OneWay}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
<TextBlock
Margin="10,0"
VerticalAlignment="Center"
Style="{StaticResource OrangeBaseTextBlockStyle}">
<Run Text="{Binding TrackCacheSize}" />
<Run Text="MB used" />
</TextBlock>
</StackPanel>
</StackPanel>
</PivotItem>
</Pivot>
......
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using SonicStreamer.Common.System;
using SonicStreamer.ViewModels;
namespace SonicStreamer.Pages
{
public sealed partial class SettingsPage : Page
{
private readonly ApplicationSettingsViewModel _settingsVm;
public SettingsPage()
{
InitializeComponent();
if (ResourceLoader.Current.GetResource(ref _settingsVm, Constants.ViewModelApplicationSettings) == false)
_settingsVm = new ApplicationSettingsViewModel();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
Microsoft.HockeyApp.HockeyClient.Current.TrackPageView(GetType().Name);
await _settingsVm.LoadDataAsync();
}
}
}
\ No newline at end of file
using SonicStreamer.Common.System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml.Input;
using NUnit.Framework.Constraints;
namespace SonicStreamer.ViewModels
{
......@@ -43,18 +48,57 @@ namespace SonicStreamer.ViewModels
set { Set(ref _version, value); }
}
private string _trackCacheSize;
public string TrackCacheSize
{
get { return _trackCacheSize; }
set { Set(ref _trackCacheSize, value); }
}
private const string Homepage = "http://axelander.net";
private const string Mail = "mailto:sonicstreamer@outlook.com";
private const string Twitter = "http://twitter.com/SonicStreamer";
private const string Policy = "http://axelander.net/?page_id=41";
public ApplicationSettingsViewModel()
public async Task LoadDataAsync()
{
var packageVersion = Windows.ApplicationModel.Package.Current.Id.Version;
Version = string.Format("{0}.{1} (Build {2})", packageVersion.Major.ToString(),
packageVersion.Minor.ToString(), packageVersion.Build.ToString());
RestoreData();
await GetTrackCacheSize();
}
private async Task GetTrackCacheSize()
{
try
{
var trackFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("tracks");
var folderSize = await GetFolderSizeAsync(trackFolder);
TrackCacheSize = (ConvertBytesToMegabytes(folderSize)).ToString("#.000");
}
catch
{
TrackCacheSize = "0";
}
// Local function to get sizes for all files in all subfolders (Recursion)
async Task<ulong> GetFolderSizeAsync(IStorageFolder folder)
{
var fileSizeTasks =
(await folder.GetFilesAsync()).Select(async file => (await file.GetBasicPropertiesAsync()).Size);
var sizes = await Task.WhenAll(fileSizeTasks);
var folderSize = sizes.Aggregate<ulong, ulong>(0, (current, size) => current + size);
var tasks =
(from subfolder in await folder.GetFoldersAsync() select GetFolderSizeAsync(subfolder)).ToList();
folderSize = (await Task.WhenAll(tasks)).Aggregate(folderSize,
(current, subFolderSize) => current + subFolderSize);
return folderSize;
}
double ConvertBytesToMegabytes(ulong bytes) => (bytes / 1024f) / 1024f;
}
/// <summary>
......@@ -115,6 +159,7 @@ namespace SonicStreamer.ViewModels
{
var trackFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("tracks");
await trackFolder.DeleteAsync();
TrackCacheSize = "0";
}
catch (Exception)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment