Created
October 1, 2019 18:22
-
-
Save AchrafAmil/062323eb6a09a7a3faa6a4934644b93d to your computer and use it in GitHub Desktop.
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
ProfileActivity.kt | |
import com.jakewharton.rxbinding2.view.clicks | |
... | |
viewModel.bind(follow_button.clicks().map{ ProfileUiEvent.Follow }) | |
… | |
viewModel.uiState.observe(this, ::showState) | |
viewModel.uiContent.observe(this, ::showContent) | |
ProfileUiEvent.kt | |
sealed class ProfileUiEvent { | |
data class Show(val userId: String) : ProfileUiEvent() | |
data class OpenPhoto(val photoId: String) : ProfileUiEvent() | |
object Follow : ProfileUiEvent() | |
... | |
} | |
ProfileViewModel.kt | |
internal class ProfileViewModel @Inject constructor( | |
... | |
getUserInteractor: GetUserInteractor, | |
followUserInteractor: FollowUserInteractor, | |
) : ReactiveViewModel<ProfileUiEvent>() { | |
val uiState: LiveData<UiState> | |
val uiContent: LiveData<ProfileUiModel> | |
init { | |
val userDetailsStream = eventStream | |
.ofType<ProfileUiEvent.Show>() | |
.map { GetUserRequest(it.userId) } | |
.compose(getUserInteractor) | |
val followUserStream = eventsStream | |
.ofType<ProfileUiEvent.Follow>() | |
.map { FollowRequest(it.userId) } | |
.compose(followUserInteractor) | |
val uiStateStream = Flowable | |
.merge( | |
userDetailsStream.mapUserState(), | |
followUserStream.mapFollowState() | |
) | |
val uiContentStream = userDetailsStream | |
.ofType<GetUserRequest.Success> | |
.mapToUserUiModel() | |
uiState = fromPublisher(uiStateStream) | |
uiContent = fromPublisher(uiContentStream) | |
} | |
private fun Flowable<FollowResponse>.mapFollowState(): Flowable<UiState> { | |
return this.map { response -> | |
when (response) { | |
is FollowResponse.InFlight -> UiState.Loading | |
is FollowResponse.Error -> UiState.Error(mapError(response)) | |
is FollowResponse.Success -> UiState.Success | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment