Add DTR entry with number of visible pairs (#52)

* Add DTR entry with number of visible pairs

* Simplify DTR visible pair counter

- Merge the new PairHandlerInvisibleMessage and PairHandlerVisibleDisposedMessage ;
- Leave alone a bunch of messages that were actually unneccessary.

* Match encodings with the rest of the codebase

* Make the DTR entry opt-in and add explanations

* More reliable publishing of PairHandlerVisible/Invisible

* Simplify things for DTR

* Rework async
This commit is contained in:
N. Lo
2023-06-18 23:01:39 +02:00
committed by GitHub
parent de36e9e8bd
commit 0e84d312dc
5 changed files with 115 additions and 1 deletions

View File

@@ -0,0 +1,99 @@
using Dalamud.Game.Gui.Dtr;
using MareSynchronos.MareConfiguration;
using MareSynchronos.MareConfiguration.Configurations;
using MareSynchronos.PlayerData.Pairs;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI;
using Microsoft.Extensions.Hosting;
namespace MareSynchronos.UI;
public sealed class DtrEntry : IDisposable, IHostedService
{
private readonly DtrBarEntry _entry;
private readonly ConfigurationServiceBase<MareConfig> _configService;
private readonly PairManager _pairManager;
private readonly ApiController _apiController;
private readonly CancellationTokenSource _cancellationTokenSource = new();
private Task? _runTask;
private string? _text;
public DtrEntry(DtrBar dtrBar, ConfigurationServiceBase<MareConfig> configService, PairManager pairManager, ApiController apiController)
{
_entry = dtrBar.Get("Mare Synchronos");
_configService = configService;
_pairManager = pairManager;
_apiController = apiController;
Clear();
}
public void Dispose()
{
_entry.Dispose();
}
private void Update()
{
if (!_configService.Current.EnableDtrEntry)
{
if (_entry.Shown)
{
Clear();
}
return;
}
if (!_entry.Shown)
{
_entry.Shown = true;
}
string text;
if (_apiController.IsConnected)
{
text = $"\uE044 {_pairManager.GetVisibleUserCount()}";
}
else
{
text = "\uE044 \uE04C";
}
if (!string.Equals(text, _text, StringComparison.Ordinal))
{
_text = text;
_entry.Text = text;
}
}
private void Clear()
{
_text = null;
_entry.Shown = false;
_entry.Text = null;
}
private async Task RunAsync()
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
Update();
await Task.Delay(1000, _cancellationTokenSource.Token).ConfigureAwait(false);
}
}
public Task StartAsync(CancellationToken cancellationToken)
{
_runTask = Task.Run(RunAsync, _cancellationTokenSource.Token);
return Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_cancellationTokenSource.Cancel();
await _runTask!.ConfigureAwait(false);
_cancellationTokenSource.Dispose();
Clear();
}
}

View File

@@ -515,6 +515,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
var profileDelay = _configService.Current.ProfileDelay;
var profileOnRight = _configService.Current.ProfilePopoutRight;
var enableRightClickMenu = _configService.Current.EnableRightClickMenus;
var enableDtrEntry = _configService.Current.EnableDtrEntry;
var preferNotesInsteadOfName = _configService.Current.PreferNotesOverNamesForVisible;
if (ImGui.Checkbox("Enable Game Right Click Menu Entries", ref enableRightClickMenu))
@@ -524,6 +525,13 @@ public class SettingsUi : WindowMediatorSubscriberBase
}
UiSharedService.DrawHelpText("This will add Mare related right click menu entries in the game UI on paired players.");
if (ImGui.Checkbox("Display status and visible pair count in Server Info Bar", ref enableDtrEntry))
{
_configService.Current.EnableDtrEntry = enableDtrEntry;
_configService.Save();
}
UiSharedService.DrawHelpText("This will add Mare connection status and visible pair count in the Server Info Bar.\nYou can further configure this through your Dalamud Settings.");
if (ImGui.Checkbox("Show separate Visible group", ref showVisibleSeparate))
{
_configService.Current.ShowVisibleUsersSeparately = showVisibleSeparate;