OrangeGuidanceTomestone/client/Ui/MainWindowTabs/MessageList.cs

74 lines
2.1 KiB
C#
Raw Normal View History

2022-09-04 05:03:59 +00:00
using ImGuiNET;
using Newtonsoft.Json;
using OrangeGuidanceTomestone.Helpers;
namespace OrangeGuidanceTomestone.Ui.MainWindowTabs;
internal class MessageList : ITab {
public string Name => "Your messages";
private Plugin Plugin { get; }
private SemaphoreSlim MessagesMutex { get; } = new(1, 1);
private List<MessageWithTerritory> Messages { get; } = new();
internal MessageList(Plugin plugin) {
this.Plugin = plugin;
}
public void Draw() {
if (ImGui.Button("Refresh")) {
this.Refresh();
}
2022-09-04 05:10:48 +00:00
2022-09-04 05:03:59 +00:00
this.MessagesMutex.Wait();
foreach (var message in this.Messages) {
ImGui.TextUnformatted(message.Text);
2022-09-04 05:15:27 +00:00
ImGui.TreePush();
2022-09-04 05:10:48 +00:00
ImGui.TextUnformatted($"Likes: {message.PositiveVotes}");
ImGui.TextUnformatted($"Dislikes: {message.NegativeVotes}");
if (ImGui.Button($"Delete##{message.Id}")) {
this.Delete(message.Id);
}
2022-09-04 05:15:27 +00:00
ImGui.TreePop();
ImGui.Separator();
2022-09-04 05:03:59 +00:00
}
this.MessagesMutex.Release();
}
private void Refresh() {
Task.Run(async () => {
var resp = await ServerHelper.SendRequest(
this.Plugin.Config.ApiKey,
HttpMethod.Get,
"/messages"
);
var json = await resp.Content.ReadAsStringAsync();
var messages = JsonConvert.DeserializeObject<MessageWithTerritory[]>(json)!;
await this.MessagesMutex.WaitAsync();
this.Messages.Clear();
this.Messages.AddRange(messages);
this.MessagesMutex.Release();
});
}
2022-09-04 05:10:48 +00:00
private void Delete(Guid id) {
Task.Run(async () => {
var resp = await ServerHelper.SendRequest(
this.Plugin.Config.ApiKey,
HttpMethod.Delete,
$"/messages/{id}"
);
if (resp.IsSuccessStatusCode) {
this.Refresh();
2022-09-04 05:15:27 +00:00
this.Plugin.Vfx.RemoveStatic(id);
2022-09-04 05:19:30 +00:00
this.Plugin.Messages.Remove(id);
2022-09-04 05:10:48 +00:00
}
});
}
2022-09-04 05:03:59 +00:00
}