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();