-
Notifications
You must be signed in to change notification settings - Fork 0
Pagination / Search Dialog #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aae454b
feat: implement paginated dialog functionality with state management …
ammodev b6e5710
Merge branch 'version/1.21.11' into feat/default-dialogs
ammodev 1703f36
chore: update ABI and bump version [skip ci]
github-actions[bot] f7275c5
Merge branch 'version/1.21.11' into feat/default-dialogs
ammodev 16e81ba
feat: add dialog composition and pagination classes for enhanced dial…
ammodev 5fd8b42
Apply suggestions from code review
ammodev 2a090d0
feat: add dialog test commands for pagination and search functionality
ammodev f482637
Merge remote-tracking branch 'origin/feat/default-dialogs' into feat/…
ammodev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...it-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/dialog/composition/DialogScope.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| @file:Suppress("UnstableApiUsage") | ||
|
|
||
| package dev.slne.surf.surfapi.bukkit.api.dialog.composition | ||
|
|
||
| import dev.slne.surf.surfapi.bukkit.api.dialog.state.DialogState | ||
| import kotlinx.coroutines.CoroutineScope | ||
|
|
||
| class DialogScope<S : DialogState>( | ||
| private val store: DialogStore<S>, | ||
| private val scope: CoroutineScope, | ||
| ) { | ||
| fun state(): S = store.getState() | ||
|
|
||
| suspend fun setState(transform: S.() -> S) { | ||
| store.update(transform) | ||
| } | ||
|
|
||
| suspend fun <T> remember( | ||
| vararg keys: Any?, | ||
| block: suspend CoroutineScope.() -> T | ||
| ): T { | ||
| val key = keys.toList() | ||
| val cached = store.recall<T>(key) | ||
|
|
||
| if (cached != null) return cached | ||
|
|
||
| val value = block(scope) | ||
|
|
||
| store.remember<T>(key, value) | ||
|
|
||
| return value | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
...it-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/dialog/composition/DialogStore.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package dev.slne.surf.surfapi.bukkit.api.dialog.composition | ||
|
|
||
| import dev.slne.surf.surfapi.bukkit.api.dialog.state.DialogState | ||
| import dev.slne.surf.surfapi.bukkit.api.extensions.server | ||
| import io.papermc.paper.dialog.Dialog | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import java.util.* | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| class DialogStore<S : DialogState>( | ||
| initialState: S, | ||
| private val playerUuid: UUID, | ||
| private val renderer: suspend DialogScope<S>.() -> Dialog, | ||
| private val scope: CoroutineScope, | ||
| ) { | ||
| private var currentState: S = initialState | ||
| private var mounted = true | ||
|
|
||
| private val memory = ConcurrentHashMap<List<Any?>, Any?>() | ||
|
|
||
| suspend fun open() { | ||
| rerender() | ||
| } | ||
|
|
||
| suspend fun update(transform: S.() -> S) { | ||
| currentState = currentState.transform() | ||
| rerender() | ||
| } | ||
|
ammodev marked this conversation as resolved.
|
||
|
|
||
| fun getState(): S = currentState | ||
|
|
||
| internal suspend fun rerender() { | ||
| if (!mounted) return | ||
|
|
||
| val dialog = DialogScope(this, scope).renderer() | ||
| findPlayer()?.showDialog(dialog) | ||
| } | ||
|
ammodev marked this conversation as resolved.
|
||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| fun <T> remember(key: List<Any?>, value: Any?) { | ||
| memory[key] = value | ||
| } | ||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| fun <T> recall(key: List<Any?>): T? { | ||
| return memory[key] as? T | ||
| } | ||
|
|
||
| private fun findPlayer() = server.getPlayer(playerUuid) | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
...api-bukkit-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/dialog/composition/dsl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| @file:Suppress("UnstableApiUsage") | ||
|
|
||
| package dev.slne.surf.surfapi.bukkit.api.dialog.composition | ||
|
|
||
| import dev.slne.surf.surfapi.bukkit.api.dialog.state.DialogState | ||
| import io.papermc.paper.dialog.Dialog | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import org.bukkit.entity.Player | ||
|
|
||
| suspend fun <S : DialogState> composableDialog( | ||
| player: Player, | ||
| initialState: S, | ||
| scope: CoroutineScope, | ||
| content: suspend DialogScope<S>.() -> Dialog | ||
| ) { | ||
| val store = DialogStore( | ||
| initialState = initialState, | ||
| playerUuid = player.uniqueId, | ||
| renderer = content, | ||
| scope = scope | ||
| ) | ||
|
|
||
| store.open() | ||
| } | ||
|
ammodev marked this conversation as resolved.
|
||
148 changes: 148 additions & 0 deletions
148
...api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/dialog/pagination/PaginatedDialog.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| @file:Suppress("UnstableApiUsage") | ||
|
|
||
| package dev.slne.surf.surfapi.bukkit.api.dialog.pagination | ||
|
|
||
| import dev.slne.surf.surfapi.bukkit.api.dialog.base | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.builder.actionButton | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.composition.composableDialog | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.dialog | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.query.DialogQuery | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.query.PageResult | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.query.PageState | ||
| import dev.slne.surf.surfapi.bukkit.api.dialog.type | ||
| import dev.slne.surf.surfapi.core.api.messages.builder.SurfComponentBuilder | ||
| import io.papermc.paper.dialog.Dialog | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.launch | ||
| import net.kyori.adventure.text.Component | ||
| import org.bukkit.entity.Player | ||
|
|
||
| suspend fun <T> paginatedDialog( | ||
| player: Player, | ||
| query: DialogQuery<PageState, T>, | ||
| titleBuilder: SurfComponentBuilder.(PageState, PageResult<T>) -> Unit = { _, result -> | ||
| if (result.totalPages == 0) { | ||
| text("Keine Ergebnisse") | ||
| } else { | ||
| text("Seite ${result.page} von ${result.totalPages}") | ||
| } | ||
| }, | ||
| searchable: Boolean = false, | ||
| itemBuilder: (T) -> Pair<Component, Dialog>, | ||
| scope: CoroutineScope | ||
| ) = composableDialog( | ||
| player = player, | ||
| initialState = PageState(), | ||
| scope = scope | ||
| ) { | ||
| val state = state() | ||
| val page = remember(state.page, state.search) { | ||
| query.execute(state) | ||
| } | ||
|
|
||
| dialog { | ||
| base { | ||
| title { | ||
| titleBuilder(this, state, page) | ||
| } | ||
|
|
||
| if (searchable) { | ||
| input { | ||
| text("search") { | ||
| label { | ||
| text("Suche") | ||
| } | ||
|
|
||
| initial(state.search ?: "") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type { | ||
| multiAction { | ||
| columns(1) | ||
|
|
||
| // Item Buttons | ||
| page.items.forEach { item -> | ||
| val (dialogTitle, dialog) = itemBuilder(item) | ||
|
|
||
| action(actionButton { | ||
| label(dialogTitle) | ||
|
|
||
| action { | ||
| playerCallback { | ||
| player.showDialog(dialog) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Pagination Buttons | ||
| if (state.page > 1) { | ||
| action(actionButton { | ||
| label { | ||
| text("Zurück") | ||
| } | ||
|
|
||
| action { | ||
| playerCallback { | ||
| scope.launch { | ||
| setState { copy(page = page.page - 1) } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if (state.page < page.totalPages) { | ||
| action(actionButton { | ||
| label { | ||
| text("Weiter") | ||
| } | ||
|
|
||
| action { | ||
| playerCallback { | ||
| scope.launch { | ||
| setState { copy(page = page.page + 1) } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if (searchable) { | ||
| action(actionButton { | ||
| label { | ||
| text("Suche zurücksetzen") | ||
| } | ||
|
|
||
| action { | ||
| playerCallback { | ||
| scope.launch { | ||
| setState { copy(search = null, page = 1) } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| action(actionButton { | ||
| label { | ||
| text("Suchen") | ||
| } | ||
|
|
||
| action { | ||
| customPlayerClick { response, _ -> | ||
| val search = response.getText("search") ?: "" | ||
|
|
||
| scope.launch { | ||
| setState { copy(search = search, page = 1) } | ||
| } | ||
|
ammodev marked this conversation as resolved.
|
||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
...f-api-bukkit-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/dialog/query/queries.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package dev.slne.surf.surfapi.bukkit.api.dialog.query | ||
|
|
||
| import dev.slne.surf.surfapi.bukkit.api.dialog.state.DialogState | ||
|
|
||
| fun interface DialogQuery<S : DialogState, T> { | ||
| suspend fun execute(state: S): PageResult<T> | ||
| } | ||
|
|
||
| fun interface CursorDialogQuery<S : DialogState, T> { | ||
| suspend fun execute(state: S): CursorResult<T> | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.