Merge branch 'master' into master-rx-java-2
# Conflicts: # src/main/java/ru/touchin/roboswag/core/observables/collections/ObservableFilteredList.java
This commit is contained in:
commit
29dc6ce1c0
|
|
@ -7,6 +7,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.functions.Function;
|
||||
|
|
@ -41,6 +42,9 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
return result;
|
||||
}
|
||||
|
||||
// we need to filter on 1 thread to prevent parallel filtering
|
||||
@NonNull
|
||||
private final Scheduler filterScheduler = Schedulers.from(Executors.newSingleThreadExecutor());
|
||||
@NonNull
|
||||
private List<TItem> filteredList;
|
||||
@NonNull
|
||||
|
|
@ -50,6 +54,10 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
@Nullable
|
||||
private Disposable sourceCollectionSubscription;
|
||||
|
||||
public ObservableFilteredList() {
|
||||
this(new ArrayList<>(), null);
|
||||
}
|
||||
|
||||
public ObservableFilteredList(@NonNull final Function<TItem, Boolean> filter) {
|
||||
this(new ArrayList<>(), filter);
|
||||
}
|
||||
|
|
@ -63,7 +71,7 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
this.filter = filter;
|
||||
this.sourceCollection = sourceCollection;
|
||||
this.filteredList = filterCollection(this.sourceCollection.getItems(), this.filter);
|
||||
update();
|
||||
updateInternal();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -73,7 +81,7 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
*/
|
||||
public void setSourceCollection(@Nullable final ObservableCollection<TItem> sourceCollection) {
|
||||
this.sourceCollection = sourceCollection != null ? sourceCollection : new ObservableList<>();
|
||||
update();
|
||||
updateInternal();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -83,7 +91,7 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
*/
|
||||
public void setSourceCollection(@Nullable final Collection<TItem> sourceCollection) {
|
||||
this.sourceCollection = sourceCollection != null ? new ObservableList<>(sourceCollection) : new ObservableList<>();
|
||||
update();
|
||||
updateInternal();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -93,19 +101,16 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
*/
|
||||
public void setFilter(@Nullable final Function<TItem, Boolean> filter) {
|
||||
this.filter = filter;
|
||||
update();
|
||||
updateInternal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates collection by current filter. Use it if some item's parameter which is important for filtering have changing.
|
||||
*/
|
||||
private void update() {
|
||||
private void updateInternal() {
|
||||
if (sourceCollectionSubscription != null) {
|
||||
sourceCollectionSubscription.dispose();
|
||||
sourceCollectionSubscription = null;
|
||||
}
|
||||
sourceCollectionSubscription = sourceCollection.observeItems()
|
||||
.observeOn(Schedulers.computation())
|
||||
.observeOn(filterScheduler)
|
||||
.subscribe(items -> {
|
||||
final List<TItem> oldFilteredList = filteredList;
|
||||
filteredList = filterCollection(items, filter);
|
||||
|
|
@ -113,6 +118,13 @@ public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates collection by current filter. Use it if some item's parameter which is important for filtering have changing.
|
||||
*/
|
||||
public void update() {
|
||||
updateInternal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return filteredList.size();
|
||||
|
|
|
|||
Loading…
Reference in New Issue