add dialog to add secret key to main ui when no secret key is present

This commit is contained in:
rootdarkarchon
2023-02-12 13:45:10 +01:00
parent 58db24e2b4
commit 73f009757c
5 changed files with 107 additions and 63 deletions

View File

@@ -3,7 +3,6 @@ using MareSynchronos.MareConfiguration.Models;
using MareSynchronos.Models;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI;
using Microsoft.Extensions.Logging.Abstractions;
using System.Diagnostics;
namespace MareSynchronos.Managers;
@@ -172,6 +171,7 @@ public class ServerConfigurationManager
{
var server = GetServerByIndex(serverSelectionIndex);
server.Authentications.Remove(item);
Save();
}
internal void AddServer(ServerStorage serverStorage)

View File

@@ -3,7 +3,7 @@
<PropertyGroup>
<Authors></Authors>
<Company></Company>
<Version>0.7.25</Version>
<Version>0.7.26</Version>
<Description></Description>
<Copyright></Copyright>
<PackageProjectUrl>https://github.com/Penumbra-Sync/client</PackageProjectUrl>

View File

@@ -722,9 +722,44 @@ public class CompactUi : WindowMediatorSubscriberBase, IDisposable
if (_apiController.ServerState is not ServerState.Connected)
{
UiShared.ColorTextWrapped(GetServerError(), GetUidColor());
if (_apiController.ServerState is ServerState.NoSecretKey)
{
DrawAddCharacter();
}
}
}
private void DrawAddCharacter()
{
ImGui.Dummy(new(10));
var keys = _serverManager.CurrentServer!.SecretKeys;
if (keys.TryGetValue(secretKeyIdx, out var secretKey))
{
var friendlyName = secretKey.FriendlyName;
if (UiShared.IconTextButton(FontAwesomeIcon.Plus, "Add current character with secret key"))
{
_serverManager.CurrentServer!.Authentications.Add(new MareConfiguration.Models.Authentication()
{
CharacterName = _uiShared.PlayerName,
WorldId = _uiShared.WorldId,
SecretKeyIdx = secretKeyIdx
});
_serverManager.Save();
_ = _apiController.CreateConnections(true);
}
_uiShared.DrawCombo("Secret Key##addCharacterSecretKey", keys, (f) => f.Value.FriendlyName, (f) => secretKeyIdx = f.Key);
}
else
{
UiShared.ColorTextWrapped("No secret keys are configured for the current server.", ImGuiColors.DalamudYellow);
}
}
private int secretKeyIdx = 0;
private string GetServerError()
{
@@ -739,7 +774,7 @@ public class CompactUi : WindowMediatorSubscriberBase, IDisposable
"Your plugin or the server you are connecting to is out of date. Please update your plugin now. If you already did so, contact the server provider to update their server to the latest version.",
ServerState.RateLimited => "You are rate limited for (re)connecting too often. Disconnect, wait 10 minutes and try again.",
ServerState.Connected => string.Empty,
ServerState.NoSecretKey => "You have no secret key set for this current character. Open the settings and set a secret key for this current character. If this character does not exist in Mare, it needs to be added. You can reuse one secret key for different characters.",
ServerState.NoSecretKey => "You have no secret key set for this current character. Use the button below or open the settings and set a secret key for the current character. You can reuse the same secret key for multiple characters.",
_ => string.Empty
};
}

View File

