Skip to content

Stores and remembered properties

A signal created in init() lives as long as one opening of its UI. A volume setting must outlive that: several UIs read it, and it must still be there at the next launch. A store holds such values; a remembered property keeps one field of a UI across launches.

You will learn

  • How long a value lives, from a local variable to the disk
  • How to write a store and use it from several UIs
  • How a store reaches the disk and comes back
  • How to remember a field of a UI with UIProperty

Pick the shortest life that fits:

The value must livePut it in
For one opening of the UIA local variable or signal of init()
As long as the UI objectA field of the UI
Across launches, for one UIA field marked UIProperty
As long as the UI is open, shared by its partsA LOCAL store
As long as the program, shared by every UIA GLOBAL store
Across launches, shared by every UIA PERMANENT store

init() runs again at each opening and at each reload, so a signal created there starts over each time.

A store is a class that extends UIStore and carries UIStoreData. Its values are signals, so the nodes that show them follow them:

package com.example.state;
import java.util.LinkedHashMap;
import java.util.Map;
import dev.joid.lib.signal.impl.primitive.BooleanSignal;
import dev.joid.lib.signal.impl.primitive.IntegerSignal;
import dev.joid.lib.ui.core.hook.store.UIStore;
import dev.joid.lib.ui.core.hook.store.UIStoreData;
import dev.joid.lib.ui.core.hook.store.UIStoreScope;
@UIStoreData(id = "sound", scope = UIStoreScope.PERMANENT)
public class SettingsStore extends UIStore {
private final IntegerSignal volume = new IntegerSignal(5);
private final BooleanSignal muted = new BooleanSignal(false);
public IntegerSignal getVolume() {
return this.volume;
}
public BooleanSignal getMuted() {
return this.muted;
}
@Override
public Map<String, Object> toJson() {
final Map<String, Object> json = new LinkedHashMap<>();
json.put("volume", this.volume.peek());
json.put("muted", this.muted.peek());
return json;
}
@Override
public void fromJson(final Map<String, Object> json) {
if (json.containsKey("volume")) {
this.volume.set(((Number) json.get("volume")).intValue());
}
if (json.containsKey("muted")) {
this.muted.set((Boolean) json.get("muted"));
}
}
}

scope takes a UIStoreScope:

ScopeOne instanceEnds
LOCAL, the defaultPer UI that uses itWhen that UI closes, with a call of its destroy() hook, empty by default
GLOBALFor the whole programWith the program
PERMANENTFor the whole programWith the program, after its values reach the disk

toJson() and fromJson(...) matter only for a PERMANENT store: toJson() puts its values in a Map<String, Object>, and fromJson(...) receives that map back. JOID turns the map into JSON and back itself (Keep a store on disk).

A class without @UIStoreData is refused with an IllegalStateException at its first use.

UI.useStore gives the store of a class, and creates it at the first call:

final SettingsStore settings = super.useStore(SettingsStore.class, SettingsStore::new);

Full file: SettingsUI.java

RectNode
.create(200, 60, 120, 60)
.color(SettingsUI.ACCENT)
.onClick(event -> settings.getVolume().increment())
.label(Text.create("+", sign))
.attach(card);
TextNode
.create(360, 72)
.text(Text.create(settings.getVolume().map(volume -> "Volume: " + volume), info))
.attach(card);

Full file: SettingsUI.java

The overlay that shows the volume asks for the same store, and gets the same instance:

final SettingsStore settings = super.useStore(SettingsStore.class, SettingsStore::new);
RectNode
.create(1560, 40, 320, 70)
.color(VolumeOverlay.ACCENT)
.label(Text.create(settings.getVolume().map(volume -> "Volume " + volume), info))
.attach(this);

Full file: VolumeOverlay.java

Two UIs, one store: the overlay follows the volume that the settings screen changes.

The second argument is the factory: JOID calls it when no instance exists yet, then calls the init() hook of the new store, empty by default, where a store prepares what its fields do not. A LOCAL store is the same instance for every call made by one UI, and stays through its reloads; another UI gets its own. A node reaches the store of its UI with Node.useStore.

The store of Write a store is PERMANENT:

@UIStoreData(id = "sound", scope = UIStoreScope.PERMANENT)

Full file: SettingsStore.java

A PERMANENT store is written to store/<id>.store in the data folder, here store/sound.store, as the JSON of toJson():

At its first use in a launch, JOID creates it with the factory, calls init(), then fromJson(...) with the content of the file when the file exists. fromJson receives only the keys that the file holds, so test each key: a store saved before a new value existed starts that value from its default.

The map comes back with the types of JSON, whatever the type you put in it:

In the fileIn the map of fromJson
A whole numberLong
A decimal numberDouble
true or falseBoolean
A textString
An arrayList<Object>
An objectMap<String, Object>

Read a number through Number, as in ((Number) json.get("volume")).intValue(), never with a cast to Integer. toJson() writes any other object field by field, and it comes back as a Map; a null value is not written.

Without an id, the file takes the full name of the class: give an id to a store that you may rename or move.

When the file holds invalid JSON, or when fromJson throws, JOID renames the file to <id>.store.corrupt, warns, and starts the store from its defaults, as when no file exists: the next save writes a fresh file and the old data stays for you to inspect.

Build a settings screen binds controls to a store kept on disk.

A field marked UIProperty keeps its value from one launch to the next:

@UIProperty
private int opened;

Full file: SettingsUI.java

this.opened++;

Full file: SettingsUI.java

The count is written when the UI closes and read back before the next init(), so each opening adds one to it.

JOID writes the remembered fields of a UI to property/<class name>.property in the data folder, as JSON, and reads them back into the fields before each init(). It writes them when the UI closes, before a reload, and when the program ends, if their text changed.

  • The field is not final nor static; any type that Gson can convert works, from an int to a list of records.
  • The key is the name of the field; @UIProperty("key") gives another one, to rename the field without losing the value.
  • A field set to null is removed from the file, and comes back with its default value.
  • A value that the file holds with another type is refused with a warning, and the field keeps its default.
  • The file belongs to the class: two instances of one UI class share their remembered values.
  • Give a value the shortest life that fits: init(), a field, @UIProperty, then a LOCAL, GLOBAL or PERMANENT store.
  • A store extends UIStore, carries @UIStoreData, and holds signals.
  • useStore(Class, factory) gives the one instance of its scope, created by the factory at the first call.
  • A PERMANENT store is saved through toJson() when a UI closes and when the program ends, and restored through fromJson().
  • A store file of invalid JSON becomes .store.corrupt, and the store starts from its defaults.
  • @UIProperty remembers a field of a UI across launches, read before each init().

Remember the last tab. A settings screen has three tabs, and must open on the tab that was open last time. Where do you keep the index of the tab?

Solution

In a field of the screen marked @UIProperty, such as @UIProperty private int tab;. Only that screen reads it, so a store would share it for nothing.

A store that loses its values. A store keeps the name of the player, but every launch starts with an empty name. Which two places of the store do you check?

Solution

The scope: only @UIStoreData(scope = UIStoreScope.PERMANENT) reaches the disk. Then toJson() and fromJson(...): the default toJson() writes an empty object, so the store writes the name there and reads it back.

Report a problem with this page