Skip to content

Commit

Permalink
Fix Cache Size
Browse files Browse the repository at this point in the history
  • Loading branch information
MaKrotos committed May 20, 2024
1 parent dbb2bba commit 5470194
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 33 deletions.
2 changes: 1 addition & 1 deletion VK UI3 (Package)/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Identity
Name="FDW.VKM"
Publisher="CN=FDW"
Version="0.1.7.0" />
Version="0.1.7.1" />

<mp:PhoneIdentity PhoneProductId="b7c1f34e-9025-4935-9d7a-7d0a632a368d" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

Expand Down
100 changes: 76 additions & 24 deletions VK UI3/Helpers/Animations/AnimationsChangeImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using vkPosterBot.DB;

namespace VK_UI3.Helpers.Animations
{
Expand Down Expand Up @@ -64,12 +66,12 @@ public void ChangeImageWithAnimation(string? newImageSourceUrl, bool justDoIt =

if (imageSourceNow != null && imageSourceNow == newImageSourceUrl && !justDoIt)
return;

dispatcherQueue.TryEnqueue(async () =>
{
GetImageAsync(newImageSourceUrl);
});

Task<BitmapImage> GetImageTask = null ;

GetImageTask = GetImageAsync(newImageSourceUrl);


imageSourceNow = newImageSourceUrl;
dispatcherQueue.TryEnqueue(async () =>
{
Expand All @@ -90,7 +92,7 @@ public void ChangeImageWithAnimation(string? newImageSourceUrl, bool justDoIt =

if ((element as FrameworkElement).Opacity == 0 || imageSourceNow == null)
{
showImage((element as FrameworkElement));
showImage((element as FrameworkElement), await GetImageTask);
}
else
{
Expand All @@ -113,7 +115,7 @@ public void ChangeImageWithAnimation(string? newImageSourceUrl, bool justDoIt =
// Отписка от события после его выполнения
storyboard.Completed -= storyboardCompletedHandler;
if (image != null)
showImage(element as FrameworkElement);
showImage(element as FrameworkElement, await GetImageTask);


};
Expand All @@ -125,7 +127,7 @@ public void ChangeImageWithAnimation(string? newImageSourceUrl, bool justDoIt =
});
}

private void showImage(FrameworkElement element)
private void showImage(FrameworkElement element, BitmapImage image)
{


Expand Down Expand Up @@ -172,22 +174,28 @@ private async Task<BitmapImage> GetImageAsync(string newImageSourceUrl)
var fileName = Path.Combine(databaseFolderPath, GetHashString(newImageSourceUrl));

if (!Directory.Exists(databaseFolderPath)) Directory.CreateDirectory(databaseFolderPath);

BitmapImage bitmap = null ;
if (new Uri(newImageSourceUrl).IsFile)
{
var bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(newImageSourceUrl);
image = bitmapImage;

return bitmapImage;
var tcs = new TaskCompletionSource<BitmapImage>();
dispatcherQueue.TryEnqueue(() =>
{
bitmap = new BitmapImage(new Uri(fileName));
image = bitmap;
tcs.SetResult(bitmap);
});
return await tcs.Task;
}
else if (File.Exists(fileName))
{
var bitmapImage = new BitmapImage(new Uri(fileName));
bitmapImage.UriSource = new Uri(fileName);
image = bitmapImage;

return bitmapImage;
var tcs = new TaskCompletionSource<BitmapImage>();
dispatcherQueue.TryEnqueue(() =>
{
bitmap = new BitmapImage(new Uri(fileName));
image = bitmap;
tcs.SetResult(bitmap);
});
return await tcs.Task;
}
else
{
Expand All @@ -200,11 +208,19 @@ private async Task<BitmapImage> GetImageAsync(string newImageSourceUrl)
{
await File.WriteAllBytesAsync(fileName, buffer);

var bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(fileName);
image = bitmapImage;

return bitmapImage;
var tcs = new TaskCompletionSource<BitmapImage>();
dispatcherQueue.TryEnqueue(() =>
{
bitmap = new BitmapImage(new Uri(fileName));
image = bitmap;
tcs.SetResult(bitmap);
});

_ = CheckAndDeleteOldFilesAsync(databaseFolderPath);
return await tcs.Task;



}
else
{
Expand All @@ -224,6 +240,42 @@ private async Task<BitmapImage> GetImageAsync(string newImageSourceUrl)

}

private async Task CheckAndDeleteOldFilesAsync(string directoryPath)
{
_ = Task.Run(async() =>
{
int val = 100;
var a = SettingsTable.GetSetting("photoCacheSize");
if (a != null) val = int.Parse(a.settingValue);

long maxDirectorySize = val * 1024 * 1024;
if (Directory.Exists(directoryPath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
FileInfo[] files = directoryInfo.GetFiles();
long directorySize = files.Sum(file => file.Length);

if (directorySize > maxDirectorySize)
{
var orderedFiles = files.OrderBy(file => file.CreationTime);
foreach (var file in orderedFiles)
{

directorySize -= file.Length;
file.Delete();
if (directorySize <= maxDirectorySize)
{
break;
}
}
}
}
else
{

}
});
}

private string GetHashString(string inputString)
{
Expand Down
13 changes: 5 additions & 8 deletions VK UI3/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,9 @@ private void LoadIcon(string aIconName)



private void checkUpdate()
public async Task<bool> checkUpdate()
{


Task.Run(async () =>
{


Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
PackageId packageId = package.Id;
Expand All @@ -553,15 +550,15 @@ private void checkUpdate()
if (updateAvailable)
{


// Создайте новое окно
dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
{
ContentFrame.Navigate(typeof(UpdatePage), appUpdater, new DrillInNavigationTransitionInfo());
});

return true;
}
});

return false;
}


Expand Down
84 changes: 84 additions & 0 deletions VK UI3/Views/Settings/CheckUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VK_UI3.Views.Settings
{
class CheckUpdate : Button
{
public CheckUpdate()
{
this.CornerRadius = new CornerRadius(8);
Click += CheckUpdate_Click;
Style style = Application.Current.Resources["DefaultButtonStyle"] as Style;
this.Content = "Проверить обновления";
}

private async void CheckUpdate_Click(object sender, RoutedEventArgs e)
{
try
{
this.IsEnabled = false;
var a = await MainWindow.mainWindow.checkUpdate();
if (!a)
{

MenuFlyout myFlyout = new MenuFlyout();

// Добавьте элементы в меню
MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "Обновлений не найдено" };

myFlyout.Items.Add(firstItem);


// Покажите всплывающее меню
myFlyout.ShowAt(this);

// Создайте таймер, который закроет меню через 5 секунд
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(10);
timer.Tick += (s, e) =>
{
myFlyout.Hide();
timer.Stop();
this.IsEnabled = true;
};
timer.Start();
}
else
{
this.IsEnabled = true;
}
}
catch (Exception ex)
{
MenuFlyout myFlyout = new MenuFlyout();

// Добавьте элементы в меню
MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = $"Произошла ошибка. Проверьте настройки сети.\n{ex.Message}" };

myFlyout.Items.Add(firstItem);


// Покажите всплывающее меню
myFlyout.ShowAt(this);

// Создайте таймер, который закроет меню через 5 секунд
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(10);
timer.Tick += (s, e) =>
{
myFlyout.Hide();
timer.Stop();

};
timer.Start();
this.IsEnabled = true;
}
}
}
}
52 changes: 52 additions & 0 deletions VK UI3/Views/Settings/PhotoCacheSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using vkPosterBot.DB;

namespace VK_UI3.Views.Settings
{
class PhotoCacheSize : Slider
{
public PhotoCacheSize()
{
this.ValueChanged += OnValueChanged;

this.Minimum = 10;
this.Maximum = 1000;
this.StepFrequency = 10;


this.Style = Application.Current.Resources["DefaultSliderStyle"] as Style;

double val = 100;
var set = SettingsTable.GetSetting("photoCacheSize");
if (set == null)
{
SettingsTable.SetSetting("photoCacheSize", "100");
}
else
{
val = double.Parse(set.settingValue);
}

this.Value = val;
}


private void OnValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{

string a = e.NewValue.ToString();
Task.Run(() =>
{
SettingsTable.SetSetting("photoCacheSize", a);
});
}
}
}
18 changes: 18 additions & 0 deletions VK UI3/Views/Settings/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
<local:DownloadAllSetting />
</Border>

<Border>
<StackPanel>
<TextBlock Text="Размер кеша фотографий"></TextBlock>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<local:PhotoCacheSize Grid.Column="0" x:Name="photocache" />
<TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Grid.Column="1" Text="{x:Bind photocache.Value, Mode=OneWay}"></TextBlock>
<TextBlock VerticalAlignment="Center" Grid.Column="2" Text="МБ"></TextBlock>
</Grid>
</StackPanel>
</Border>
<Border>
<local:CheckUpdate />
</Border>


<TextBlock Margin="0,20,0,0" TextAlignment="Center">
Expand Down
1 change: 1 addition & 0 deletions VK UI3/Views/Settings/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public SettingsPage()
{
this.InitializeComponent();
this.Loaded += SettingsPage_Loaded;

}

private void SettingsPage_Loaded(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 5470194

Please sign in to comment.