Last active
April 16, 2020 20:56
-
-
Save erfanegtfi/596812287020aed84900be66dacacd8d to your computer and use it in GitHub Desktop.
Observe on list events, addAll, add, remove, update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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