From 0e85045ac4395388f7415092af557c5c55fb99a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 22 Nov 2024 18:51:24 +0000 Subject: [PATCH] interaction delay key per element. (#21) --- .../java/net/infumia/frame/element/Element.java | 4 ++++ .../net/infumia/frame/element/ElementBuilder.java | 6 ++++++ .../infumia/frame/element/ElementItemBuilder.java | 6 ++++++ .../infumia/frame/element/ElementBuilderImpl.java | 11 +++++++++++ .../java/net/infumia/frame/element/ElementImpl.java | 13 +++++++++++-- .../element/ServiceClickInteractionDelay.java | 10 +++++++++- 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/infumia/frame/element/Element.java b/common/src/main/java/net/infumia/frame/element/Element.java index 8077d6a..dbaad45 100644 --- a/common/src/main/java/net/infumia/frame/element/Element.java +++ b/common/src/main/java/net/infumia/frame/element/Element.java @@ -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; @@ -25,6 +26,9 @@ public interface Element { @Nullable Consumer onInteractionDelay(); + @Nullable + Function interactionDelayKey(); + @Nullable Predicate displayIf(); diff --git a/common/src/main/java/net/infumia/frame/element/ElementBuilder.java b/common/src/main/java/net/infumia/frame/element/ElementBuilder.java index b24bdc3..22e124b 100644 --- a/common/src/main/java/net/infumia/frame/element/ElementBuilder.java +++ b/common/src/main/java/net/infumia/frame/element/ElementBuilder.java @@ -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; @@ -35,6 +36,11 @@ public interface ElementBuilder { @NotNull ElementBuilder onInteractionDelay(@NotNull Consumer onInteractionDelay); + @NotNull + ElementBuilder interactionDelayKey( + @NotNull Function interactionDelayKey + ); + @NotNull ElementBuilder updateOnStateChange(@NotNull State state, @NotNull State... otherStates); diff --git a/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java b/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java index bdaacfd..9872c50 100644 --- a/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java +++ b/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java @@ -61,6 +61,12 @@ ElementItemBuilder onInteractionDelay( @NotNull Consumer onInteractionDelay ); + @NotNull + @Override + ElementItemBuilder interactionDelayKey( + @NotNull Function interactionDelayKey + ); + @NotNull @Override ElementItemBuilder updateOnStateChange( diff --git a/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java b/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java index a25606a..3f600de 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java +++ b/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java @@ -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; @@ -23,6 +24,7 @@ public class ElementBuilderImpl> boolean updateOnClick; Duration interactionDelay; Consumer onInteractionDelay; + Function interactionDelayKey; Predicate displayIf; Collection> updateOnStateChange; Collection> updateOnStateAccess; @@ -109,6 +111,15 @@ public Self onInteractionDelay( return this.self(); } + @NotNull + @Override + public Self interactionDelayKey( + @NotNull final Function interactionDelayKey + ) { + this.interactionDelayKey = interactionDelayKey; + return this.self(); + } + @NotNull @Override public Self updateOnStateChange( diff --git a/core/src/main/java/net/infumia/frame/element/ElementImpl.java b/core/src/main/java/net/infumia/frame/element/ElementImpl.java index d13ea7f..0a8c6a8 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementImpl.java +++ b/core/src/main/java/net/infumia/frame/element/ElementImpl.java @@ -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; @@ -25,13 +26,14 @@ public class ElementImpl implements ElementRich { final boolean closeOnClick; final Duration interactionDelay; final Consumer onInteractionDelay; + final Function interactionDelayKey; final Predicate displayIf; final Collection> updateOnStateChange; final Collection> updateOnStateAccess; private boolean visible = true; public ElementImpl( - @NotNull final ElementBuilderImpl builder, + @NotNull final ElementBuilderImpl builder, @NotNull final ContextBase parent ) { this.key = UUID.randomUUID().toString(); @@ -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; @@ -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 @@ -117,6 +120,12 @@ public Consumer onInteractionDelay() { return this.onInteractionDelay; } + @Nullable + @Override + public Function interactionDelayKey() { + return this.interactionDelayKey; + } + @Nullable @Override public Predicate displayIf() { diff --git a/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickInteractionDelay.java b/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickInteractionDelay.java index a895cdd..111809c 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickInteractionDelay.java +++ b/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickInteractionDelay.java @@ -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; @@ -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 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) {