增加运行时配置管理,可以记录分组选择状态
This commit is contained in:
parent
7357c86238
commit
3fe2d0fea4
81
PrivateBox/AppRunTimeSettings.cs
Normal file
81
PrivateBox/AppRunTimeSettings.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using PrivateBox.DataContext;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PrivateBox
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序运行时配置
|
||||
/// </summary>
|
||||
public class AppRunTimeSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库上下文
|
||||
/// </summary>
|
||||
public DbContext Db { get; set; }
|
||||
/// <summary>
|
||||
/// 构造
|
||||
/// </summary>
|
||||
/// <param name="db">数据库上下文</param>
|
||||
public AppRunTimeSettings(DbContext db) {
|
||||
this.Db = db;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置配置值。
|
||||
/// </summary>
|
||||
/// <param name="key">唯一键</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns>实际设置的值</returns>
|
||||
/// <exception cref="ArgumentNullException">键不能为空</exception>
|
||||
public string SetValue(string key,string value) {
|
||||
if(string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
var model = new AppSettingsTable {
|
||||
Key = key,
|
||||
Value = value,
|
||||
};
|
||||
var storage = this.Db.Storageable(model).WhereColumns(a => a.Key).ToStorage();
|
||||
storage.AsInsertable.ExecuteCommand();
|
||||
storage.AsUpdateable.ExecuteCommand();
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置配置对象值
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">值类型</typeparam>
|
||||
/// <param name="key">唯一键</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns>实际设置的字符串值</returns>
|
||||
public string SetValue<TValue>(string key,TValue value) {
|
||||
var str = JsonSerializer.Serialize(value);
|
||||
return SetValue(key,str);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取键值
|
||||
/// </summary>
|
||||
/// <param name="key">配置的键</param>
|
||||
/// <returns>配置的值</returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public string? GetValue(string? key) {
|
||||
if(string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
var qu = this.Db.Queryable<AppSettingsTable>()
|
||||
.Where(a => a.Key == key)
|
||||
.ToList();
|
||||
return qu.Any() ? qu.First().Value : string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置的对象值
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">值类型</typeparam>
|
||||
/// <param name="key">唯一键</param>
|
||||
/// <returns>配置对象</returns>
|
||||
public TValue? GetValue<TValue>(string? key) {
|
||||
var str = GetValue(key);
|
||||
return JsonSerializer.Deserialize<TValue>(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
PrivateBox/DataContext/AppSettingsTable.cs
Normal file
21
PrivateBox/DataContext/AppSettingsTable.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace PrivateBox.DataContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序配置
|
||||
/// </summary>
|
||||
public class AppSettingsTable
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用配置
|
||||
/// </summary>
|
||||
[Key, Required, MaxLength(50)]
|
||||
public string? Key { get; set; }
|
||||
/// <summary>
|
||||
/// 配置值
|
||||
/// </summary>
|
||||
[MaxLength(4000)]
|
||||
public string? Value { get; set; }
|
||||
}
|
||||
}
|
||||
@ -38,6 +38,7 @@ namespace PrivateBox
|
||||
this.CodeFirst.SetStringDefaultLength(200);
|
||||
this.CodeFirst.InitTables<CryptionKey>();
|
||||
this.CodeFirst.InitTables<CrytionItem>();
|
||||
this.CodeFirst.InitTables<AppSettingsTable>();
|
||||
IsInited = true;
|
||||
}
|
||||
|
||||
|
||||
@ -10,12 +10,15 @@ namespace PrivateBox
|
||||
public IEncryptionService Encrytion { get; init; }
|
||||
public DbContext Db { get; init; }
|
||||
public string CrytionKey { get; init; }
|
||||
public AppRunTimeSettings Settings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 默认分组名
|
||||
/// </summary>
|
||||
private const string _defaultGroupName = "未分组";
|
||||
|
||||
private const string _groupListSelectedKey = "MainForm_GroupList_Selected";
|
||||
|
||||
/// <summary>
|
||||
/// 允许刷新Key列表
|
||||
/// </summary>
|
||||
@ -25,6 +28,7 @@ namespace PrivateBox
|
||||
InitializeComponent();
|
||||
this.Encrytion = provider.GetRequiredService<IEncryptionService>();
|
||||
this.Db = provider.GetRequiredService<DbContext>();
|
||||
this.Settings = provider.GetRequiredService<AppRunTimeSettings>();
|
||||
this.CrytionKey = this.Db.GetCrytionKey();
|
||||
|
||||
this.Load += (s,d) => {
|
||||
@ -35,6 +39,11 @@ namespace PrivateBox
|
||||
this.lbKeyList.SelectedIndexChanged += LbKeyList_SelectedIndexChanged;
|
||||
this.clb_group.ItemCheck += (s,e) => {
|
||||
this.BeginInvoke(new Action(() => {
|
||||
var groupList = new List<string>();
|
||||
foreach(var i in this.clb_group.CheckedItems) {
|
||||
groupList.Add(i?.ToString());
|
||||
}
|
||||
this.Settings.SetValue(_groupListSelectedKey,groupList);
|
||||
RefreshKeyList();
|
||||
}));
|
||||
};
|
||||
@ -76,11 +85,17 @@ namespace PrivateBox
|
||||
if(!clbi.Contains(_defaultGroupName)) {
|
||||
clbi.Add(_defaultGroupName);
|
||||
}
|
||||
if(clbi.Count == 1) {
|
||||
var sels = this.Settings.GetValue<List<string>>(_groupListSelectedKey) ?? new List<string>();
|
||||
this.EnableRefreshKeyList = false;
|
||||
this.clb_group.SetItemChecked(0,true);
|
||||
this.EnableRefreshKeyList = true;
|
||||
for(int i = 0;i < clbi.Count;i++) {
|
||||
var val = clbi[i].ToString();
|
||||
var ckd = sels.Contains(val);
|
||||
this.clb_group.SetItemChecked(i,ckd);
|
||||
}
|
||||
if(clbi.Count == 1) {
|
||||
this.clb_group.SetItemChecked(0,true);
|
||||
}
|
||||
this.EnableRefreshKeyList = true;
|
||||
RefreshKeyList();
|
||||
}
|
||||
/// <summary>
|
||||
|
||||
@ -16,6 +16,7 @@ namespace PrivateBox
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
var appConfig = new AppConfig();
|
||||
services.AddSingleton(appConfig);
|
||||
services.AddSingleton<AppRunTimeSettings>();
|
||||
AddServices(services,appConfig);
|
||||
using ServiceProvider provider = GetServiceProvider(services);
|
||||
var mainForm = provider.GetRequiredService<MainForm>();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user