Last active
March 17, 2021 04:38
-
-
Save cbedoy/bc1eb2f85c9e308cfc1a499374d35e1c to your computer and use it in GitHub Desktop.
Common MVI
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
| class LoginFragment { | |
| val viewModel by viewModel<LoginViewModel>() | |
| override fun onViewCreadted(){ | |
| with(binding){ | |
| action.setOnClickListener{ | |
| viewModel.performIntent( | |
| intent = PerformLoginWith( | |
| username = usernameField.textAsString | |
| ) | |
| ) | |
| } | |
| } | |
| lifecycleScope.launch{ | |
| viewModel.state.collect { state -> | |
| handleState(state) | |
| } | |
| } | |
| } | |
| fun handleState(intent: LoginState){ | |
| //..... | |
| } | |
| } |
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
| sealed class LoginIntent{ | |
| data class PerformLoginWith(username: String, password: String): LoginIntent() | |
| object RequestSupport: LoginIntent() | |
| } |
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
| class LoginRepository ( | |
| val service: AppService | |
| ){ | |
| suspend fun getSessionWith(username: String) = service.getSession(username) | |
| } |
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
| sealed class LoginState{ | |
| object Ilde: LoginState(), | |
| data class ShowMessage(val message:STring): LoginState() | |
| data class ShowOrHideLoader(val isVisible): LoginState() | |
| } |
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
| class LoginUseCase( | |
| val repository: LoginRepository | |
| ){ | |
| fun performLoginWithUserName(username: String) = flow { | |
| emit(ShowLoader(isVisible = true)) | |
| when(val response = repository.getSessionWith(username)){ | |
| is Success -> { | |
| emit(ShowMessage(message = "Success")) | |
| } | |
| else -> { | |
| emit(ShowMessage(message = "failure")) | |
| } | |
| } | |
| emit(ShowLoader(isVisible = false)) | |
| emit(Ilde) | |
| } | |
| // if success then you will get flow state of: | |
| // (ShowLoader(isVisible = true), ShowMessage(message = "Success"), ShowLoader(isVisible = false), Ilde) | |
| // else | |
| // (ShowLoader(isVisible = true), ShowMessage(message = "false"), ShowLoader(isVisible = false), Ilde) | |
| } |
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
| class LoginViewModel( | |
| private val useCase: LoginUseCase | |
| ) : ViewModel(){ | |
| private val channel = Channel<LoginIntent>() | |
| private val _state = MutableStateFlow<LoginState>(Ilde) | |
| val state: StateFlow<LoginState> get() = _state | |
| init { | |
| viewModelScope.launch { | |
| handleIntents() | |
| } | |
| } | |
| suspend fun handleIntents(){ | |
| channel.collectAsFlow(){ intent -> | |
| when(intent){ | |
| is PerformLoginWith -> { | |
| useCase.performLoginWith(intent.username) { | |
| _state.value = it | |
| } | |
| } | |
| } | |
| } | |
| } | |
| fun performIntent(val intent: LoginIntent) = viewModelScope.launch { | |
| channel.offer(intent) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment