FieldWorkClient/ViewModel/Sync/SyncViewModel.cs

152 lines
3.9 KiB
C#

using System.IO;
using System.Windows.Input;
using HeBianGu.Base.WpfBase;
using HeBianGu.Service.Mvc;
using Hopetry.Services;
using Microsoft.WindowsAPICodePack.Dialogs;
using SystemSetting = FileUploader.Models.SystemSetting;
namespace HeBianGu.App.Disk.ViewModel.Sync;
[ViewModel("Sync")]
public class SyncViewModel : MvcViewModelBase
{
public ICommand OpenDirCommand { get; set; }
private readonly ISerializerService _serializerService;
private readonly MinioService _minioService;
private readonly SystemSetting _setting;
protected override void Init()
{
Console.WriteLine("init");
EnumValues = ["1", "2", "3", "4", "5"];
//RaisePropertyChanged();
/*LinkActions.Add(new LinkAction() { Action = "Space", Controller = "Loyout", DisplayName = "会话", Logo = "\xe613" });
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
{
SelectLink = LinkActions[0];
}));*/
}
public SyncViewModel(ISerializerService serializerService, MinioService minioService)
{
Console.WriteLine("SyncViewModel");
_minioService = minioService;
_serializerService = serializerService;
if (File.Exists("./settings.xml"))
{
_setting = _serializerService.Load<SystemSetting>("./settings.xml");
// 同步字段
TaskCount = _setting.TaskCount;
SyncDir = _setting.SyncDir;
}
else
{
_setting = new SystemSetting();
}
ComboBox_SelectionChanged = new RelayCommand( async()=> await ComboBox_SelectionUpdate());
OpenDirCommand = new RelayCommand(async () => await ButtonBase_OnClick());
SyncData = new RelayCommand(async () => await SyncDataQuick());
}
private async Task ComboBox_SelectionUpdate()
{
_setting.TaskCount = TaskCount;
_serializerService.Save("./settings.xml", _setting);
}
private async Task<object> SyncDataQuick()
{
return _minioService.MirrorAsync1(_minioService._bucketName, SyncDir, _taskCount);
}
protected override void Loaded(string args)
{
Console.WriteLine("Loaded");
}
private List<string> _enumValues;
public List<string> EnumValues
{
get { return _enumValues; }
set
{
_enumValues = value;
RaisePropertyChanged();
}
}
private int _taskCount = 3;
public int TaskCount
{
get => _taskCount;
set
{
_taskCount = value;
RaisePropertyChanged();
}
}
public string _syncDir;
public string SyncDir
{
get => _syncDir;
set
{
_syncDir = value;
RaisePropertyChanged();
}
}
public RelayCommand SyncData { get; set; }
public RelayCommand ComboBox_SelectionChanged { get; set; }
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute)
{
_execute = execute;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_execute();
}
}
private async Task ButtonBase_OnClick()
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog
{
IsFolderPicker = true
};
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
var folderPath = dialog.FileName; // 获取选中的文件夹路径
//MessageBox.Show("folderPath " + folderPath);
SyncDir = folderPath;
_setting.SyncDir = folderPath;
_serializerService.Save("./settings.xml", _setting);
// 处理选中的文件夹路径
}
}
}