Skip to content

Instantly share code, notes, and snippets.

@kevivforever
Last active August 17, 2019 06:02
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 kevivforever/dc5582baef7c6a4a1b53b4843b9f390a to your computer and use it in GitHub Desktop.
Save kevivforever/dc5582baef7c6a4a1b53b4843b9f390a to your computer and use it in GitHub Desktop.
RxJava
Creating observables
create() - Observable.create()
just() - Observable.just("first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth")
.subscribeOn(Schedulers.io()) // What thread to do the work on
.observeOn(AndroidSchedulers.mainThread())
range() - Observable.range(0,11)
repeat() - Observable.range(0,3)
.repeat(2)
// converts an array of objects into observables
// Task[] list = new Task[5];
fromArray() - Observable
.fromArray(list)
// converts an array of iterables like List, ArrayList, Set into obserbvables
// List<Task> taskList = new ArrayList<>();
fromIterable() - Observable
.fromIterable(taskList)
// fromCallable() will execute a block of code (usually a method) and return a result.
//You need to return a Task object from a local SQLite database cache.
//All database operations must be done on a background thread.
//Then the result is returned to the main thread.
fromCallable() - Observable
.fromCallable(new Callable<Task>() {
@Override
public Task call() throws Exception {
return MyDatabase.getTask();
}
})
fun loadUserData() {
val userPosts = getUserData()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe {
setUserData(it)
}
}
/**
* get user data with posts using flatmap operator
*/
private fun getUserWithPosts(): Observable<UserWithPosts> {
return userRepository.getUserDetailObservable(uid).flatMap { user ->
val userWithPosts = UserWithPosts(user, arrayListOf())
userRepository.getAllUserPosts(uid, 1).map {
val userPostList = mutableListOf<UserPosts>()
userPostList.add(UserPosts("Posts", "Post is empty", UserPostsActivity.POST, it))
userWithPosts.userPosts = userPostList
userWithPosts
}
}.flatMap { userWithPosts ->
userRepository.getUserRecommededPosts(uid, 1).map {
val userPostList = mutableListOf<UserPosts>()
userPostList.add(UserPosts("Recommendation", "Recommendation is empty", UserPostsActivity.RECOMMEDATION, it))
userWithPosts.userPosts = userPostList
userWithPosts
}
}.flatMap { userWithPosts ->
userRepository.getUserWishList(uid, 1).map {
val userPostList = mutableListOf<UserPosts>()
userPostList.add(UserPosts("WishList", "Wishlist is empty", UserPostsActivity.WISHLIST, it))
userWithPosts.userPosts = userPostList
userWithPosts
}
}
}
/**
* get user data with posts using zip operator
*/
private fun getUserData(): Observable<UserWithPosts> {
return Observable.zip(userRepository.getUserDetailObservable(uid),
userRepository.getAllUserPosts(uid, 1),
userRepository.getUserRecommededPosts(uid, 1),
userRepository.getUserWishList(uid, 1),
Function4<User, List<Post>, List<Post>, List<Post>, UserWithPosts>
{ user, allposts, recommendedposts, wishlistposts ->
val userPostList = mutableListOf<UserPosts>()
userPostList.add(UserPosts("Posts", "Post is empty", UserPostsActivity.POST, allposts))
userPostList.add(UserPosts("Recommendation", "Recommendation is empty", UserPostsActivity.RECOMMEDATION, recommendedposts))
userPostList.add(UserPosts("WishList", "Wishlist is empty", UserPostsActivity.WISHLIST, wishlistposts))
return@Function4 UserWithPosts(user, userPostList)
})
}
private fun getUserPosts(): Observable<MutableList<UserPosts>> {
return userRepository.getUserDetailObservable(uid).flatMap { resource ->
// do something with resource here.
userRepository.getAllUserPosts(uid, 1).map {
val userPostList = mutableListOf<UserPosts>()
userPostList.add(UserPosts("Posts", "0 Posts", UserPostsActivity.POST, it))
userPostList
}
}.flatMap { userPostList ->
userRepository.getUserRecommededPosts(uid, 1).map {
userPostList.add(UserPosts("Recommendation", "0 Recommendations", UserPostsActivity.RECOMMEDATION, it))
userPostList
}
}.flatMap { userPostList ->
userRepository.getUserWishList(uid, 1).map {
userPostList.add(UserPosts("WishList", "0 Wishlist", UserPostsActivity.WISHLIST, it))
userPostList
}
}
}
filter
distinct
take
takeWhile
https://codingwithmitch.com/courses/rxjava-rxandroid-for-beginners
https://blog.mindorks.com/implement-search-using-rxjava-operators-c8882b64fe1d
RxView.clicks(post_detail_ib_recommendation).throttleFirst(2, TimeUnit.SECONDS)
.subscribe {
// your code
}
RxView.clicks(post_detail_bt_follow).throttleFirst(2, TimeUnit.SECONDS)
.subscribe {
// your code
}
map
buffer
debounce
ThrottleFirst
flatMap
switchMap
concatMap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment