From f3bc8f5390859a56ab211bfc217b776dfa84777f Mon Sep 17 00:00:00 2001 From: crschnick Date: Sat, 24 Feb 2024 06:44:16 +0000 Subject: [PATCH] Rework preview handling --- .../java/io/xpipe/app/ext/ScanProvider.java | 15 ++++++++++++++ .../io/xpipe/app/storage/DataStorage.java | 15 +++++++++----- .../io/xpipe/app/util/LicensedFeature.java | 20 ++++++++++++++++++- .../java/io/xpipe/app/util/ScanAlert.java | 11 +++++++++- .../java/io/xpipe/core/process/CountDown.java | 8 ++++---- 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/ext/ScanProvider.java b/app/src/main/java/io/xpipe/app/ext/ScanProvider.java index 565e379e0..0815c72fa 100644 --- a/app/src/main/java/io/xpipe/app/ext/ScanProvider.java +++ b/app/src/main/java/io/xpipe/app/ext/ScanProvider.java @@ -5,6 +5,7 @@ import io.xpipe.core.process.ShellControl; import io.xpipe.core.store.DataStore; import io.xpipe.core.util.FailableRunnable; import io.xpipe.core.util.ModuleLayerLoader; +import lombok.AllArgsConstructor; import lombok.Value; import java.util.Comparator; @@ -29,11 +30,25 @@ public abstract class ScanProvider { } @Value + @AllArgsConstructor public static class ScanOperation { String nameKey; boolean disabled; boolean defaultSelected; FailableRunnable scanner; + String licenseFeatureId; + + public ScanOperation(String nameKey, boolean disabled, boolean defaultSelected, FailableRunnable scanner) { + this.nameKey = nameKey; + this.disabled = disabled; + this.defaultSelected = defaultSelected; + this.scanner = scanner; + this.licenseFeatureId = null; + } + + public String getLicensedFeatureId() { + return licenseFeatureId; + } } public static class Loader implements ModuleLayerLoader { diff --git a/app/src/main/java/io/xpipe/app/storage/DataStorage.java b/app/src/main/java/io/xpipe/app/storage/DataStorage.java index af4075f0c..cac425c5c 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStorage.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStorage.java @@ -421,11 +421,16 @@ public abstract class DataStorage { // pair.getKey().setStoreInternal(pair.getValue().getStore(), false); // Update state by merging - if (pair.getKey().getStorePersistentState() != null - && pair.getValue().get().getStorePersistentState() != null) { - var mergedState = pair.getKey().getStorePersistentState().deepCopy(); - mergedState.merge(pair.getValue().get().getStorePersistentState()); - pair.getKey().setStorePersistentState(mergedState); + if (pair.getKey().getStorePersistentState() != null && pair.getValue().get().getStorePersistentState() != null) { + var classMatch = pair.getKey().getStorePersistentState().getClass() + .equals(pair.getValue().get().getStorePersistentState().getClass()); + // Children classes might not be the same, the same goes for state classes + // This can happen when there are multiple child classes and the ids got switched around + if (classMatch) { + var mergedState = pair.getKey().getStorePersistentState().deepCopy(); + mergedState.merge(pair.getValue().get().getStorePersistentState()); + pair.getKey().setStorePersistentState(mergedState); + } } }); saveAsync(); diff --git a/app/src/main/java/io/xpipe/app/util/LicensedFeature.java b/app/src/main/java/io/xpipe/app/util/LicensedFeature.java index 050c14dca..768e503ff 100644 --- a/app/src/main/java/io/xpipe/app/util/LicensedFeature.java +++ b/app/src/main/java/io/xpipe/app/util/LicensedFeature.java @@ -1,7 +1,21 @@ package io.xpipe.app.util; +import java.util.Optional; + public interface LicensedFeature { + default Optional getDescriptionSuffix() { + if (isSupported()) { + return Optional.empty(); + } + + if (isPreviewSupported()) { + return Optional.of("Preview"); + } + + return Optional.of("Pro"); + } + String getId(); String getDisplayName(); @@ -12,5 +26,9 @@ public interface LicensedFeature { boolean isPreviewSupported(); - void throwIfUnsupported() throws LicenseRequiredException; + default void throwIfUnsupported() throws LicenseRequiredException { + if (!isSupported()) { + throw new LicenseRequiredException(this); + } + } } diff --git a/app/src/main/java/io/xpipe/app/util/ScanAlert.java b/app/src/main/java/io/xpipe/app/util/ScanAlert.java index 018b21106..802f9664a 100644 --- a/app/src/main/java/io/xpipe/app/util/ScanAlert.java +++ b/app/src/main/java/io/xpipe/app/util/ScanAlert.java @@ -180,9 +180,18 @@ public class ScanAlert { .filter(scanOperation -> scanOperation.isDefaultSelected() && !scanOperation.isDisabled()) .toList()); + Function nameFunc = (ScanProvider.ScanOperation s) -> { + var n = AppI18n.get(s.getNameKey()); + if (s.getLicensedFeatureId() == null) { + return n; + } + + var suffix = LicenseProvider.get().getFeature(s.getLicensedFeatureId()); + return n + suffix.getDescriptionSuffix().map(d -> " (" + d + ")").orElse(""); + }; var r = new ListSelectorComp<>( a, - scanOperation -> AppI18n.get(scanOperation.getNameKey()), + nameFunc, selected, scanOperation -> scanOperation.isDisabled(), a.size() > 3) diff --git a/core/src/main/java/io/xpipe/core/process/CountDown.java b/core/src/main/java/io/xpipe/core/process/CountDown.java index e2d243218..59d579f6a 100644 --- a/core/src/main/java/io/xpipe/core/process/CountDown.java +++ b/core/src/main/java/io/xpipe/core/process/CountDown.java @@ -5,14 +5,14 @@ import lombok.Setter; public class CountDown { - private long lastMillis = -1; - private long millisecondsLeft; + private volatile long lastMillis = -1; + private volatile long millisecondsLeft; @Setter - private boolean active; + private volatile boolean active; @Getter - private long maxMillis; + private volatile long maxMillis; private CountDown() {}