Skip to content

Commit bd4fa3a

Browse files
authored
Improve profile refresh (#1390)
* Improve refresh * Improve refresh
1 parent 5c2ded1 commit bd4fa3a

File tree

5 files changed

+61
-96
lines changed

5 files changed

+61
-96
lines changed

Source/NETworkManager.Profiles/IProfileManager.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace NETworkManager.Profiles
66
public interface IProfileManager
77
{
88
ICollectionView Profiles { get; }
9-
void RefreshProfiles();
109
void OnProfileDialogOpen();
1110
void OnProfileDialogClose();
1211
ICommand AddProfileCommand { get; }

Source/NETworkManager.Profiles/ProfileManager.cs

+38-20
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,7 @@ public static void AddGroups(List<GroupInfo> groups)
682682
/// <param name="group"></param>
683683
public static void AddGroup(GroupInfo group)
684684
{
685-
// Possible fix for appcrash --> when icollection view is refreshed...
686-
//System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
687-
//{
688-
//lock (Profiles)
689685
Groups.Add(group);
690-
//}));
691686

692687
ProfilesUpdated();
693688
}
@@ -697,18 +692,21 @@ public static GroupInfo GetGroup(string name)
697692
return Groups.First(x => x.Name.Equals(name));
698693
}
699694

695+
public static void ReplaceGroup(GroupInfo oldGroup, GroupInfo newGroup)
696+
{
697+
Groups.Remove(oldGroup);
698+
Groups.Add(newGroup);
699+
700+
ProfilesUpdated();
701+
}
702+
700703
/// <summary>
701704
///
702705
/// </summary>
703706
/// <param name="group"></param>
704707
public static void RemoveGroup(GroupInfo group)
705708
{
706-
// Possible fix for appcrash --> when icollection view is refreshed...
707-
//System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
708-
//{
709-
//lock (Profiles)
710709
Groups.Remove(group);
711-
//}));
712710

713711
ProfilesUpdated();
714712
}
@@ -725,7 +723,7 @@ public static List<string> GetGroupNames()
725723
list.Add(groups.Name);
726724

727725
return list;
728-
}
726+
}
729727

730728
/// <summary>
731729
/// Method to check if a profile exists.
@@ -761,28 +759,48 @@ public static bool IsGroupEmpty(string name)
761759
/// <param name="profile"><see cref="ProfileInfo"/> to add.</param>
762760
public static void AddProfile(ProfileInfo profile)
763761
{
764-
string group = profile.Group;
765-
766-
if (!GroupExists(group))
767-
AddGroup(new GroupInfo(group));
762+
if (!GroupExists(profile.Group))
763+
AddGroup(new GroupInfo(profile.Group));
768764

769765
Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Add(profile);
770766

771767
ProfilesUpdated();
772768
}
773769

770+
public static void ReplaceProfile(ProfileInfo oldProfile, ProfileInfo newProfile)
771+
{
772+
// Remove
773+
Groups.First(x => x.Name.Equals(oldProfile.Group)).Profiles.Remove(oldProfile);
774+
775+
// Add
776+
if (!GroupExists(newProfile.Group))
777+
AddGroup(new GroupInfo(newProfile.Group));
778+
779+
Groups.First(x => x.Name.Equals(newProfile.Group)).Profiles.Add(newProfile);
780+
781+
// Notify
782+
ProfilesUpdated();
783+
}
784+
774785
/// <summary>
775786
/// Remove a profile from a group.
776787
/// </summary>
777788
/// <param name="profile"><see cref="ProfileInfo"/> to remove.</param>
778789
public static void RemoveProfile(ProfileInfo profile)
779790
{
780-
string group = profile.Group;
791+
Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Remove(profile);
792+
793+
ProfilesUpdated();
794+
}
781795

