Improve browser state data structure

This commit is contained in:
crschnick 2023-06-16 03:55:51 +00:00
parent 0274ade547
commit f8b2afe44c
4 changed files with 22 additions and 35 deletions

View file

@ -14,6 +14,7 @@ import io.xpipe.app.fxcomps.SimpleCompStructure;
import io.xpipe.app.fxcomps.augment.GrowAugment;
import io.xpipe.app.fxcomps.impl.FancyTooltipAugment;
import io.xpipe.app.fxcomps.impl.PrettyImageComp;
import io.xpipe.app.fxcomps.util.BindingsHelper;
import io.xpipe.app.fxcomps.util.PlatformThread;
import io.xpipe.app.util.BusyProperty;
import io.xpipe.app.util.ThreadHelper;
@ -138,9 +139,9 @@ public class BrowserComp extends SimpleComp {
private Node createTabs() {
var multi = new MultiContentComp(Map.of(
Comp.of(() -> createTabPane()),
Bindings.isNotEmpty(model.getOpenFileSystems()),
BindingsHelper.persist(Bindings.isNotEmpty(model.getOpenFileSystems())),
new BrowserWelcomeComp(model),
Bindings.isEmpty(model.getOpenFileSystems())));
BindingsHelper.persist(Bindings.isEmpty(model.getOpenFileSystems()))));
return multi.createRegion();
}

View file

@ -68,33 +68,33 @@ public class BrowserModel {
private final ObservableList<BrowserEntry> selection = FXCollections.observableArrayList();
public void restoreState(BrowserSavedState state) {
state.getLastSystems().forEach((uuid, s) -> {
var storageEntry = DataStorage.get().getStoreEntry(uuid);
state.getLastSystems().forEach(e -> {
var storageEntry = DataStorage.get().getStoreEntry(e.getUuid());
storageEntry.ifPresent(entry -> {
openFileSystemAsync(entry.getName(), entry.getStore().asNeeded(), s, new SimpleBooleanProperty());
openFileSystemAsync(entry.getName(), entry.getStore().asNeeded(), e.getPath(), new SimpleBooleanProperty());
});
});
}
public void reset() {
var map = new LinkedHashMap<UUID, String>();
var list = new ArrayList<BrowserSavedState.Entry>();
openFileSystems.forEach(model -> {
var storageEntry = DataStorage.get().getStoreEntryIfPresent(model.getStore());
storageEntry.ifPresent(
entry -> map.put(entry.getUuid(), model.getCurrentPath().get()));
entry -> list.add(new BrowserSavedState.Entry(entry.getUuid(), model.getCurrentPath().get())));
});
// Don't override state if it is empty
if (map.size() == 0) {
if (list.size() == 0) {
return;
}
var meaningful = map.size() > 1 || map.values().stream().allMatch(s -> s != null);
var meaningful = list.size() > 1 || list.stream().allMatch(s -> s.getPath() != null);
if (!meaningful) {
return;
}
var state = new BrowserSavedState(map);
var state = BrowserSavedState.builder().lastSystems(list).build();
state.save();
}

View file

@ -1,18 +1,18 @@
package io.xpipe.app.browser;
import io.xpipe.app.core.AppCache;
import io.xpipe.app.storage.DataStorage;
import io.xpipe.core.store.FileSystemStore;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
@Value
@Jacksonized
@Builder
@Getter
public class BrowserSavedState {
@ -26,29 +26,15 @@ public class BrowserSavedState {
@Value
@Jacksonized
@Builder
public static class RecentEntry {
public static class Entry {
String directory;
Instant time;
UUID uuid;
String path;
}
@NonNull
private final LinkedHashMap<UUID, String> lastSystems;
public BrowserSavedState() {
lastSystems = new LinkedHashMap<>();
}
public BrowserSavedState(@NonNull LinkedHashMap<UUID, String> lastSystems) {
this.lastSystems = lastSystems;
}
@NonNull List<Entry> lastSystems;
public void save() {
AppCache.update("browser-state", this);
}
public void open(FileSystemStore store) {
var storageEntry = DataStorage.get().getStoreEntryIfPresent(store);
storageEntry.ifPresent(entry -> lastSystems.put(entry.getUuid(), null));
}
}

View file

@ -45,8 +45,8 @@ public class BrowserWelcomeComp extends SimpleComp {
var storeList = new VBox();
storeList.setPadding(new Insets(0, 0, 0, 10));
storeList.setSpacing(8);
state.getLastSystems().forEach((uuid, s) -> {
var entry = DataStorage.get().getStoreEntry(uuid);
state.getLastSystems().forEach(e-> {
var entry = DataStorage.get().getStoreEntry(e.getUuid());
if (entry.isEmpty()) {
return;
}
@ -54,7 +54,7 @@ public class BrowserWelcomeComp extends SimpleComp {
var graphic =
entry.get().getProvider().getDisplayIconFileName(entry.get().getStore());
var view = new PrettyImageComp(new SimpleStringProperty(graphic), 24, 24);
var l = new Label(entry.get().getName() + (s != null ? ": " + s : ""), view.createRegion());
var l = new Label(entry.get().getName() + (e.getPath() != null ? ": " + e.getPath() : ""), view.createRegion());
l.setGraphicTextGap(10);
storeList.getChildren().add(l);
});