Skip to content

Commit

Permalink
There is an issue where saving a profile causes duplicates in Manager…
Browse files Browse the repository at this point in the history
…Config.json

This was caused by CharacterSummaryViewModel having the rememberLogin defaulting to true - if the profile was already saved the login box is not shown and this variable then remains true, which causes the code that checks to see if the connection to EFT was successful and rememberLogin was also true to add the profile to the config.

Have also added a clean up routine in CharacterSelectionViewModel that removes all duplicate saved profiles (if there are any).  It chooses the profile to keep by grouping by username and password, then selecting either the first entry with a ProfileID or the first entry if there are no Profile ID's.  All profiles for that username/password are then removed, the profile to be kept placed back into the config and the configuration saved to disk.
  • Loading branch information
Steve Jackson committed May 23, 2024
1 parent 9c2b41c commit 86cc652
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
36 changes: 32 additions & 4 deletions SIT.Manager/ViewModels/Play/CharacterSelectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FluentAvalonia.UI.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharpCompress;
using SIT.Manager.Interfaces;
using SIT.Manager.Interfaces.ManagedProcesses;
using SIT.Manager.Models.Aki;
Expand Down Expand Up @@ -116,17 +117,20 @@ private async Task ReloadCharacterList()
{
_logger.LogError(ex, "An error occured while fetching characters");
}

if (_connectedServer.Characters.Any())
{
CheckAndRemoveDuplicateSavedProfiles();
}
}

protected override async void OnActivated()
{
base.OnActivated();

AkiServer? currentServer = _configService.Config.BookmarkedServers.FirstOrDefault(x => x.Address == _connectedServer.Address);
if (currentServer != null)
{
_connectedServer = currentServer;
}

if (currentServer != null) _connectedServer = currentServer;

await ReloadCharacterList();

Expand All @@ -135,4 +139,28 @@ protected override async void OnActivated()
ShowOnlySavedProfiles = true;
}
}

private void CheckAndRemoveDuplicateSavedProfiles()
{
var characterGrouping = _connectedServer.Characters.GroupBy(x => new { x.Username, x.Password });

bool updateConfig = false;

characterGrouping.Where(x => x.Count() > 1).ForEach(duplications =>
{
// Get the first entry with a profile ID - or just the first entry if no saved profile ID.
AkiCharacter? duplicateToKeep = duplications.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.ProfileID)) ??
duplications.First();
_connectedServer.Characters.RemoveAll(x => x.Username == duplicateToKeep.Username && x.Password == duplicateToKeep.Password);
_connectedServer.Characters.Add(duplicateToKeep);
updateConfig = true;
});

if (updateConfig)
{
_configService.UpdateConfig(_configService.Config);
}
}
}
5 changes: 4 additions & 1 deletion SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ private async Task Play()
}

AkiCharacter? character = _connectedServer.Characters.FirstOrDefault(x => x.Username == Profile.Username);
bool rememberLogin = true;

// Set this to false rather than true - this was causing duplicate saved profiles
// If we were already logged on the code to see if EFT was launched AND remember password would pass and save a duplicate each time
bool rememberLogin = false;

if (character == null)
{
Expand Down

0 comments on commit 86cc652

Please sign in to comment.