782-
Groups.First(x => x.Name.Equals(group)).Profiles.Remove(profile);
783-
784-
//if (IsGroupEmpty(group))
785-
// RemoveGroup(GetGroup(group));
796+
/// <summary>
797+
/// Remove profiles from a group.
798+
/// </summary>
799+
/// <param name="profile"><see cref="ProfileInfo"/> to remove.</param>
800+
public static void RemoveProfiles(IList<ProfileInfo> profiles)
801+
{
802+
foreach (ProfileInfo profile in profiles)
803+
Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Remove(profile);
786804

787805
ProfilesUpdated();
788806
}

Source/NETworkManager/ProfileDialogManager.cs

+13-39
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public static async Task ShowAddProfileDialog(IProfileManager viewModel, IDialog
3030
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
3131
viewModel.OnProfileDialogClose();
3232

33-
AddProfile(instance);
34-
35-
viewModel.RefreshProfiles();
33+
ProfileManager.AddProfile(ParseProfileInfo(instance));
3634
}, async instance =>
3735
{
3836
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -62,10 +60,7 @@ public static async Task ShowEditProfileDialog(IProfileManager viewModel, IDialo
6260
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
6361
viewModel.OnProfileDialogClose();
6462

65-
RemoveProfile(profile);
66-
AddProfile(instance);
67-
68-
viewModel.RefreshProfiles();
63+
ProfileManager.ReplaceProfile(profile, ParseProfileInfo(instance));
6964
}, async instance =>
7065
{
7166
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -94,9 +89,7 @@ public static async Task ShowCopyAsProfileDialog(IProfileManager viewModel, IDia
9489
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
9590
viewModel.OnProfileDialogClose();
9691

97-
AddProfile(instance);
98-
99-
viewModel.RefreshProfiles();
92+
ProfileManager.AddProfile(ParseProfileInfo(instance));
10093
}, async instance =>
10194
{
10295
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -124,10 +117,8 @@ public static async Task ShowDeleteProfileDialog(IProfileManager viewModel, IDia
124117
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
125118
viewModel.OnProfileDialogClose();
126119

127-
foreach (var profile in profiles)
128-
RemoveProfile(profile);
120+
ProfileManager.RemoveProfiles(profiles);
129121

130-
viewModel.RefreshProfiles();
131122
}, async instance =>
132123
{
133124
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -158,9 +149,7 @@ public static async Task ShowAddGroupDialog(IProfileManager viewModel, IDialogCo
158149
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
159150
viewModel.OnProfileDialogClose();
160151

161-
AddGroup(instance);
162-
163-
viewModel.RefreshProfiles();
152+
ProfileManager.AddGroup(ParseGroupInfo(instance));
164153
}, async instance =>
165154
{
166155
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -189,10 +178,7 @@ public static async Task ShowEditGroupDialog(IProfileManager viewModel, IDialogC
189178
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
190179
viewModel.OnProfileDialogClose();
191180

192-
RemoveGroup(instance.Group);
193-
AddGroup(instance);
194-
195-
viewModel.RefreshProfiles();
181+
ProfileManager.ReplaceGroup(instance.Group, ParseGroupInfo(instance));
196182
}, async instance =>
197183
{
198184
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -220,9 +206,7 @@ public static async Task ShowDeleteGroupDialog(IProfileManager viewModel, IDialo
220206
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
221207
viewModel.OnProfileDialogClose();
222208

223-
RemoveGroup(group);
224-
225-
viewModel.RefreshProfiles();
209+
ProfileManager.RemoveGroup(group);
226210
}, async instance =>
227211
{
228212
await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog);
@@ -240,9 +224,9 @@ public static async Task ShowDeleteGroupDialog(IProfileManager viewModel, IDialo
240224
#endregion
241225

242226
#region Methods to add and remove profile
243-
public static void AddProfile(ProfileViewModel instance)
227+
public static ProfileInfo ParseProfileInfo(ProfileViewModel instance)
244228
{
245-
ProfileManager.AddProfile(new ProfileInfo
229+
return new ProfileInfo
246230
{
247231
Name = instance.Name.Trim(),
248232
Host = instance.Host.Trim(),
@@ -383,17 +367,12 @@ public static void AddProfile(ProfileViewModel instance)
383367
Whois_Enabled = instance.Whois_Enabled,
384368
Whois_InheritHost = instance.Whois_InheritHost,
385369
Whois_Domain = instance.Whois_InheritHost ? instance.Host?.Trim() : instance.Whois_Domain?.Trim()
386-
});
387-
}
388-
389-
public static void RemoveProfile(ProfileInfo profile)
390-
{
391-
ProfileManager.RemoveProfile(profile);
370+
};
392371
}
393372
#endregion
394373

395374
#region Methods to add and remove group
396-
public static void AddGroup(GroupViewModel instance)
375+
public static GroupInfo ParseGroupInfo(GroupViewModel instance)
397376
{
398377
List<ProfileInfo> profiles = instance.Group.Profiles;
399378

@@ -413,7 +392,7 @@ public static void AddGroup(GroupViewModel instance)
413392
}
414393
}
415394

416-
ProfileManager.AddGroup(new GroupInfo
395+
return new GroupInfo
417396
{
418397
Name = name,
419398

@@ -498,12 +477,7 @@ public static void AddGroup(GroupViewModel instance)
498477
TigerVNC_Enabled = instance.TigerVNC_Enabled,
499478
TigerVNC_OverridePort = instance.TigerVNC_OverridePort,
500479
TigerVNC_Port = instance.TigerVNC_Port
501-
});
502-
}
503-
504-
public static void RemoveGroup(GroupInfo group)
505-
{
506-
ProfileManager.RemoveGroup(group);
480+
};
507481
}
508482
#endregion
509483
}

Source/NETworkManager/ViewModels/IPScannerViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private async Task AddProfileSelectedHostAction()
342342
{
343343
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
344344

345-
ProfileDialogManager.AddProfile(instance);
345+
ProfileManager.AddProfile(ProfileDialogManager.ParseProfileInfo(instance));
346346
}, instance =>
347347
{
348348
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);

Source/NETworkManager/ViewModels/ProfilesViewModel.cs

+9-35
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public ICollectionView Groups
3434
}
3535
}
3636

