Last active
January 10, 2021 15:55
-
-
Save iammert/59d5a26d4795342bdb3a6ebea19571e0 to your computer and use it in GitHub Desktop.
croutine_intro
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
api.fetchUser(object: Callback{ | |
fun onResponse(userInfo: UserInfo){ | |
userTextView.text = userInfo.name | |
} | |
}) |
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
api.fetchUser(object: Callback{ | |
fun onResponse(userInfo: UserInfo){ | |
api.fetchLastOnlineTime(userInfo.userId, object: Callback{ | |
fun onResponse(userActivity: UserActivity){ | |
userTextView.text = userInfo.name | |
userLastOnlineTime.text = userActivity.time | |
} | |
}) | |
} | |
}) |
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
suspend fun fetchUser(): UserInfo { | |
apiService.fetchUser() | |
} | |
suspend fun fetchLastOnlineTime(userId: String) : UserActivity { | |
apiService.fetchLastOnlineTime(userId) | |
} | |
suspend fun fetchData() = withContext(Dispatcher.IO){ | |
val userInfo = apiService.fetchUser() | |
val userActivity = apiService.fetchLastOnlineTime(userId) | |
val mappedData = map(userInfo, userActivity) | |
mappedData | |
} |
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
GlobalScope.launch(Dispatchers.Main) { | |
val userInfoAndActivity = fetchData() | |
statusTextView.text = userInfoAndActivity.lastOnlineTime | |
} |
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
api.fetchUser() | |
.doOnNext(userInfo -> userNameText.text = userInfo.name) | |
.flatMap(userInfo -> api.fetchLastOnlineTime(userInfo.userId)) | |
.doOnNext(userActivity -> userActivity.text = userActivity.name) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe() |
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
val userInfo = api.fetchUser() | |
val userActivity = api.fetchLastOnlineTime(userInfo.userId) | |
userTextView.text = userInfo.name | |
userLastOnlineTime.text = userActivity.time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment