From bd4fa3abe0d66d29bd240a5c878aa8e203e868e7 Mon Sep 17 00:00:00 2001 From: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com> Date: Mon, 21 Feb 2022 04:56:08 +0100 Subject: [PATCH] Improve profile refresh (#1390) * Improve refresh * Improve refresh --- .../IProfileManager.cs | 1 - .../NETworkManager.Profiles/ProfileManager.cs | 58 ++++++++++++------- Source/NETworkManager/ProfileDialogManager.cs | 52 +++++------------ .../ViewModels/IPScannerViewModel.cs | 2 +- .../ViewModels/ProfilesViewModel.cs | 44 +++----------- 5 files changed, 61 insertions(+), 96 deletions(-) diff --git a/Source/NETworkManager.Profiles/IProfileManager.cs b/Source/NETworkManager.Profiles/IProfileManager.cs index cccc1f10a8..efa6665e80 100644 --- a/Source/NETworkManager.Profiles/IProfileManager.cs +++ b/Source/NETworkManager.Profiles/IProfileManager.cs @@ -6,7 +6,6 @@ namespace NETworkManager.Profiles public interface IProfileManager { ICollectionView Profiles { get; } - void RefreshProfiles(); void OnProfileDialogOpen(); void OnProfileDialogClose(); ICommand AddProfileCommand { get; } diff --git a/Source/NETworkManager.Profiles/ProfileManager.cs b/Source/NETworkManager.Profiles/ProfileManager.cs index 0d7fbfc3db..a290090dff 100644 --- a/Source/NETworkManager.Profiles/ProfileManager.cs +++ b/Source/NETworkManager.Profiles/ProfileManager.cs @@ -682,12 +682,7 @@ public static void AddGroups(List groups) /// public static void AddGroup(GroupInfo group) { - // Possible fix for appcrash --> when icollection view is refreshed... - //System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate - //{ - //lock (Profiles) Groups.Add(group); - //})); ProfilesUpdated(); } @@ -697,18 +692,21 @@ public static GroupInfo GetGroup(string name) return Groups.First(x => x.Name.Equals(name)); } + public static void ReplaceGroup(GroupInfo oldGroup, GroupInfo newGroup) + { + Groups.Remove(oldGroup); + Groups.Add(newGroup); + + ProfilesUpdated(); + } + /// /// /// /// public static void RemoveGroup(GroupInfo group) { - // Possible fix for appcrash --> when icollection view is refreshed... - //System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate - //{ - //lock (Profiles) Groups.Remove(group); - //})); ProfilesUpdated(); } @@ -725,7 +723,7 @@ public static List GetGroupNames() list.Add(groups.Name); return list; - } + } /// /// Method to check if a profile exists. @@ -761,28 +759,48 @@ public static bool IsGroupEmpty(string name) /// to add. public static void AddProfile(ProfileInfo profile) { - string group = profile.Group; - - if (!GroupExists(group)) - AddGroup(new GroupInfo(group)); + if (!GroupExists(profile.Group)) + AddGroup(new GroupInfo(profile.Group)); Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Add(profile); ProfilesUpdated(); } + public static void ReplaceProfile(ProfileInfo oldProfile, ProfileInfo newProfile) + { + // Remove + Groups.First(x => x.Name.Equals(oldProfile.Group)).Profiles.Remove(oldProfile); + + // Add + if (!GroupExists(newProfile.Group)) + AddGroup(new GroupInfo(newProfile.Group)); + + Groups.First(x => x.Name.Equals(newProfile.Group)).Profiles.Add(newProfile); + + // Notify + ProfilesUpdated(); + } + /// /// Remove a profile from a group. /// /// to remove. public static void RemoveProfile(ProfileInfo profile) { - string group = profile.Group; + Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Remove(profile); + + ProfilesUpdated(); + } - Groups.First(x => x.Name.Equals(group)).Profiles.Remove(profile); - - //if (IsGroupEmpty(group)) - // RemoveGroup(GetGroup(group)); + /// + /// Remove profiles from a group. + /// + /// to remove. + public static void RemoveProfiles(IList profiles) + { + foreach (ProfileInfo profile in profiles) + Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Remove(profile); ProfilesUpdated(); } diff --git a/Source/NETworkManager/ProfileDialogManager.cs b/Source/NETworkManager/ProfileDialogManager.cs index 733966767e..f459ce191f 100644 --- a/Source/NETworkManager/ProfileDialogManager.cs +++ b/Source/NETworkManager/ProfileDialogManager.cs @@ -30,9 +30,7 @@ public static async Task ShowAddProfileDialog(IProfileManager viewModel, IDialog await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - AddProfile(instance); - - viewModel.RefreshProfiles(); + ProfileManager.AddProfile(ParseProfileInfo(instance)); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -62,10 +60,7 @@ public static async Task ShowEditProfileDialog(IProfileManager viewModel, IDialo await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - RemoveProfile(profile); - AddProfile(instance); - - viewModel.RefreshProfiles(); + ProfileManager.ReplaceProfile(profile, ParseProfileInfo(instance)); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -94,9 +89,7 @@ public static async Task ShowCopyAsProfileDialog(IProfileManager viewModel, IDia await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - AddProfile(instance); - - viewModel.RefreshProfiles(); + ProfileManager.AddProfile(ParseProfileInfo(instance)); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -124,10 +117,8 @@ public static async Task ShowDeleteProfileDialog(IProfileManager viewModel, IDia await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - foreach (var profile in profiles) - RemoveProfile(profile); + ProfileManager.RemoveProfiles(profiles); - viewModel.RefreshProfiles(); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -158,9 +149,7 @@ public static async Task ShowAddGroupDialog(IProfileManager viewModel, IDialogCo await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - AddGroup(instance); - - viewModel.RefreshProfiles(); + ProfileManager.AddGroup(ParseGroupInfo(instance)); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -189,10 +178,7 @@ public static async Task ShowEditGroupDialog(IProfileManager viewModel, IDialogC await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - RemoveGroup(instance.Group); - AddGroup(instance); - - viewModel.RefreshProfiles(); + ProfileManager.ReplaceGroup(instance.Group, ParseGroupInfo(instance)); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -220,9 +206,7 @@ public static async Task ShowDeleteGroupDialog(IProfileManager viewModel, IDialo await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); viewModel.OnProfileDialogClose(); - RemoveGroup(group); - - viewModel.RefreshProfiles(); + ProfileManager.RemoveGroup(group); }, async instance => { await dialogCoordinator.HideMetroDialogAsync(viewModel, customDialog); @@ -240,9 +224,9 @@ public static async Task ShowDeleteGroupDialog(IProfileManager viewModel, IDialo #endregion #region Methods to add and remove profile - public static void AddProfile(ProfileViewModel instance) + public static ProfileInfo ParseProfileInfo(ProfileViewModel instance) { - ProfileManager.AddProfile(new ProfileInfo + return new ProfileInfo { Name = instance.Name.Trim(), Host = instance.Host.Trim(), @@ -383,17 +367,12 @@ public static void AddProfile(ProfileViewModel instance) Whois_Enabled = instance.Whois_Enabled, Whois_InheritHost = instance.Whois_InheritHost, Whois_Domain = instance.Whois_InheritHost ? instance.Host?.Trim() : instance.Whois_Domain?.Trim() - }); - } - - public static void RemoveProfile(ProfileInfo profile) - { - ProfileManager.RemoveProfile(profile); + }; } #endregion #region Methods to add and remove group - public static void AddGroup(GroupViewModel instance) + public static GroupInfo ParseGroupInfo(GroupViewModel instance) { List profiles = instance.Group.Profiles; @@ -413,7 +392,7 @@ public static void AddGroup(GroupViewModel instance) } } - ProfileManager.AddGroup(new GroupInfo + return new GroupInfo { Name = name, @@ -498,12 +477,7 @@ public static void AddGroup(GroupViewModel instance) TigerVNC_Enabled = instance.TigerVNC_Enabled, TigerVNC_OverridePort = instance.TigerVNC_OverridePort, TigerVNC_Port = instance.TigerVNC_Port - }); - } - - public static void RemoveGroup(GroupInfo group) - { - ProfileManager.RemoveGroup(group); + }; } #endregion } diff --git a/Source/NETworkManager/ViewModels/IPScannerViewModel.cs b/Source/NETworkManager/ViewModels/IPScannerViewModel.cs index fa49941aef..d8e6108842 100644 --- a/Source/NETworkManager/ViewModels/IPScannerViewModel.cs +++ b/Source/NETworkManager/ViewModels/IPScannerViewModel.cs @@ -342,7 +342,7 @@ private async Task AddProfileSelectedHostAction() { _dialogCoordinator.HideMetroDialogAsync(this, customDialog); - ProfileDialogManager.AddProfile(instance); + ProfileManager.AddProfile(ProfileDialogManager.ParseProfileInfo(instance)); }, instance => { _dialogCoordinator.HideMetroDialogAsync(this, customDialog); diff --git a/Source/NETworkManager/ViewModels/ProfilesViewModel.cs b/Source/NETworkManager/ViewModels/ProfilesViewModel.cs index 967665f8e6..6de088d0ea 100644 --- a/Source/NETworkManager/ViewModels/ProfilesViewModel.cs +++ b/Source/NETworkManager/ViewModels/ProfilesViewModel.cs @@ -34,8 +34,6 @@ public ICollectionView Groups } } - private string _lastSelectedGroup; - private GroupInfo _selectedGroup = new GroupInfo(); public GroupInfo SelectedGroup { @@ -70,8 +68,6 @@ public ICollectionView Profiles } } - private string _lastSelectedProfile; - private ProfileInfo _selectedProfile = new ProfileInfo(); public ProfileInfo SelectedProfile { @@ -180,10 +176,7 @@ public void SetProfilesView(string groupName) }; // Select first profile, or the last selected profile - if (string.IsNullOrEmpty(_lastSelectedProfile)) SelectedProfile = Profiles.SourceCollection.Cast().OrderBy(x => x.Name).FirstOrDefault(); - else - SelectedProfile = Profiles.SourceCollection.Cast().FirstOrDefault(x => x.Name.Equals(_lastSelectedProfile)); SelectedProfiles = new List { SelectedProfile }; // Fix --> Count need to be 1 for EditProfile_CanExecute } @@ -194,9 +187,6 @@ public void SetProfilesView(string groupName) private void AddProfileAction() { - _lastSelectedGroup = SelectedGroup?.Name; - _lastSelectedProfile = SelectedProfile?.Name; - ProfileDialogManager.ShowAddProfileDialog(this, _dialogCoordinator, SelectedGroup?.Name); } @@ -206,9 +196,6 @@ private void AddProfileAction() private void EditProfileAction() { - _lastSelectedGroup = SelectedGroup?.Name; - _lastSelectedProfile = SelectedProfile?.Name; - ProfileDialogManager.ShowEditProfileDialog(this, _dialogCoordinator, SelectedProfile); } @@ -216,9 +203,6 @@ private void EditProfileAction() private void CopyAsProfileAction() { - _lastSelectedGroup = SelectedGroup?.Name; - _lastSelectedProfile = SelectedProfile?.Name; - ProfileDialogManager.ShowCopyAsProfileDialog(this, _dialogCoordinator, SelectedProfile); } @@ -226,9 +210,6 @@ private void CopyAsProfileAction() private void DeleteProfileAction() { - _lastSelectedGroup = SelectedGroup?.Name; - _lastSelectedProfile = null; - ProfileDialogManager.ShowDeleteProfileDialog(this, _dialogCoordinator, new List(SelectedProfiles.Cast())); } @@ -236,9 +217,6 @@ private void DeleteProfileAction() private void AddGroupAction() { - _lastSelectedGroup = SelectedGroup?.Name; - _lastSelectedProfile = SelectedProfile?.Name; - ProfileDialogManager.ShowAddGroupDialog(this, _dialogCoordinator); } @@ -246,9 +224,6 @@ private void AddGroupAction() private void EditGroupAction() { - _lastSelectedGroup = SelectedGroup?.Name; - _lastSelectedProfile = SelectedProfile?.Name; - ProfileDialogManager.ShowEditGroupDialog(this, _dialogCoordinator, SelectedGroup); } @@ -256,9 +231,6 @@ private void EditGroupAction() private void DeleteGroupAction() { - _lastSelectedGroup = null; - _lastSelectedProfile = null; - ProfileDialogManager.ShowDeleteGroupDialog(this, _dialogCoordinator, SelectedGroup); } #endregion @@ -290,14 +262,16 @@ private void StopDelayedSearch() public void RefreshProfiles() { - if (Profiles == null) + Debug.WriteLine("ProfilesViewModel: Refresh profiles..."); + + if (SelectedGroup == null) { - Debug.WriteLine("Profiles is null"); - return; + Debug.WriteLine("ProfilesViewModel: SelectedGroup is null, try to set"); + SetGroupView(); } - if (!string.IsNullOrEmpty(_lastSelectedGroup)) - SelectedGroup = Groups.SourceCollection.Cast().FirstOrDefault(x => x.Name.Equals(_lastSelectedGroup)); + Groups?.Refresh(); + Profiles?.Refresh(); } public void OnProfileDialogOpen() @@ -314,8 +288,8 @@ public void OnProfileDialogClose() #region Event private void ProfileManager_OnProfilesUpdated(object sender, EventArgs e) { - // Update group view (and profile view) when the profile file has changed - SetGroupView(); + // Update group view (and profile view) when the profile file has changed + RefreshProfiles(); } private void SearchDispatcherTimer_Tick(object sender, EventArgs e)