From bded19130ee0ced0159b12371088ae20c84b2fc6 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 5 Oct 2022 21:11:57 +1100 Subject: [PATCH] use Linq and move delete log logic to the viewmodel --- Flow.Launcher/SettingWindow.xaml.cs | 18 +++++------ .../ViewModel/SettingWindowViewModel.cs | 30 +++++++++++-------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index b6da755db..dc8c3a300 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -14,6 +14,7 @@ using Microsoft.Win32; using ModernWpf; using System; using System.IO; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; @@ -272,20 +273,17 @@ namespace Flow.Launcher } private void ClearLogFolder(object sender, RoutedEventArgs e) { - var confirmResult = MessageBox.Show(InternationalizationManager.Instance.GetTranslation("clearlogfolderMessage"),InternationalizationManager.Instance.GetTranslation("clearlogfolder"), MessageBoxButton.YesNo); + var confirmResult = MessageBox.Show( + InternationalizationManager.Instance.GetTranslation("clearlogfolderMessage"), + InternationalizationManager.Instance.GetTranslation("clearlogfolder"), + MessageBoxButton.YesNo); + if (confirmResult == MessageBoxResult.Yes) { - DirectoryInfo Di = new DirectoryInfo(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, Constant.Version)); - foreach (FileInfo file in Di.EnumerateFiles()) - { - file.Delete(); - } + viewModel.ClearLogFolder(); + ClearLogFolderBtn.Content = viewModel.CheckLogFolder; } - else - { - } - } private void OnPluginStoreRefreshClick(object sender, RoutedEventArgs e) diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index d05461467..039ba8baf 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -576,25 +576,31 @@ namespace Flow.Launcher.ViewModel public string Github => Constant.GitHub; public static string Version => Constant.Version; public string ActivatedTimes => string.Format(_translater.GetTranslation("about_activate_times"), Settings.ActivateTimes); + public string CheckLogFolder { get - { - DirectoryInfo dirInfo = new DirectoryInfo(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, Constant.Version)); - long size = 0; - foreach (FileInfo fi in dirInfo.GetFiles("*", SearchOption.AllDirectories)) - { - size += fi.Length; - } - return _translater.GetTranslation("clearlogfolder") + " (" + FormatBytes(size) + ")" ; - } - set { + var dirInfo = new DirectoryInfo(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, Constant.Version)); + long size = dirInfo.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length); + + return _translater.GetTranslation("clearlogfolder") + " (" + FormatBytes(size) + ")" ; } - } - public string FormatBytes(long bytes) + internal void ClearLogFolder() + { + var directory = new DirectoryInfo( + Path.Combine( + DataLocation.DataDirectory(), + Constant.Logs, + Constant.Version)); + + directory.EnumerateFiles() + .ToList() + .ForEach(x => x.Delete()); + } + internal string FormatBytes(long bytes) { const int scale = 1024; string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };