Skip to content

Commit

Permalink
Merge branch 'develop' into 'layer72'
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Oct 10, 2017
2 parents c5355d7 + 7674c8b commit a2587b7
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 42 deletions.
12 changes: 6 additions & 6 deletions Unigram/Unigram/Selectors/WallPaperTemplateSelector.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Api.TL;
using Telegram.Api.TL;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

Expand All @@ -18,6 +13,11 @@ public class WallPaperTemplateSelector : DataTemplateSelector

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item == null)
{
return base.SelectTemplate(item);
}

if (item is TLWallPaperSolid)
{
return SolidTemplate;
Expand Down
6 changes: 6 additions & 0 deletions Unigram/Unigram/ViewModels/DialogViewModel.Handle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unigram.Services;
using Windows.System.Profile;
using Windows.UI.Notifications;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;

namespace Unigram.ViewModels
Expand Down Expand Up @@ -612,6 +613,11 @@ private void InsertMessage(TLMessageCommonBase messageCommon)
if (user != null && user.IsBot)
{
SetReplyMarkup(message);
if (message.ReplyMarkup is TLReplyKeyboardMarkup)
{
InputPane.GetForCurrentView().TryHide();
}
}
}
Expand Down
190 changes: 172 additions & 18 deletions Unigram/Unigram/ViewModels/Settings/SettingsStorageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Telegram.Api.Services;
using Telegram.Api.Services.Cache;
using Unigram.Common;
using Unigram.Controls;
using Unigram.Native;
using Windows.Storage;
using Windows.Storage.Search;
Expand All @@ -18,17 +17,44 @@ namespace Unigram.ViewModels.Settings
{
public class SettingsStorageViewModel : UnigramViewModelBase
{
public SettingsStorageViewModel(IMTProtoService protoService, ICacheService cacheService, ITelegramEventAggregator aggregator)
public SettingsStorageViewModel(IMTProtoService protoService, ICacheService cacheService, ITelegramEventAggregator aggregator)
: base(protoService, cacheService, aggregator)
{
}

public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
{
await UpdateCacheSizeAsync();
await UpdateCacheSizeAsync(resetInitialCacheSize: true, updateDetailedCacheSizes: true);
}

private long _cacheSize, _initialCacheSize, _imagesCacheSize, _videosCacheSize, _otherFilesCacheSize;
private double _percentage;
private bool _taskCompleted;

public long InitialCacheSize
{
get
{
return _initialCacheSize;
}
set
{
Set(ref _initialCacheSize, value);
}
}

public double Percentage
{
get
{
return _percentage;
}
set
{
Set(ref _percentage, value);
}
}

private long _cacheSize;
public long CacheSize
{
get
Expand All @@ -41,44 +67,172 @@ public long CacheSize
}
}

private async Task UpdateCacheSizeAsync()
public long ImagesCacheSize
{
get
{
return _imagesCacheSize;
}
set
{
Set(ref _imagesCacheSize, value);
}
}

public long VideosCacheSize
{
get
{
return _videosCacheSize;
}
set
{
Set(ref _videosCacheSize, value);
}
}

public long OtherFilesCacheSize
{
get
{
return _otherFilesCacheSize;
}
set
{
Set(ref _otherFilesCacheSize, value);
}
}

public bool TaskCompleted
{
get
{
return _taskCompleted;
}
set
{
Set(ref _taskCompleted, value);
}
}

private async Task UpdateCacheSizeAsync(bool resetInitialCacheSize, bool updateDetailedCacheSizes)
{
CacheSize = 0;
if (resetInitialCacheSize)
InitialCacheSize = 0;

try
{
CacheSize = NativeUtils.GetDirectorySize(FileUtils.GetTempFileName(string.Empty));
var cacheSize = NativeUtils.GetDirectorySize(FileUtils.GetTempFileName(string.Empty));
CacheSize = cacheSize;
if (resetInitialCacheSize)
InitialCacheSize = cacheSize;
Percentage = InitialCacheSize > 0 ? Math.Round((double)(CacheSize * 100) / InitialCacheSize, 1) : 0.0D;
}
catch { }
finally
{
if (updateDetailedCacheSizes)
{
var files = await this.RetrieveCacheFilesAsync();

UpdateCacheTypes(files);
}
}
}

public void UpdateCacheTypes(IReadOnlyList<StorageFile> files)
{
if (files == null || files.Count == 0)
{
ImagesCacheSize = 0;
VideosCacheSize = 0;
OtherFilesCacheSize = 0;
return;
}

ImagesCacheSize = files.OfImageType().Sum(f => (long)f.GetFileSize());
VideosCacheSize = files.OfVideoType().Sum(f => (long)f.GetFileSize());
OtherFilesCacheSize = files.OfOtherTypes().Sum(f => (long)f.GetFileSize());
}

public RelayCommand ClearCacheCommand => new RelayCommand(ClearCacheExecute);
private async void ClearCacheExecute()
{
IsLoading = true;
TaskCompleted = false;

var folder = await StorageFolder.GetFolderFromPathAsync(FileUtils.GetTempFileName(string.Empty));
var queryOptions = new QueryOptions();
queryOptions.FolderDepth = FolderDepth.Deep;

var query = folder.CreateFileQueryWithOptions(queryOptions);
var result = await query.GetFilesAsync();
var files = await this.RetrieveCacheFilesAsync();

foreach (var file in result)
foreach (var file in files)
{
try
{
//await file.DeleteAsync();
NativeUtils.Delete(file.Path);
await UpdateCacheSizeAsync();
await UpdateCacheSizeAsync(resetInitialCacheSize: false, updateDetailedCacheSizes: false);
}
catch { }
}

IsLoading = false;

await UpdateCacheSizeAsync();
await TLMessageDialog.ShowAsync("Done", "Done", "Done");
await UpdateCacheSizeAsync(resetInitialCacheSize: true, updateDetailedCacheSizes: true);
TaskCompleted = true;
}

private async Task<IReadOnlyList<StorageFile>> RetrieveCacheFilesAsync()
{
var folder = await StorageFolder.GetFolderFromPathAsync(FileUtils.GetTempFileName(string.Empty));
var queryOptions = new QueryOptions
{
FolderDepth = FolderDepth.Deep
};

var query = folder.CreateFileQueryWithOptions(queryOptions);
var result = await query.GetFilesAsync();

return result;
}
}

