增加运行时配置管理,可以记录分组选择状态

This commit is contained in:
Falcon 2026-08-14 10:06:18 +08:00
parent 7357c86238
commit 3fe2d0fea4
5 changed files with 123 additions and 4 deletions

View 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);
}
}
}

View 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; }
}
}

View File

@ -38,6 +38,7 @@ namespace PrivateBox
this.CodeFirst.SetStringDefaultLength(200); this.CodeFirst.SetStringDefaultLength(200);
this.CodeFirst.InitTables<CryptionKey>(); this.CodeFirst.InitTables<CryptionKey>();
this.CodeFirst.InitTables<CrytionItem>(); this.CodeFirst.InitTables<CrytionItem>();
this.CodeFirst.InitTables<AppSettingsTable>();
IsInited = true; IsInited = true;
} }

View File

@ -10,12 +10,15 @@ namespace PrivateBox
public IEncryptionService Encrytion { get; init; } public IEncryptionService Encrytion { get; init; }
public DbContext Db { get; init; } public DbContext Db { get; init; }
public string CrytionKey { get; init; } public string CrytionKey { get; init; }
public AppRunTimeSettings Settings { get; set; }
/// <summary> /// <summary>
/// 默认分组名 /// 默认分组名
/// </summary> /// </summary>
private const string _defaultGroupName = "未分组"; private const string _defaultGroupName = "未分组";
private const string _groupListSelectedKey = "MainForm_GroupList_Selected";
/// <summary> /// <summary>
/// 允许刷新Key列表 /// 允许刷新Key列表
/// </summary> /// </summary>
@ -25,6 +28,7 @@ namespace PrivateBox
InitializeComponent(); InitializeComponent();
this.Encrytion = provider.GetRequiredService<IEncryptionService>(); this.Encrytion = provider.GetRequiredService<IEncryptionService>();
this.Db = provider.GetRequiredService<DbContext>(); this.Db = provider.GetRequiredService<DbContext>();
this.Settings = provider.GetRequiredService<AppRunTimeSettings>();
this.CrytionKey = this.Db.GetCrytionKey(); this.CrytionKey = this.Db.GetCrytionKey();
this.Load += (s,d) => { this.Load += (s,d) => {
@ -35,6 +39,11 @@ namespace PrivateBox
this.lbKeyList.SelectedIndexChanged += LbKeyList_SelectedIndexChanged; this.lbKeyList.SelectedIndexChanged += LbKeyList_SelectedIndexChanged;
this.clb_group.ItemCheck += (s,e) => { this.clb_group.ItemCheck += (s,e) => {
this.BeginInvoke(new Action(() => { 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(); RefreshKeyList();
})); }));
}; };
@ -76,11 +85,17 @@ namespace PrivateBox
if(!clbi.Contains(_defaultGroupName)) { if(!clbi.Contains(_defaultGroupName)) {
clbi.Add(_defaultGroupName); clbi.Add(_defaultGroupName);
} }
if(clbi.Count == 1) { var sels = this.Settings.GetValue<List<string>>(_groupListSelectedKey) ?? new List<string>();
this.EnableRefreshKeyList = false; this.EnableRefreshKeyList = false;
this.clb_group.SetItemChecked(0,true); for(int i = 0;i < clbi.Count;i++) {
this.EnableRefreshKeyList = true; 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(); RefreshKeyList();
} }
/// <summary> /// <summary>

View File

@ -16,6 +16,7 @@ namespace PrivateBox
IServiceCollection services = new ServiceCollection(); IServiceCollection services = new ServiceCollection();
var appConfig = new AppConfig(); var appConfig = new AppConfig();
services.AddSingleton(appConfig); services.AddSingleton(appConfig);
services.AddSingleton<AppRunTimeSettings>();
AddServices(services,appConfig); AddServices(services,appConfig);
using ServiceProvider provider = GetServiceProvider(services); using ServiceProvider provider = GetServiceProvider(services);
var mainForm = provider.GetRequiredService<MainForm>(); var mainForm = provider.GetRequiredService<MainForm>();