Skip to content

Add clean cache option & Add exception handler for cleaning folders #3411

New issue

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

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

Already on GitHub? # to your account

Merged
merged 5 commits into from
Apr 2, 2025

Conversation

Jack251970
Copy link
Contributor

image

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new clean cache option to the settings view, updating translation calls across the board and introducing functionality for clearing the cache folder.

  • Updated translation calls to use App.API.GetTranslation
  • Added a new CacheFolderSize property to display the cache size
  • Introduced AskClearCacheFolderConfirmation and supporting methods to clear the cache folder
Files not reviewed (2)
  • Flow.Launcher/Languages/en.xaml: Language not supported
  • Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml: Language not supported
Comments suppressed due to low confidence (1)

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs:173

  • Consider checking if the cache directory exists (e.g. using cacheDirectory.Exists) before enumerating files and directories to avoid potential runtime exceptions when the directory is missing.
private void ClearCacheFolder()

This comment has been minimized.

Copy link

gitstream-cm bot commented Apr 1, 2025

🥷 Code experts: Yusyuriv, onesounds

Jack251970 has most 👩‍💻 activity in the files.
Yusyuriv, Jack251970 have most 🧠 knowledge in the files.

See details

Flow.Launcher/Languages/en.xaml

Activity based on git-commit:

Jack251970
APR 5 additions & 0 deletions
MAR 67 additions & 42 deletions
FEB 15 additions & 9 deletions
JAN 1 additions & 0 deletions
DEC
NOV

Knowledge based on git-blame:
Jack251970: 10%
Yusyuriv: 5%

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs

Activity based on git-commit:

Jack251970
APR
MAR 27 additions & 2 deletions
FEB 7 additions & 8 deletions
JAN 2 additions & 2 deletions
DEC
NOV 2 additions & 3 deletions

Knowledge based on git-blame:
Yusyuriv: 75%
Jack251970: 20%

Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml

Activity based on git-commit:

Jack251970
APR
MAR 9 additions & 1 deletions
FEB
JAN
DEC
NOV

Knowledge based on git-blame:
Yusyuriv: 41%
Jack251970: 6%

To learn more about /:\ gitStream - Visit our Docs

Copy link
Contributor

coderabbitai bot commented Apr 1, 2025

📝 Walkthrough

Walkthrough

This pull request introduces enhancements to cache management in the application. Three new string resources related to cache clearing messages are added to the language file. The settings view model is updated with a new property for cache size and methods for prompting user confirmation and clearing the cache. Additionally, a new button is added in the view to trigger the cache clearing functionality, along with updates to localization calls and method names for consistency.

Changes

File(s) Change Summary
Flow.Launcher/Languages/en.xaml Added three string resources: clearcachefolder ("Clear Caches"), clearcachefolderMessage ("Are you sure you want to delete all caches?"), and clearfolderfailMessage ("Failed to clear folders and files").
Flow.Launcher/SettingPages/…/SettingsPaneAboutViewModel.cs &
Flow.Launcher/SettingPages/…/SettingsPaneAbout.xaml
Introduced cache clearing functionality: added CacheFolderSize property, new methods (AskClearCacheFolderConfirmation, ClearCacheFolder), updated localization calls, and modified the update method signature. A new button was added in the view to trigger the cache clearing confirmation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI
    participant VM as ViewModel
    participant FS as FileSystem

    User->>UI: Click "Clear Caches" button
    UI->>VM: Execute AskClearCacheFolderConfirmationCommand
    VM->>User: Show confirmation dialog ("Are you sure you want to delete all caches?")
    User-->>VM: Confirm
    VM->>FS: Delete cache files
    FS-->>VM: Deletion complete
    VM->>UI: Update CacheFolderSize property
Loading

Suggested labels

enhancement, bug, kind/ui, 5 min review

Suggested reviewers

  • onesounds

Poem

I'm just a rabbit with a hop so spry,
Clearing caches as the bytes fly by.
With buttons clicked and code set free,
My burrow's neat, as neat as can be.
🐇 Code and carrots, oh my!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 35ea1e3 and 7fc4533.

📒 Files selected for processing (2)
  • Flow.Launcher/Languages/en.xaml (1 hunks)
  • Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • Flow.Launcher/Languages/en.xaml
  • Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: gitStream workflow automation
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs (1)

149-161: Refactor duplicated code in folder clearing methods.

