rework the whole connection garbage/custom servers, other cleanups/refactors

This commit is contained in:
Stanley Dimant
2022-06-27 01:19:45 +02:00
parent 404ba8e278
commit 61b178e2c0
12 changed files with 386 additions and 302 deletions

View File

@@ -51,31 +51,20 @@ namespace MareSynchronos.UI
return;
}
if (_apiController.SecretKey != "-" && !_apiController.IsConnected && _apiController.ServerAlive)
{
if (ImGui.Button("Reset Secret Key"))
{
_configuration.ClientSecret.Clear();
_configuration.Save();
_apiController.RestartHeartbeat();
}
}
else
{
var pluginState = _uiShared.DrawOtherPluginState();
var pluginState = _uiShared.DrawOtherPluginState();
DrawSettingsContent(pluginState);
}
if (pluginState)
DrawSettingsContent();
}
private void DrawSettingsContent(bool pluginState)
private void DrawSettingsContent()
{
_uiShared.PrintServerState();
ImGui.Separator();
ImGui.SetWindowFontScale(1.2f);
ImGui.Text("Your UID");
ImGui.SameLine();
if (_apiController.ServerAlive)
if (_apiController.IsConnected)
{
ImGui.TextColored(ImGuiColors.ParsedGreen, _apiController.UID);
ImGui.SameLine();
@@ -89,18 +78,18 @@ namespace MareSynchronos.UI
}
else
{
ImGui.TextColored(ImGuiColors.DalamudRed, "No UID (Service unavailable)");
string error = _configuration.FullPause ? "Fully Paused" : "Service unavailable";
ImGui.TextColored(ImGuiColors.DalamudRed, $"No UID ({error})");
ImGui.SetWindowFontScale(1.0f);
}
ImGui.Separator();
if (_apiController.ServerAlive)
if (_apiController.IsConnected)
DrawPairedClientsContent();
DrawFileCacheSettings();
if (_apiController.ServerAlive)
if (_apiController.IsConnected)
DrawCurrentTransfers();
DrawAdministration(_apiController.ServerAlive);
DrawAdministration(_apiController.IsConnected);
}
private bool _deleteFilesPopupModalShown = false;
@@ -181,15 +170,39 @@ namespace MareSynchronos.UI
}
}
var marePaused = _configuration.FullPause;
if (ImGui.Checkbox("Pause Mare Synchronos", ref marePaused))
if (!_configuration.FullPause)
{
_configuration.FullPause = marePaused;
_configuration.Save();
_apiController.RestartHeartbeat();
ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudYellow);
UiShared.TextWrapped("Note: to change servers you need to pause Mare Synchronos.");
ImGui.PopStyleColor();
}
var marePaused = _configuration.FullPause;
if (_configuration.HasValidSetup)
{
if (ImGui.Checkbox("Pause Mare Synchronos", ref marePaused))
{
_configuration.FullPause = marePaused;
_configuration.Save();
Task.Run(_apiController.CreateConnections);
}
UiShared.DrawHelpText("Completely pauses the sync and clear your current data (not uploaded files) on the service.");
}
else
{
ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudYellow);
ImGui.TextUnformatted("You cannot resume pause without a valid account on the service.");
ImGui.PopStyleColor();
}
if (marePaused)
{
_uiShared.DrawServiceSelection();
}
UiShared.DrawHelpText("Completely pauses the sync and clear your current data (not uploaded files) on the service.");
ImGui.TreePop();
}

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Dalamud.Interface;
@@ -74,7 +75,10 @@ namespace MareSynchronos.UI
public void PrintServerState()
{
ImGui.Text("Service status of " + (string.IsNullOrEmpty(_pluginConfiguration.ApiUri) ? ApiController.MainServer : _pluginConfiguration.ApiUri));
var serverName = _apiController.ServerDictionary.ContainsKey(_pluginConfiguration.ApiUri)
? _apiController.ServerDictionary[_pluginConfiguration.ApiUri]
: _pluginConfiguration.ApiUri;
ImGui.Text("Service status of " + serverName);
ImGui.SameLine();
var color = _apiController.ServerAlive ? ImGuiColors.ParsedGreen : ImGuiColors.DalamudRed;
ImGui.TextColored(color, _apiController.ServerAlive ? "Available" : "Unavailable");
@@ -132,59 +136,91 @@ namespace MareSynchronos.UI
}
private int _serverSelectionIndex = 0;
private string _customServerName = "";
private string _customServerUri = "";
public void DrawServiceSelection()
{
string[] comboEntries = new[] { ApiController.MainServer, "Custom Service" };
if (ImGui.BeginCombo("Service", comboEntries[_serverSelectionIndex]))
string[] comboEntries = _apiController.ServerDictionary.Values.ToArray();
_serverSelectionIndex = Array.IndexOf(_apiController.ServerDictionary.Keys.ToArray(), _pluginConfiguration.ApiUri);
if (ImGui.BeginCombo("Select Service", comboEntries[_serverSelectionIndex]))
{
for (int n = 0; n < comboEntries.Length; n++)
for (int i = 0; i < comboEntries.Length; i++)
{
bool isSelected = _serverSelectionIndex == n;
if (ImGui.Selectable(comboEntries[n], isSelected))
bool isSelected = _serverSelectionIndex == i;
if (ImGui.Selectable(comboEntries[i], isSelected))
{
_serverSelectionIndex = n;
_pluginConfiguration.ApiUri = _apiController.ServerDictionary.Single(k => k.Value == comboEntries[i]).Key;
_pluginConfiguration.Save();
}
if (isSelected)
{
ImGui.SetItemDefaultFocus();
}
bool useCustomService = _serverSelectionIndex != 0;
if (_apiController.UseCustomService != useCustomService)
{
_apiController.UseCustomService = useCustomService;
_pluginConfiguration.Save();
}
}
ImGui.EndCombo();
}
if (_apiController.UseCustomService)
if (_serverSelectionIndex != 0)
{
string serviceAddress = _pluginConfiguration.ApiUri;
if (ImGui.InputText("Service address", ref serviceAddress, 255))
ImGui.SameLine();
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button(FontAwesomeIcon.Trash.ToIconString() + "##deleteService"))
{
if (_pluginConfiguration.ApiUri != serviceAddress)
{
_pluginConfiguration.ApiUri = serviceAddress;
_apiController.RestartHeartbeat();
_pluginConfiguration.Save();
}
_pluginConfiguration.CustomServerList.Remove(_pluginConfiguration.ApiUri);
_pluginConfiguration.ApiUri = ApiController.MainServiceUri;
_pluginConfiguration.Save();
}
ImGui.PopFont();
}
PrintServerState();
if (_apiController.ServerAlive)
if (_apiController.ServerAlive && !_pluginConfiguration.ClientSecret.ContainsKey(_pluginConfiguration.ApiUri))
{
if (ImGui.Button("Register"))
{
_pluginConfiguration.FullPause = false;
_pluginConfiguration.Save();
Task.WaitAll(_apiController.Register());
}
}
else
{
ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudYellow);
TextWrapped("You already have an account on this server.");
ImGui.PopStyleColor();
ImGui.SameLine();
if (ImGui.Button("Connect##connectToService"))
{
_pluginConfiguration.FullPause = false;
_pluginConfiguration.Save();
Task.Run(_apiController.CreateConnections);
}
}
if (ImGui.TreeNode("Custom Service"))
{
ImGui.SetNextItemWidth(250);
ImGui.InputText("Custom Service Name", ref _customServerName, 255);
ImGui.SetNextItemWidth(250);
ImGui.InputText("Custom Service Address", ref _customServerUri, 255);
if (ImGui.Button("Add Custom Service"))
{
if (!string.IsNullOrEmpty(_customServerUri)
&& !string.IsNullOrEmpty(_customServerName)
&& !_pluginConfiguration.CustomServerList.ContainsValue(_customServerName))
{
_pluginConfiguration.CustomServerList[_customServerUri] = _customServerName;
_customServerUri = string.Empty;
_customServerName = string.Empty;
_pluginConfiguration.Save();
}
}
ImGui.TreePop();
}
}
public static void DrawHelpText(string helpText)