File tree Expand file tree Collapse file tree
data/src/main/java/com/threegap/bitnagil/data/emotion/repositoryImpl
presentation/src/main/java/com/threegap/bitnagil/presentation/screen/home Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,12 +10,14 @@ import kotlinx.coroutines.flow.Flow
1010import kotlinx.coroutines.flow.MutableStateFlow
1111import kotlinx.coroutines.flow.onSubscription
1212import java.time.LocalDate
13+ import java.util.concurrent.atomic.AtomicBoolean
1314import javax.inject.Inject
1415
1516class 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}
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments