-
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.
- Loading branch information
Showing
7 changed files
with
99 additions
and
40 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.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,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) | ||
} |
44 changes: 44 additions & 0 deletions
44
core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.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,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 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.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 |
---|---|---|
@@ -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, | ||
) |
40 changes: 40 additions & 0 deletions
40
core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiState.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,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일" |
37 changes: 0 additions & 37 deletions
37
core/ui/src/main/java/com/withpeace/withpeace/core/ui/LocalDateTimeUtil.kt
This file was deleted.
Oops, something went wrong.
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