37-
private string _lastSelectedGroup;
38-
3937
private GroupInfo _selectedGroup = new GroupInfo();
4038
public GroupInfo SelectedGroup
4139
{
@@ -70,8 +68,6 @@ public ICollectionView Profiles
7068
}
7169
}
7270

73-
private string _lastSelectedProfile;
74-
7571
private ProfileInfo _selectedProfile = new ProfileInfo();
7672
public ProfileInfo SelectedProfile
7773
{
@@ -180,10 +176,7 @@ public void SetProfilesView(string groupName)
180176
};
181177

182178
// Select first profile, or the last selected profile
183-
if (string.IsNullOrEmpty(_lastSelectedProfile))
184179
SelectedProfile = Profiles.SourceCollection.Cast<ProfileInfo>().OrderBy(x => x.Name).FirstOrDefault();
185-
else
186-
SelectedProfile = Profiles.SourceCollection.Cast<ProfileInfo>().FirstOrDefault(x => x.Name.Equals(_lastSelectedProfile));
187180

188181
SelectedProfiles = new List<ProfileInfo> { SelectedProfile }; // Fix --> Count need to be 1 for EditProfile_CanExecute
189182
}
@@ -194,9 +187,6 @@ public void SetProfilesView(string groupName)
194187

195188
private void AddProfileAction()
196189
{
197-
_lastSelectedGroup = SelectedGroup?.Name;
198-
_lastSelectedProfile = SelectedProfile?.Name;
199-
200190
ProfileDialogManager.ShowAddProfileDialog(this, _dialogCoordinator, SelectedGroup?.Name);
201191
}
202192

@@ -206,59 +196,41 @@ private void AddProfileAction()
206196

