Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active August 8, 2016 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yongjhih/bbe3b528873c7eb671c6 to your computer and use it in GitHub Desktop.
Save yongjhih/bbe3b528873c7eb671c6 to your computer and use it in GitHub Desktop.
RxJava
Observable.merge(Observable.from(getRawSelfPosts()), Observable.from(getRawFriendPosts()), Observable.from(getRawNearbyPosts(activity)))
.distinct((post) -> post.getObjectId())
.subscribeOn(Schedulers.io());
getRawPostsObs(activity).map((post) -> postToCardModel(post));
new RxList(getRawPostsObs(activity));
getPostsObs(activity).toList().toBlocking().single();
postsObs.toSortedList((o, o2) -> comparator.compare(o, o2)).toBlocking().single();
new RxList(Observable.zip(Observable.from(mLocations), Observable.from(mImages), (location, image) -> {
if (location == null || image == null) return null;
return rpp.addPhoto(location.getLatitude(), location.getLongitude(), image);
}).subscribeOn(Schedulers.io()).filter((photo) -> photo != null));
Observable.from(friends)
.filter((f) -> f.getInstalled())
.map((f) -> 1)
.scan((a, b) -> a + b)
.subscribe((Integer i) -> view.setText("" + i));
AndroidObservable.bindFragment(this, Observable.defer(() -> Observable.just(ParseUtil.getFriends(mProfile.parseUser).size())))
.subscribeOn(Schedulers.io())
.subscribe((Integer i) -> mFollowingView.setText("" + i));
Observable<List<Profile>> friendsObs = Observable.from(ParseUtil.getFriends()).map((user) -> Profile.load(user)).subscribeOn(Schedulers.io()).toList();
Observable<CardModel> cards = ParseUtil.getPostsObs(getActivity());
final MemoryComparator comparator = new MemoryComparator(getActivity(), friendsObs.toBlocking().single()); // TODO Comparator<Post>
Observable<List<CardModel>> cardListObs = cards.toSortedList((o, o2) -> comparator.compare(o, o2));
Observable<List<Movie>> movieListObs = Observable.concat(cardListObs.flatMap((list) -> Observable.from(list)).map((card) -> { // cardObs
Movie movie = new Movie();
movie.setTitle(card.getPlaceName() == null ? "" : card.getPlaceName());
movie.setStudio(card.post.getMesage() == null ? "" : card.post.getMesage());
movie.setDescription(card.post.getMesage() == null ? "" : card.post.getMesage());
movie.setCategory("Social");
movie.setCardImageUrl(card.thumbUrl); // assertNull
movie.setBackgroundImageUrl(card.thumbUrl); // assertNull
return movie;
})
.window(3)
.map((obs) -> obs.toList()));
final ArrayList<List<Integer>> lists = new ArrayList<List<Integer>>();
Observable.concat(Observable.from(1, 2, 3, 4, 5, 6).window(3).map(new Func1<Observable<Integer>, Observable<List<Integer>>>() {
@Override
public Observable<List<Integer>> call(Observable<Integer> xs) {
return xs.toList();
}
})).toBlocking().forEach(new Action1<List<Integer>>() {
@Override
public void call(List<Integer> xs) {
lists.add(xs);
}
});
// Problem:
// input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
// even (%2 == 0), > 5
// output:
// 1 3
// 2 4
// 5 7 9
// 6 8 10
Observable.range(1, 10)
.groupBy(n -> n % 2 == 0)
.flatMap((GroupedObservable<Boolean, Integer> g) -> {
return Observable.just(g).flatMap(ObservableUtils.<Boolean, Integer>flatGroup()).groupBy(n -> n > 5);
})
.subscribe((final GroupedObservable<Boolean, Integer> g) -> {
Observable.just(g).flatMap(ObservableUtils.<Boolean, Integer>flatGroup()).forEach(n -> Log.d("andrew", g + ":" + n));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment