From 67dafeabf73a0a9a32af52c5adecf931b9c920a5 Mon Sep 17 00:00:00 2001 From: crschnick Date: Fri, 22 Mar 2024 09:03:01 +0000 Subject: [PATCH] Fix date ordering --- .../app/comp/store/StoreEntryWrapper.java | 2 +- .../xpipe/app/comp/store/StoreSortMode.java | 32 +++------------ .../xpipe/app/storage/DataStoreCategory.java | 9 ++--- .../io/xpipe/app/storage/DataStoreEntry.java | 40 ++++++++----------- .../io/xpipe/app/storage/StorageElement.java | 21 +++++----- 5 files changed, 37 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/comp/store/StoreEntryWrapper.java b/app/src/main/java/io/xpipe/app/comp/store/StoreEntryWrapper.java index f7fc8aa6..17a58aaf 100644 --- a/app/src/main/java/io/xpipe/app/comp/store/StoreEntryWrapper.java +++ b/app/src/main/java/io/xpipe/app/comp/store/StoreEntryWrapper.java @@ -199,7 +199,7 @@ public class StoreEntryWrapper { } var found = getDefaultActionProvider().getValue(); - entry.updateLastUsed(); + entry.notifyUpdate(true, false); if (found != null) { found.createAction(entry.ref()).execute(); } else { diff --git a/app/src/main/java/io/xpipe/app/comp/store/StoreSortMode.java b/app/src/main/java/io/xpipe/app/comp/store/StoreSortMode.java index d5a7c81c..a500fddd 100644 --- a/app/src/main/java/io/xpipe/app/comp/store/StoreSortMode.java +++ b/app/src/main/java/io/xpipe/app/comp/store/StoreSortMode.java @@ -49,15 +49,8 @@ public interface StoreSortMode { StoreSortMode DATE_DESC = new StoreSortMode() { @Override public StoreSection representative(StoreSection s) { - var c = comparator(); - return Stream.of( - s.getShownChildren().stream() - .max((o1, o2) -> { - return c.compare(representative(o1), representative(o2)); - }) - .orElse(s), - s) - .max(c) + return Stream.concat(s.getShownChildren().stream().map(this::representative), Stream.of(s)) + .max(Comparator.comparing(section -> section.getWrapper().getEntry().getLastAccess())) .orElseThrow(); } @@ -69,25 +62,15 @@ public interface StoreSortMode { @Override public Comparator comparator() { return Comparator.comparing(e -> { - return flatten(e) - .map(entry -> entry.getLastAccess()) - .max(Comparator.naturalOrder()) - .orElseThrow(); + return e.getWrapper().getEntry().getLastAccess(); }); } }; StoreSortMode DATE_ASC = new StoreSortMode() { @Override public StoreSection representative(StoreSection s) { - var c = comparator(); - return Stream.of( - s.getShownChildren().stream() - .min((o1, o2) -> { - return c.compare(representative(o1), representative(o2)); - }) - .orElse(s), - s) - .min(c) + return Stream.concat(s.getShownChildren().stream().map(this::representative), Stream.of(s)) + .max(Comparator.comparing(section -> section.getWrapper().getEntry().getLastAccess())) .orElseThrow(); } @@ -99,10 +82,7 @@ public interface StoreSortMode { @Override public Comparator comparator() { return Comparator.comparing(e -> { - return flatten(e) - .map(entry -> entry.getLastAccess()) - .max(Comparator.naturalOrder()) - .orElseThrow(); + return e.getWrapper().getEntry().getLastAccess(); }) .reversed(); } diff --git a/app/src/main/java/io/xpipe/app/storage/DataStoreCategory.java b/app/src/main/java/io/xpipe/app/storage/DataStoreCategory.java index 40906cf0..6e4eeb19 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStoreCategory.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStoreCategory.java @@ -116,8 +116,7 @@ public class DataStoreCategory extends StorageElement { var changed = this.sortMode != sortMode; if (changed) { this.sortMode = sortMode; - this.dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } } @@ -125,15 +124,13 @@ public class DataStoreCategory extends StorageElement { var changed = share != newShare; if (changed) { this.share = newShare; - this.dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } } public void setParentCategory(UUID parentCategory) { this.parentCategory = parentCategory; - this.dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } public boolean canShare() { diff --git a/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java b/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java index e2d11d94..e709f08d 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java @@ -214,11 +214,11 @@ public class DataStoreEntry extends StorageElement { var lastUsed = Optional.ofNullable(stateJson.get("lastUsed")) .map(jsonNode -> jsonNode.textValue()) .map(Instant::parse) - .orElse(Instant.now()); + .orElse(Instant.EPOCH); var lastModified = Optional.ofNullable(stateJson.get("lastModified")) .map(jsonNode -> jsonNode.textValue()) .map(Instant::parse) - .orElse(Instant.now()); + .orElse(Instant.EPOCH); var configuration = Optional.ofNullable(json.get("configuration")) .map(node -> { try { @@ -281,7 +281,7 @@ public class DataStoreEntry extends StorageElement { var changed = inRefresh != newRefresh; if (changed) { this.inRefresh = newRefresh; - notifyUpdate(); + notifyUpdate(false, false); } } @@ -291,7 +291,7 @@ public class DataStoreEntry extends StorageElement { public void setStoreCache(String key, Object value) { if (!Objects.equals(storeCache.put(key, value), value)) { - notifyUpdate(); + notifyUpdate(false, false); } } @@ -321,21 +321,18 @@ public class DataStoreEntry extends StorageElement { this.storePersistentState = value; this.storePersistentStateNode = JacksonMapper.getDefault().valueToTree(value); if (changed) { - this.dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } } public void setConfiguration(Configuration configuration) { this.configuration = configuration; - this.dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } public void setCategoryUuid(UUID categoryUuid) { - this.dirty = true; this.categoryUuid = categoryUuid; - notifyUpdate(); + notifyUpdate(false, true); } @Override @@ -376,8 +373,7 @@ public class DataStoreEntry extends StorageElement { var changed = expanded != this.expanded; this.expanded = expanded; if (changed) { - dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } } @@ -385,8 +381,7 @@ public class DataStoreEntry extends StorageElement { var changed = !Objects.equals(color, newColor); this.color = newColor; if (changed) { - dirty = true; - notifyUpdate(); + notifyUpdate(false, true); } } @@ -399,14 +394,12 @@ public class DataStoreEntry extends StorageElement { storeNode = e.storeNode; store = e.store; validity = e.validity; - lastModified = Instant.now(); - dirty = true; provider = e.provider; childrenCache = null; validity = store == null ? Validity.LOAD_FAILED : store.isComplete() ? Validity.COMPLETE : Validity.INCOMPLETE; storePersistentState = e.storePersistentState; storePersistentStateNode = e.storePersistentStateNode; - notifyUpdate(); + notifyUpdate(false, true); } public void setStoreInternal(DataStore store, boolean updateTime) { @@ -475,7 +468,8 @@ public class DataStoreEntry extends StorageElement { } validity = Validity.COMPLETE; - notifyUpdate(); + // Don't count this as modification as this is done always + notifyUpdate(false, false); return true; } @@ -500,7 +494,7 @@ public class DataStoreEntry extends StorageElement { } validity = Validity.INCOMPLETE; - notifyUpdate(); + notifyUpdate(false, false); return true; } @@ -509,14 +503,14 @@ public class DataStoreEntry extends StorageElement { if (store instanceof ExpandedLifecycleStore lifecycleStore) { try { inRefresh = true; - notifyUpdate(); + notifyUpdate(false, false); lifecycleStore.initializeValidate(); inRefresh = false; } catch (Exception e) { inRefresh = false; ErrorEvent.fromThrowable(e).handle(); } finally { - notifyUpdate(); + notifyUpdate(false, false); } } } @@ -526,12 +520,12 @@ public class DataStoreEntry extends StorageElement { if (store instanceof ExpandedLifecycleStore lifecycleStore) { try { inRefresh = true; - notifyUpdate(); + notifyUpdate(false, false); lifecycleStore.finalizeValidate(); } catch (Exception e) { ErrorEvent.fromThrowable(e).handle(); } finally { - notifyUpdate(); + notifyUpdate(false, false); } } } diff --git a/app/src/main/java/io/xpipe/app/storage/StorageElement.java b/app/src/main/java/io/xpipe/app/storage/StorageElement.java index 8582a39b..6a58734d 100644 --- a/app/src/main/java/io/xpipe/app/storage/StorageElement.java +++ b/app/src/main/java/io/xpipe/app/storage/StorageElement.java @@ -47,14 +47,15 @@ public abstract class StorageElement { public abstract Path[] getShareableFiles(); - public void updateLastUsed() { - this.lastUsed = Instant.now(); - this.dirty = true; - notifyUpdate(); - } - - protected void notifyUpdate() { - lastModified = Instant.now(); + public void notifyUpdate(boolean used, boolean modified) { + if (used) { + lastUsed = Instant.now(); + dirty = true; + } + if (modified) { + lastModified = Instant.now(); + dirty = true; + } listeners.forEach(l -> l.onUpdate()); } @@ -86,9 +87,7 @@ public abstract class StorageElement { } this.name = name; - this.dirty = true; - this.lastModified = Instant.now(); - notifyUpdate(); + notifyUpdate(false, true); } public interface Listener {