Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <Context> The type of the pagination context
* @param <Builder> The builder used to build the paginated element
* @param <V> The value that represents the current element being paginated
*/
@FunctionalInterface
public interface PaginationElementConsumer<Context, Builder, V> {

/**
* 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -30,21 +27,20 @@ public final class PaginationStateBuilder<
}

/**
* Sets the element factory for pagination.
* Sets the item factory for pagination.
* <p>
* 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
* parameter is the current element being paginated.
* <p>
* 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<Context, SlotContext, Builder, V> elementFactory(
@NotNull PaginationElementFactory<Context, V> elementFactory) {
this.elementFactory = elementFactory;
return this;
public PaginationStateBuilder<Context, SlotContext, Builder, V> itemFactory(
@NotNull BiConsumer<Builder, V> itemFactory) {
return itemFactory(((context, builder, index, value) -> itemFactory.accept(builder, value)));
}

/**
Expand All @@ -60,21 +56,22 @@ public PaginationStateBuilder<Context, SlotContext, Builder, V> elementFactory(
* @return This pagination state builder.
*/
public PaginationStateBuilder<Context, SlotContext, Builder, V> itemFactory(
@NotNull BiConsumer<Builder, V> itemFactory) {
return elementFactory((context, index, slot, value) -> {
@NotNull PaginationElementConsumer<Context, Builder, V> 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.
* <p>
* 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}.
* <p>
* If there is no layout configured, pagination will be rendered throughout the view container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -528,6 +524,23 @@ protected final <T> State<Pagination> 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 <T> The pagination data type.
* @return A new immutable pagination state.
*/
protected final <T> State<Pagination> paginationState(
@NotNull List<? super T> sourceProvider,
@NotNull PaginationElementConsumer<TContext, TItem, T> itemFactory) {
return this.<T>buildPaginationState(sourceProvider)
.itemFactory(itemFactory)
.build();
}

/**
* Creates a new unmodifiable dynamic pagination state.
*
Expand All @@ -544,6 +557,23 @@ protected final <T> State<Pagination> 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 <T> The pagination data type.
* @return A new immutable pagination state.
*/
protected final <T> State<Pagination> paginationState(
@NotNull Function<TContext, List<? super T>> sourceProvider,
@NotNull PaginationElementConsumer<TContext, TItem, T> itemFactory) {
return this.buildPaginationState(sourceProvider)
.itemFactory(itemFactory)
.build();
}

/**
* Creates a new unmodifiable dynamic pagination state.
*
Expand All @@ -560,6 +590,23 @@ protected final <T> State<Pagination> 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 <T> The pagination data type.
* @return A new immutable pagination state.
*/
protected final <T> State<Pagination> paginationState(
@NotNull Supplier<List<? super T>> sourceProvider,
@NotNull PaginationElementConsumer<TContext, TItem, T> itemFactory) {
return this.buildPaginationState(sourceProvider)
.itemFactory(itemFactory)
.build();
}

/**
* Creates a new unmodifiable asynchronous pagination state.
* <p>
Expand All @@ -581,6 +628,27 @@ protected final <T> State<Pagination> asyncPaginationState(
.build();
}

/**
* Creates a new unmodifiable asynchronous pagination state.
* <p>
* <b><i> 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. </i></b>
*
* @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 <T> The pagination data type.
* @return A new immutable pagination state.
*/
@ApiStatus.Experimental
protected final <T> State<Pagination> asyncPaginationState(
@NotNull Function<TContext, CompletableFuture<List<T>>> sourceProvider,
@NotNull PaginationElementConsumer<TContext, TItem, T> itemFactory) {
return this.buildAsyncPaginationState(sourceProvider)
.itemFactory(itemFactory)
.build();
}

/**
* Creates a new unmodifiable static pagination state builder.
*
Expand Down