From 050485fece029fe43c0be2985a79c62770a990da Mon Sep 17 00:00:00 2001 From: Infi Date: Sat, 11 May 2024 06:47:21 +0200 Subject: [PATCH] Load up to 1000 emotes --- ChatTwo/EmoteCache.cs | 23 ++++++++++++----- ChatTwo/Ui/SettingsTabs/Emote.cs | 44 ++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/ChatTwo/EmoteCache.cs b/ChatTwo/EmoteCache.cs index a1bd1d5..a84f480 100644 --- a/ChatTwo/EmoteCache.cs +++ b/ChatTwo/EmoteCache.cs @@ -15,6 +15,9 @@ public static class EmoteCache { [JsonPropertyName("emote")] public Emote Emote { get; set; } + + [JsonPropertyName("id")] + public string Id { get; set; } } public struct Emote @@ -29,7 +32,7 @@ public static class EmoteCache public bool Animated { get; set; } }; - private enum LoadingState + public enum LoadingState { Unloaded, Loading, @@ -39,12 +42,13 @@ public static class EmoteCache private const string BetterTTV = "https://api.betterttv.net/3"; private const string GlobalEmotes = $"{BetterTTV}/cached/emotes/global"; private const string Top100Emotes = $"{BetterTTV}/emotes/shared/top?limit=100"; + private const string Top100BeforeEmotes = "{0}/emotes/shared/top?before={1}&limit=100"; private const string EmotePath = "https://cdn.betterttv.net/emote/{0}/3x"; private static readonly HttpClient Client = new(); // All of this data is uninitalized while State is not `LoadingState.Done` - private static LoadingState State = LoadingState.Unloaded; + public static LoadingState State = LoadingState.Unloaded; private static Dictionary EmoteImages = new(); private static Dictionary Cache = new(); @@ -65,11 +69,18 @@ public static class EmoteCache foreach (var emote in JsonSerializer.Deserialize(globalList)!) Cache.TryAdd(emote.Code, emote); - var top = await Client.GetAsync(Top100Emotes); - var topList = await top.Content.ReadAsStringAsync(); + var lastId = string.Empty; + for (var i = 0; i < 10; i++) + { + var top = await Client.GetAsync(lastId == string.Empty ? Top100Emotes : Top100BeforeEmotes.Format(BetterTTV, lastId)); + var topList = await top.Content.ReadAsStringAsync(); - foreach (var emote in JsonSerializer.Deserialize>(topList)!) - Cache.TryAdd(emote.Emote.Code, emote.Emote); + var jsonList = JsonSerializer.Deserialize>(topList)!; + foreach (var emote in jsonList) + Cache.TryAdd(emote.Emote.Code, emote.Emote); + + lastId = jsonList.Last().Id; + } EmoteCodeArray = Cache.Keys.ToArray(); State = LoadingState.Done; diff --git a/ChatTwo/Ui/SettingsTabs/Emote.cs b/ChatTwo/Ui/SettingsTabs/Emote.cs index 9a0a8d4..66acb9d 100644 --- a/ChatTwo/Ui/SettingsTabs/Emote.cs +++ b/ChatTwo/Ui/SettingsTabs/Emote.cs @@ -2,6 +2,7 @@ using System.Numerics; using ChatTwo.Resources; using ChatTwo.Util; using Dalamud.Interface; +using Dalamud.Interface.Colors; using Dalamud.Interface.Utility.Raii; using ImGuiNET; @@ -44,26 +45,41 @@ internal sealed class Emote : ISettingsTab if (SearchSelector.SelectorPopup("WordAddPopup", out var newWord, WordPopupOptions)) Mutable.BlockedEmotes.Add(newWord); - using var table = ImRaii.Table("##BlockedWords", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner); - if (table) + using(var table = ImRaii.Table("##BlockedWords", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner)) { - ImGui.TableSetupColumn(Language.Options_Emote_EmoteTable); - ImGui.TableSetupColumn("##Del", 0, 0.07f); - - ImGui.TableHeadersRow(); - - var copiedList = Mutable.BlockedEmotes.ToArray(); - foreach (var word in copiedList) + if (table) { - ImGui.TableNextColumn(); - ImGui.TextUnformatted(word); + ImGui.TableSetupColumn(Language.Options_Emote_EmoteTable); + ImGui.TableSetupColumn("##Del", 0, 0.07f); - ImGui.TableNextColumn(); - if (ImGuiUtil.Button($"##{word}Del", FontAwesomeIcon.Trash, !ImGui.GetIO().KeyCtrl)) - Mutable.BlockedEmotes.Remove(word); + ImGui.TableHeadersRow(); + + var copiedList = Mutable.BlockedEmotes.ToArray(); + foreach (var word in copiedList) + { + ImGui.TableNextColumn(); + ImGui.TextUnformatted(word); + + ImGui.TableNextColumn(); + if (ImGuiUtil.Button($"##{word}Del", FontAwesomeIcon.Trash, !ImGui.GetIO().KeyCtrl)) + Mutable.BlockedEmotes.Remove(word); + } } } + ImGui.Spacing(); + ImGui.Separator(); + ImGui.Spacing(); + + if (EmoteCache.State is EmoteCache.LoadingState.Done) + ImGui.TextColored(ImGuiColors.HealerGreen, "Ready"); + else + ImGui.TextColored(ImGuiColors.DPSRed, "Not Ready"); + + ImGui.TextUnformatted($"Emotes loaded: {EmoteCache.EmoteCodeArray.Length}"); + foreach (var word in EmoteCache.EmoteCodeArray) + ImGui.TextUnformatted(word); + ImGui.PopTextWrapPos(); } }