From 21fb4c6053b61fa1c17fa2a798893060bd46a688 Mon Sep 17 00:00:00 2001 From: Yusyuriv Date: Mon, 29 Apr 2024 15:05:12 +0600 Subject: [PATCH] Restore functionality in About pane of the settings window --- .../ViewModels/SettingsPaneAboutViewModel.cs | 134 ++++++++++++++++++ .../SettingPages/Views/SettingsPaneAbout.xaml | 34 +++-- .../Views/SettingsPaneAbout.xaml.cs | 29 ++-- 3 files changed, 175 insertions(+), 22 deletions(-) create mode 100644 Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs new file mode 100644 index 000000000..b6563d3e5 --- /dev/null +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; +using CommunityToolkit.Mvvm.Input; +using Flow.Launcher.Core; +using Flow.Launcher.Core.Plugin; +using Flow.Launcher.Core.Resource; +using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.SettingPages.ViewModels; + +public partial class SettingsPaneAboutViewModel : BaseModel +{ + private readonly Settings _settings; + private readonly Updater _updater; + + public string LogFolderSize + { + get + { + var size = GetLogFiles().Sum(file => file.Length); + return $"{InternationalizationManager.Instance.GetTranslation("clearlogfolder")} ({BytesToReadableString(size)})"; + } + } + + public string Website => Constant.Website; + public string SponsorPage => Constant.SponsorPage; + public string ReleaseNotes => _updater.GitHubRepository + "/releases/latest"; + public string Documentation => Constant.Documentation; + public string Docs => Constant.Docs; + public string Github => Constant.GitHub; + + public string Version => Constant.Version switch + { + "1.0.0" => Constant.Dev, + _ => Constant.Version + }; + + public string ActivatedTimes => string.Format( + InternationalizationManager.Instance.GetTranslation("about_activate_times"), + _settings.ActivateTimes + ); + + public SettingsPaneAboutViewModel(Settings settings, Updater updater) + { + _settings = settings; + _updater = updater; + } + + [RelayCommand] + private void OpenWelcomeWindow() + { + var window = new WelcomeWindow(_settings); + window.ShowDialog(); + } + + [RelayCommand] + private void AskClearLogFolderConfirmation() + { + var confirmResult = MessageBox.Show( + InternationalizationManager.Instance.GetTranslation("clearlogfolderMessage"), + InternationalizationManager.Instance.GetTranslation("clearlogfolder"), + MessageBoxButton.YesNo + ); + + if (confirmResult == MessageBoxResult.Yes) + { + ClearLogFolder(); + } + } + + [RelayCommand] + private void OpenSettingsFolder() + { + PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Settings)); + } + + [RelayCommand] + private void OpenLogsFolder() + { + App.API.OpenDirectory(GetLogDir(Constant.Version).FullName); + } + + [RelayCommand] + private Task UpdateApp() => _updater.UpdateAppAsync(App.API, false); + + private void ClearLogFolder() + { + var logDirectory = GetLogDir(); + var logFiles = GetLogFiles(); + + logFiles.ForEach(f => f.Delete()); + + logDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) + .Where(dir => !Constant.Version.Equals(dir.Name)) + .ToList() + .ForEach(dir => dir.Delete()); + + OnPropertyChanged(nameof(LogFolderSize)); + } + + private static DirectoryInfo GetLogDir(string version = "") + { + return new DirectoryInfo(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, version)); + } + + private static List GetLogFiles(string version = "") + { + return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList(); + } + + private static string BytesToReadableString(long bytes) + { + const int scale = 1024; + string[] orders = { "GB", "MB", "KB", "B" }; + long max = (long)Math.Pow(scale, orders.Length - 1); + + foreach (string order in orders) + { + if (bytes > max) + return $"{decimal.Divide(bytes, max):##.##} {order}"; + + max /= scale; + } + + return "0 B"; + } + +} diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml index f76dc1740..bec0d7bea 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml @@ -6,18 +6,12 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ui="http://schemas.modernwpf.com/2019" xmlns:cc="clr-namespace:Flow.Launcher.Resources.Controls" + xmlns:settingsVm="clr-namespace:Flow.Launcher.SettingPages.ViewModels" Title="About" + d:DataContext="{d:DesignInstance Type=settingsVm:SettingsPaneAboutViewModel}" d:DesignHeight="450" d:DesignWidth="800" mc:Ignorable="d"> - - - - - - - - -