public static class StorageFileExtensions
{
private const string JpegExtension = "jpg";
private const string PngExtension = "png";
private const string Mp4Extension = "mp4";

public static IEnumerable<StorageFile> OfImageType(this IEnumerable<StorageFile> storageFiles)
{
if (storageFiles == null)
throw new ArgumentNullException(nameof(storageFiles));

return storageFiles.Where(f => f.FileType.ToLower().Contains(JpegExtension)
|| f.FileType.ToLower().Contains(PngExtension));
}

public static IEnumerable<StorageFile> OfVideoType(this IEnumerable<StorageFile> storageFiles)
{
if (storageFiles == null)
throw new ArgumentNullException(nameof(storageFiles));

return storageFiles.Where(f => f.FileType.ToLower().Contains(Mp4Extension));
}

public static IEnumerable<StorageFile> OfOtherTypes(this IEnumerable<StorageFile> storageFiles)
{
if (storageFiles == null)
throw new ArgumentNullException(nameof(storageFiles));

return storageFiles.Where(f => !f.FileType.ToLower().Contains(JpegExtension)
&& !f.FileType.ToLower().Contains(PngExtension)
&& !f.FileType.ToLower().Contains(Mp4Extension));
}

public static ulong GetFileSize(this StorageFile storageFile)
{
var task = storageFile.GetBasicPropertiesAsync().AsTask();
task.Wait();
return task.Result.Size;
}
}
}
}
87 changes: 69 additions & 18 deletions Unigram/Unigram/Views/Settings/SettingsStoragePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
x:Class="Unigram.Views.Settings.SettingsStoragePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Unigram.Views.Settings"
xmlns:local="using:Unigram.Views"
xmlns:controls="using:Unigram.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -13,25 +13,76 @@
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<controls:PageHeader Text="Storage usage"/>
<controls:PageHeader Text="Storage usage"
IsLoading="{x:Bind ViewModel.IsLoading, Mode=OneWay}"/>

<StackPanel Grid.Row="1">
<Button Command="{x:Bind ViewModel.ClearCacheCommand}" IsEnabled="{x:Bind ViewModel.IsLoading, Mode=OneWay, Converter={StaticResource BooleanNegationConverter}}" Grid.Column="1">
<StackPanel>
<StackPanel Style="{StaticResource SettingsGroupPanelStyle}">
<TextBlock Text="CACHE"
Margin="0,8"
FontWeight="SemiBold"
Foreground="{ThemeResource SystemControlForegroundAccentBrush}"
Style="{StaticResource CaptionTextBlockStyle}"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<Button Content="Clear"
Command="{x:Bind ViewModel.ClearCacheCommand}"
IsEnabled="{x:Bind ViewModel.IsLoading, Mode=OneWay, Converter={StaticResource BooleanNegationConverter}}"
Grid.Column="0" />
<TextBlock Text="{x:Bind ViewModel.CacheSize, Mode=OneWay, Converter={StaticResource FileSizeConverter}}"
VerticalAlignment="Center"
Margin="12,-6"
Grid.Column="1" />
<ProgressBar Margin="8,0"
VerticalAlignment="Center"
Value="{x:Bind ViewModel.Percentage, Mode=OneWay}"
Visibility="{x:Bind ViewModel.IsLoading, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"
Maximum="100"
Minimum="0"
Grid.Column="2" />
<FontIcon Glyph="&#xE8FB;"
FontSize="20"
FontWeight="Bold"
HorizontalAlignment="Left"
Visibility="{x:Bind ViewModel.TaskCompleted, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"
Foreground="{ThemeResource SystemControlForegroundAccentBrush}"
FontFamily="{ThemeResource SymbolThemeFontFamily}"
Grid.Column="2" />
</Grid>
</StackPanel>
<StackPanel Style="{StaticResource SettingsGroupPanelStyle}">
<TextBlock Text="CACHE DETAIL"
Margin="0,8"
FontWeight="SemiBold"
Foreground="{ThemeResource SystemControlForegroundAccentBrush}"
Style="{StaticResource CaptionTextBlockStyle}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Clear cache" VerticalAlignment="Center"/>
<ContentControl Content="{x:Bind ViewModel.CacheSize, Mode=OneWay, Converter={StaticResource FileSizeConverter}}" Height="32" Padding="0,6,4,6" Margin="12,-6,0,-6">
<ContentControl.ContentTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</ContentControl.ContentTransitions>
<ContentControl.Clip>
<RectangleGeometry Rect="0,0,200,32"/>
</ContentControl.Clip>
</ContentControl>
<TextBlock Text="Images"
VerticalAlignment="Center"/>
<TextBlock Text="{x:Bind ViewModel.ImagesCacheSize, Mode=OneWay, Converter={StaticResource FileSizeConverter}}"
VerticalAlignment="Center"
Margin="12,0"/>
</StackPanel>
</Button>

<StackPanel Orientation="Horizontal" Margin="0,8">
<TextBlock Text="Videos"
VerticalAlignment="Center"/>
<TextBlock Text="{x:Bind ViewModel.VideosCacheSize, Mode=OneWay, Converter={StaticResource FileSizeConverter}}"
VerticalAlignment="Center"
Margin="12,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Other"
VerticalAlignment="Center"/>
<TextBlock Text="{x:Bind ViewModel.OtherFilesCacheSize, Mode=OneWay, Converter={StaticResource FileSizeConverter}}"
VerticalAlignment="Center"
Margin="12,0"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</Page>
</Page>

0 comments on commit a2587b7

Please sign in to comment.