|
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 | | -import com.threegap.bitnagil.domain.emotion.model.EmotionChangeEvent |
8 | 8 | import com.threegap.bitnagil.domain.emotion.model.EmotionRecommendRoutine |
9 | 9 | import com.threegap.bitnagil.domain.emotion.repository.EmotionRepository |
10 | 10 | import kotlinx.coroutines.flow.Flow |
11 | | -import kotlinx.coroutines.flow.MutableSharedFlow |
12 | | -import kotlinx.coroutines.flow.asSharedFlow |
| 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 |
| 17 | +import java.time.LocalDate |
13 | 18 | import javax.inject.Inject |
14 | 19 |
|
15 | 20 | class EmotionRepositoryImpl @Inject constructor( |
16 | | - private val emotionDataSource: EmotionDataSource, |
| 21 | + private val emotionRemoteDataSource: EmotionRemoteDataSource, |
| 22 | + private val emotionLocalDataSource: EmotionLocalDataSource, |
17 | 23 | ) : EmotionRepository { |
| 24 | + |
| 25 | + private val fetchMutex = Mutex() |
| 26 | + |
18 | 27 | override suspend fun getEmotions(): Result<List<Emotion>> { |
19 | | - return emotionDataSource.getEmotions().map { response -> |
| 28 | + return emotionRemoteDataSource.getEmotions().map { response -> |
20 | 29 | response.map { it.toDomain() } |
21 | 30 | } |
22 | 31 | } |
23 | 32 |
|
24 | 33 | override suspend fun registerEmotion(emotionMarbleType: String): Result<List<EmotionRecommendRoutine>> { |
25 | | - return emotionDataSource.registerEmotion(emotionMarbleType).map { |
26 | | - it.recommendedRoutines.map { |
27 | | - emotionRecommendedRoutineDto -> |
| 34 | + return emotionRemoteDataSource.registerEmotion(emotionMarbleType).map { |
| 35 | + it.recommendedRoutines.map { emotionRecommendedRoutineDto -> |
28 | 36 | emotionRecommendedRoutineDto.toEmotionRecommendRoutine() |
29 | 37 | } |
30 | 38 | }.also { |
31 | | - if (it.isSuccess) { |
32 | | - _emotionChangeEventFlow.emit(EmotionChangeEvent.ChangeEmotion(emotionMarbleType)) |
33 | | - } |
| 39 | + if (it.isSuccess) fetchAndSaveDailyEmotion(today = LocalDate.now(), forceRefresh = true) |
34 | 40 | } |
35 | 41 | } |
36 | 42 |
|
37 | | - override suspend fun fetchDailyEmotion(currentDate: String): Result<DailyEmotion> = |
38 | | - emotionDataSource.fetchDailyEmotion(currentDate).map { it.toDomain() } |
| 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 | + emotionLocalDataSource.dailyEmotion |
| 52 | + .filterNotNull() |
| 53 | + .map { Result.success(it) }, |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + override fun clearCache() { |
| 58 | + emotionLocalDataSource.clearCache() |
| 59 | + } |
| 60 | + |
| 61 | + private suspend fun fetchAndSaveDailyEmotion( |
| 62 | + today: LocalDate, |
| 63 | + forceRefresh: Boolean = false, |
| 64 | + ): Result<DailyEmotion> { |
| 65 | + if (!forceRefresh) { |
| 66 | + val currentLocalData = emotionLocalDataSource.dailyEmotion.value |
| 67 | + if (currentLocalData != null && !currentLocalData.isStale(today)) { |
| 68 | + return Result.success(currentLocalData) |
| 69 | + } |
| 70 | + } |
39 | 71 |
|
40 | | - private val _emotionChangeEventFlow = MutableSharedFlow<EmotionChangeEvent>() |
41 | | - override suspend fun getEmotionChangeEventFlow(): Flow<EmotionChangeEvent> = _emotionChangeEventFlow.asSharedFlow() |
| 72 | + return fetchMutex.withLock { |
| 73 | + if (!forceRefresh) { |
| 74 | + val doubleCheckData = emotionLocalDataSource.dailyEmotion.value |
| 75 | + if (doubleCheckData != null && !doubleCheckData.isStale(today)) { |
| 76 | + return@withLock Result.success(doubleCheckData) |
| 77 | + } |
| 78 | + } |
| 79 | + emotionRemoteDataSource.fetchDailyEmotion(today.toString()) |
| 80 | + .onSuccess { emotionLocalDataSource.saveDailyEmotion(it.toDomain(today)) } |
| 81 | + .map { it.toDomain(today) } |
| 82 | + } |
| 83 | + } |
42 | 84 | } |
0 commit comments