Skip to content

Commit

Permalink
interaction delay key per element. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek authored Nov 22, 2024
1 parent 261c5d8 commit 0e85045
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions common/src/main/java/net/infumia/frame/element/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import net.infumia.frame.context.element.ContextElementClick;
import net.infumia.frame.context.element.ContextElementRender;
Expand All @@ -25,6 +26,9 @@ public interface Element {
@Nullable
Consumer<ContextElementClick> onInteractionDelay();

@Nullable
Function<ContextElementClick, String> interactionDelayKey();

@Nullable
Predicate<ContextElementRender> displayIf();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.Duration;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import net.infumia.frame.context.element.ContextElementClick;
import net.infumia.frame.context.element.ContextElementRender;
Expand Down Expand Up @@ -35,6 +36,11 @@ public interface ElementBuilder {
@NotNull
ElementBuilder onInteractionDelay(@NotNull Consumer<ContextElementClick> onInteractionDelay);

@NotNull
ElementBuilder interactionDelayKey(
@NotNull Function<ContextElementClick, String> interactionDelayKey
);

@NotNull
ElementBuilder updateOnStateChange(@NotNull State<?> state, @NotNull State<?>... otherStates);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ ElementItemBuilder onInteractionDelay(
@NotNull Consumer<ContextElementClick> onInteractionDelay
);

@NotNull
@Override
ElementItemBuilder interactionDelayKey(
@NotNull Function<ContextElementClick, String> interactionDelayKey
);

@NotNull
@Override
ElementItemBuilder updateOnStateChange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.element.ContextElementClick;
Expand All @@ -23,6 +24,7 @@ public class ElementBuilderImpl<Self extends ElementBuilderImpl<Self>>
boolean updateOnClick;
Duration interactionDelay;
Consumer<ContextElementClick> onInteractionDelay;
Function<ContextElementClick, String> interactionDelayKey;
Predicate<ContextElementRender> displayIf;
Collection<State<?>> updateOnStateChange;
Collection<State<?>> updateOnStateAccess;
Expand Down Expand Up @@ -109,6 +111,15 @@ public Self onInteractionDelay(
return this.self();
}

@NotNull
@Override
public Self interactionDelayKey(
@NotNull final Function<ContextElementClick, String> interactionDelayKey
) {
this.interactionDelayKey = interactionDelayKey;
return this.self();
}

@NotNull
@Override
public Self updateOnStateChange(
Expand Down
13 changes: 11 additions & 2 deletions core/src/main/java/net/infumia/frame/element/ElementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.element.ContextElementClick;
Expand All @@ -25,13 +26,14 @@ public class ElementImpl implements ElementRich {
final boolean closeOnClick;
final Duration interactionDelay;
final Consumer<ContextElementClick> onInteractionDelay;
final Function<ContextElementClick, String> interactionDelayKey;
final Predicate<ContextElementRender> displayIf;
final Collection<State<?>> updateOnStateChange;
final Collection<State<?>> updateOnStateAccess;
private boolean visible = true;

public ElementImpl(
@NotNull final ElementBuilderImpl builder,
@NotNull final ElementBuilderImpl<?> builder,
@NotNull final ContextBase parent
) {
this.key = UUID.randomUUID().toString();
Expand All @@ -40,6 +42,7 @@ public ElementImpl(
this.closeOnClick = builder.closeOnClick;
this.interactionDelay = builder.interactionDelay;
this.onInteractionDelay = builder.onInteractionDelay;
this.interactionDelayKey = builder.interactionDelayKey;
this.displayIf = builder.displayIf;
this.updateOnStateChange = builder.updateOnStateChange;
this.updateOnStateAccess = builder.updateOnStateAccess;
Expand Down Expand Up @@ -87,7 +90,7 @@ public boolean intersects(@NotNull final Element element) {
@NotNull
@Override
public ElementBuilder toBuilder() {
return new ElementBuilderImpl(this);
return new ElementBuilderImpl<>(this);
}

@Override
Expand Down Expand Up @@ -117,6 +120,12 @@ public Consumer<ContextElementClick> onInteractionDelay() {
return this.onInteractionDelay;
}

@Nullable
@Override
public Function<ContextElementClick, String> interactionDelayKey() {
return this.interactionDelayKey;
}

@Nullable
@Override
public Predicate<ContextElementRender> displayIf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import net.infumia.frame.context.element.ContextElementClick;
import net.infumia.frame.element.ElementRich;
import net.infumia.frame.metadata.MetadataAccess;
Expand Down Expand Up @@ -44,7 +45,14 @@ public void accept(@NotNull final PipelineContextElement.Click ctx) {
lastInteractions = new HashMap<>();
metadata.setFixed(MetadataKeyHolder.LAST_INTERACTION_ELEMENT, lastInteractions);
}
final String key = element.key();
final Function<ContextElementClick, String> interactionDelayKey =
element.interactionDelayKey();
final String key;
if (interactionDelayKey == null) {
key = element.key();
} else {
key = interactionDelayKey.apply(context);
}
final Long lastInteraction = lastInteractions.get(key);
final long now = System.currentTimeMillis();
if (lastInteraction == null) {
Expand Down

0 comments on commit 0e85045

Please sign in to comment.