generated from AND-SOPT-ANDROID/and-sopt-android-template
-
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.
- Loading branch information
1 parent
6a98a06
commit 3186454
Showing
4 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
presentation/src/main/java/org/sopt/and/presentation/util/base/BaseViewModel.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,45 @@ | ||
package org.sopt.and.presentation.util.base | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
abstract class BaseViewModel<State: UiState, Event: UiEvent, SideEffect: UiSideEffect>() : ViewModel() { | ||
|
||
private val initialState: State by lazy { createInitialState() } | ||
abstract fun createInitialState(): State | ||
|
||
private val _uiState: MutableStateFlow<State> = MutableStateFlow(initialState) | ||
val uiState = _uiState.asStateFlow() | ||
val currentState: State | ||
get() = _uiState.value | ||
|
||
private val _event: MutableSharedFlow<Event> = MutableSharedFlow() | ||
val event = _event.asSharedFlow() | ||
|
||
private val _sideEffect: MutableSharedFlow<SideEffect> = MutableSharedFlow() | ||
val sideEffect = _sideEffect.asSharedFlow() | ||
|
||
fun setState(reduce: State.() -> State) { | ||
_uiState.value = currentState.reduce() | ||
} | ||
|
||
open fun setEvent(event: Event) { | ||
dispatchEvent(event) | ||
} | ||
|
||
private fun dispatchEvent(event: Event) = viewModelScope.launch { | ||
handleEvent(event) | ||
} | ||
|
||
protected abstract suspend fun handleEvent(event: Event) | ||
|
||
fun setSideEffect(sideEffect: SideEffect) { | ||
viewModelScope.launch { _sideEffect.emit(sideEffect) } | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
presentation/src/main/java/org/sopt/and/presentation/util/base/UiEvent.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,3 @@ | ||
package org.sopt.and.presentation.util.base | ||
|
||
interface UiEvent |
3 changes: 3 additions & 0 deletions
3
presentation/src/main/java/org/sopt/and/presentation/util/base/UiSideEffect.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,3 @@ | ||
package org.sopt.and.presentation.util.base | ||
|
||
interface UiSideEffect |
3 changes: 3 additions & 0 deletions
3
presentation/src/main/java/org/sopt/and/presentation/util/base/UiState.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,3 @@ | ||
package org.sopt.and.presentation.util.base | ||
|
||
interface UiState |