Skip to content

Commit

Permalink
feat : Date 계산 로직 도메인으로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
chws0508 committed Mar 19, 2024
1 parent 5940514 commit 894eeb7
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -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일"

This file was deleted.

1 change: 1 addition & 0 deletions core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<string name="minute_format">%1$d분 전</string>
<string name="hour_format">%1$d시간 전</string>
<string name="day_format">%1$d일 전</string>
<string name="years_format">%1$d년 전</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
)
},
Expand Down

0 comments on commit 894eeb7

Please sign in to comment.