Skip to content

Commit

Permalink
Component state registry and access implementation (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNatan authored Dec 9, 2023
1 parent d6602b0 commit 757e5cc
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public interface Component extends VirtualView, Pipelined {
@ApiStatus.Internal
boolean isManagedExternally();

// TODO Needs documentation
boolean shouldRender(IFContext context);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
public interface IFSlotClickContext extends IFSlotContext, IFConfinedContext {

// TODO needs documentation
@NotNull
ViewContainer getClickedContainer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final SELF onClick(@Nullable Consumer<? super SlotClickContext> clickHand
/** {@inheritDoc} */
@Override
public Component buildComponent(VirtualView root) {
return new BukkitComponentImpl<>(
return new BukkitComponentImpl(
getKey(),
root,
getReference(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
import java.util.function.Predicate;
import me.devnatan.inventoryframework.Ref;
import me.devnatan.inventoryframework.VirtualView;
import me.devnatan.inventoryframework.context.Context;
import me.devnatan.inventoryframework.context.IFComponentRenderContext;
import me.devnatan.inventoryframework.context.IFComponentUpdateContext;
import me.devnatan.inventoryframework.context.IFContext;
import me.devnatan.inventoryframework.context.IFSlotClickContext;
import me.devnatan.inventoryframework.state.State;
import org.jetbrains.annotations.NotNull;

public final class BukkitComponentImpl<COMPONENT_BUILDER extends BukkitComponentBuilder<COMPONENT_BUILDER>>
extends PlatformComponent<Context, COMPONENT_BUILDER> {
public final class BukkitComponentImpl extends PlatformComponent {

BukkitComponentImpl(
String key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import me.devnatan.inventoryframework.Ref;
import me.devnatan.inventoryframework.VirtualView;
import me.devnatan.inventoryframework.context.ComponentRenderContext;
import me.devnatan.inventoryframework.context.Context;
import me.devnatan.inventoryframework.context.IFComponentContext;
import me.devnatan.inventoryframework.context.IFComponentRenderContext;
import me.devnatan.inventoryframework.context.IFComponentUpdateContext;
Expand All @@ -22,7 +21,7 @@
/**
* {@link ItemComponent} implementation for Bukkit platform.
*/
public final class BukkitItemComponentImpl extends PlatformComponent<Context, Void> implements ItemComponent {
public final class BukkitItemComponentImpl extends PlatformComponent implements ItemComponent {

private int position;
private final ItemStack stack;
Expand Down Expand Up @@ -55,7 +54,7 @@ public final class BukkitItemComponentImpl extends PlatformComponent<Context, Vo
updateOnClick);
this.position = position;
this.stack = itemStack;
setHandle(new Handle(this));
setHandle(new Handle());
}

@Override
Expand Down Expand Up @@ -110,12 +109,6 @@ public String toString() {

class Handle extends BukkitComponentHandle<BukkitItemComponentBuilder<Void>> {

private BukkitItemComponentImpl component;

Handle(BukkitItemComponentImpl component) {
this.component = component;
}

@Override
public void rendered(@NotNull ComponentRenderContext context) {
final BukkitItemComponentImpl component = (BukkitItemComponentImpl) context.getComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public CloseContext(@NotNull Viewer subject, @NotNull IFRenderContext parent) {
this.parent = parent;
}

// TODO Needs documentation
public final @NotNull Player getPlayer() {
return player;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public RenderContext(
return (View) root;
}

// TODO documentation
public @NotNull Player getPlayer() {
tryThrowDoNotWorkWithSharedContext("getAllPlayers");
return player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public abstract class PlatformView<
private FRAMEWORK framework;
private boolean initialized;
private final StateAccess<PLATFORM_CONTEXT, ITEM_BUILDER> stateAccess =
new StateAccessImpl<>(this, getElementFactory(), getStateRegistry());
new StateAccessImpl<>(this, getStateRegistry());

// region Open & Close
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,139 +4,149 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;
import me.devnatan.inventoryframework.VirtualView;
import me.devnatan.inventoryframework.state.MutableIntState;
import me.devnatan.inventoryframework.state.MutableState;
import me.devnatan.inventoryframework.state.State;
import me.devnatan.inventoryframework.state.StateAccess;
import me.devnatan.inventoryframework.state.StateAccessImpl;
import me.devnatan.inventoryframework.state.StateRegistry;
import org.jetbrains.annotations.NotNull;

public abstract class AbstractComponentHandle<CONTEXT, COMPONENT_BUILDER> extends ComponentHandle
implements StateAccess<CONTEXT, COMPONENT_BUILDER> {
implements StateAccess<CONTEXT, COMPONENT_BUILDER>, VirtualView {

private final StateRegistry stateRegistry = new StateRegistry();
private final StateAccess<CONTEXT, COMPONENT_BUILDER> stateAccess = new StateAccessImpl<>(this, stateRegistry);

protected AbstractComponentHandle() {}

public abstract COMPONENT_BUILDER builder();

@Override
public <T> State<T> state(T initialValue) {
return null;
public final <T> State<T> state(T initialValue) {
return stateAccess.state(initialValue);
}

@Override
public <T> MutableState<T> mutableState(T initialValue) {
return null;
public final <T> MutableState<T> mutableState(T initialValue) {
return stateAccess.mutableState(initialValue);
}

@Override
public MutableIntState mutableState(int initialValue) {
return null;
public final MutableIntState mutableState(int initialValue) {
return stateAccess.mutableState(initialValue);
}

@Override
public <T> State<T> computedState(@NotNull Function<CONTEXT, T> computation) {
return null;
public final <T> State<T> computedState(@NotNull Function<CONTEXT, T> computation) {
return stateAccess.computedState(computation);
}

@Override
public <T> State<T> computedState(@NotNull Supplier<T> computation) {
return null;
public final <T> State<T> computedState(@NotNull Supplier<T> computation) {
return stateAccess.computedState(computation);
}

@Override
public <T> State<T> lazyState(@NotNull Function<CONTEXT, T> computation) {
return null;
public final <T> State<T> lazyState(@NotNull Function<CONTEXT, T> computation) {
return stateAccess.lazyState(computation);
}

@Override
public <T> State<T> lazyState(@NotNull Supplier<T> computation) {
return null;
public final <T> State<T> lazyState(@NotNull Supplier<T> computation) {
return stateAccess.lazyState(computation);
}

@Override
public <T> MutableState<T> initialState() {
return null;
public final <T> MutableState<T> initialState() {
return stateAccess.initialState();
}

@Override
public <T> MutableState<T> initialState(@NotNull String key) {
return null;
public final <T> MutableState<T> initialState(@NotNull String key) {
return stateAccess.initialState(key);
}

@Override
public <T> State<Pagination> paginationState(
public final <T> State<Pagination> paginationState(
@NotNull List<? super T> sourceProvider,
@NotNull PaginationValueConsumer<CONTEXT, COMPONENT_BUILDER, T> elementConsumer) {
return null;
return stateAccess.paginationState(sourceProvider, elementConsumer);
}

@Override
public <T> State<Pagination> computedPaginationState(
public final <T> State<Pagination> computedPaginationState(
@NotNull Function<CONTEXT, List<? super T>> sourceProvider,
@NotNull PaginationValueConsumer<CONTEXT, COMPONENT_BUILDER, T> valueConsumer) {
return null;

return stateAccess.computedPaginationState(sourceProvider, valueConsumer);
}

@Override
public <T> State<Pagination> computedAsyncPaginationState(
public final <T> State<Pagination> computedAsyncPaginationState(
@NotNull Function<CONTEXT, CompletableFuture<List<T>>> sourceProvider,
@NotNull PaginationValueConsumer<CONTEXT, COMPONENT_BUILDER, T> valueConsumer) {
return null;
return stateAccess.computedAsyncPaginationState(sourceProvider, valueConsumer);
}

@Override
public <T> State<Pagination> lazyPaginationState(
public final <T> State<Pagination> lazyPaginationState(
@NotNull Function<CONTEXT, List<? super T>> sourceProvider,
@NotNull PaginationValueConsumer<CONTEXT, COMPONENT_BUILDER, T> valueConsumer) {
return null;

return stateAccess.lazyPaginationState(sourceProvider, valueConsumer);
}

@Override
public <T> State<Pagination> lazyPaginationState(
public final <T> State<Pagination> lazyPaginationState(
@NotNull Supplier<List<? super T>> sourceProvider,
@NotNull PaginationValueConsumer<CONTEXT, COMPONENT_BUILDER, T> valueConsumer) {
return null;
return stateAccess.lazyPaginationState(sourceProvider, valueConsumer);
}

@Override
public <T> State<Pagination> lazyAsyncPaginationState(
public final <T> State<Pagination> lazyAsyncPaginationState(
@NotNull Function<CONTEXT, CompletableFuture<List<T>>> sourceProvider,
@NotNull PaginationValueConsumer<CONTEXT, COMPONENT_BUILDER, T> valueConsumer) {
return null;

return stateAccess.lazyAsyncPaginationState(sourceProvider, valueConsumer);
}

@Override
public <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildPaginationState(
public final <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildPaginationState(
@NotNull List<? super T> sourceProvider) {
return null;
return stateAccess.buildPaginationState(sourceProvider);
}

@Override
public <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildComputedPaginationState(
public final <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildComputedPaginationState(
@NotNull Function<CONTEXT, List<? super T>> sourceProvider) {
return null;
return stateAccess.buildComputedPaginationState(sourceProvider);
}

@Override
public <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildComputedAsyncPaginationState(
public final <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildComputedAsyncPaginationState(
@NotNull Function<CONTEXT, CompletableFuture<List<T>>> sourceProvider) {
return null;
return stateAccess.buildComputedAsyncPaginationState(sourceProvider);
}

@Override
public <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildLazyPaginationState(
public final <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildLazyPaginationState(
@NotNull Supplier<List<? super T>> sourceProvider) {
return null;
return stateAccess.buildLazyPaginationState(sourceProvider);
}

@Override
public <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildLazyPaginationState(
public final <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildLazyPaginationState(
@NotNull Function<CONTEXT, List<? super T>> sourceProvider) {
return null;
return stateAccess.buildLazyPaginationState(sourceProvider);
}

@Override
public <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildLazyAsyncPaginationState(
public final <T> PaginationBuilder<CONTEXT, COMPONENT_BUILDER, T> buildLazyAsyncPaginationState(
@NotNull Function<CONTEXT, CompletableFuture<List<T>>> sourceProvider) {
return null;
return stateAccess.buildLazyAsyncPaginationState(sourceProvider);
}
// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import java.util.function.BiConsumer;
import me.devnatan.inventoryframework.VirtualView;
import me.devnatan.inventoryframework.internal.ElementFactory;
import me.devnatan.inventoryframework.internal.LayoutSlot;
import me.devnatan.inventoryframework.internal.PlatformUtils;
import me.devnatan.inventoryframework.state.State;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public final class PaginationBuilder<CONTEXT, BUILDER, V>
extends PlatformComponentBuilder<PaginationBuilder<CONTEXT, BUILDER, V>, CONTEXT> {

private final ElementFactory internalElementFactory;
private final Object sourceProvider;
private char layoutTarget = LayoutSlot.DEFAULT_SLOT_FILL_CHAR;
private PaginationElementFactory<V> paginationElementFactory;
Expand All @@ -23,9 +22,7 @@ public final class PaginationBuilder<CONTEXT, BUILDER, V>
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public PaginationBuilder(
ElementFactory internalElementFactory, Object sourceProvider, boolean async, boolean computed) {
this.internalElementFactory = internalElementFactory;
public PaginationBuilder(Object sourceProvider, boolean async, boolean computed) {
this.sourceProvider = sourceProvider;
this.async = async;
this.computed = computed;
Expand Down Expand Up @@ -64,7 +61,7 @@ public PaginationBuilder<CONTEXT, BUILDER, V> elementFactory(
@NotNull PaginationValueConsumer<CONTEXT, BUILDER, V> elementConsumer) {
this.paginationElementFactory = (pagination, index, slot, value) -> {
CONTEXT context = (CONTEXT) pagination.getRoot();
BUILDER builder = (BUILDER) internalElementFactory.createComponentBuilder(pagination);
BUILDER builder = (BUILDER) PlatformUtils.getFactory().createComponentBuilder(pagination);
if (builder instanceof ItemComponentBuilder) ((ItemComponentBuilder) builder).setPosition(slot);

elementConsumer.accept(context, builder, index, value);
Expand Down
Loading

0 comments on commit 757e5cc

Please sign in to comment.