-
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.
[FEATURE]#155 : SideEffectState 및 SideEffect에 따른 처리 컴포넌트 구현
- Loading branch information
1 parent
95f7346
commit 482f954
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
core/ui/src/main/kotlin/com/bff/wespot/util/SideEffectUtil.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,30 @@ | ||
package com.bff.wespot.util | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import com.bff.wespot.base.BaseViewModel | ||
import com.bff.wespot.model.SideEffect | ||
|
||
@SuppressLint("ComposableNaming") | ||
@Composable | ||
fun BaseViewModel.collectBaseSideEffect( | ||
lifecycleState: Lifecycle.State = androidx.lifecycle.Lifecycle.State.STARTED, | ||
sideEffect: (suspend (sideEffect: SideEffect) -> Unit), | ||
) { | ||
val sideEffectFlow = this.sideEffect | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
val callback by rememberUpdatedState(newValue = sideEffect) | ||
|
||
LaunchedEffect(sideEffectFlow, lifecycleOwner) { | ||
lifecycleOwner.lifecycle.repeatOnLifecycle(lifecycleState) { | ||
sideEffectFlow.collect { callback(it) } | ||
} | ||
} | ||
} |