Skip to content

Commit

Permalink
feat(model): 更新操作参数以支持桌面路径
Browse files Browse the repository at this point in the history
- 在 OperationParameters 类中添加对桌面路径的特殊处理
- 使用 CommunityToolkit.WinUI 库获取本地化桌面路径
- 在 FileEventHandler 类中添加多个私有方法以提高代码可读性和维护性
- 在多个视图模型中添加注释以解释方法功能
  • Loading branch information
SaboZhang committed Nov 7, 2024
1 parent fef23db commit 12e3ee4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/EasyTidy.Model/OperationParameters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CommunityToolkit.WinUI;
using System;
using System.Collections.Generic;

namespace EasyTidy.Model;
Expand Down Expand Up @@ -40,7 +41,9 @@ public static OperationParameters CreateOperationParameters(OperationParameters
{
return new OperationParameters(
parameter.OperationMode,
parameter.SourcePath,
parameter.SourcePath = parameter.SourcePath.Equals("DesktopText".GetLocalized())
? Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
: parameter.SourcePath,
parameter.TargetPath,
parameter.FileOperationType,
parameter.HandleSubfolders,
Expand Down
29 changes: 29 additions & 0 deletions src/EasyTidy.Service/FileEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static void MonitorFolder(OperationParameters parameter, int delaySeconds
_watchers[parameter.SourcePath] = watcher;
}

/// <summary>
/// 更新源路径和目标路径的缓存
/// </summary>
/// <param name="parameter"></param>
private static void UpdateSourceCache(OperationParameters parameter)
{
var operationParams = OperationParameters.CreateOperationParameters(parameter);
Expand All @@ -53,6 +57,11 @@ private static void UpdateSourceCache(OperationParameters parameter)
: new List<OperationParameters> { operationParams };
}

/// <summary>
/// 创建文件系统监控器
/// </summary>
/// <param name="sourcePath"></param>
/// <returns></returns>
private static FileSystemWatcher CreateFileSystemWatcher(string sourcePath)
{
return new FileSystemWatcher
Expand All @@ -65,6 +74,12 @@ private static FileSystemWatcher CreateFileSystemWatcher(string sourcePath)
};
}

/// <summary>
/// 绑定文件变化事件
/// </summary>
/// <param name="watcher"></param>
/// <param name="delaySeconds"></param>
/// <param name="parameter"></param>
private static void BindFileSystemEvents(FileSystemWatcher watcher, int delaySeconds, OperationParameters parameter)
{
void onChange(FileSystemEventArgs e) => OnFileChange(e, delaySeconds, () => HandleFileChange(e.FullPath, parameter));
Expand All @@ -75,6 +90,12 @@ private static void BindFileSystemEvents(FileSystemWatcher watcher, int delaySec
watcher.Renamed += (sender, e) => onChange(e);
}

/// <summary>
/// 延时执行
/// </summary>
/// <param name="e"></param>
/// <param name="delaySeconds"></param>
/// <param name="action"></param>
private static void OnFileChange(FileSystemEventArgs e, int delaySeconds, Action action)
{
ThreadPool.QueueUserWorkItem(state =>
Expand All @@ -92,6 +113,11 @@ private static void OnFileChange(FileSystemEventArgs e, int delaySeconds, Action
});
}

/// <summary>
/// 处理文件变化
/// </summary>
/// <param name="path"></param>
/// <param name="parameter"></param>
private static void HandleFileChange(string path, OperationParameters parameter)
{
try
Expand Down Expand Up @@ -163,6 +189,9 @@ private static void HandleFileChange(string path, OperationParameters parameter)

}

/// <summary>
/// 停止监控
/// </summary>
public static void StopAllMonitoring()
{
foreach (var watcher in _watchers.Values)
Expand Down
4 changes: 4 additions & 0 deletions src/EasyTidy/ViewModels/Filters/FilterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ await Task.Run(() =>
IsActive = false;
}

/// <summary>
/// 添加
/// </summary>
/// <returns></returns>
[RelayCommand]
private async Task OnAddFilterClickedAsync()
{
Expand Down
23 changes: 23 additions & 0 deletions src/EasyTidy/ViewModels/General/GeneralViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ public bool AutoBackup

#endregion

/// <summary>
/// 初始化
/// </summary>
[RelayCommand]
private void OnPageLoaded()
{
Expand Down Expand Up @@ -339,6 +342,10 @@ private async Task OnSelectPath()
}
}

/// <summary>
/// 备份
/// </summary>
/// <returns></returns>
[RelayCommand]
private async Task BackupConfigsClickAsync()
{
Expand Down Expand Up @@ -374,6 +381,10 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = null

}

/// <summary>
/// 本地备份
/// </summary>
/// <returns></returns>
private async Task LocalBackupAsync()
{
try
Expand Down Expand Up @@ -406,6 +417,10 @@ private async Task LocalBackupAsync()
}
}

/// <summary>
/// 显示备份信息
/// </summary>
/// <param name="file"></param>
private void ShowBackInfo(string file)
{

Expand All @@ -421,6 +436,10 @@ private void ShowBackInfo(string file)
Settings.Save();
}

/// <summary>
/// WebDav备份
/// </summary>
/// <returns></returns>
private async Task WebDavBackupAsync()
{
try
Expand Down Expand Up @@ -451,6 +470,10 @@ private async Task WebDavBackupAsync()
}
}

/// <summary>
/// 延时关闭提示
/// </summary>
/// <returns></returns>
private async Task DelayCloseMessageVisible()
{
await Task.Delay(10000);
Expand Down
18 changes: 18 additions & 0 deletions src/EasyTidy/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ private void GoToSettingPage(object sender)
}
}

/// <summary>
/// 语言标签和语言ID
/// </summary>
private Dictionary<string, string> langTagsAndIds = new Dictionary<string, string>
{
{ string.Empty, "Default_language" },
Expand Down Expand Up @@ -68,6 +71,9 @@ private void GoToSettingPage(object sender)
{ "zh-TW", "Chinese_Traditional_Language" },
};

/// <summary>
/// 初始化
/// </summary>
private void InitializeLanguages()
{
var lang = Settings.Language ?? string.Empty;
Expand Down Expand Up @@ -136,6 +142,12 @@ public bool LanguageChanged
}
}

/// <summary>
/// 获取语言index
/// </summary>
/// <param name="language"></param>
/// <param name="isDefault"></param>
/// <returns></returns>
private int GetLanguageIndex(string language, bool isDefault)
{
if (Languages.Count == 0 || isDefault)
Expand All @@ -154,6 +166,9 @@ private int GetLanguageIndex(string language, bool isDefault)
return Languages.Count;
}

/// <summary>
/// 重启
/// </summary>
public void Restart()
{
Logger.Info("Restarting application");
Expand All @@ -163,6 +178,9 @@ public void Restart()
Environment.Exit(0);
}

/// <summary>
/// 通知语言更改
/// </summary>
private void NotifyLanguageChanged()
{
// 更改语言
Expand Down

0 comments on commit 12e3ee4

Please sign in to comment.