Last active
August 17, 2019 06:02
-
-
Save kevivforever/dc5582baef7c6a4a1b53b4843b9f390a to your computer and use it in GitHub Desktop.
RxJava
This file contains 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
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(); | |
} | |
}) |
This file contains 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
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 | |
} | |
} | |
} | |
This file contains 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
filter | |
distinct | |
take | |
takeWhile |
This file contains 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
https://codingwithmitch.com/courses/rxjava-rxandroid-for-beginners | |
https://blog.mindorks.com/implement-search-using-rxjava-operators-c8882b64fe1d |
This file contains 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
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 | |
} |
This file contains 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
@SuppressLint("CheckResult") | |
private fun bindSearchAction() { | |
Observable.create(ObservableOnSubscribe<String> { subscriber -> | |
et_field.addTextChangedListener(object : TextWatcher { | |
override fun afterTextChanged(s: Editable?) { | |
subscriber.onNext(s.toString()) | |
} | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |
subscriber.onNext(s.toString()) | |
} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | |
subscriber.onNext(s.toString()) | |
} | |
}) | |
}).map { text -> text.toLowerCase().trim() } | |
.debounce(250, TimeUnit.MILLISECONDS) | |
.distinctUntilChanged() | |
.filter { text -> text.isNotBlank() } | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe { text -> | |
searchForUser(text) | |
} | |
} |
This file contains 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
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