Skip to content

Commit

Permalink
Add Next/Previous sections to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Jan 27, 2025
1 parent 3c72883 commit 3ffe36e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface IPaginationComponentCreator : IComponentCreator
public DiscordButtonComponent CreateNextPageButton(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages);
public DiscordButtonComponent CreateLastPageButton(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages);
public DiscordSelectComponent CreateDropdown(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages);
public DiscordSelectComponentOption CreatePreviousSectionOption(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages);
public DiscordSelectComponentOption CreateNextSectionOption(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,12 @@ public DiscordSelectComponent CreateDropdown(Ulid id, int currentPageIndex, IRea
int section = currentPageIndex / 23;
if (section > 0)
{
int previousSectionIndex = Math.Max(0, ((section - 1) * 23) - 12);
int previousSectionMaxIndex = previousSectionIndex + CalculatePageCount(previousSectionIndex, pages.Count);
options.Insert(0, new DiscordSelectComponentOption(
label: "Previous Section",
value: $"{(section - 1) * 23}{char.MinValue}",
description: $"Go to the previous section (pages {previousSectionIndex + 1}-{previousSectionMaxIndex})",
isDefault: false,
emoji: new DiscordComponentEmoji("⬅️")
));
options.Insert(0, CreatePreviousSectionOption(id, currentPageIndex, pages));
}

if (maxIndex < pages.Count)
{
int nextSectionIndex = Math.Max(1, ((section + 1) * 23) - 12);
int nextSectionMaxIndex = nextSectionIndex + CalculatePageCount(nextSectionIndex, pages.Count);
options.Add(new DiscordSelectComponentOption(
label: "Next Section",
value: $"{(section + 1) * 23}{char.MinValue}",
description: $"Go to the next section (pages {nextSectionIndex + 1}-{nextSectionMaxIndex})",
isDefault: false,
emoji: new DiscordComponentEmoji("➡️")
));
options.Add(CreateNextSectionOption(id, currentPageIndex, pages));
}
}

Expand All @@ -105,5 +89,33 @@ private static int CalculatePageCount(int startingIndex, int totalPageCount)
// Return 23 so that the "Previous Section" and "Next Section" buttons are added.
return totalPageCount - startingIndex > 24 ? 23 : totalPageCount - startingIndex;
}

public DiscordSelectComponentOption CreatePreviousSectionOption(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages)
{
int section = currentPageIndex / 23;
int previousSectionIndex = Math.Max(0, ((section - 1) * 23) - 12);
int previousSectionMaxIndex = previousSectionIndex + CalculatePageCount(previousSectionIndex, pages.Count);
return new DiscordSelectComponentOption(
label: "Previous Section",
value: $"{(section - 1) * 23}{char.MinValue}",
description: $"Go to the previous section (pages {previousSectionIndex + 1}-{previousSectionMaxIndex})",
isDefault: false,
emoji: new DiscordComponentEmoji("⬅️")
);
}

public DiscordSelectComponentOption CreateNextSectionOption(Ulid id, int currentPageIndex, IReadOnlyList<Page> pages)
{
int section = currentPageIndex / 23;
int nextSectionIndex = Math.Max(1, ((section + 1) * 23) - 12);
int nextSectionMaxIndex = nextSectionIndex + CalculatePageCount(nextSectionIndex, pages.Count);
return new DiscordSelectComponentOption(
label: "Next Section",
value: $"{(section + 1) * 23}{char.MinValue}",
description: $"Go to the next section (pages {nextSectionIndex + 1}-{nextSectionMaxIndex})",
isDefault: false,
emoji: new DiscordComponentEmoji("➡️")
);
}
}
}

0 comments on commit 3ffe36e

Please sign in to comment.