filter list added

This commit is contained in:
Gavriil Sitnikov 2016-06-02 02:31:15 +03:00
parent 796a44d6cd
commit 2cc8bfd011
4 changed files with 115 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.internal.util.RxRingBuffer;
@ -34,6 +35,8 @@ import rx.subjects.PublishSubject;
*/
public abstract class ObservableCollection<TItem> {
private static final long MIN_TIME_BEFORE_UPDATES = 100;
@NonNull
private final PublishSubject<Change> changeSubject = PublishSubject.create();
@ -41,9 +44,15 @@ public abstract class ObservableCollection<TItem> {
changeSubject.onNext(change);
}
protected void notifyAboutChanges(@NonNull final List<Change> changes) {
for (final Change change : changes) {
changeSubject.onNext(change);
}
}
@NonNull
public Observable<Change> observeChanges() {
return changeSubject;
public Observable<List<Change>> observeChanges() {
return changeSubject.buffer(MIN_TIME_BEFORE_UPDATES, TimeUnit.MILLISECONDS);
}
public abstract int size();

View File

@ -0,0 +1,76 @@
package ru.touchin.roboswag.core.observables.collections;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import rx.Observable;
import rx.functions.Func1;
/**
* Created by Gavriil Sitnikov on 02/06/2016.
* TODO: fill description
*/
public class ObservableFilteredList<TItem> extends ObservableCollection<TItem> {
@NonNull
private static <TItem> Collection<TItem> filterCollection(@NonNull final Collection<TItem> sourceCollection,
@NonNull final Func1<TItem, Boolean> filter) {
final List<TItem> result = new ArrayList<>(sourceCollection.size());
for (final TItem item : sourceCollection) {
if (filter.call(item)) {
result.add(item);
}
}
return result;
}
@NonNull
private final ObservableList<TItem> filteredList = new ObservableList<>();
@NonNull
private Collection<TItem> sourceCollection = new ArrayList<>();
@Nullable
private Func1<TItem, Boolean> filter;
public ObservableFilteredList() {
filteredList.observeChanges().subscribe(change -> {
//do not change - bug of RetroLambda
notifyAboutChanges(change);
});
}
public void setSourceCollection(@NonNull final Collection<TItem> sourceCollection) {
this.sourceCollection = sourceCollection;
if (filter != null) {
filteredList.set(filterCollection(sourceCollection, filter));
} else {
filteredList.set(sourceCollection);
}
}
public void setFilter(@NonNull final Func1<TItem, Boolean> filter) {
this.filter = filter;
filteredList.set(filterCollection(sourceCollection, filter));
}
@Override
public int size() {
return filteredList.size();
}
@NonNull
@Override
public TItem get(final int position) {
return filteredList.get(position);
}
@NonNull
@Override
public Observable<TItem> loadItem(final int position) {
return filteredList.loadItem(position);
}
}

View File

@ -86,9 +86,33 @@ public class ObservableList<TItem> extends ObservableCollection<TItem> {
notifyAboutChange(new Change(Change.Type.CHANGED, position, 1));
}
public void set(@NonNull final Collection<TItem> items) {
clear();
addAll(items);
public void set(@NonNull final Collection<TItem> newItems) {
int currentItemsStart = 0;
for (final TItem item : newItems) {
boolean found = false;
for (int i = currentItemsStart; i < items.size(); i++) {
if (item.equals(items.get(i))) {
found = true;
for (int j = currentItemsStart; j < i; j++) {
items.remove(currentItemsStart);
}
notifyAboutChange(new Change(Change.Type.REMOVED, currentItemsStart, i - currentItemsStart));
break;
}
}
if (!found) {
add(currentItemsStart, item);
}
currentItemsStart++;
}
if (currentItemsStart < items.size()) {
final int itemsToRemove = items.size() - currentItemsStart;
for (int i = 0; i < itemsToRemove; i++) {
items.remove(currentItemsStart);
}
notifyAboutChange(new Change(Change.Type.REMOVED, currentItemsStart, itemsToRemove));
}
}
public boolean isEmpty() {

View File

@ -56,7 +56,7 @@ public class LoadingGrowingList<TItemId, TItem extends ItemWithId<TItemId>>
this.loadingMoreRequestCreator = loadingMoreRequestCreator;
innerList.observeChanges().subscribe(change -> {
//do not change - bug of RetroLambda
notifyAboutChange(change);
notifyAboutChanges(change);
});
}