Skip to content

Commit

Permalink
feat: CollectableHelper drops ui thread safe action
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 15, 2024
1 parent d50c861 commit 200056b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/main/java/viewtify/Viewtify.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.sun.javafx.application.PlatformImpl;

import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.DoubleExpression;
Expand Down Expand Up @@ -74,6 +72,9 @@
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

import com.sun.javafx.application.PlatformImpl;

import kiss.Decoder;
import kiss.Disposable;
import kiss.Encoder;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/viewtify/edit/Edito.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,13 @@ protected void save() {
protected T clone(T value) {
return Edito.clone(value);
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return latest.toString();
}
}
}
60 changes: 19 additions & 41 deletions src/main/java/viewtify/ui/helper/CollectableHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.WeakHashMap;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
Expand All @@ -38,6 +37,7 @@
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;

import kiss.Disposable;
import kiss.I;
import kiss.Signal;
Expand Down Expand Up @@ -139,45 +139,22 @@ 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 -> {
modifyItem(list -> {
list.clear();
});
} else {
if (this instanceof CollectableItemRenderingHelper && !items.isEmpty() && items.get(0) instanceof Translatable) {
((CollectableItemRenderingHelper<?, Translatable>) this).renderByVariable(Translatable::toTraslated);
}

modifyItemUISafely(list -> {
modifyItem(list -> {
list.setAll(items);
});
}
return (Self) this;
}

/**
* Sets all values as items.
*
* @param items All items to set.
* @return Chainable API.
*/
default Self items(ObservableList<E> items) {
Objects.requireNonNull(items);

if (this instanceof CollectableItemRenderingHelper && !items.isEmpty() && items.get(0) instanceof Translatable) {
((CollectableItemRenderingHelper<?, Translatable>) this).renderByVariable(Translatable::toTraslated);
}

if (items != null) {
modifyItemUISafely(list -> refer().items.setValue(items));
}
return (Self) this;
}

/**
* Specify all values from the start value to the end value.
*
Expand Down Expand Up @@ -415,7 +392,7 @@ default boolean has(E item) {
*/
default Self addItemAt(int index, E item) {
if (item != null && 0 <= index) {
modifyItemUISafely(list -> list.add(Math.min(index, list.size()), item));
modifyItem(list -> list.add(Math.min(index, list.size()), item));
}
return (Self) this;
}
Expand All @@ -441,7 +418,7 @@ default Self addItemAtIfAbsent(int index, E item) {
* @return Chainable API.
*/
default Self addItemAtFirst(E item) {
modifyItemUISafely(list -> list.add(0, item));
modifyItem(list -> list.add(0, item));
return (Self) this;
}

Expand All @@ -466,7 +443,7 @@ default Self addItemAtFirstIfAbsent(E item) {
*/
default Self addItemAtLast(E item) {
if (item != null) {
modifyItemUISafely(list -> list.add(item));
modifyItem(list -> list.add(item));
}
return (Self) this;
}
Expand All @@ -493,7 +470,7 @@ default Self addItemAtLastIfAbsent(E item) {
*/
default Self setItemAt(int index, E item) {
if (item != null && 0 <= index) {
modifyItemUISafely(list -> list.set(Math.min(index, list.size()), item));
modifyItem(list -> list.set(Math.min(index, list.size()), item));
}
return (Self) this;
}
Expand All @@ -506,7 +483,7 @@ default Self setItemAt(int index, E item) {
*/
default Self removeItem(E item) {
if (item != null) {
modifyItemUISafely(list -> {
modifyItem(list -> {
Iterator<E> iterator = list.iterator();

while (iterator.hasNext()) {
Expand All @@ -529,7 +506,7 @@ default Self removeItem(E item) {
* @return Chainable API.
*/
default Self removeItems(Collection<E> items) {
modifyItemUISafely(list -> list.removeAll(items));
modifyItem(list -> list.removeAll(items));
return (Self) this;
}

Expand All @@ -539,7 +516,7 @@ default Self removeItems(Collection<E> items) {
* @return
*/
default Self removeItemAll() {
modifyItemUISafely(List<E>::clear);
modifyItem(List<E>::clear);
return (Self) this;
}

Expand All @@ -552,7 +529,7 @@ default Self removeItemAt(int... indexies) {
if (indexies.length != 0) {
Arrays.sort(indexies);

modifyItemUISafely(list -> {
modifyItem(list -> {
for (int i = indexies.length - 1; 0 <= i; i--) {
list.remove(indexies[i]);
}
Expand All @@ -579,7 +556,7 @@ default Self removeItemAt(List<Integer> indexies) {
* @return Chainable API.
*/
default Self removeItemAtFirst() {
modifyItemUISafely(list -> {
modifyItem(list -> {
Iterator<E> iterator = list.iterator();
if (iterator.hasNext()) {
iterator.next();
Expand All @@ -595,7 +572,7 @@ default Self removeItemAtFirst() {
* @return Chainable API.
*/
default Self removeItemAtLast() {
modifyItemUISafely(list -> {
modifyItem(list -> {
ListIterator<E> iterator = list.listIterator(list.size());
if (iterator.hasPrevious()) {
iterator.previous();
Expand Down Expand Up @@ -625,7 +602,7 @@ default Self replaceItemAt(Variable<E> target, E replacer) {
*/
default Self replaceItemAt(E target, E replacer) {
if (target != null && replacer != null) {
modifyItemUISafely(list -> {
modifyItem(list -> {
int index = list.indexOf(target);
if (index != -1) {
list.set(index, replacer);
Expand All @@ -643,19 +620,20 @@ default Self replaceItemAt(E target, E replacer) {
* @return Chainable API.
*/
default Self swap(int one, int other) {
modifyItemUISafely(list -> {
modifyItem(list -> {
Collections.swap(list, one, other);
});
return (Self) this;
}

/**
* Modify items in UI thread.
* Item manipulation.
*
* @param action
*/
private void modifyItemUISafely(Consumer<ObservableList<E>> action) {
Viewtify.inUI(() -> action.accept(items()));
private void modifyItem(Consumer<ObservableList<E>> action) {
action.accept(items());
// Viewtify.inUI(() -> action.accept(items()));
}

/**
Expand Down

0 comments on commit 200056b

Please sign in to comment.