From 3fe2d0fea426baec85e7258ff1e193cb3824f4df Mon Sep 17 00:00:00 2001 From: Falcon <12919280+falconfly@user.noreply.gitee.com> Date: Fri, 14 Aug 2026 10:06:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=90=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=AE=A1=E7=90=86=EF=BC=8C=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=88=86=E7=BB=84=E9=80=89=E6=8B=A9=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrivateBox/AppRunTimeSettings.cs | 81 ++++++++++++++++++++++ PrivateBox/DataContext/AppSettingsTable.cs | 21 ++++++ PrivateBox/DataContext/DbContext.cs | 1 + PrivateBox/Forms/MainForm.cs | 23 ++++-- PrivateBox/Program.cs | 1 + 5 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 PrivateBox/AppRunTimeSettings.cs create mode 100644 PrivateBox/DataContext/AppSettingsTable.cs diff --git a/PrivateBox/AppRunTimeSettings.cs b/PrivateBox/AppRunTimeSettings.cs new file mode 100644 index 0000000..6234955 --- /dev/null +++ b/PrivateBox/AppRunTimeSettings.cs @@ -0,0 +1,81 @@ +using PrivateBox.DataContext; +using System.Text.Json; + +namespace PrivateBox +{ + /// + /// 应用程序运行时配置 + /// + public class AppRunTimeSettings + { + /// + /// 数据库上下文 + /// + public DbContext Db { get; set; } + /// + /// 构造 + /// + /// 数据库上下文 + public AppRunTimeSettings(DbContext db) { + this.Db = db; + } + /// + /// 设置配置值。 + /// + /// 唯一键 + /// 值 + /// 实际设置的值 + /// 键不能为空 + 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; + } + + /// + /// 设置配置对象值 + /// + /// 值类型 + /// 唯一键 + /// 值 + /// 实际设置的字符串值 + public string SetValue(string key,TValue value) { + var str = JsonSerializer.Serialize(value); + return SetValue(key,str); + } + /// + /// 获取键值 + /// + /// 配置的键 + /// 配置的值 + /// + public string? GetValue(string? key) { + if(string.IsNullOrEmpty(key)) { + throw new ArgumentNullException(nameof(key)); + } + var qu = this.Db.Queryable() + .Where(a => a.Key == key) + .ToList(); + return qu.Any() ? qu.First().Value : string.Empty; + } + + /// + /// 获取配置的对象值 + /// + /// 值类型 + /// 唯一键 + /// 配置对象 + public TValue? GetValue(string? key) { + var str = GetValue(key); + return JsonSerializer.Deserialize(str); + } + } +} diff --git a/PrivateBox/DataContext/AppSettingsTable.cs b/PrivateBox/DataContext/AppSettingsTable.cs new file mode 100644 index 0000000..18d0cfa --- /dev/null +++ b/PrivateBox/DataContext/AppSettingsTable.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; + +namespace PrivateBox.DataContext +{ + /// + /// 应用程序配置 + /// + public class AppSettingsTable + { + /// + /// 应用配置 + /// + [Key, Required, MaxLength(50)] + public string? Key { get; set; } + /// + /// 配置值 + /// + [MaxLength(4000)] + public string? Value { get; set; } + } +} diff --git a/PrivateBox/DataContext/DbContext.cs b/PrivateBox/DataContext/DbContext.cs index e8d3fc8..c049ea1 100644 --- a/PrivateBox/DataContext/DbContext.cs +++ b/PrivateBox/DataContext/DbContext.cs @@ -38,6 +38,7 @@ namespace PrivateBox this.CodeFirst.SetStringDefaultLength(200); this.CodeFirst.InitTables(); this.CodeFirst.InitTables(); + this.CodeFirst.InitTables(); IsInited = true; } diff --git a/PrivateBox/Forms/MainForm.cs b/PrivateBox/Forms/MainForm.cs index 9fdda91..dbd8325 100644 --- a/PrivateBox/Forms/MainForm.cs +++ b/PrivateBox/Forms/MainForm.cs @@ -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; } /// /// 默认分组名 /// private const string _defaultGroupName = "未分组"; + private const string _groupListSelectedKey = "MainForm_GroupList_Selected"; + /// /// 允许刷新Key列表 /// @@ -25,6 +28,7 @@ namespace PrivateBox InitializeComponent(); this.Encrytion = provider.GetRequiredService(); this.Db = provider.GetRequiredService(); + this.Settings = provider.GetRequiredService(); 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(); + 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) { - this.EnableRefreshKeyList = false; - this.clb_group.SetItemChecked(0,true); - this.EnableRefreshKeyList = true; + var sels = this.Settings.GetValue>(_groupListSelectedKey) ?? new List(); + this.EnableRefreshKeyList = false; + 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(); } /// diff --git a/PrivateBox/Program.cs b/PrivateBox/Program.cs index 497cea8..2469f44 100644 --- a/PrivateBox/Program.cs +++ b/PrivateBox/Program.cs @@ -16,6 +16,7 @@ namespace PrivateBox IServiceCollection services = new ServiceCollection(); var appConfig = new AppConfig(); services.AddSingleton(appConfig); + services.AddSingleton(); AddServices(services,appConfig); using ServiceProvider provider = GetServiceProvider(services); var mainForm = provider.GetRequiredService();