Skip to content

Commit f93e709

Browse files
committed
fix: edit note bug when list sort by darg.
refactor: improve note edit. close #374
1 parent ec42bbb commit f93e709

File tree

9 files changed

+117
-95
lines changed

9 files changed

+117
-95
lines changed

Ui/Controls/NoteDisplay/NoteDisplayAndEditor.xaml.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using System.Windows.Input;
66
using _1RM.Model;
77
using _1RM.Model.Protocol.Base;
8+
using _1RM.Service.DataSource;
89
using Shawn.Utils;
910
using Shawn.Utils.Wpf;
1011
using Shawn.Utils.Wpf.Controls;
12+
using Stylet;
1113

1214
namespace _1RM.Controls.NoteDisplay
1315
{
@@ -27,6 +29,7 @@ private static void OnServerChanged(DependencyObject d, DependencyPropertyChange
2729
server0.PropertyChanged -= control.ServerOnPropertyChanged;
2830
if (server1 != null)
2931
server1.PropertyChanged += control.ServerOnPropertyChanged;
32+
control.EditEnable = control.EditEnable && server1?.GetDataSource()?.IsWritable == true;
3033
}
3134
}
3235

@@ -48,14 +51,32 @@ public ProtocolBase? Server
4851
public static readonly DependencyProperty CommandOnCloseRequestProperty = DependencyProperty.Register(
4952
"CommandOnCloseRequest", typeof(RelayCommand), typeof(NoteDisplayAndEditor), new FrameworkPropertyMetadata(default(RelayCommand), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
5053

54+
5155
public RelayCommand CommandOnCloseRequest
5256
{
5357
get => (RelayCommand)GetValue(CommandOnCloseRequestProperty);
5458
set => SetValue(CommandOnCloseRequestProperty, value);
5559
}
5660

5761
public bool CloseEnable { get; set; }
58-
public bool EditEnable { get; set; }
62+
63+
private bool _editEnable;
64+
public bool EditEnable
65+
{
66+
get => _editEnable;
67+
set
68+
{
69+
_editEnable = value;
70+
if (IsLoaded)
71+
{
72+
Execute.OnUIThreadSync(() =>
73+
{
74+
ButtonEdit.IsEnabled = EditEnable;
75+
ButtonEdit.Visibility = EditEnable ? Visibility.Visible : Visibility.Collapsed;
76+
});
77+
}
78+
}
79+
}
5980

6081
public NoteDisplayAndEditor()
6182
{
@@ -122,8 +143,8 @@ private void ButtonSave_OnClick(object sender, RoutedEventArgs e)
122143
{
123144
Server.Note = TbMarkdown.Text.Trim();
124145
IoC.Get<GlobalData>().UpdateServer(Server);
125-
EndEdit();
126146
}
147+
EndEdit();
127148
}
128149

129150
private void ButtonCancelEdit_OnClick(object sender, RoutedEventArgs e)

Ui/Controls/NoteDisplay/NoteIcon.xaml.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Windows.Controls;
66
using System.Windows.Input;
77
using _1RM.Model.Protocol.Base;
8+
using _1RM.Service.DataSource;
89
using Shawn.Utils;
10+
using Stylet;
911

1012
namespace _1RM.Controls.NoteDisplay
1113
{
@@ -44,12 +46,23 @@ protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName
4446
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
4547
}
4648

47-
49+
private NoteDisplayAndEditor _noteDisplayAndEditor;
4850
public NoteIcon(ProtocolBase server)
4951
{
5052
Server = server;
5153
InitializeComponent();
5254
IsBriefNoteShown = false;
55+
Execute.OnUIThreadSync(() =>
56+
{
57+
_noteDisplayAndEditor = new NoteDisplayAndEditor()
58+
{
59+
Server = Server,
60+
Width = 400,
61+
Height = 300,
62+
EditEnable = Server.GetDataSource()?.IsWritable == true,
63+
CloseEnable = false,
64+
};
65+
});
5366
}
5467

5568
private void OnMouseMove(object sender, MouseEventArgs args)
@@ -102,14 +115,7 @@ private async void ButtonShowNote_OnMouseEnter(object sender, MouseEventArgs e)
102115
{
103116
if (PopupNoteContent.Content is not NoteDisplayAndEditor)
104117
{
105-
PopupNoteContent.Content = new NoteDisplayAndEditor()
106-
{
107-
Server = Server,
108-
Width = 400,
109-
Height = 300,
110-
EditEnable = true,
111-
CloseEnable = false,
112-
};
118+
PopupNoteContent.Content = _noteDisplayAndEditor;
113119
}
114120
await Task.Yield();
115121
PopupNote.IsOpen = false;
@@ -123,14 +129,7 @@ private async void ButtonBriefNote_OnClick(object sender, RoutedEventArgs e)
123129
{
124130
if (PopupNoteContent.Content is not NoteDisplayAndEditor)
125131
{
126-
PopupNoteContent.Content = new NoteDisplayAndEditor()
127-
{
128-
Server = Server,
129-
Width = 400,
130-
Height = 300,
131-
EditEnable = true,
132-
CloseEnable = false,
133-
};
132+
PopupNoteContent.Content = _noteDisplayAndEditor;
134133
}
135134
await Task.Yield();
136135
PopupNote.IsOpen = false;

Ui/Model/DAO/Dapper/DapperDataBase.cs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public override void CloseConnection()
3737

3838
public IDbConnection? Connection => _dbConnection;
3939

40-
public override void OpenConnection()
40+
protected override bool OpenConnection(bool showErrorAlert = false)
4141
{
4242
if (string.IsNullOrWhiteSpace(_connectionString))
43-
return;
44-
if (IsConnected()) return;
43+
return false;
44+
if (IsConnected()) return true;
4545
lock (this)
4646
{
47-
if (IsConnected()) return;
47+
if (IsConnected()) return true;
4848
if (_dbConnection == null)
4949
{
5050
_dbConnection?.Close();
@@ -70,12 +70,17 @@ public override void OpenConnection()
7070
try
7171
{
7272
_dbConnection.Open();
73+
return IsConnected();
7374
}
7475
catch (Exception e)
7576
{
7677
MsAppCenterHelper.Error(e, new Dictionary<string, string>() { { "_databaseType", _databaseType.ToString() } });
77-
MessageBoxHelper.ErrorAlert("Access Denied: " + e.Message);
78+
if (showErrorAlert)
79+
{
80+
MessageBoxHelper.ErrorAlert("Access Denied: " + e.Message);
81+
}
7882
}
83+
return false;
7984
}
8085
}
8186

@@ -111,8 +116,7 @@ public override bool IsConnected()
111116

112117
public override void InitTables()
113118
{
114-
OpenConnection();
115-
if (!IsConnected()) return;
119+
if(!OpenConnection(true)) return;
116120
_dbConnection?.Execute(@$"
117121
CREATE TABLE IF NOT EXISTS `{Config.TABLE_NAME}` (
118122
`{nameof(Config.Key)}` VARCHAR (64) PRIMARY KEY
@@ -140,8 +144,7 @@ NOT NULL
140144
{
141145
lock (this)
142146
{
143-
OpenConnection();
144-
if (!IsConnected()) return null;
147+
if (!OpenConnection()) return null;
145148
Debug.Assert(id > 0);
146149
var dbServer =
147150
_dbConnection?.QueryFirstOrDefault<Server>(
@@ -155,8 +158,7 @@ NOT NULL
155158
{
156159
lock (this)
157160
{
158-
OpenConnection();
159-
if (!IsConnected()) return null;
161+
if (!OpenConnection()) return null;
160162
#pragma warning disable CS8619
161163
return _dbConnection?.Query<Server>($"SELECT * FROM `{Server.TABLE_NAME}`").Select(x => x?.ToProtocolServerBase()).Where(x => x != null).ToList();
162164
#pragma warning restore CS8619
@@ -167,8 +169,7 @@ public override int GetServerCount()
167169
{
168170
lock (this)
169171
{
170-
OpenConnection();
171-
if (!IsConnected()) return 0;
172+
if (!OpenConnection()) return 0;
172173
return _dbConnection?.ExecuteScalar<int>($"SELECT COUNT(*) FROM `{Server.TABLE_NAME}`") ?? 0;
173174
}
174175
}
@@ -188,8 +189,7 @@ public override string AddServer(ProtocolBase protocolBase)
188189
{
189190
lock (this)
190191
{
191-
OpenConnection();
192-
if (!IsConnected()) return string.Empty;
192+
if (!OpenConnection(true)) return string.Empty;
193193
if (protocolBase.IsTmpSession())
194194
protocolBase.Id = Ulid.NewUlid().ToString();
195195
var server = protocolBase.ToDbServer();
@@ -204,8 +204,7 @@ public override int AddServer(IEnumerable<ProtocolBase> protocolBases)
204204
{
205205
lock (this)
206206
{
207-
OpenConnection();
208-
if (!IsConnected()) return 0;
207+
if (!OpenConnection(true)) return 0;
209208
var rng = new NUlid.Rng.MonotonicUlidRng();
210209
foreach (var protocolBase in protocolBases)
211210
{
@@ -229,8 +228,7 @@ public override bool UpdateServer(ProtocolBase server)
229228
{
230229
lock (this)
231230
{
232-
OpenConnection();
233-
if (!IsConnected()) return false;
231+
if (!OpenConnection(true)) return false;
234232
var ret = _dbConnection?.Execute(SqlUpdate, server.ToDbServer()) > 0;
235233
if (ret)
236234
SetDataUpdateTimestamp();
@@ -242,8 +240,7 @@ public override bool UpdateServer(IEnumerable<ProtocolBase> servers)
242240
{
243241
lock (this)
244242
{
245-
OpenConnection();
246-
if (!IsConnected()) return false;
243+
if (!OpenConnection(true)) return false;
247244
var dbss = servers.Select(x => x.ToDbServer());
248245
var ret = _dbConnection?.Execute(SqlUpdate, dbss) > 0;
249246
if (ret)
@@ -256,8 +253,7 @@ public override bool DeleteServer(string id)
256253
{
257254
lock (this)
258255
{
259-
OpenConnection();
260-
if (!IsConnected()) return false;
256+
if (!OpenConnection(true)) return false;
261257
var ret = _dbConnection?.Execute($@"DELETE FROM `{Server.TABLE_NAME}` WHERE `{nameof(Server.Id)}` = @{nameof(Server.Id)};", new { Id = id }) > 0;
262258
if (ret)
263259
SetDataUpdateTimestamp();
@@ -269,8 +265,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
269265
{
270266
lock (this)
271267
{
272-
OpenConnection();
273-
if (!IsConnected()) return false;
268+
if (!OpenConnection(true)) return false;
274269
var ret = _dbConnection?.Execute($@"DELETE FROM `{Server.TABLE_NAME}` WHERE `{nameof(Server.Id)}` IN @{nameof(Server.Id)};", new { Id = ids }) > 0;
275270
if (ret)
276271
SetDataUpdateTimestamp();
@@ -287,8 +282,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
287282
{
288283
lock (this)
289284
{
290-
OpenConnection();
291-
if (!IsConnected()) return null;
285+
if (!OpenConnection()) return null;
292286
var config = _dbConnection?.QueryFirstOrDefault<Config>($"SELECT * FROM `{Config.TABLE_NAME}` WHERE `{nameof(Config.Key)}` = @{nameof(Config.Key)}",
293287
new { Key = key, });
294288
return config?.Value;
@@ -307,8 +301,7 @@ protected bool SetConfigPrivate(string key, string value)
307301
{
308302
lock (this)
309303
{
310-
OpenConnection();
311-
if (!IsConnected()) return false;
304+
if (!OpenConnection(true)) return false;
312305
var existed = GetConfigPrivate(key) != null;
313306
return _dbConnection?.Execute(existed ? SqlUpdateConfig : SqlInsertConfig, new { Key = key, Value = value, }) > 0;
314307
}

Ui/Model/DAO/IDataBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum DatabaseType
2323
public abstract class IDatabase
2424
{
2525
public abstract void CloseConnection();
26-
public abstract void OpenConnection();
26+
protected abstract bool OpenConnection(bool showErrorAlert = false);
2727
public abstract void OpenNewConnection(DatabaseType type, string newConnectionString);
2828

2929
public abstract bool IsConnected();

Ui/Model/GlobalData.cs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,6 @@ public GlobalData(ConfigurationService configurationService)
3434
_timer.Start();
3535
}
3636

37-
private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
38-
{
39-
try
40-
{
41-
var mainWindowViewModel = IoC.Get<MainWindowViewModel>();
42-
var listPageViewModel = IoC.Get<ServerListPageViewModel>();
43-
var launcherWindowViewModel = IoC.Get<LauncherWindowViewModel>();
44-
// do not reload when any selected / launcher is shown / editor view is show
45-
if (mainWindowViewModel.EditorViewModel != null
46-
|| listPageViewModel.VmServerList.Any(x => x.IsSelected)
47-
|| launcherWindowViewModel.View?.IsVisible == true)
48-
{
49-
return;
50-
}
51-
52-
if (ReloadServerList())
53-
{
54-
SimpleLogHelper.Debug("check database update - reload data");
55-
}
56-
else
57-
{
58-
SimpleLogHelper.Debug("check database update - no need reload");
59-
}
60-
}
61-
finally
62-
{
63-
lock (this)
64-
{
65-
if (_timerStopFlag == false && _configurationService.DatabaseCheckPeriod > 0)
66-
{
67-
_timer.Interval = _configurationService.DatabaseCheckPeriod * 1000;
68-
_timer.Start();
69-
}
70-
}
71-
System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet;
72-
}
73-
}
74-
7537
private DataSourceService? _sourceService;
7638
private readonly ConfigurationService _configurationService;
7739

@@ -383,5 +345,42 @@ public void StartTick()
383345
}
384346
}
385347
}
348+
349+
private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
350+
{
351+
try
352+
{
353+
var mainWindowViewModel = IoC.Get<MainWindowViewModel>();
354+
var listPageViewModel = IoC.Get<ServerListPageViewModel>();
355+
var launcherWindowViewModel = IoC.Get<LauncherWindowViewModel>();
356+
// do not reload when any selected / launcher is shown / editor view is show
357+
if (mainWindowViewModel.EditorViewModel != null
358+
|| listPageViewModel.VmServerList.Any(x => x.IsSelected)
359+
|| launcherWindowViewModel.View?.IsVisible == true)
360+
{
361+
return;
362+
}
363+
364+
if (ReloadServerList())
365+
{
366+
SimpleLogHelper.Debug("check database update - reload data" + _timer.GetHashCode());
367+
}
368+
else
369+
{
370+
SimpleLogHelper.Debug("check database update - no need reload" + _timer.GetHashCode());
371+
}
372+
}
373+
finally
374+
{
375+
lock (this)
376+
{
377+
if (_timerStopFlag == false && _configurationService.DatabaseCheckPeriod > 0)
378+
{
379+
_timer.Start();
380+
}
381+
}
382+
System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet;
383+
}
384+
}
386385
}
387386
}

0 commit comments

Comments
 (0)