添加项目文件。
This commit is contained in:
229
QuickModeling/ViewModels/ConfigurationManagerViewModel.cs
Normal file
229
QuickModeling/ViewModels/ConfigurationManagerViewModel.cs
Normal file
@@ -0,0 +1,229 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Documents;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
using Microsoft.Win32;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using QuickModeling.Models;
|
||||
using QuickModeling.Views;
|
||||
|
||||
namespace QuickModeling.ViewModels
|
||||
{
|
||||
public partial class ConfigurationManagerViewModel : ObservableRecipient, IRecipient<ComponentConfig>
|
||||
{
|
||||
private readonly string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Configs", "Configurations.json");
|
||||
public ConfigurationManagerViewModel()
|
||||
{
|
||||
//接收配置
|
||||
WeakReferenceMessenger.Default.Register(this, "config");
|
||||
EnsureConfigFileExists();
|
||||
var jsonString = File.ReadAllText(path);
|
||||
Configs = new ObservableCollection<ComponentConfig>(JsonConvert.DeserializeObject<List<ComponentConfig>>(jsonString));
|
||||
}
|
||||
/// <summary>
|
||||
/// 确保配置文件存在,如果不存在则创建
|
||||
/// </summary>
|
||||
private void EnsureConfigFileExists()
|
||||
{
|
||||
var dir = Path.GetDirectoryName(path);
|
||||
if (Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
File.Create(path).Close();
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<ComponentConfig> Configs { get; set; }
|
||||
[RelayCommand]
|
||||
private void RemoveConfig(object obj)
|
||||
{
|
||||
if (obj is ComponentConfig config)
|
||||
{
|
||||
var result = MessageBox.Show("确定要删除配置吗?", "删除配置", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
||||
if (result == MessageBoxResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Configs.Remove(config);
|
||||
}
|
||||
}
|
||||
[RelayCommand]
|
||||
private void AddConfig()
|
||||
{
|
||||
var configAdd = new ComponentConfig
|
||||
{
|
||||
Name = "新建配置",
|
||||
|
||||
};
|
||||
ConfigurationViewModel configViewModel = new ConfigurationViewModel(configAdd);
|
||||
|
||||
var segementConfigView = new ConfigurationView
|
||||
{
|
||||
DataContext = configViewModel
|
||||
};
|
||||
segementConfigView.ShowDialog();
|
||||
}
|
||||
[RelayCommand]
|
||||
private void ImportConfigs()
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog
|
||||
{
|
||||
Filter = "JSON文件 (*.json)|*.json",
|
||||
Multiselect = false
|
||||
};
|
||||
if (openFileDialog.ShowDialog() == true)
|
||||
{
|
||||
StringBuilder configsAdd = new StringBuilder();
|
||||
StringBuilder configsOverride = new StringBuilder();
|
||||
var jsonStr = File.ReadAllText(openFileDialog.FileName);
|
||||
var configs = JsonConvert.DeserializeObject<List<ComponentConfig>>(jsonStr);
|
||||
if (configs != null)
|
||||
{
|
||||
foreach (var config in configs)
|
||||
{
|
||||
if (!Configs.Any(c => c.Guid == config.Guid))
|
||||
{
|
||||
Configs.Add(config);
|
||||
configsAdd.AppendLine(config.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
//如果存在相同的Guid,则更新配置
|
||||
var existConfig = Configs.First(c => c.Guid == config.Guid);
|
||||
var index = Configs.IndexOf(existConfig);
|
||||
Configs[index] = config;
|
||||
configsOverride.AppendLine(config.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
MessageBox.Show($"导入完成\n\r新增:\n{configsAdd},覆盖:{configsOverride}", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
|
||||
}
|
||||
[RelayCommand]
|
||||
private void ExportConfigs()
|
||||
{
|
||||
SaveFileDialog saveFileDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "JSON文件 (*.json)|*.json",
|
||||
FileName = "Configurations.json"
|
||||
};
|
||||
if (saveFileDialog.ShowDialog() == true)
|
||||
{
|
||||
var jsonStr = JsonConvert.SerializeObject(Configs, Formatting.Indented);
|
||||
File.WriteAllText(saveFileDialog.FileName, jsonStr);
|
||||
MessageBox.Show(jsonStr.Length > 0 ? "导出成功" : "导出失败", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
}
|
||||
[RelayCommand]
|
||||
private void CopyConfig(object obj)
|
||||
{
|
||||
if (obj is ComponentConfig config)
|
||||
{
|
||||
var configNew = config.Clone() as ComponentConfig;
|
||||
//需要支持复制多份,配置名需要带序号
|
||||
int copyCount = Configs.Count(c => c.Name.StartsWith(config.Name));
|
||||
configNew.Name = $@"{config.Name}({copyCount})";
|
||||
Configs.Insert(Configs.IndexOf(config) + 1, configNew);
|
||||
//Configs.Add(configNew);
|
||||
}
|
||||
}
|
||||
[ObservableProperty]
|
||||
private bool areAllEnabled = true;
|
||||
//[RelayCommand]
|
||||
//private void ToggleAllEnabled()
|
||||
//{
|
||||
// foreach (var config in Configs)
|
||||
// {
|
||||
// config.IsEnabled = areAllEnabled;
|
||||
// }
|
||||
//}
|
||||
partial void OnAreAllEnabledChanged(bool value)
|
||||
{
|
||||
foreach (var config in Configs)
|
||||
{
|
||||
config.IsEnabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 编辑配置
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
[RelayCommand]
|
||||
private void EditConfig(object obj)
|
||||
{
|
||||
if (obj is ComponentConfig config)
|
||||
{
|
||||
var copy = config.Clone() as ComponentConfig;
|
||||
copy.Guid = config.Guid;
|
||||
|
||||
var configViewModel = new ConfigurationViewModel(copy);
|
||||
|
||||
var configurationView = new ConfigurationView
|
||||
{
|
||||
DataContext = configViewModel
|
||||
};
|
||||
configurationView.ShowDialog();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置到文件
|
||||
/// </summary>
|
||||
/// <param name="window"></param>
|
||||
[RelayCommand]
|
||||
private void SaveConfigs(Window window)
|
||||
{
|
||||
var jsonStr = JsonConvert.SerializeObject(Configs, Formatting.Indented);
|
||||
EnsureConfigFileExists();
|
||||
File.WriteAllText(path, jsonStr);
|
||||
try
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send(jsonStr, "manager");
|
||||
window?.Close();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 接收编辑后的配置
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public void Receive(ComponentConfig message)
|
||||
{
|
||||
var existCofig = Configs.Where(c => c.Guid == message.Guid).FirstOrDefault();
|
||||
//接收返回的配置,如果存在则更新,否则添加新的配置
|
||||
if (existCofig != null)
|
||||
{
|
||||
var index = Configs.IndexOf(existCofig);
|
||||
Configs[index] = message;
|
||||
//Configs.Remove(existCofig);
|
||||
//Configs.Insert(index, message);
|
||||
//.ToList().ForEach(c => Configs.Remove(c));
|
||||
}
|
||||
else
|
||||
{
|
||||
Configs.Add(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user