Skip to content

Commit a447f0d

Browse files
committed
fix: IsWritable do not change after edit datasource #282.
1 parent 0432aad commit a447f0d

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

Ui/Model/GlobalData.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public void AddServer(ProtocolBase protocolServer, DataSourceBase dataSource)
187187
VmItemList.Add(@new);
188188
IoC.Get<ServerListPageViewModel>()?.AppendServer(@new); // invoke main list ui change
189189
IoC.Get<ServerSelectionsViewModel>()?.AppendServer(@new); // invoke launcher ui change
190+
ReadTagsFromServers();
190191
}
191192
else
192193
{
@@ -209,6 +210,7 @@ public void UpdateServer(ProtocolBase protocolServer)
209210
var old = VmItemList.First(x => x.Id == protocolServer.Id && x.Server.DataSourceName == source.DataSourceName);
210211
// invoke main list ui change & invoke launcher ui change
211212
old.Server = protocolServer;
213+
ReadTagsFromServers();
212214
}
213215
else
214216
{
@@ -247,6 +249,10 @@ public void UpdateServer(IEnumerable<ProtocolBase> protocolServers)
247249
{
248250
ReloadServerList();
249251
}
252+
else
253+
{
254+
ReadTagsFromServers();
255+
}
250256
StartTick();
251257
}
252258

@@ -267,6 +273,7 @@ public void DeleteServer(ProtocolBase protocolServer)
267273
VmItemList.Remove(old);
268274
IoC.Get<ServerListPageViewModel>()?.VmServerList?.Remove(old); // invoke main list ui change
269275
IoC.Get<ServerSelectionsViewModel>()?.VmServerList?.Remove(old); // invoke launcher ui change
276+
ReadTagsFromServers();
270277
}
271278
else
272279
{
@@ -304,6 +311,10 @@ public void DeleteServer(IEnumerable<ProtocolBase> protocolServers)
304311
{
305312
ReloadServerList();
306313
}
314+
else
315+
{
316+
ReadTagsFromServers();
317+
}
307318
StartTick();
308319
}
309320

Ui/Service/DataSource/DataSourceService.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,16 @@ public EnumDbStatus AddOrUpdateDataSource(DataSourceBase config, int connectTime
113113
return InitLocalDataSource(localConfig);
114114
}
115115

116+
// remove the old one
116117
var olds = AdditionalSources.Where(x => x.Value == config);
117118
foreach (var pair in olds)
118119
{
119120
AdditionalSources.TryRemove(pair.Key, out var _);
120121
}
122+
AdditionalSources.TryRemove(config.DataSourceName, out var _);
121123

122-
// remove the old one
123-
if (AdditionalSources.TryRemove(config.DataSourceName, out var old))
124-
{
125-
old?.Database_CloseConnection();
126-
}
127124

125+
config.Database_CloseConnection();
128126
var ret = config.Database_SelfCheck();
129127
AdditionalSources.AddOrUpdate(config.DataSourceName, config, (name, source) => config);
130128
return ret;

Ui/Service/DataSource/Model/DataSourceBase.Source.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ namespace _1RM.Service.DataSource.Model
1818
{
1919
public abstract partial class DataSourceBase : NotifyPropertyChangedBase
2020
{
21-
protected bool _isWritable = true;
21+
private bool _isWritable = true;
2222
[JsonIgnore]
23-
public bool IsWritable => _isWritable;
23+
public bool IsWritable
24+
{
25+
get => _isWritable;
26+
protected set => SetAndNotifyIfChanged(ref _isWritable, value);
27+
}
2428

2529
/// <summary>
2630
/// 已缓存的服务器信息
@@ -97,7 +101,15 @@ public bool Database_OpenConnection(int connectTimeOutSeconds = 5)
97101
var dataBase = GetDataBase();
98102
// open db or create db.
99103
Debug.Assert(dataBase != null);
100-
dataBase.OpenNewConnection(DatabaseType, GetConnectionString(connectTimeOutSeconds));
104+
105+
var connectionString = GetConnectionString(connectTimeOutSeconds);
106+
if (connectionString != _lastConnectionString)
107+
{
108+
dataBase.CloseConnection();
109+
_lastConnectionString = connectionString;
110+
}
111+
112+
dataBase.OpenNewConnection(DatabaseType, connectionString);
101113
try
102114
{
103115
dataBase.InitTables();
@@ -106,7 +118,7 @@ public bool Database_OpenConnection(int connectTimeOutSeconds = 5)
106118
{
107119
SimpleLogHelper.Warning(e);
108120
}
109-
dataBase.OpenNewConnection(DatabaseType, GetConnectionString(connectTimeOutSeconds));
121+
dataBase.OpenNewConnection(DatabaseType, connectionString);
110122
if (dataBase.IsConnected())
111123
{
112124
if (Status != EnumDbStatus.NotConnectedYet)
@@ -133,17 +145,26 @@ public virtual void Database_CloseConnection()
133145
dataBase.CloseConnection();
134146
}
135147

148+
149+
private static string _lastConnectionString = "";
136150
public virtual EnumDbStatus Database_SelfCheck(int connectTimeOutSeconds = 5)
137151
{
138152
EnumDbStatus ret = EnumDbStatus.NotConnectedYet;
139153
var dataBase = GetDataBase();
140154

155+
var connectionString = GetConnectionString(connectTimeOutSeconds);
156+
if (connectionString != _lastConnectionString)
157+
{
158+
dataBase.CloseConnection();
159+
_lastConnectionString = connectionString;
160+
}
161+
141162
// check connectable
142163
if (dataBase.IsConnected() == false)
143164
{
144165
try
145166
{
146-
dataBase.OpenNewConnection(DatabaseType, GetConnectionString(connectTimeOutSeconds));
167+
dataBase.OpenNewConnection(DatabaseType, connectionString);
147168
}
148169
catch (Exception e)
149170
{
@@ -174,7 +195,7 @@ public virtual EnumDbStatus Database_SelfCheck(int connectTimeOutSeconds = 5)
174195
}
175196

176197
// check readable
177-
_isWritable = dataBase.CheckWritable();
198+
IsWritable = dataBase.CheckWritable();
178199
var isReadable = dataBase.CheckReadable();
179200
if (isReadable == false)
180201
{

Ui/Service/DataSource/Model/SqliteSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public string Path
2323
var newValue = value.Replace(Environment.CurrentDirectory, ".");
2424
SetAndNotifyIfChanged(ref _path, newValue);
2525
var fi = new FileInfo(Path);
26-
_isWritable = fi.IsReadOnly == false;
26+
IsWritable = fi.IsReadOnly == false;
2727
}
2828
}
2929

Ui/View/ServerList/ServerListPageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public RelayCommand CmdImportFromJson
446446
}
447447
}
448448
source.Database_InsertServer(list);
449-
AppData.ReloadServerList();
449+
AppData.ReloadServerList(true);
450450
GlobalEventHelper.ShowProcessingRing?.Invoke(Visibility.Collapsed, "");
451451
Execute.OnUIThread(() =>
452452
{

0 commit comments

Comments
 (0)