Skip to content

Commit

Permalink
Improve profile refresh (#1390)
Browse files Browse the repository at this point in the history
* Improve refresh

* Improve refresh
  • Loading branch information
BornToBeRoot authored Feb 21, 2022
1 parent 5c2ded1 commit bd4fa3a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 96 deletions.
1 change: 0 additions & 1 deletion Source/NETworkManager.Profiles/IProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace NETworkManager.Profiles
public interface IProfileManager
{
ICollectionView Profiles { get; }
void RefreshProfiles();
void OnProfileDialogOpen();
void OnProfileDialogClose();
ICommand AddProfileCommand { get; }
Expand Down
58 changes: 38 additions & 20 deletions Source/NETworkManager.Profiles/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,7 @@ public static void AddGroups(List<GroupInfo> groups)
/// <param name="group"></param>
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();
}
Expand All @@ -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();
}

/// <summary>
///
/// </summary>
/// <param name="group"></param>
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();
}
Expand All @@ -725,7 +723,7 @@ public static List<string> GetGroupNames()
list.Add(groups.Name);

return list;
}
}

/// <summary>
/// Method to check if a profile exists.
Expand Down Expand Up @@ -761,28 +759,48 @@ public static bool IsGroupEmpty(string name)
/// <param name="profile"><see cref="ProfileInfo"/> to add.</param>
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();
}

/// <summary>
/// Remove a profile from a group.
/// </summary>
/// <param name="profile"><see cref="ProfileInfo"/> to remove.</param>
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));
/// <summary>
/// Remove profiles from a group.
/// </summary>
/// <param name="profile"><see cref="ProfileInfo"/> to remove.</param>
public static void RemoveProfiles(IList<ProfileInfo> profiles)
{
foreach (ProfileInfo profile in profiles)
Groups.First(x => x.Name.Equals(profile.Group)).Profiles.Remove(profile);

ProfilesUpdated();
}
Expand Down
52 changes: 13 additions & 39 deletions Source/NETworkManager/ProfileDialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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(),
Expand Down Expand Up @@ -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<ProfileInfo> profiles = instance.Group.Profiles;

Expand All @@ -413,7 +392,7 @@ public static void AddGroup(GroupViewModel instance)
}
}

ProfileManager.AddGroup(new GroupInfo
return new GroupInfo
{
Name = name,

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Source/NETworkManager/ViewModels/IPScannerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 9 additions & 35 deletions Source/NETworkManager/ViewModels/ProfilesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public ICollectionView Groups
}
}

private string _lastSelectedGroup;

private GroupInfo _selectedGroup = new GroupInfo();
public GroupInfo SelectedGroup
{
Expand Down Expand Up @@ -70,8 +68,6 @@ public ICollectionView Profiles
}
}

private string _lastSelectedProfile;

private ProfileInfo _selectedProfile = new ProfileInfo();
public ProfileInfo SelectedProfile
{
Expand Down Expand Up @@ -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<ProfileInfo>().OrderBy(x => x.Name).FirstOrDefault();
else
SelectedProfile = Profiles.SourceCollection.Cast<ProfileInfo>().FirstOrDefault(x => x.Name.Equals(_lastSelectedProfile));

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

private void AddProfileAction()
{
_lastSelectedGroup = SelectedGroup?.Name;
_lastSelectedProfile = SelectedProfile?.Name;

ProfileDialogManager.ShowAddProfileDialog(this, _dialogCoordinator, SelectedGroup?.Name);
}

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

private void EditProfileAction()
{
_lastSelectedGroup = SelectedGroup?.Name;
_lastSelectedProfile = SelectedProfile?.Name;

ProfileDialogManager.ShowEditProfileDialog(this, _dialogCoordinator, SelectedProfile);
}

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

private void CopyAsProfileAction()
{
_lastSelectedGroup = SelectedGroup?.Name;
_lastSelectedProfile = SelectedProfile?.Name;

ProfileDialogManager.ShowCopyAsProfileDialog(this, _dialogCoordinator, SelectedProfile);
}

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

private void DeleteProfileAction()
{
_lastSelectedGroup = SelectedGroup?.Name;
_lastSelectedProfile = null;

ProfileDialogManager.ShowDeleteProfileDialog(this, _dialogCoordinator, new List<ProfileInfo>(SelectedProfiles.Cast<ProfileInfo>()));
}

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

private void AddGroupAction()
{
_lastSelectedGroup = SelectedGroup?.Name;
_lastSelectedProfile = SelectedProfile?.Name;

ProfileDialogManager.ShowAddGroupDialog(this, _dialogCoordinator);
}

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

private void EditGroupAction()
{
_lastSelectedGroup = SelectedGroup?.Name;
_lastSelectedProfile = SelectedProfile?.Name;

ProfileDialogManager.ShowEditGroupDialog(this, _dialogCoordinator, SelectedGroup);
}

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

private void DeleteGroupAction()
{
_lastSelectedGroup = null;
_lastSelectedProfile = null;

ProfileDialogManager.ShowDeleteGroupDialog(this, _dialogCoordinator, SelectedGroup);
}
#endregion
Expand Down Expand Up @@ -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<GroupInfo>().FirstOrDefault(x => x.Name.Equals(_lastSelectedGroup));
Groups?.Refresh();
Profiles?.Refresh();
}

public void OnProfileDialogOpen()
Expand All @@ -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)
Expand Down

0 comments on commit bd4fa3a

Please sign in to comment.