Skip to content

Commit

Permalink
fix: remove StorableList and StorableModel
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 4, 2023
1 parent 650c27c commit e1abc00
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 161 deletions.
12 changes: 11 additions & 1 deletion src/main/java/viewtify/model/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class Preferences implements Storable<Preferences>, Extensible {
public final Preference<String> name = initialize("");

/** The grouping container. */
Storable container;
protected Storable container;

/**
* Hide constructor.
Expand Down Expand Up @@ -208,6 +208,16 @@ public static <P extends Preferences> List<P> all(Class<P> type) {
return CACHE.computeIfAbsent(type, PreferencesList::new);
}

/**
* Save the specified user preferences.
*/
public static <P extends Preferences> void store(Class<P> type) {
CACHE.computeIfPresent(type, (x, list) -> {
list.store();
return list;
});
}

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

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.ArrayList;

import com.sun.javafx.collections.ObservableListWrapper;

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

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

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

private boolean restoring;

/**
* Hide constructor.
*/
PreferencesList(Class<E> type) {
super(new ArrayList());

Managed annotation = type.getAnnotation(Managed.class);
if (annotation == null) {
id = type.getName();
Expand Down Expand Up @@ -52,11 +57,73 @@ final class PreferencesList<E extends Preferences> extends CopyOnWriteArrayList<
});
}

/**
* {@inheritDoc}
*/
@Override
public PreferencesList<E> restore() {
try {
restoring = true;
return Storable.super.restore();
} finally {
restoring = false;
}
}

/**
* {@inheritDoc}
*/
@Override
public PreferencesList<E> store() {
if (!restoring) {
Storable.super.store();
}
return this;
}

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

/**
* {@inheritDoc}
*/
@Override
protected void doAdd(int index, E element) {
super.doAdd(index, element);
store();

if (element != null && element.name.is("") && element.getClass().getSimpleName().equals("Task")) {
new Error().printStackTrace();
}
}

/**
* {@inheritDoc}
*/
@Override
protected E doSet(int index, E element) {
E e = super.doSet(index, element);
store();

if (element != null && element.name.is("") && element.getClass().getSimpleName().equals("Task")) {
new Error().printStackTrace();
}

return e;
}

/**
* {@inheritDoc}
*/
@Override
protected E doRemove(int index) {
E e = super.doRemove(index);
store();
return e;
}
}
116 changes: 0 additions & 116 deletions src/main/java/viewtify/model/StorableList.java

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/java/viewtify/model/StorableModel.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/java/viewtify/ui/helper/CollectableHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ default Self items(Iterable<E> items) {
* @return Chainable API.
*/
default Self items(List<E> items) {
if (items instanceof ObservableList) {
return items((ObservableList<E>) items);
}

if (items == null || items.isEmpty()) {
modifyItemUISafely(list -> {
list.clear();
Expand Down

0 comments on commit e1abc00

Please sign in to comment.