Skip to content

Commit

Permalink
refactor: requireNotNull 로직 제거, isTodayWithinDateRange 로직 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
librarywon committed Dec 8, 2024
1 parent 103cbc7 commit 1427e0c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.teamhy2.main.domain.model.Promotion
import com.teamhy2.main.domain.util.DateUtil
import kotlinx.coroutines.tasks.await
import kotlinx.serialization.json.Json
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import javax.inject.Inject

class RemoteConfigPromotionDataSource
Expand All @@ -30,27 +28,14 @@ class RemoteConfigPromotionDataSource
val promotionDto: PromotionDto = Json.decodeFromString(promotionDataJson)
promotionDto.copy(
isActive =
calculatePromotionActive(
promotionDto.startDate,
promotionDto.endDate,
DateUtil.isTodayWithinDateRange(
startDateString = promotionDto.startDate,
endDateString = promotionDto.endDate,
),
).toDomain()
}
}

private fun calculatePromotionActive(
startDate: String,
endDate: String,
): Boolean {
return runCatching {
val formatter: DateTimeFormatter = DateTimeFormatter.ISO_DATE
val startDate: LocalDate = LocalDate.parse(startDate, formatter)
val endDate: LocalDate = LocalDate.parse(endDate, formatter)

DateUtil.isTodayWithinDateRange(startDate = startDate, endDate = endDate)
}.getOrDefault(false)
}

companion object {
const val PROMOTION_POPUP_KEY = "promotionPopup"
const val DEFAULT_PROMOTION_JSON =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ class PromotionDataStore
dataStore.data.map { preferences ->
val startDateString: String? = preferences[START_DATE]
val endDateString: String? = preferences[END_DATE]
requireNotNull(startDateString) { ERROR_START_DATE_MISSING }
requireNotNull(endDateString) { ERROR_END_DATE_MISSING }
if (startDateString == null || endDateString == null) return@map false

val startDate: LocalDate = LocalDate.parse(startDateString, formatter)
val endDate: LocalDate = LocalDate.parse(endDateString, formatter)

DateUtil.isTodayWithinDateRange(startDate = startDate, endDate = endDate)
DateUtil.isTodayWithinDateRange(startDateString = startDateString, endDateString = endDateString)
}

suspend fun savePromotionDismissPeriod(
Expand All @@ -46,9 +42,6 @@ class PromotionDataStore
private const val PROMOTION_START_DATE_KEY = "promotion_start_date"
private const val PROMOTION_END_DATE_KEY = "promotion_end_date"

const val ERROR_START_DATE_MISSING = "DataStore에 시작 날짜가 없습니다."
const val ERROR_END_DATE_MISSING = "DataStore에 종료 날짜가 없습니다."

val START_DATE: Preferences.Key<String> =
stringPreferencesKey(PROMOTION_START_DATE_KEY)
val END_DATE: Preferences.Key<String> =
Expand Down
34 changes: 27 additions & 7 deletions main-domain/src/main/java/com/teamhy2/main/domain/util/DateUtil.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package com.teamhy2.main.domain.util

import java.time.LocalDate
import java.time.format.DateTimeFormatter

object DateUtil {
/**
* Checks if the current date is within the specified date range.
*
* @param startDate The start date of the range.
* @param endDate The end date of the range.
* @param startDateString The start date string of the range.
* @param endDateString The end date string of the range.
* @return True if the current date is within the range, false otherwise.
*/
fun isTodayWithinDateRange(
startDate: LocalDate,
endDate: LocalDate,
startDateString: String,
endDateString: String,
): Boolean {
val today: LocalDate = LocalDate.now()
return today.isEqual(startDate) || today.isEqual(endDate) ||
(today.isAfter(startDate) && today.isBefore(endDate))
val formatter = DateTimeFormatter.ofPattern("[yyyy-MM-dd][yyyy-M-d]")

val startDateResult =
runCatching {
LocalDate.parse(startDateString, formatter)
}

val endDateResult =
runCatching {
LocalDate.parse(endDateString, formatter)
}

if (startDateResult.isFailure || endDateResult.isFailure) {
return false
}

val startDate = startDateResult.getOrNull()
val endDate = endDateResult.getOrNull()
val todayDate: LocalDate = LocalDate.now()

return todayDate.isEqual(startDate) || todayDate.isEqual(endDate) ||
(todayDate.isAfter(startDate) && todayDate.isBefore(endDate))
}
}

0 comments on commit 1427e0c

Please sign in to comment.