@@ -3,12 +3,14 @@ package com.terning.feature.mypage.mypage
33import androidx.lifecycle.ViewModel
44import androidx.lifecycle.viewModelScope
55import com.kakao.sdk.user.UserApiClient
6+ import com.terning.core.designsystem.extension.groupBy
67import com.terning.core.designsystem.state.UiState
78import com.terning.core.designsystem.type.AlarmType.DISABLED
89import com.terning.core.designsystem.type.AlarmType.ENABLED
910import com.terning.domain.mypage.entity.AlarmStatus
1011import com.terning.domain.mypage.repository.MyPageRepository
1112import com.terning.domain.user.repository.UserRepository
13+ import com.terning.feature.mypage.mypage.model.AlarmInfo
1214import dagger.hilt.android.lifecycle.HiltViewModel
1315import kotlinx.coroutines.ExperimentalCoroutinesApi
1416import kotlinx.coroutines.FlowPreview
@@ -19,6 +21,7 @@ import kotlinx.coroutines.flow.StateFlow
1921import kotlinx.coroutines.flow.asSharedFlow
2022import kotlinx.coroutines.flow.asStateFlow
2123import kotlinx.coroutines.flow.debounce
24+ import kotlinx.coroutines.flow.flatMapMerge
2225import kotlinx.coroutines.flow.update
2326import kotlinx.coroutines.launch
2427import javax.inject.Inject
@@ -37,9 +40,9 @@ class MyPageViewModel @Inject constructor(
3740 private val _sideEffects = MutableSharedFlow <MyPageSideEffect >()
3841 val sideEffects: SharedFlow <MyPageSideEffect > get() = _sideEffects .asSharedFlow()
3942
40- private val debounceFlow = MutableSharedFlow <Boolean >()
43+ private val debounceFlow = MutableSharedFlow <AlarmInfo >()
4144
42- private var lastSuccessfulAlarmStatus: Boolean? = null
45+ private val lastSuccessfulAlarmStatus = mutableMapOf< String , Boolean >()
4346
4447 init {
4548 handleDebouncedAlarm()
@@ -48,14 +51,15 @@ class MyPageViewModel @Inject constructor(
4851 private fun handleDebouncedAlarm () {
4952 viewModelScope.launch {
5053 debounceFlow
51- .debounce(DEBOUNCE_DURATION )
52- .collect { isEnabled ->
54+ .groupBy { it.id }
55+ .flatMapMerge { (_, flow) -> flow.debounce(DEBOUNCE_DURATION ) }
56+ .collect { info ->
5357 myPageRepository.updateAlarmState(
54- AlarmStatus (if (isEnabled ) ENABLED .value else DISABLED .value)
58+ AlarmStatus (if (info.isAlarmAvailable ) ENABLED .value else DISABLED .value)
5559 ).onSuccess {
56- lastSuccessfulAlarmStatus = isEnabled
60+ lastSuccessfulAlarmStatus[info.id] = info.isAlarmAvailable
5761 }.onFailure {
58- val previous = lastSuccessfulAlarmStatus ? : ! isEnabled
62+ val previous = lastSuccessfulAlarmStatus[info.id] ? : ! info.isAlarmAvailable
5963 _state .update { currentState ->
6064 currentState.copy(alarmStatus = if (previous) ENABLED .value else DISABLED .value)
6165 }
@@ -128,7 +132,8 @@ class MyPageViewModel @Inject constructor(
128132 }
129133 }.onFailure {
130134 _sideEffects .emit(MyPageSideEffect .ShowToast (DesignSystemR .string.server_failure))
131- _state .value = _state .value.copy(isGetSuccess = UiState .Failure (it.toString()))
135+ _state .value =
136+ _state .value.copy(isGetSuccess = UiState .Failure (it.toString()))
132137 }
133138 }
134139 }
@@ -180,7 +185,7 @@ class MyPageViewModel @Inject constructor(
180185 userRepository.setAlarmAvailable(availability)
181186
182187 viewModelScope.launch {
183- debounceFlow.emit(availability)
188+ debounceFlow.emit(AlarmInfo (id = DEBOUNCE_KEY , isAlarmAvailable = availability) )
184189 }
185190 }
186191
@@ -196,5 +201,6 @@ class MyPageViewModel @Inject constructor(
196201
197202 companion object {
198203 private const val DEBOUNCE_DURATION = 300L
204+ private const val DEBOUNCE_KEY = " NOTIFICATION"
199205 }
200206}
0 commit comments