Fix date sorting

This commit is contained in:
crschnick 2023-08-13 09:31:13 +00:00
parent 3bf0594920
commit 2d1549a328
3 changed files with 30 additions and 10 deletions

View file

@ -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 {
@ -17,7 +20,7 @@ public interface StoreSortMode {
@Override
public Comparator<StoreSection> comparator() {
return Comparator.<StoreSection, String>comparing(
e -> e.getWrapper().getName().toLowerCase(Locale.ROOT));
e -> e.getWrapper().getName().toLowerCase(Locale.ROOT));
}
};
@ -30,7 +33,7 @@ public interface StoreSortMode {
@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();
}
};
@ -43,8 +46,12 @@ public interface StoreSortMode {
@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();
});
}
};
@ -56,16 +63,29 @@ public interface StoreSortMode {
@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();

View file

@ -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());

View file

@ -115,13 +115,13 @@ public class ErrorEvent {
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;
}
}