diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt new file mode 100644 index 00000000..249061ec --- /dev/null +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt @@ -0,0 +1,10 @@ +package com.withpeace.withpeace.core.domain.model.date; + +import java.time.LocalDateTime + +data class Date( + val date: LocalDateTime, +) { + val durationFromNow: DurationFromNow + get() = DurationFromNow.from(date) +} diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt new file mode 100644 index 00000000..e57af8a2 --- /dev/null +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt @@ -0,0 +1,44 @@ +package com.withpeace.withpeace.core.domain.model.date + +import java.time.Duration +import java.time.LocalDateTime + +sealed class DurationFromNow( + val value: Duration, +) { + data class LessThanOneMinute(val duration: Duration) : DurationFromNow(duration) + data class OneMinuteToOneHour(val duration: Duration) : DurationFromNow(duration) + data class OneHourToOneDay(val duration: Duration) : DurationFromNow(duration) + data class OneDayToSevenDay(val duration: Duration) : DurationFromNow(duration) + data class SevenDayToOneYear(val duration: Duration) : DurationFromNow(duration) + data class OverOneYear(val duration: Duration) : DurationFromNow(duration) + + val seconds = value.seconds + val minutes = value.toMinutes() + val hours = value.toHours() + val days = value.toDays() + val years = value.toDays() / DAYS_FOR_YEAR + + companion object { + fun from(date: LocalDateTime): DurationFromNow { + val duration = Duration.between(date, LocalDateTime.now()) + return when { + duration.isLessThanOneMinute() -> LessThanOneMinute(duration) + duration.isLessThanOneHour() -> OneMinuteToOneHour(duration) + duration.isLessThanOneDay() -> OneHourToOneDay(duration) + duration.isLessThanWeekDays() -> OneDayToSevenDay(duration) + duration.isLessOneYear() -> SevenDayToOneYear(duration) + else -> OverOneYear(duration) + } + } + + private fun Duration.isLessThanOneMinute() = toMinutes() < 1 + private fun Duration.isLessThanOneHour() = toHours() < 1 + private fun Duration.isLessThanOneDay() = toDays() < 1 + private fun Duration.isLessThanWeekDays() = toDays() < DAYS_FOR_WEEK + private fun Duration.isLessOneYear() = toDays() < DAYS_FOR_YEAR + + private const val DAYS_FOR_YEAR = 365 + private const val DAYS_FOR_WEEK = 7 + } +} diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt index 33f108e8..76fe4cf2 100644 --- a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt @@ -1,12 +1,12 @@ package com.withpeace.withpeace.core.domain.model.post -import java.time.LocalDateTime +import com.withpeace.withpeace.core.domain.model.date.Date data class Post( val postId: Long, val title: String, val content: String, val postTopic: PostTopic, - val createDate: LocalDateTime, + val createDate: Date, val postImageUrl: String, ) diff --git a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiState.kt b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiState.kt new file mode 100644 index 00000000..cdf560fe --- /dev/null +++ b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiState.kt @@ -0,0 +1,40 @@ +package com.withpeace.withpeace.core.ui + +import android.content.Context +import com.withpeace.withpeace.core.domain.model.date.Date +import com.withpeace.withpeace.core.domain.model.date.DurationFromNow +import java.time.format.DateTimeFormatter + +fun Date.toRelativeString(context: Context): String { + return when (durationFromNow) { + is DurationFromNow.LessThanOneMinute -> { + context.getString( + R.string.second_format, + durationFromNow.seconds, + ) + } + + is DurationFromNow.OneHourToOneDay -> context.getString( + R.string.hour_format, + durationFromNow.minutes, + ) + + is DurationFromNow.OneDayToSevenDay -> context.getString( + R.string.second_format, + durationFromNow.days, + ) + + is DurationFromNow.OneMinuteToOneHour -> context.getString( + R.string.day_format, + durationFromNow.days, + ) + + is DurationFromNow.SevenDayToOneYear -> date.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + is DurationFromNow.OverOneYear -> context.getString( + R.string.years_format, + durationFromNow.years, + ) + } +} + +private const val DATE_FORMAT = "MM월 DD일" diff --git a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/LocalDateTimeUtil.kt b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/LocalDateTimeUtil.kt deleted file mode 100644 index d91f2b7a..00000000 --- a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/LocalDateTimeUtil.kt +++ /dev/null @@ -1,37 +0,0 @@ -package com.withpeace.withpeace.core.ui - -import android.content.Context -import java.time.Duration -import java.time.LocalDateTime -import java.time.format.DateTimeFormatter - -fun LocalDateTime.toRelativeString(context: Context): String { - val currentTime = LocalDateTime.now() - val duration = Duration.between(this, currentTime) - return when { - duration.isLessThanOneMinute() -> context.getString( - R.string.second_format, - duration.seconds, - ) - - duration.isLessThanOneHour() -> context.getString( - R.string.minute_format, - duration.toMinutes(), - ) - - duration.isLessThanOneDay() -> context.getString( - R.string.hour_format, - duration.toHours(), - ) - - duration.isLessThanSevenDays() -> context.getString(R.string.day_format, duration.toDays()) - else -> format(DateTimeFormatter.ofPattern(DATE_FORMAT)) - } -} - -private fun Duration.isLessThanOneMinute() = toMinutes() < 1 -private fun Duration.isLessThanOneHour() = toHours() < 1 -private fun Duration.isLessThanOneDay() = toDays() < 1 -private fun Duration.isLessThanSevenDays() = toDays() < 7 - -private const val DATE_FORMAT = "MM월 DD일" diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index d44e05ac..c6eb36af 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -10,5 +10,6 @@ %1$d분 전 %1$d시간 전 %1$d일 전 + %1$d년 전 diff --git a/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt b/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt index 2cde6848..9b4e3bd2 100644 --- a/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt +++ b/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt @@ -28,6 +28,7 @@ import com.skydoves.landscapist.glide.GlideImage import com.withpeace.withpeace.core.designsystem.theme.PretendardFont import com.withpeace.withpeace.core.designsystem.theme.WithpeaceTheme import com.withpeace.withpeace.core.designsystem.ui.WithpeaceCard +import com.withpeace.withpeace.core.domain.model.date.Date import com.withpeace.withpeace.core.domain.model.post.Post import com.withpeace.withpeace.core.domain.model.post.PostTopic import com.withpeace.withpeace.core.ui.R @@ -144,7 +145,7 @@ private fun PostListScreenPreview() { title = "periculis", content = "pellentesq\nuehaha", postTopic = PostTopic.INFORMATION, - createDate = LocalDateTime.now(), + createDate = Date(LocalDateTime.now()), postImageUrl = "https://duckduckgo.com/?q=verterem", ) },