diff --git a/inventory-framework-api/src/main/java/me/devnatan/inventoryframework/component/PaginationElementConsumer.java b/inventory-framework-api/src/main/java/me/devnatan/inventoryframework/component/PaginationElementConsumer.java new file mode 100644 index 00000000..db7432e4 --- /dev/null +++ b/inventory-framework-api/src/main/java/me/devnatan/inventoryframework/component/PaginationElementConsumer.java @@ -0,0 +1,24 @@ +package me.devnatan.inventoryframework.component; + +import org.jetbrains.annotations.NotNull; + +/** + * Represents an pagination element mapping operation that accepts three arguments and returns no result. + * This is a {@link FunctionalInterface functional interface} whose functional method is {@link #accept(Object, Object, int, Object)}. + * @param The type of the pagination context + * @param The builder used to build the paginated element + * @param The value that represents the current element being paginated + */ +@FunctionalInterface +public interface PaginationElementConsumer { + + /** + * Performs this operation on the given arguments. + * + * @param context The pagination context. + * @param builder The builder used to modify the element being paginated + * @param index The index of the element being paginated in the pagination + * @param value The value that represents the current element being paginated + */ + void accept(@NotNull Context context, @NotNull Builder builder, int index, @NotNull V value); +} diff --git a/inventory-framework-core/src/main/java/me/devnatan/inventoryframework/component/PaginationImpl.java b/inventory-framework-core/src/main/java/me/devnatan/inventoryframework/component/PaginationImpl.java index c675c260..f0709b7c 100644 --- a/inventory-framework-core/src/main/java/me/devnatan/inventoryframework/component/PaginationImpl.java +++ b/inventory-framework-core/src/main/java/me/devnatan/inventoryframework/component/PaginationImpl.java @@ -15,10 +15,7 @@ import java.util.function.Supplier; import me.devnatan.inventoryframework.ViewContainer; import me.devnatan.inventoryframework.VirtualView; -import me.devnatan.inventoryframework.context.IFContext; -import me.devnatan.inventoryframework.context.IFRenderContext; -import me.devnatan.inventoryframework.context.IFSlotClickContext; -import me.devnatan.inventoryframework.context.IFSlotRenderContext; +import me.devnatan.inventoryframework.context.*; import me.devnatan.inventoryframework.internal.LayoutSlot; import me.devnatan.inventoryframework.state.AbstractStateValue; import me.devnatan.inventoryframework.state.State; @@ -253,7 +250,7 @@ private void loadComponentsForLayeredPagination(IFRenderContext context, List * in context, if a layout is configured in the layout so this property must be the count of * {@link #getLayoutTarget() layout target} characters in the layout configured layout. *

- * When without a configured layout in the root, the page size is the entire size of {@link IFContext#getContainer() context's container}. + * When without a configured layout in the root, the page size is the entire size of context's container. * * @param context The render context. */ diff --git a/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PaginationStateBuilder.java b/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PaginationStateBuilder.java index 58064eb2..a1bc4d3c 100644 --- a/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PaginationStateBuilder.java +++ b/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PaginationStateBuilder.java @@ -1,10 +1,7 @@ package me.devnatan.inventoryframework; import java.util.function.BiConsumer; -import me.devnatan.inventoryframework.component.ComponentFactory; -import me.devnatan.inventoryframework.component.ItemComponentBuilder; -import me.devnatan.inventoryframework.component.Pagination; -import me.devnatan.inventoryframework.component.PaginationElementFactory; +import me.devnatan.inventoryframework.component.*; import me.devnatan.inventoryframework.context.IFContext; import me.devnatan.inventoryframework.context.IFSlotContext; import me.devnatan.inventoryframework.internal.LayoutSlot; @@ -30,7 +27,7 @@ public final class PaginationStateBuilder< } /** - * Sets the element factory for pagination. + * Sets the item factory for pagination. *

* It consists of a function whose first parameter is a derivation of the * {@link ItemComponentBuilder} that must be used to configure the item, and the second @@ -38,13 +35,12 @@ public final class PaginationStateBuilder< *

* This function is called for every single paginated element. * - * @param elementFactory The element factory. + * @param itemFactory The item factory. * @return This pagination state builder. */ - public PaginationStateBuilder elementFactory( - @NotNull PaginationElementFactory elementFactory) { - this.elementFactory = elementFactory; - return this; + public PaginationStateBuilder itemFactory( + @NotNull BiConsumer itemFactory) { + return itemFactory(((context, builder, index, value) -> itemFactory.accept(builder, value))); } /** @@ -60,21 +56,22 @@ public PaginationStateBuilder elementFactory( * @return This pagination state builder. */ public PaginationStateBuilder itemFactory( - @NotNull BiConsumer itemFactory) { - return elementFactory((context, index, slot, value) -> { + @NotNull PaginationElementConsumer itemFactory) { + this.elementFactory = (context, index, slot, value) -> { @SuppressWarnings("unchecked") Builder builder = (Builder) root.getElementFactory().createComponentBuilder(context); builder.withSlot(slot).withExternallyManaged(true); - itemFactory.accept(builder, value); + itemFactory.accept(context, builder, index, value); return builder; - }); + }; + return this; } /** * Defines a target character in the layout whose pagination will be rendered. *

* By default, if there is a layout available and a target character has not - * been explicitly defined in the layout, the layout's renderization target + * been explicitly defined in the layout, the layout's rendering target * character will be the {@link LayoutSlot#FILLED_RESERVED_CHAR reserved layout character}. *

* If there is no layout configured, pagination will be rendered throughout the view container. diff --git a/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PlatformView.java b/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PlatformView.java index cf389e23..99ee8bdd 100644 --- a/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PlatformView.java +++ b/inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PlatformView.java @@ -9,11 +9,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.Supplier; -import me.devnatan.inventoryframework.component.ComponentFactory; -import me.devnatan.inventoryframework.component.ItemComponentBuilder; -import me.devnatan.inventoryframework.component.Pagination; -import me.devnatan.inventoryframework.component.PaginationElementFactory; -import me.devnatan.inventoryframework.component.PaginationImpl; +import me.devnatan.inventoryframework.component.*; import me.devnatan.inventoryframework.context.IFCloseContext; import me.devnatan.inventoryframework.context.IFContext; import me.devnatan.inventoryframework.context.IFOpenContext; @@ -528,6 +524,23 @@ protected final State paginationState( .build(); } + /** + * Creates a new unmodifiable static pagination state. + * + * @param sourceProvider The data source for pagination. + * @param itemFactory The function for creating pagination items, this function is called for + * each paged element (item) on a page. + * @param The pagination data type. + * @return A new immutable pagination state. + */ + protected final State paginationState( + @NotNull List sourceProvider, + @NotNull PaginationElementConsumer itemFactory) { + return this.buildPaginationState(sourceProvider) + .itemFactory(itemFactory) + .build(); + } + /** * Creates a new unmodifiable dynamic pagination state. * @@ -544,6 +557,23 @@ protected final State paginationState( .build(); } + /** + * Creates a new unmodifiable dynamic pagination state. + * + * @param sourceProvider The data source for pagination. + * @param itemFactory The function for creating pagination items, this function is called for + * each paged element (item) on a page. + * @param The pagination data type. + * @return A new immutable pagination state. + */ + protected final State paginationState( + @NotNull Function> sourceProvider, + @NotNull PaginationElementConsumer itemFactory) { + return this.buildPaginationState(sourceProvider) + .itemFactory(itemFactory) + .build(); + } + /** * Creates a new unmodifiable dynamic pagination state. * @@ -560,6 +590,23 @@ protected final State paginationState( .build(); } + /** + * Creates a new unmodifiable dynamic pagination state. + * + * @param sourceProvider The data source for pagination. + * @param itemFactory The function for creating pagination items, this function is called for + * each paged element (item) on a page. + * @param The pagination data type. + * @return A new immutable pagination state. + */ + protected final State paginationState( + @NotNull Supplier> sourceProvider, + @NotNull PaginationElementConsumer itemFactory) { + return this.buildPaginationState(sourceProvider) + .itemFactory(itemFactory) + .build(); + } + /** * Creates a new unmodifiable asynchronous pagination state. *

@@ -581,6 +628,27 @@ protected final State asyncPaginationState( .build(); } + /** + * Creates a new unmodifiable asynchronous pagination state. + *

+ * This API is experimental and is not subject to the general compatibility guarantees + * such API may be changed or may be removed completely in any further release. + * + * @param sourceProvider The asynchronous data source for pagination. + * @param itemFactory The function for creating pagination items, this function is called for + * each paged element (item) on a page. + * @param The pagination data type. + * @return A new immutable pagination state. + */ + @ApiStatus.Experimental + protected final State asyncPaginationState( + @NotNull Function>> sourceProvider, + @NotNull PaginationElementConsumer itemFactory) { + return this.buildAsyncPaginationState(sourceProvider) + .itemFactory(itemFactory) + .build(); + } + /** * Creates a new unmodifiable static pagination state builder. *