207197
private void EditProfileAction()
208198
{
209-
_lastSelectedGroup = SelectedGroup?.Name;
210-
_lastSelectedProfile = SelectedProfile?.Name;
211-
212199
ProfileDialogManager.ShowEditProfileDialog(this, _dialogCoordinator, SelectedProfile);
213200
}
214201

215202
public ICommand CopyAsProfileCommand => new RelayCommand(p => CopyAsProfileAction());
216203

217204
private void CopyAsProfileAction()
218205
{
219-
_lastSelectedGroup = SelectedGroup?.Name;
220-
_lastSelectedProfile = SelectedProfile?.Name;
221-
222206
ProfileDialogManager.ShowCopyAsProfileDialog(this, _dialogCoordinator, SelectedProfile);
223207
}
224208

225209
public ICommand DeleteProfileCommand => new RelayCommand(p => DeleteProfileAction());
226210

227211
private void DeleteProfileAction()
228212
{
229-
_lastSelectedGroup = SelectedGroup?.Name;
230-
_lastSelectedProfile = null;
231-
232213
ProfileDialogManager.ShowDeleteProfileDialog(this, _dialogCoordinator, new List<ProfileInfo>(SelectedProfiles.Cast<ProfileInfo>()));
233214
}
234215

235216
public ICommand AddGroupCommand => new RelayCommand(p => AddGroupAction());
236217

237218
private void AddGroupAction()
238219
{
239-
_lastSelectedGroup = SelectedGroup?.Name;
240-
_lastSelectedProfile = SelectedProfile?.Name;
241-
242220
ProfileDialogManager.ShowAddGroupDialog(this, _dialogCoordinator);
243221
}
244222

245223
public ICommand EditGroupCommand => new RelayCommand(p => EditGroupAction());
246224

247225
private void EditGroupAction()
248226
{
249-
_lastSelectedGroup = SelectedGroup?.Name;
250-
_lastSelectedProfile = SelectedProfile?.Name;
251-
252227
ProfileDialogManager.ShowEditGroupDialog(this, _dialogCoordinator, SelectedGroup);
253228
}
254229

255230
public ICommand DeleteGroupCommand => new RelayCommand(p => DeleteGroupAction());
256231

257232
private void DeleteGroupAction()
258233
{
259-
_lastSelectedGroup = null;
260-
_lastSelectedProfile = null;
261-
262234
ProfileDialogManager.ShowDeleteGroupDialog(this, _dialogCoordinator, SelectedGroup);
263235
}
264236
#endregion
@@ -290,14 +262,16 @@ private void StopDelayedSearch()
290262

291263
public void RefreshProfiles()
292264
{
293-
if (Profiles == null)
265+
Debug.WriteLine("ProfilesViewModel: Refresh profiles...");
266+
267+
if (SelectedGroup == null)
294268
{
295-
Debug.WriteLine("Profiles is null");
296-
return;
269+
Debug.WriteLine("ProfilesViewModel: SelectedGroup is null, try to set");
270+
SetGroupView();
297271
}
298272

299-
if (!string.IsNullOrEmpty(_lastSelectedGroup))
300-
SelectedGroup = Groups.SourceCollection.Cast<GroupInfo>().FirstOrDefault(x => x.Name.Equals(_lastSelectedGroup));
273+
Groups?.Refresh();
274+
Profiles?.Refresh();
301275
}
302276

303277
public void OnProfileDialogOpen()
@@ -314,8 +288,8 @@ public void OnProfileDialogClose()
314288
#region Event
315289
private void ProfileManager_OnProfilesUpdated(object sender, EventArgs e)
316290
{
317-
// Update group view (and profile view) when the profile file has changed
318-
SetGroupView();
291+
// Update group view (and profile view) when the profile file has changed
292+
RefreshProfiles();
319293
}
320294

321295
private void SearchDispatcherTimer_Tick(object sender, EventArgs e)

0 commit comments

Comments
 (0)