Skip to content

Commit

Permalink
Fix date sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Aug 13, 2023
1 parent 3bf0594 commit 2d1549a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.xpipe.app.comp.storage.store;

import io.xpipe.app.storage.DataStoreEntry;

import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Stream;

public interface StoreSortMode {

Expand All @@ -17,7 +20,7 @@ public String getId() {
@Override
public Comparator<StoreSection> comparator() {
return Comparator.<StoreSection, String>comparing(
e -> e.getWrapper().getName().toLowerCase(Locale.ROOT));
e -> e.getWrapper().getName().toLowerCase(Locale.ROOT));
}
};

Expand All @@ -30,7 +33,7 @@ public String getId() {
@Override
public Comparator<StoreSection> comparator() {
return Comparator.<StoreSection, String>comparing(
e -> e.getWrapper().getName().toLowerCase(Locale.ROOT))
e -> e.getWrapper().getName().toLowerCase(Locale.ROOT))
.reversed();
}
};
Expand All @@ -43,8 +46,12 @@ public String getId() {

@Override
public Comparator<StoreSection> comparator() {
return Comparator.<StoreSection, Instant>comparing(
e -> e.getWrapper().getLastAccess());
return Comparator.comparing(e -> {
return flatten(e)
.map(entry -> entry.getLastAccess())
.max(Comparator.naturalOrder())
.orElseThrow();
});
}
};

Expand All @@ -56,16 +63,29 @@ public String getId() {

@Override
public Comparator<StoreSection> comparator() {
return Comparator.<StoreSection, Instant>comparing(e -> e.getWrapper().getLastAccess())
.reversed();
return Comparator.<StoreSection, Instant>comparing(e -> {
return flatten(e)
.map(entry -> entry.getLastAccess())
.max(Comparator.naturalOrder())
.orElseThrow();
}).reversed();
}
};

static Stream<DataStoreEntry> flatten(StoreSection section) {
return Stream.concat(
Stream.of(section.getWrapper().getEntry()),
section.getChildren().stream().flatMap(section1 -> flatten(section1)));
}

static List<StoreSortMode> ALL = List.of(ALPHABETICAL_DESC, ALPHABETICAL_ASC, DATE_DESC, DATE_ASC);

static Optional<StoreSortMode> fromId(String id) {
return ALL.stream().filter(storeSortMode -> storeSortMode.getId().equals(id)).findFirst();
return ALL.stream()
.filter(storeSortMode -> storeSortMode.getId().equals(id))
.findFirst();
}

String getId();

Comparator<StoreSection> comparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class StoreViewState {
private StoreViewState() {
var val = AppCache.getIfPresent("sortMode", String.class)
.flatMap(StoreSortMode::fromId)
.orElse(StoreSortMode.ALPHABETICAL_DESC);
.orElse(StoreSortMode.DATE_ASC);
this.sortMode = new SimpleObjectProperty<>(val);
this.sortMode.addListener((observable, oldValue, newValue) -> {
AppCache.update("sortMode", newValue.getId());
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/xpipe/app/issue/ErrorEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ public static <T extends Throwable> T unreportableIfContains(T t, String... s) {

public static <T extends Throwable> T unreportableIf(T t, boolean b) {
if (b) {
// EVENT_BASES.put(t, ErrorEvent.fromThrowable(t).unreportable());
EVENT_BASES.put(t, ErrorEvent.fromThrowable(t).unreportable());
}
return t;
}

public static <T extends Throwable> T unreportable(T t) {
// EVENT_BASES.put(t, ErrorEvent.fromThrowable(t).unreportable());
EVENT_BASES.put(t, ErrorEvent.fromThrowable(t).unreportable());
return t;
}
}

0 comments on commit 2d1549a

Please sign in to comment.