Skip to content

Commit 3edbfd1

Browse files
committed
Refactor: DailyEmotion 내 isStale 로직 개선 및 LocalDate 주입 방식 변경
1 parent ead9462 commit 3edbfd1

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

data/src/main/java/com/threegap/bitnagil/data/emotion/model/response/DailyEmotionResponse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ data class DailyEmotionResponse(
1818
val emotionMarbleHomeMessage: String?,
1919
)
2020

21-
fun DailyEmotionResponse.toDomain(): DailyEmotion =
21+
fun DailyEmotionResponse.toDomain(fetchedDate: LocalDate): DailyEmotion =
2222
DailyEmotion(
2323
type = emotionMarbleType,
2424
name = emotionMarbleName,
2525
imageUrl = imageUrl,
2626
homeMessage = emotionMarbleHomeMessage,
27-
fetchedDate = LocalDate.now(),
27+
fetchedDate = fetchedDate,
2828
)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EmotionRepositoryImpl @Inject constructor(
2121
private val _dailyEmotionFlow = MutableStateFlow(DailyEmotion.INIT)
2222
override val dailyEmotionFlow: Flow<DailyEmotion> = _dailyEmotionFlow
2323
.onSubscription {
24-
if (_dailyEmotionFlow.value.isStale) fetchDailyEmotion()
24+
if (_dailyEmotionFlow.value.isStale(LocalDate.now())) fetchDailyEmotion()
2525
}
2626

2727
override suspend fun getEmotions(): Result<List<Emotion>> {
@@ -43,9 +43,9 @@ class EmotionRepositoryImpl @Inject constructor(
4343
override suspend fun fetchDailyEmotion(): Result<Unit> {
4444
if (!isFetching.compareAndSet(false, true)) return Result.success(Unit)
4545
return try {
46-
val currentDate = LocalDate.now().toString()
47-
emotionDataSource.fetchDailyEmotion(currentDate).map {
48-
_dailyEmotionFlow.value = it.toDomain()
46+
val today = LocalDate.now()
47+
emotionDataSource.fetchDailyEmotion(today.toString()).map {
48+
_dailyEmotionFlow.value = it.toDomain(today)
4949
}
5050
} finally {
5151
isFetching.set(false)

domain/src/main/java/com/threegap/bitnagil/domain/emotion/model/DailyEmotion.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ data class DailyEmotion(
99
val homeMessage: String?,
1010
val fetchedDate: LocalDate = LocalDate.MIN,
1111
) {
12-
val isStale: Boolean
13-
get() = fetchedDate != LocalDate.now()
12+
fun isStale(today: LocalDate): Boolean = fetchedDate != today
1413

1514
companion object {
1615
val INIT = DailyEmotion(

0 commit comments

Comments
 (0)