Skip to content

Commit

Permalink
feat: Generalized preference store.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 4, 2023
1 parent b67ae87 commit eaef37a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 158 deletions.
32 changes: 0 additions & 32 deletions src/main/java/viewtify/model/NamedPreferences.java

This file was deleted.

87 changes: 28 additions & 59 deletions src/main/java/viewtify/model/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@
import kiss.Storable;
import kiss.Variable;
import kiss.WiseFunction;
import viewtify.Viewtify;

public abstract class Preferences implements Storable<Preferences>, Extensible {

/** The cache for preferences. */
private static final Map<String, Preferences> cache = new ConcurrentHashMap();
private static final Map<Class, PreferencesList> CACHE = new ConcurrentHashMap();

/** The user defined name. */
public final Preference<String> name = initialize("");

/** The preference id. */
private String id;
/** The grouping container. */
Storable container;

/**
* Hide constructor.
Expand Down Expand Up @@ -108,31 +110,12 @@ protected final <V> Preference<V> initialize(Variable<V> defaultValue) {
}

/**
* Synchronize data from/to source.
*/
void sync() {
Viewtify.UserPreference.observing().to(x -> {
// Not all property values are preserved in the restore source, so they must always be
// reset before restoring. If not reset, some properties may continue to apply the
// previous user's values to the new user.
reset();
restore();
});
auto();
}

/**
* Synchronize data from/to source.
* {@inheritDoc}
*/
void sync2() {
Viewtify.UserPreference.observing().to(x -> {
// Not all property values are preserved in the restore source, so they must always be
// reset before restoring. If not reset, some properties may continue to apply the
// previous user's values to the new user.
reset();
restore();
});
auto();
@Override
public Preferences store() {
container.store();
return this;
}

/**
Expand Down Expand Up @@ -177,15 +160,7 @@ private <V> Variable<Preference<V>> findBy(Property property) {
}

/**
* {@inheritDoc}
*/
@Override
public String locate() {
return Viewtify.UserPreference.exact().file(id + ".json").path();
}

/**
* Get the user preferences by type.
* Search the user preference of the specified type.
*
* @param <P>
* @param type
Expand All @@ -196,46 +171,40 @@ public static <P extends Preferences> P of(Class<P> type) {
}

/**
* Get the user preferences by type and key.
* Search the user preference of the specified type and name.
*
* @param <P>
* @param type
* @param key
* @return
*/
public static <P extends Preferences> P of(Class<P> type, String key) {
String id = type.getName();
if (key != null) {
id = id + "@" + key;
}

return (P) cache.computeIfAbsent(id, x -> {
Preferences prefs = I.make(type);
prefs.id = x;
prefs.sync();
return prefs;
});
}
public static <P extends Preferences> P of(Class<P> type, String name) {
name = Objects.requireNonNullElse(name, "");

private static final Map<Class, PreferencesList> CACHE = new ConcurrentHashMap();

public static <P extends NamedPreferences> P by(Class<P> type, String key) {
PreferencesList<P> list = list(type);
PreferencesList<P> list = CACHE.computeIfAbsent(type, PreferencesList::new);
for (P preferences : list) {
if (preferences.name.is(key)) {
if (preferences.name.is(name)) {
return preferences;
}
}

P named = I.make(type);
named.name.set(key);
named.name.set(name);
named.container = list;
named.auto();

list.add(named);

return named;
}

public static <P extends Preferences> PreferencesList<P> list(Class<P> type) {
/**
* Search all user preferences of the specified type.
*
* @param <P>
* @param type
* @return
*/
public static <P extends Preferences> List<P> all(Class<P> type) {
return CACHE.computeIfAbsent(type, PreferencesList::new);
}

Expand Down
70 changes: 9 additions & 61 deletions src/main/java/viewtify/model/PreferencesList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@
*/
package viewtify.model;

import java.util.ArrayList;

import javafx.collections.ModifiableObservableListBase;
import java.util.concurrent.CopyOnWriteArrayList;

import kiss.Managed;
import kiss.Storable;
import viewtify.Viewtify;

final class PreferencesList<E extends Preferences> extends ModifiableObservableListBase<E> implements Storable<PreferencesList<E>> {
@SuppressWarnings("serial")
final class PreferencesList<E extends Preferences> extends CopyOnWriteArrayList<E> implements Storable<PreferencesList<E>> {

/** The model id. */
private final String id;

/** The actual container. */
private final ArrayList<E> list = new ArrayList();

/**
* Hide constructor.
*/
Expand All @@ -41,67 +37,19 @@ final class PreferencesList<E extends Preferences> extends ModifiableObservableL
}
}

sync();
}

/**
* Synchronize data from/to source.
*/
protected final void sync() {
// Synchronize data from/to source.
Viewtify.UserPreference.observing().to(x -> {
// Not all property values are preserved in the restore source, so they must always be
// reset before restoring. If not reset, some properties may continue to apply the
// previous user's values to the new user.
clear();
restore();
});
}

/**
* {@inheritDoc}
*/
@Override
public E get(int index) {
return list.get(index);
}

/**
* {@inheritDoc}
*/
@Override
public int size() {
return list.size();
}

/**
* {@inheritDoc}
*/
@Override
protected void doAdd(int index, E model) {
if (model != null) {
System.out.println("Add " + model);
model.sync2();
list.add(index, model);
}
}

/**
* {@inheritDoc}
*/
@Override
protected E doSet(int index, E model) {
System.out.println("Set " + model);
model.sync2();

return list.set(index, model);
}

/**
* {@inheritDoc}
*/
@Override
protected E doRemove(int model) {
return list.remove(model);
for (E item : this) {
item.container = this;
item.auto();
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected ViewDSL declareUI() {
*/
@Override
protected void initialize() {
TimeEventSourceSetting setting = Preferences.by(TimeEventSourceSetting.class, source.name());
TimeEventSourceSetting setting = Preferences.of(TimeEventSourceSetting.class, source.name());

enable.text(source.name()).sync(setting.enable);
color.disableWhen(enable.isNotSelected()).sync(setting.color, FXUtils::color, FXUtils::color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*/
package viewtify.ui.calendar;

import viewtify.model.NamedPreferences;
import viewtify.model.Preferences;

/**
* Preference for calendar.
*/
public class TimeEventSourceSetting extends NamedPreferences {
public class TimeEventSourceSetting extends Preferences {

/** The availability. */
public final Preference<Boolean> enable = initialize(true);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/viewtify/ui/view/PreferenceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ protected ViewDSL declareUI() {
*/
interface style extends StyleDSL {
Style left = () -> {
display.width(200, px);
padding.vertical(40, px).horizontal(10, px);
display.minWidth(180, px);
padding.vertical(40, px).left(10, px);
};

Style navi = () -> {
display.width(180, px);
display.minWidth(160, px);
font.size(14, px).smooth.grayscale();
padding.vertical(10, px).left(20, px);
cursor.pointer();
Expand Down

0 comments on commit eaef37a

Please sign in to comment.