The ClearLogFolder and ClearCacheFolder methods contain very similar code with only minor differences, which introduces maintenance challenges.

Consider refactoring to a more generic method:

+ private void ClearFolder(DirectoryInfo directory, Func<List<FileInfo>> getFiles, string propertyToUpdate, bool deleteSubdirectories = false, Func<DirectoryInfo, bool> directoryFilter = null)
+ {
+     var files = getFiles();
+     
+     try {
+         files.ForEach(f => {
+             try {
+                 f.Delete();
+             } catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException) {
+                 Log.Error($"Failed to delete file {f.FullName}: {ex.Message}", ex);
+             }
+         });
+         
+         var directories = directory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly);
+         if (directoryFilter != null)
+             directories = directories.Where(directoryFilter);
+             
+         directories.ToList().ForEach(dir => {
+             try {
+                 dir.Delete(deleteSubdirectories);
+             } catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException) {
+                 Log.Error($"Failed to delete directory {dir.FullName}: {ex.Message}", ex);
+             }
+         });
+     } finally {
+         OnPropertyChanged(propertyToUpdate);
+     }
+ }
+
- 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 void ClearLogFolder()
+ {
+     ClearFolder(
+         GetLogDir(), 
+         GetLogFiles, 
+         nameof(LogFolderSize),
+         false,
+         dir => !Constant.Version.Equals(dir.Name));
+ }

- private void ClearCacheFolder()
- {
-     var cacheDirectory = GetCacheDir();
-     var cacheFiles = GetCacheFiles();
-
-     cacheFiles.ForEach(f => f.Delete());
-
-     cacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
-         .ToList()
-         .ForEach(dir => dir.Delete(true));
-
-     OnPropertyChanged(nameof(CacheFolderSize));
- }
+ private void ClearCacheFolder()
+ {
+     ClearFolder(
+         GetCacheDir(), 
+         GetCacheFiles, 
+         nameof(CacheFolderSize),
+         true);
+ }

This refactoring reduces code duplication, adds error handling, and makes future maintenance easier.

Also applies to: 173-185

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3c18a6 and 482fdc9.

📒 Files selected for processing (3)
  • Flow.Launcher/Languages/en.xaml (1 hunks)
  • Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs (6 hunks)
  • Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs (4)
Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs (4)
  • RelayCommand (241-251)
  • RelayCommand (253-263)
  • RelayCommand (265-270)
  • RelayCommand (272-277)
Flow.Launcher/ViewModel/PluginViewModel.cs (7)
  • RelayCommand (153-158)
  • RelayCommand (160-166)
  • RelayCommand (168-172)
  • RelayCommand (174-179)
  • RelayCommand (181-186)
  • RelayCommand (188-193)
  • Task (46-50)
Flow.Launcher/ViewModel/MainViewModel.cs (10)
  • RelayCommand (272-280)
  • RelayCommand (282-294)
  • RelayCommand (296-304)
  • RelayCommand (313-324)
  • RelayCommand (326-337)
  • RelayCommand (339-353)
  • Task (259-270)
  • Task (641-668)
  • Task (900-927)
  • Task (951-955)
Flow.Launcher.Core/Plugin/PluginManager.cs (11)
  • Task (78-89)
  • Task (91-99)
  • Task (101-108)
  • Task (110-117)
  • Task (119-126)
  • Task (187-244)
  • Task (260-302)
  • Task (467-472)
  • Task (485-488)
  • Task (568-635)
  • List (335-362)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
🔇 Additional comments (8)
Flow.Launcher/Languages/en.xaml (1)

328-329: New string resources added for cache cleaning feature.

The new string resources follow the same pattern as existing log clearing resources, providing clear and consistent messaging for the new cache clearing functionality.

Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml (1)

93-96: New button for clearing cache added.

The button is properly positioned alongside the existing log clearing button, with consistent margin and binding pattern. It uses the CacheFolderSize property to display the current size of the cache.

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs (6)

22-28: Translation API method updated.

Changed from using InternationalizationManager.Instance.GetTranslation to App.API.GetTranslation for consistent API usage throughout the application.


30-36: New CacheFolderSize property added.

The property calculates the total size of all cache files using the same pattern as LogFolderSize, providing a consistent user experience.


53-53: Translation API method updated.