@@ -275,33 +275,25 @@ public class SettingsUi : WindowMediatorSubscriberBase, IDisposable
_serverConfigurationManager.Save();
}
if (ImGui.BeginCombo("World", worldPreview))
{
foreach (var world in data)
_uiShared.DrawCombo("World##" + item.CharacterName + i, data, (w) => w.Value,
(w) =>
{
bool isSelected = worldIdx == world.Key;
if (ImGui.Selectable(world.Value, isSelected))
if (item.WorldId != w.Key)
{
item.WorldId = world.Key;
item.WorldId = w.Key;
_serverConfigurationManager.Save();
}
}
ImGui.EndCombo();
}
}, EqualityComparer<KeyValuePair<ushort, string>>.Default.Equals(data.FirstOrDefault(f => f.Key == worldIdx), default) ? data.First() : data.First(f => f.Key == worldIdx));
if (ImGui.BeginCombo("Secret Key", friendlyName))
{
foreach (var kvp in keys)
_uiShared.DrawCombo("Secret Key##" + item.CharacterName + i, keys, (w) => w.Value.FriendlyName,
(w) =>
{
bool isSelected = kvp.Key == secretKeyIdx;
if (ImGui.Selectable(kvp.Value.FriendlyName, isSelected))
if (w.Key != item.SecretKeyIdx)
{
item.SecretKeyIdx = kvp.Key;
item.SecretKeyIdx = w.Key;
_serverConfigurationManager.Save();
}
}
ImGui.EndCombo();
}
}, EqualityComparer<KeyValuePair<int, SecretKey>>.Default.Equals(keys.FirstOrDefault(f => f.Key == item.SecretKeyIdx), default) ? keys.First() : keys.First(f => f.Key == item.SecretKeyIdx));
if (UiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete Character"))
{
@@ -482,63 +474,40 @@ public class SettingsUi : WindowMediatorSubscriberBase, IDisposable
var onlineNotifs = _configService.Current.ShowOnlineNotifications;
var onlineNotifsPairsOnly = _configService.Current.ShowOnlineNotificationsOnlyForIndividualPairs;
var onlineNotifsNamedOnly = _configService.Current.ShowOnlineNotificationsOnlyForNamedPairs;
var infoNotifLocation = _configService.Current.InfoNotification;
var warnNotifLocation = _configService.Current.WarningNotification;
var errorNotifLocation = _configService.Current.ErrorNotification;
UiShared.FontText("Notifications", _uiShared.UidFont);
if (ImGui.BeginCombo("Info Notification Display", infoNotifLocation.ToString()))
_uiShared.DrawCombo("Info Notification Display##settingsUi", (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)), (i) => i.ToString(),
(i) =>
{
foreach (var item in (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)))
{
bool isSelected = item == infoNotifLocation;
if (ImGui.Selectable(item.ToString(), isSelected))
{
_configService.Current.InfoNotification = item;
_configService.Save();
}
}
ImGui.EndCombo();
}
_configService.Current.InfoNotification = i;
_configService.Save();
}, _configService.Current.InfoNotification);
UiShared.DrawHelpText("The location where \"Info\" notifications will display."
+ Environment.NewLine + "'Nowhere' will not show any Info notifications"
+ Environment.NewLine + "'Chat' will print Info notifications in chat"
+ Environment.NewLine + "'Toast' will show Warning toast notifications in the bottom right corner"
+ Environment.NewLine + "'Both' will show chat as well as the toast notification");
+ Environment.NewLine + "'Nowhere' will not show any Info notifications"
+ Environment.NewLine + "'Chat' will print Info notifications in chat"
+ Environment.NewLine + "'Toast' will show Warning toast notifications in the bottom right corner"
+ Environment.NewLine + "'Both' will show chat as well as the toast notification");
if (ImGui.BeginCombo("Warning Notification Display", warnNotifLocation.ToString()))
_uiShared.DrawCombo("Warning Notification Display##settingsUi", (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)), (i) => i.ToString(),
(i) =>
{
foreach (var item in (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)))
{
bool isSelected = item == warnNotifLocation;
if (ImGui.Selectable(item.ToString(), isSelected))
{
_configService.Current.WarningNotification = item;
_configService.Save();
}
}
ImGui.EndCombo();
}
_configService.Current.WarningNotification = i;
_configService.Save();
}, _configService.Current.WarningNotification);
UiShared.DrawHelpText("The location where \"Warning\" notifications will display."
+ Environment.NewLine + "'Nowhere' will not show any Warning notifications"
+ Environment.NewLine + "'Chat' will print Warning notifications in chat"
+ Environment.NewLine + "'Toast' will show Warning toast notifications in the bottom right corner"
+ Environment.NewLine + "'Both' will show chat as well as the toast notification");
if (ImGui.BeginCombo("Error Notification Display", errorNotifLocation.ToString()))
_uiShared.DrawCombo("Error Notification Display##settingsUi", (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)), (i) => i.ToString(),
(i) =>
{
foreach (var item in (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)))
{
bool isSelected = item == errorNotifLocation;
if (ImGui.Selectable(item.ToString(), isSelected))
{
_configService.Current.ErrorNotification = item;
_configService.Save();
}
}
ImGui.EndCombo();
}
_configService.Current.ErrorNotification = i;
_configService.Save();
}, _configService.Current.ErrorNotification);
UiShared.DrawHelpText("The location where \"Error\" notifications will display."
+ Environment.NewLine + "'Nowhere' will not show any Error notifications"
+ Environment.NewLine + "'Chat' will print Error notifications in chat"

View File

@@ -40,6 +40,7 @@ public partial class UiShared : MediatorSubscriberBase
public string PlayerName => _dalamudUtil.PlayerName;
public uint WorldId => _dalamudUtil.WorldId;
public Dictionary<ushort, string> WorldData => _dalamudUtil.WorldData.Value;
private Dictionary<string, object> _selectedComboItems = new(StringComparer.Ordinal);
public bool HasValidPenumbraModPath => !(_ipcManager.PenumbraModDirectory ?? string.Empty).IsNullOrEmpty() && Directory.Exists(_ipcManager.PenumbraModDirectory);
public bool EditTrackerPosition { get; set; }
public ImFontPtr UidFont { get; private set; }
@@ -100,6 +101,45 @@ public partial class UiShared : MediatorSubscriberBase
return buttonSize;
}
public T? DrawCombo<T>(string comboName, IEnumerable<T> comboItems, Func<T, string> toName,
Action<T>? onSelected = null, T? initialSelectedItem = default)
{
if (!comboItems.Any()) return default;
if (!_selectedComboItems.TryGetValue(comboName, out var selectedItem) && selectedItem == null)
{
if (!EqualityComparer<T>.Default.Equals(initialSelectedItem, default))
{
selectedItem = initialSelectedItem;
_selectedComboItems[comboName] = selectedItem!;
onSelected?.Invoke(initialSelectedItem);
}
else
{
selectedItem = comboItems.First();
_selectedComboItems[comboName] = selectedItem!;
}
}
if (ImGui.BeginCombo(comboName, toName((T)selectedItem!)))
{
foreach (var item in comboItems)
{
bool isSelected = EqualityComparer<T>.Default.Equals(item, (T)selectedItem);
if (ImGui.Selectable(toName(item), isSelected))
{
_selectedComboItems[comboName] = item!;
onSelected?.Invoke(item!);
}
}
ImGui.EndCombo();
}
return (T)_selectedComboItems[comboName];
}
private void BuildFont()
{
var fontFile = Path.Combine(_pluginInterface.DalamudAssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf");