|
1 | 1 | package com.threegap.bitnagil.data.emotion.repositoryImpl |
2 | 2 |
|
3 | | -import com.threegap.bitnagil.data.emotion.datasource.EmotionDataSource |
| 3 | +import com.threegap.bitnagil.data.emotion.datasource.EmotionLocalDataSource |
| 4 | +import com.threegap.bitnagil.data.emotion.datasource.EmotionRemoteDataSource |
4 | 5 | import com.threegap.bitnagil.data.emotion.model.response.toDomain |
5 | 6 | import com.threegap.bitnagil.domain.emotion.model.DailyEmotion |
6 | 7 | import com.threegap.bitnagil.domain.emotion.model.Emotion |
7 | 8 | import com.threegap.bitnagil.domain.emotion.model.EmotionRecommendRoutine |
8 | 9 | import com.threegap.bitnagil.domain.emotion.repository.EmotionRepository |
9 | 10 | import kotlinx.coroutines.flow.Flow |
10 | | -import kotlinx.coroutines.flow.MutableStateFlow |
11 | | -import kotlinx.coroutines.flow.onSubscription |
| 11 | +import kotlinx.coroutines.flow.emitAll |
| 12 | +import kotlinx.coroutines.flow.filterNotNull |
| 13 | +import kotlinx.coroutines.flow.flow |
| 14 | +import kotlinx.coroutines.flow.map |
| 15 | +import kotlinx.coroutines.sync.Mutex |
| 16 | +import kotlinx.coroutines.sync.withLock |
12 | 17 | import java.time.LocalDate |
13 | | -import java.util.concurrent.atomic.AtomicBoolean |
14 | 18 | import javax.inject.Inject |
15 | 19 |
|
16 | 20 | class EmotionRepositoryImpl @Inject constructor( |
17 | | - private val emotionDataSource: EmotionDataSource, |
| 21 | + private val emotionRemoteDataSource: EmotionRemoteDataSource, |
| 22 | + private val emotionLocalDateSource: EmotionLocalDataSource, |
18 | 23 | ) : EmotionRepository { |
19 | 24 |
|
20 | | - private val isFetching = AtomicBoolean(false) |
21 | | - private val _dailyEmotionFlow = MutableStateFlow(DailyEmotion.INIT) |
22 | | - override val dailyEmotionFlow: Flow<DailyEmotion> = _dailyEmotionFlow |
23 | | - .onSubscription { |
24 | | - if (_dailyEmotionFlow.value.isStale(LocalDate.now())) fetchDailyEmotion() |
25 | | - } |
| 25 | + private val fetchMutex = Mutex() |
26 | 26 |
|
27 | 27 | override suspend fun getEmotions(): Result<List<Emotion>> { |
28 | | - return emotionDataSource.getEmotions().map { response -> |
| 28 | + return emotionRemoteDataSource.getEmotions().map { response -> |
29 | 29 | response.map { it.toDomain() } |
30 | 30 | } |
31 | 31 | } |
32 | 32 |
|
33 | 33 | override suspend fun registerEmotion(emotionMarbleType: String): Result<List<EmotionRecommendRoutine>> { |
34 | | - return emotionDataSource.registerEmotion(emotionMarbleType).map { |
| 34 | + return emotionRemoteDataSource.registerEmotion(emotionMarbleType).map { |
35 | 35 | it.recommendedRoutines.map { emotionRecommendedRoutineDto -> |
36 | 36 | emotionRecommendedRoutineDto.toEmotionRecommendRoutine() |
37 | 37 | } |
38 | 38 | }.also { |
39 | | - if (it.isSuccess) fetchDailyEmotion() |
| 39 | + if (it.isSuccess) fetchAndSaveDailyEmotion(today = LocalDate.now(), forceRefresh = true) |
40 | 40 | } |
41 | 41 | } |
42 | 42 |
|
43 | | - override suspend fun fetchDailyEmotion(): Result<Unit> { |
44 | | - if (!isFetching.compareAndSet(false, true)) return Result.success(Unit) |
45 | | - return try { |
46 | | - val today = LocalDate.now() |
47 | | - emotionDataSource.fetchDailyEmotion(today.toString()).map { |
48 | | - _dailyEmotionFlow.value = it.toDomain(today) |
| 43 | + override fun observeDailyEmotion(): Flow<Result<DailyEmotion>> = flow { |
| 44 | + fetchAndSaveDailyEmotion(LocalDate.now()) |
| 45 | + .onFailure { |
| 46 | + emit(Result.failure(it)) |
| 47 | + return@flow |
| 48 | + } |
| 49 | + |
| 50 | + emitAll( |
| 51 | + emotionLocalDateSource.dailyEmotion |
| 52 | + .filterNotNull() |
| 53 | + .map { Result.success(it) }, |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + override fun clearCache() { |
| 58 | + emotionLocalDateSource.clearCache() |
| 59 | + } |
| 60 | + |
| 61 | + private suspend fun fetchAndSaveDailyEmotion( |
| 62 | + today: LocalDate, |
| 63 | + forceRefresh: Boolean = false, |
| 64 | + ): Result<DailyEmotion> { |
| 65 | + if (!forceRefresh) { |
| 66 | + val currentLocalData = emotionLocalDateSource.dailyEmotion.value |
| 67 | + if (currentLocalData != null && !currentLocalData.isStale(today)) { |
| 68 | + return Result.success(currentLocalData) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return fetchMutex.withLock { |
| 73 | + if (!forceRefresh) { |
| 74 | + val doubleCheckData = emotionLocalDateSource.dailyEmotion.value |
| 75 | + if (doubleCheckData != null && !doubleCheckData.isStale(today)) { |
| 76 | + return@withLock Result.success(doubleCheckData) |
| 77 | + } |
49 | 78 | } |
50 | | - } finally { |
51 | | - isFetching.set(false) |
| 79 | + emotionRemoteDataSource.fetchDailyEmotion(today.toString()) |
| 80 | + .onSuccess { emotionLocalDateSource.saveDailyEmotion(it.toDomain(today)) } |
| 81 | + .map { it.toDomain(today) } |
52 | 82 | } |
53 | 83 | } |
54 | 84 | } |
0 commit comments