Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor clear log folder logic #1757

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Flow.Launcher/SettingWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3041,7 +3041,7 @@
Name="ClearLogFolderBtn"
Margin="0,0,12,0"
Click="ClearLogFolder"
Content="{Binding CheckLogFolder, UpdateSourceTrigger=PropertyChanged}" />
Content="{Binding CheckLogFolder, Mode=OneWay}" />
<Button
Margin="0,0,8,0"
Content="&#xec7a;"
Expand Down
4 changes: 1 addition & 3 deletions Flow.Launcher/SettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private void OpenWelcomeWindow(object sender, RoutedEventArgs e)
}
private void OpenLogFolder(object sender, RoutedEventArgs e)
{
PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, Constant.Version));
viewModel.OpenLogFolder();
}
private void ClearLogFolder(object sender, RoutedEventArgs e)
{
Expand All @@ -262,8 +262,6 @@ private void ClearLogFolder(object sender, RoutedEventArgs e)
if (confirmResult == MessageBoxResult.Yes)
{
viewModel.ClearLogFolder();

ClearLogFolderBtn.Content = viewModel.CheckLogFolder;
}
}

Expand Down
45 changes: 31 additions & 14 deletions Flow.Launcher/ViewModel/SettingWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -863,31 +863,48 @@ public string CheckLogFolder
{
get
{
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) + ")";
var logFiles = GetLogFiles();
long size = logFiles.Sum(file => file.Length);
return string.Format("{0} ({1})", _translater.GetTranslation("clearlogfolder"), BytesToReadableString(size));
}
}

private static DirectoryInfo GetLogDir(string version = "")
{
return new DirectoryInfo(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, version));
}

private static List<FileInfo> GetLogFiles(string version = "")
{
return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList();
}

internal void ClearLogFolder()
{
var directory = new DirectoryInfo(
Path.Combine(
DataLocation.DataDirectory(),
Constant.Logs,
Constant.Version));
var logDirectory = GetLogDir();
var logFiles = GetLogFiles();

directory.EnumerateFiles()
logFiles.ForEach(f => f.Delete());

logDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
.Where(dir => !Constant.Version.Equals(dir.Name))
.ToList()
.ForEach(x => x.Delete());
.ForEach(dir => dir.Delete());

OnPropertyChanged(nameof(CheckLogFolder));
}

internal void OpenLogFolder()
{
App.API.OpenDirectory(GetLogDir(Constant.Version).FullName);
}
internal string FormatBytes(long bytes)

internal static string BytesToReadableString(long bytes)
{
const int scale = 1024;
string[] orders = new string[]
{
"GB", "MB", "KB", "Bytes"
"GB", "MB", "KB", "B"
};
long max = (long)Math.Pow(scale, orders.Length - 1);

Expand All @@ -898,7 +915,7 @@ internal string FormatBytes(long bytes)

max /= scale;
}
return "0 Bytes";
return "0 B";
}

#endregion
Expand Down