-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/#4 : Group Feature mvi base ViewModel 선언
- Intent 전달 방식 수정 - reducer 함수 강압적인 사용, intent 강제 활용 - reducer 함수 호출
- Loading branch information
Showing
11 changed files
with
296 additions
and
211 deletions.
There are no files selected for viewing
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
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
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
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
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
57 changes: 57 additions & 0 deletions
57
feature/mygroup/src/main/java/com/boostcamp/mapisode/mygroup/viewmodel/GroupBaseViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.boostcamp.mapisode.mygroup.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.boostcamp.mapisode.ui.base.SideEffect | ||
import com.boostcamp.mapisode.ui.base.UiIntent | ||
import com.boostcamp.mapisode.ui.base.UiState | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
abstract class GroupBaseViewModel<UI_INTENT: UiIntent, UI_STATE: UiState, UI_EFFECT: SideEffect >( | ||
initialState: UI_STATE | ||
): ViewModel() { | ||
private val _state = MutableStateFlow(initialState) | ||
val state = _state.asStateFlow() | ||
|
||
private val _intent = MutableSharedFlow<UI_INTENT>() | ||
|
||
private val _effect = Channel<SideEffect>() | ||
val effect = _effect.receiveAsFlow() | ||
|
||
protected val currentState: UI_STATE | ||
get() = _state.value | ||
|
||
/** | ||
* initialize the reducer with the intent argument | ||
*/ | ||
init { | ||
viewModelScope.launch { | ||
reducer(_intent.asSharedFlow()) | ||
} | ||
} | ||
|
||
/** | ||
* Use intent to update the state and send the effect | ||
*/ | ||
abstract suspend fun reducer(intent: SharedFlow<UI_INTENT>) | ||
|
||
fun sendIntent(intent: UI_INTENT) { | ||
viewModelScope.launch { _intent.emit(intent) } | ||
} | ||
|
||
protected fun sendState(reduce: UI_STATE.() -> UI_STATE) { | ||
_state.update { currentState.reduce() } | ||
} | ||
|
||
fun sendEffect(builder: () -> UI_EFFECT) { | ||
viewModelScope.launch { _effect.send(builder()) } | ||
} | ||
} |
Oops, something went wrong.