Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 3, 2023
1 parent a473100 commit b67ae87
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/main/java/viewtify/model/NamedPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
package viewtify.model;

import kiss.Managed;
import kiss.Storable;

@Managed
public abstract class NamedPreferences extends Preferences {
public class NamedPreferences extends Preferences {

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

Storable container;

/**
* {@inheritDoc}
*/
@Override
public Preferences store() {
container.store();
System.out.println("Store " + this);
return this;
}
}
36 changes: 36 additions & 0 deletions src/main/java/viewtify/model/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ void sync() {
auto();
}

/**
* Synchronize data from/to source.
*/
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();
}

/**
* Observe the value change events.
*
Expand Down Expand Up @@ -203,6 +217,28 @@ public static <P extends Preferences> P of(Class<P> type, String key) {
});
}

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);
for (P preferences : list) {
if (preferences.name.is(key)) {
return preferences;
}
}

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

return named;
}

public static <P extends Preferences> PreferencesList<P> list(Class<P> type) {
return CACHE.computeIfAbsent(type, PreferencesList::new);
}

/**
* Preference value.
*/
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/viewtify/model/PreferencesList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2023 The VIEWTIFY Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package viewtify.model;

import java.util.ArrayList;

import javafx.collections.ModifiableObservableListBase;

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

final class PreferencesList<E extends Preferences> extends ModifiableObservableListBase<E> implements Storable<PreferencesList<E>> {

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

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

/**
* Hide constructor.
*/
PreferencesList(Class<E> type) {
Managed annotation = type.getAnnotation(Managed.class);
if (annotation == null) {
id = type.getName();
} else {
String name = annotation.name();
if (name == null || name.isBlank()) {
id = type.getName();
} else {
id = name;
}
}

sync();
}

/**
* Synchronize data from/to source.
*/
protected final 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.
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);
}

/**
* {@inheritDoc}
*/
@Override
public final String locate() {
return Viewtify.UserPreference.exact().file(id + ".json").path();
}
}
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.of(TimeEventSourceSetting.class, source.name());
TimeEventSourceSetting setting = Preferences.by(TimeEventSourceSetting.class, source.name());

enable.text(source.name()).sync(setting.enable);
color.disableWhen(enable.isNotSelected()).sync(setting.color, FXUtils::color, FXUtils::color);
Expand Down

0 comments on commit b67ae87

Please sign in to comment.