-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigWindow.cs
More file actions
127 lines (111 loc) · 4.16 KB
/
Copy pathConfigWindow.cs
File metadata and controls
127 lines (111 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using GithubKookBot.Models;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.Text.Json;
namespace GithubKookBot;
public partial class ConfigWindow : Form
{
private readonly ConfigSettings _globalSettingRef;
private readonly IServiceProvider _sp;
private readonly BindingList<SubscriptionConfig> _subBindList = new();
private readonly BindingSource _subBind = new();
public ConfigWindow(ConfigSettings sourceRef, IServiceProvider sp)
{
InitializeComponent();
_globalSettingRef = sourceRef;
_sp = sp;
dgvSub.AutoGenerateColumns = false;
colOwner.DataPropertyName = "Owner";
colRepo.DataPropertyName = "Repo";
colDisp.DataPropertyName = "DisplayName";
_subBind.DataSource = _subBindList;
dgvSub.DataSource = _subBind;
LoadDataToUi();
}
private void LoadDataToUi()
{
var s = _globalSettingRef;
txtGithubToken.Text = s.GithubToken;
txtKookToken.Text = s.KookBotToken;
txtKookChannelId.Text = s.KookChannelId;
nudInterval.Value = s.CheckIntervalMinutes;
cmbMode.SelectedItem = s.UpdateMode;
txtBaiduKey.Text = s.BaiduApiKey;
txtBaiduSec.Text = s.BaiduSecretKey;
nudFetch.Value = s.FetchCommitCount;
_subBindList.Clear();
foreach (var sub in s.Subscriptions)
_subBindList.Add(sub);
}
private void SyncUiToGlobalSettingAndSave()
{
_globalSettingRef.GithubToken = txtGithubToken.Text.Trim();
_globalSettingRef.KookBotToken = txtKookToken.Text.Trim();
_globalSettingRef.KookChannelId = txtKookChannelId.Text.Trim();
_globalSettingRef.CheckIntervalMinutes = (int)nudInterval.Value;
_globalSettingRef.UpdateMode = cmbMode.SelectedItem?.ToString() ?? "Release";
_globalSettingRef.BaiduApiKey = txtBaiduKey.Text.Trim();
_globalSettingRef.BaiduSecretKey = txtBaiduSec.Text.Trim();
_globalSettingRef.FetchCommitCount = (int)nudFetch.Value;
_globalSettingRef.Subscriptions = _subBindList.ToList();
SaveConfigToDisk();
FileLogger.Info("配置已实时保存并同步至主程序内存");
}
private void SaveConfigToDisk()
{
string path = Path.Combine(AppContext.BaseDirectory, "config.json");
var root = new
{
Logging = new
{
LogLevel = new
{
Default = "Information",
Microsoft = "Warning"
}
},
Config = _globalSettingRef
};
string json = JsonSerializer.Serialize(root, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(path, json);
}
private void btnSaveAll_Click(object sender, EventArgs e)
{
SyncUiToGlobalSettingAndSave();
MessageBox.Show("配置已保存,立即生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnAddSub_Click(object sender, EventArgs e)
{
_subBindList.Add(new SubscriptionConfig());
SyncUiToGlobalSettingAndSave();
}
private void btnDelSub_Click(object sender, EventArgs e)
{
var toDelete = new List<SubscriptionConfig>();
foreach (DataGridViewRow row in dgvSub.SelectedRows)
{
if (row.DataBoundItem is SubscriptionConfig sub)
{
toDelete.Add(sub);
}
}
if (toDelete.Count == 0)
{
MessageBox.Show("请先选中要删除的订阅行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
var confirm = MessageBox.Show($"确定删除选中的 {toDelete.Count} 条订阅?", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (confirm != DialogResult.Yes)
return;
foreach (var item in toDelete)
{
_subBindList.Remove(item);
}
SyncUiToGlobalSettingAndSave();
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
SyncUiToGlobalSettingAndSave();
base.OnFormClosed(e);
}
}