Skip to content

Instantly share code, notes, and snippets.

@erfanegtfi
Last active April 16, 2020 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erfanegtfi/596812287020aed84900be66dacacd8d to your computer and use it in GitHub Desktop.
Save erfanegtfi/596812287020aed84900be66dacacd8d to your computer and use it in GitHub Desktop.
Observe on list events, addAll, add, remove, update
public class ObservableRxList<T> {
protected final List<T> list;
protected final PublishSubject<List<T>> subject;
public ObservableRxList() {
this.list = new ArrayList<T>();
this.subject = PublishSubject.create();
}
public void add(T value) {
list.add(value);
subject.onNext(list);
}
public void addAll(List<T> value) {
list.addAll(value);
subject.onNext(list);
}
//not sure about this
public void update(T value) {
for (ListIterator<T> it = list.listIterator(); it.hasNext(); ) {
if (value == it.next()) {
it.set(value);
break;
}
}
subject.onNext(list);
}
public void update(int position, T value) {
list.set(position, value);
subject.onNext(list);
}
public void remove(T value) {
list.remove(value);
subject.onNext(list);
}
public void remove(int index) {
list.remove(index);
subject.onNext(list);
}
public Observable<List<T>> getObservable() {
return subject;
}
public List<T> getCurrentList() {
return list;
}
}
mObservableRxList.getObservable().subscribe(productList-> {
this.products.clear();
this.products.addAll(productList;
productAdapter.notifyDataSetChanged();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment