-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feature] PromtionDialog를 구현합니다.
- Loading branch information
Showing
18 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
main-data/src/main/java/com/teamhy2/main/data/datasource/RemoteConfigPromotionDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.teamhy2.main.data.datasource | ||
|
||
import com.google.firebase.ktx.Firebase | ||
import com.google.firebase.remoteconfig.FirebaseRemoteConfig | ||
import com.google.firebase.remoteconfig.ktx.remoteConfig | ||
import com.teamhy2.main.data.dto.PromotionDto | ||
import com.teamhy2.main.data.mapper.toDomain | ||
import com.teamhy2.main.domain.datasource.PromotionDataSource | ||
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 | ||
@Inject | ||
constructor() : PromotionDataSource { | ||
private val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig | ||
|
||
init { | ||
remoteConfig.setDefaultsAsync(DEFAULTS) | ||
} | ||
|
||
override suspend fun fetchPromotionData(): Result<Promotion> { | ||
return runCatching { | ||
remoteConfig.fetchAndActivate().await() | ||
val promotionDataJson: String = remoteConfig.getString(PROMOTION_POPUP_KEY) | ||
val promotionDto: PromotionDto = Json.decodeFromString(promotionDataJson) | ||
promotionDto.copy( | ||
isActive = | ||
calculatePromotionActive( | ||
promotionDto.startDate, | ||
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 = | ||
"""{"imageUrl":"","detailUrl":"","startDate":"","endDate":""}""" | ||
|
||
val DEFAULTS = | ||
mapOf( | ||
PROMOTION_POPUP_KEY to DEFAULT_PROMOTION_JSON, | ||
) | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
main-data/src/main/java/com/teamhy2/main/data/datastore/PromotionDataStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.teamhy2.main.data.datastore | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.MutablePreferences | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.teamhy2.main.domain.util.DateUtil | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
import javax.inject.Inject | ||
|
||
class PromotionDataStore | ||
@Inject | ||
constructor( | ||
private val dataStore: DataStore<Preferences>, | ||
) { | ||
private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_DATE | ||
|
||
val isPromotionDismissed: Flow<Boolean> = | ||
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 } | ||
|
||
val startDate: LocalDate = LocalDate.parse(startDateString, formatter) | ||
val endDate: LocalDate = LocalDate.parse(endDateString, formatter) | ||
|
||
DateUtil.isTodayWithinDateRange(startDate = startDate, endDate = endDate) | ||
} | ||
|
||
suspend fun savePromotionDismissPeriod( | ||
startDate: LocalDate, | ||
endDate: LocalDate, | ||
) { | ||
dataStore.edit { preferences: MutablePreferences -> | ||
preferences[START_DATE] = startDate.format(formatter) | ||
preferences[END_DATE] = endDate.format(formatter) | ||
} | ||
} | ||
|
||
companion object Keys { | ||
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> = | ||
stringPreferencesKey(PROMOTION_END_DATE_KEY) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
main-data/src/main/java/com/teamhy2/main/data/dto/PromotionDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.teamhy2.main.data.dto | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class PromotionDto( | ||
val imageUrl: String, | ||
val detailUrl: String, | ||
val startDate: String, | ||
val endDate: String, | ||
val isActive: Boolean = false, | ||
) |
12 changes: 12 additions & 0 deletions
12
main-data/src/main/java/com/teamhy2/main/data/mapper/PromotionMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.teamhy2.main.data.mapper | ||
|
||
import com.teamhy2.main.data.dto.PromotionDto | ||
import com.teamhy2.main.domain.model.Promotion | ||
|
||
fun PromotionDto.toDomain(): Promotion { | ||
return Promotion( | ||
imageUrl = this.imageUrl, | ||
detailUrl = this.detailUrl, | ||
isActive = this.isActive, | ||
) | ||
} |
31 changes: 31 additions & 0 deletions
31
main-data/src/main/java/com/teamhy2/main/data/repository/DefaultPromotionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.teamhy2.main.data.repository | ||
|
||
import com.teamhy2.main.data.datastore.PromotionDataStore | ||
import com.teamhy2.main.domain.datasource.PromotionDataSource | ||
import com.teamhy2.main.domain.model.Promotion | ||
import com.teamhy2.main.domain.repository.PromotionRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import java.time.LocalDate | ||
import javax.inject.Inject | ||
|
||
class DefaultPromotionRepository | ||
@Inject | ||
constructor( | ||
private val dataSource: PromotionDataSource, | ||
private val dataStore: PromotionDataStore, | ||
) : PromotionRepository { | ||
override val isPromotionDismissed: Flow<Boolean> | ||
get() = dataStore.isPromotionDismissed | ||
|
||
override suspend fun fetchPromotionData(): Result<Promotion> { | ||
return dataSource.fetchPromotionData() | ||
} | ||
|
||
override suspend fun savePromotionDismissPeriod( | ||
startDate: LocalDate, | ||
endDate: LocalDate, | ||
): Result<Unit> = | ||
runCatching { | ||
dataStore.savePromotionDismissPeriod(startDate, endDate) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
main-domain/src/main/java/com/teamhy2/main/domain/datasource/PromotionDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.teamhy2.main.domain.datasource | ||
|
||
import com.teamhy2.main.domain.model.Promotion | ||
|
||
interface PromotionDataSource { | ||
suspend fun fetchPromotionData(): Result<Promotion> | ||
} |
16 changes: 16 additions & 0 deletions
16
main-domain/src/main/java/com/teamhy2/main/domain/model/Promotion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.teamhy2.main.domain.model | ||
|
||
data class Promotion( | ||
val imageUrl: String, | ||
val detailUrl: String, | ||
val isActive: Boolean, | ||
) { | ||
companion object { | ||
val DEFAULT = | ||
Promotion( | ||
imageUrl = "", | ||
detailUrl = "", | ||
isActive = false, | ||
) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
main-domain/src/main/java/com/teamhy2/main/domain/repository/PromotionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.teamhy2.main.domain.repository | ||
|
||
import com.teamhy2.main.domain.model.Promotion | ||
import kotlinx.coroutines.flow.Flow | ||
import java.time.LocalDate | ||
|
||
interface PromotionRepository { | ||
val isPromotionDismissed: Flow<Boolean> | ||
|
||
suspend fun fetchPromotionData(): Result<Promotion> | ||
|
||
suspend fun savePromotionDismissPeriod( | ||
startDate: LocalDate, | ||
endDate: LocalDate, | ||
): Result<Unit> | ||
} |
21 changes: 21 additions & 0 deletions
21
main-domain/src/main/java/com/teamhy2/main/domain/util/DateUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.teamhy2.main.domain.util | ||
|
||
import java.time.LocalDate | ||
|
||
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. | ||
* @return True if the current date is within the range, false otherwise. | ||
*/ | ||
fun isTodayWithinDateRange( | ||
startDate: LocalDate, | ||
endDate: LocalDate, | ||
): Boolean { | ||
val today: LocalDate = LocalDate.now() | ||
return today.isEqual(startDate) || today.isEqual(endDate) || | ||
(today.isAfter(startDate) && today.isBefore(endDate)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.