refactor: extract tabs into files

This commit is contained in:
Anna 2023-11-13 23:14:57 -05:00
parent ed81c07715
commit be6ca6a176
Signed by: anna
GPG Key ID: D0943384CD9F87D1
3 changed files with 159 additions and 108 deletions

View File

@ -2,6 +2,7 @@ using System.Numerics;
using Dalamud.Interface.Utility;
using EorzeaVotes.Config;
using EorzeaVotes.Model;
using EorzeaVotes.Ui.Tabs;
using EorzeaVotes.Util;
using ImGuiNET;
@ -17,12 +18,17 @@ internal class PluginUi : IDisposable {
#endif
private string _dateScratch = string.Empty;
private bool _voting;
private readonly List<IDrawable> _drawables = new();
private QuestionsTab QuestionsTab { get; }
private SettingsTab SettingsTab { get; }
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.QuestionsTab = new QuestionsTab(this.Plugin);
this.SettingsTab = new SettingsTab(this.Plugin);
this.UpdateDateScratch();
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
}
@ -31,7 +37,23 @@ internal class PluginUi : IDisposable {
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
}
private void Draw() {
private void UpdateDateScratch() {
this._dateScratch = this.Plugin.Config.BirthDate?.ToString("O") ?? string.Empty;
}
internal bool IsMoreDetailsOpen(Guid id) {
return this._drawables.Any(d => d is MoreDetailsWindow mdw && mdw.Question.Id == id);
}
internal void OpenMoreDetails(FullQuestion question) {
if (this.IsMoreDetailsOpen(question.Id)) {
return;
}
this._drawables.Add(new MoreDetailsWindow(this.Plugin, question));
}
private void DrawDrawables() {
this._drawables.RemoveAll(drawable => {
try {
return drawable.Draw() == DrawStatus.Finished;
@ -40,6 +62,10 @@ internal class PluginUi : IDisposable {
return true;
}
});
}
private void Draw() {
this.DrawDrawables();
if (!this.Visible) {
return;
@ -61,7 +87,7 @@ internal class PluginUi : IDisposable {
}
}
private bool DrawOptionalQuestions() {
internal bool DrawOptionalQuestions() {
var anyChanged = false;
anyChanged |= ImGuiHelper.EnumDropDownVertical(
@ -79,7 +105,8 @@ internal class PluginUi : IDisposable {
);
if (dateInput) {
this._dateScratch = this.Plugin.Config.BirthDate?.ToString("O") ?? string.Empty;
anyChanged = true;
this.UpdateDateScratch();
}
if (this.Plugin.Config.BirthDate != null) {
@ -150,108 +177,7 @@ internal class PluginUi : IDisposable {
using var endTabBar = new OnDispose(ImGui.EndTabBar);
this.DrawQuestionsTab();
this.DrawSettingsTab();
}
private void DrawQuestionsTab() {
if (!ImGui.BeginTabItem("Questions")) {
return;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
if (this.Plugin.Manager.Count == 0) {
ImGuiHelpers.CenteredText("No questions yet - waiting for plugin approval.");
return;
}
ImGuiHelpers.CenteredText("Active question");
var active = this.Plugin.Manager.FirstOrDefault(q => q.Active);
if (active == null) {
ImGui.TextUnformatted("There is no active question at the moment.");
} else {
ImGui.TextUnformatted(active.Text);
for (var i = 0; i < active.Answers.Length; i++) {
if (i != 0) {
ImGui.SameLine();
}
var disabled = this._voting || active is FullQuestion;
using var endDisabled = ImGuiHelper.WithDisabled(disabled);
if (!ImGui.Button(active.Answers[i])) {
continue;
}
this._voting = true;
var answer = i;
Task.Run(async () => {
try {
// first vote
await this.Plugin.Manager.Vote(active.Id, (ushort) answer);
// then get updated info
await this.Plugin.Manager.Check();
} finally {
this._voting = false;
}
});
}
if (active is FullQuestion full) {
full.DrawResponses();
var open = this._drawables.Any(drawable => drawable is MoreDetailsWindow mdw && mdw.Question.Id == full.Id);
using (ImGuiHelper.WithDisabled(open)) {
if (ImGui.Button($"More details##{full.Id}", new Vector2(ImGui.GetContentRegionAvail().X, 0))) {
this._drawables.Add(new MoreDetailsWindow(this.Plugin, full));
}
}
}
}
ImGui.Separator();
this.DrawInactive();
}
private void DrawInactive() {
ImGuiHelpers.CenteredText("Inactive questions");
using var endChild = new OnDispose(ImGui.EndChild);
if (!ImGui.BeginChild("ev-inactive-questions", ImGui.GetContentRegionAvail(), false)) {
return;
}
foreach (var question in this.Plugin.Manager) {
if (question.Active || question is not FullQuestion full) {
continue;
}
ImGui.TextUnformatted(full.Text);
full.DrawResponses();
var open = this._drawables.Any(drawable => drawable is MoreDetailsWindow mdw && mdw.Question.Id == full.Id);
using (ImGuiHelper.WithDisabled(open)) {
if (ImGui.Button($"More details##{full.Id}", new Vector2(ImGui.GetContentRegionAvail().X, 0))) {
this._drawables.Add(new MoreDetailsWindow(this.Plugin, full));
}
}
ImGui.Spacing();
}
}
private void DrawSettingsTab() {
if (!ImGui.BeginTabItem("Settings")) {
return;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
if (this.DrawOptionalQuestions()) {
this.Plugin.SaveConfig();
}
this.QuestionsTab.Draw();
this.SettingsTab.Draw();
}
}

View File

@ -0,0 +1,101 @@
using System.Numerics;
using Dalamud.Interface.Utility;
using EorzeaVotes.Model;
using EorzeaVotes.Util;
using ImGuiNET;
namespace EorzeaVotes.Ui.Tabs;
internal class QuestionsTab {
private Plugin Plugin { get; }
private bool _voting;
internal QuestionsTab(Plugin plugin) {
this.Plugin = plugin;
}
internal void Draw() {
if (!ImGui.BeginTabItem("Questions")) {
return;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
if (this.Plugin.Manager.Count == 0) {
ImGuiHelpers.CenteredText("No questions yet - waiting for plugin approval.");
return;
}
ImGuiHelpers.CenteredText("Active question");
var active = this.Plugin.Manager.FirstOrDefault(q => q.Active);
if (active == null) {
ImGui.TextUnformatted("There is no active question at the moment.");
} else {
ImGui.TextUnformatted(active.Text);
for (var i = 0; i < active.Answers.Length; i++) {
if (i != 0) {
ImGui.SameLine();
}
var disabled = this._voting || active is FullQuestion;
using var endDisabled = ImGuiHelper.WithDisabled(disabled);
if (!ImGui.Button(active.Answers[i])) {
continue;
}
this._voting = true;
var answer = i;
Task.Run(async () => {
try {
// first vote
await this.Plugin.Manager.Vote(active.Id, (ushort) answer);
// then get updated info
await this.Plugin.Manager.Check();
} finally {
this._voting = false;
}
});
}
if (active is FullQuestion full) {
full.DrawResponses();
this.DrawMoreDetailsButton(full);
}
}
ImGui.Separator();
this.DrawInactive();
}
private void DrawMoreDetailsButton(FullQuestion full) {
var open = this.Plugin.Ui.IsMoreDetailsOpen(full.Id);
using var endDisabled = ImGuiHelper.WithDisabled(open);
if (ImGui.Button($"More details##{full.Id}", new Vector2(ImGui.GetContentRegionAvail().X, 0))) {
this.Plugin.Ui.OpenMoreDetails(full);
}
}
private void DrawInactive() {
ImGuiHelpers.CenteredText("Inactive questions");
using var endChild = new OnDispose(ImGui.EndChild);
if (!ImGui.BeginChild("ev-inactive-questions", ImGui.GetContentRegionAvail(), false)) {
return;
}
foreach (var question in this.Plugin.Manager) {
if (question.Active || question is not FullQuestion full) {
continue;
}
ImGui.TextUnformatted(full.Text);
full.DrawResponses();
this.DrawMoreDetailsButton(full);
ImGui.Spacing();
}
}
}

View File

@ -0,0 +1,24 @@
using EorzeaVotes.Util;
using ImGuiNET;
namespace EorzeaVotes.Ui.Tabs;
internal class SettingsTab {
private Plugin Plugin { get; }
internal SettingsTab(Plugin plugin) {
this.Plugin = plugin;
}
internal void Draw() {
if (!ImGui.BeginTabItem("Settings")) {
return;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
if (this.Plugin.Ui.DrawOptionalQuestions()) {
this.Plugin.SaveConfig();
}
}
}