Consistent update from InternationalizationManager.Instance.GetTranslation to App.API.GetTranslation.


99-100: Translation API method updated.

Consistent update from InternationalizationManager.Instance.GetTranslation to App.API.GetTranslation.


110-123: New method added for confirming cache folder clearing.

The method follows the same pattern as the existing log folder confirmation, providing a consistent user experience.


146-146: Method signature changed to indicate async operation.

The method signature has been updated from UpdateApp to UpdateAppAsync to better reflect its asynchronous nature.

onesounds
onesounds previously approved these changes Apr 1, 2025

This comment has been minimized.

@Jack251970 Jack251970 changed the title Add clean cache option Add clean cache option & Add exception handler for cleaning folders Apr 1, 2025
@Jack251970 Jack251970 requested a review from Copilot April 1, 2025 12:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a clean cache option and introduces exception handling for cleaning folders, while also updating translation calls from InternationalizationManager to App.API.

  • Replaces translation calls for internationalized strings
  • Adds a new CacheFolderSize property and methods for clearing cache and retrieving cache files
  • Introduces exception handling in folder cleaning methods and renames a method for clarity
Files not reviewed (2)
  • Flow.Launcher/Languages/en.xaml: Language not supported
  • Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml: Language not supported
Comments suppressed due to low confidence (1)

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs:162

  • The method has been renamed to 'UpdateAppAsync' to reflect its asynchronous behavior; please ensure that all command bindings and invocations are updated accordingly.
private Task UpdateAppAsync() => _updater.UpdateAppAsync(false);

@Jack251970 Jack251970 requested a review from Copilot April 2, 2025 02:11
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces a clean cache option and robust error handling for the cleaning of log and cache folders. Key changes include replacing translations from InternationalizationManager with App.API, adding a new property and confirmation command for cleaning the cache folder, and incorporating try-catch error handling for file and directory deletions.

Files not reviewed (2)
  • Flow.Launcher/Languages/en.xaml: Language not supported
  • Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml: Language not supported
Comments suppressed due to low confidence (1)

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs:183

  • In ClearLogFolder the deletion of directories uses a non-recursive Delete() method, while ClearCacheFolder uses dir.Delete(true) for recursive deletion. Please verify that this difference is intentional to ensure consistent folder cleaning behavior.
dir.Delete();

This comment has been minimized.

@Jack251970 Jack251970 requested a review from Copilot April 2, 2025 02:13
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces an option to clean the cache and enhances error handling during folder clean-up operations. Key changes include:

  • Removing the unused resource import and updating translation calls to use App.API.
  • Adding a new CacheFolderSize property along with a ClearCacheFolder() method to handle cache cleaning with exception handling.
  • Renaming the asynchronous update method to UpdateAppAsync and incorporating improved error handling in log folder deletion.
Files not reviewed (2)
  • Flow.Launcher/Languages/en.xaml: Language not supported
  • Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml: Language not supported
Comments suppressed due to low confidence (3)

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs:154

  • Ensure that all callers and bindings referring to the previous UpdateApp method are updated to use the new UpdateAppAsync name for consistency with asynchronous conventions.
private Task UpdateAppAsync() => _updater.UpdateAppAsync(false);

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs:170

  • [nitpick] Consider catching more specific exceptions rather than a generic Exception in the log file deletion block to handle expected file system errors more accurately.
App.API.LogException(ClassName, "Failed to delete log file: {f.Name}", e);

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs:236

  • [nitpick] Consider catching more specific exceptions rather than a generic Exception in the cache directory deletion block to better distinguish and manage different error conditions.
App.API.LogException(ClassName, "Failed to delete cache directory: {dir.Name}", e);

Copy link

github-actions bot commented Apr 2, 2025

@check-spelling-bot Report

🔴 Please review

See the 📂 files view, the 📜action log, or 📝 job summary for details.

❌ Errors Count
❌ forbidden-pattern 22
⚠️ non-alpha-in-dictionary 19

See ❌ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

@Jack251970 Jack251970 merged commit 3b43d45 into dev Apr 2, 2025
9 checks passed
@Jack251970 Jack251970 deleted the clear_cache branch April 2, 2025 10:57
@Jack251970 Jack251970 added enhancement New feature or request and removed 10 min review labels Apr 4, 2025
@jjw24 jjw24 added this to the 1.20.0 milestone Apr 8, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants