Skip to content

Commit ead9462

Browse files
committed
Fix: EmotionRepository 내 중복 호출 방지 및 HomeViewModel 초기화 로직 수정
1 parent e96a6e7 commit ead9462

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

data/src/main/java/com/threegap/bitnagil/data/emotion/repositoryImpl/EmotionRepositoryImpl.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import kotlinx.coroutines.flow.Flow
1010
import kotlinx.coroutines.flow.MutableStateFlow
1111
import kotlinx.coroutines.flow.onSubscription
1212
import java.time.LocalDate
13+
import java.util.concurrent.atomic.AtomicBoolean
1314
import javax.inject.Inject
1415

1516
class EmotionRepositoryImpl @Inject constructor(
1617
private val emotionDataSource: EmotionDataSource,
1718
) : EmotionRepository {
1819

20+
private val isFetching = AtomicBoolean(false)
1921
private val _dailyEmotionFlow = MutableStateFlow(DailyEmotion.INIT)
2022
override val dailyEmotionFlow: Flow<DailyEmotion> = _dailyEmotionFlow
2123
.onSubscription {
@@ -39,9 +41,14 @@ class EmotionRepositoryImpl @Inject constructor(
3941
}
4042

4143
override suspend fun fetchDailyEmotion(): Result<Unit> {
42-
val currentDate = LocalDate.now().toString()
43-
return emotionDataSource.fetchDailyEmotion(currentDate).map {
44-
_dailyEmotionFlow.value = it.toDomain()
44+
if (!isFetching.compareAndSet(false, true)) return Result.success(Unit)
45+
return try {
46+
val currentDate = LocalDate.now().toString()
47+
emotionDataSource.fetchDailyEmotion(currentDate).map {
48+
_dailyEmotionFlow.value = it.toDomain()
49+
}
50+
} finally {
51+
isFetching.set(false)
4552
}
4653
}
4754
}

presentation/src/main/java/com/threegap/bitnagil/presentation/screen/home/HomeViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ class HomeViewModel @Inject constructor(
4242
private val toggleRoutineUseCase: ToggleRoutineUseCase,
4343
) : ContainerHost<HomeState, HomeSideEffect>, ViewModel() {
4444

45-
override val container: Container<HomeState, HomeSideEffect> = container(initialState = HomeState.INIT)
45+
override val container: Container<HomeState, HomeSideEffect> =
46+
container(
47+
initialState = HomeState.INIT,
48+
buildSettings = { repeatOnSubscribedStopTimeout = 5_000L },
49+
)
4650

4751
private val pendingChangesByDate = mutableMapOf<String, MutableMap<String, RoutineCompletionInfo>>()
4852
private val routineSyncTrigger = MutableSharedFlow<String>(extraBufferCapacity = 64)

0 commit comments

Comments
 (0)