Skip to content

Commit

Permalink
#6 Date -> LocalDateTime 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
pennya committed Aug 11, 2021
1 parent c364919 commit a3a9349
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 31 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
}
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand Down Expand Up @@ -74,4 +75,6 @@ dependencies {
releaseImplementation 'com.github.eastar-dev.EastarLog2:logNoOp:2.1.9'
testImplementation 'com.github.eastar-dev.EastarLog2:logTest:2.1.9'

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

}
3 changes: 3 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android {
}
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand Down Expand Up @@ -73,5 +74,7 @@ dependencies {
implementation "com.google.dagger:hilt-android:$versions.hiltVersion"
kapt "com.google.dagger:hilt-compiler:$versions.hiltVersion"

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

implementation project(":domain")
}
10 changes: 5 additions & 5 deletions data/src/main/java/com/teamtuna/emotionaldiary/DateConverter.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.teamtuna.emotionaldiary

import androidx.room.TypeConverter
import java.util.Date
import java.time.LocalDateTime

object DateConverter {

@TypeConverter
fun toDate(value: Long?): Date? {
return value?.let { Date(it) }
fun toDate(value: Long?): LocalDateTime? {
return value?.let { it.toLocalDateTime() }
}

@TypeConverter
fun fromDate(date: Date?): Long? {
return date?.time
fun fromDate(date: LocalDateTime?): Long? {
return date?.getTime()
}
}
83 changes: 83 additions & 0 deletions data/src/main/java/com/teamtuna/emotionaldiary/TimeUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.teamtuna.emotionaldiary

import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.Period
import java.time.ZoneId
import java.util.Date

fun Date.convertInstant(): Instant {
return Instant.ofEpochMilli(this.time)
}

fun Int.toLocalDateSecondTime(): LocalDateTime {
return (this * 1000L).toLocalDateTime()
}

fun Int.toLocalDate(): LocalDate {
return (this * 1000L).toLocalDate()
}

fun Int.toLocalTime(): LocalTime {
return toLocalDateSecondTime().toLocalTime()
}

fun Int.timeInMills(): Long {
return this.toLocalDateSecondTime().getTime()
}

fun Long.toLocalDateTime(): LocalDateTime {
return LocalDateTime.ofInstant(Date(this).convertInstant(), ZoneId.systemDefault())
}

fun Long.toLocalDate(): LocalDate {
return Instant.ofEpochMilli(this).atZone(ZoneId.systemDefault()).toLocalDate()
}

fun Long.timeInSeconds(): Int {
return ((this / 1000L).toInt())
}

fun LocalDateTime.getDayOfDateTime(): LocalDateTime {
return this
.withHour(0)
.withMinute(0)
.withSecond(0)
}

fun LocalDateTime.getDayOfTime(): Long {
return this
.withHour(0)
.withMinute(0)
.withSecond(0)
.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
}

fun LocalDateTime.getTime(): Long {
return this.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
}

fun Long.isToday(): Boolean {
val period = Period.between(LocalDate.now(), toLocalDateTime().toLocalDate())
return period.years == 0 && period.months == 0 && period.days == 0
}

fun Long.isYesterday(): Boolean {
val period = Period.between(LocalDate.now(), toLocalDateTime().toLocalDate())
return period.years == 0 && period.months == 0 && period.days == -1
}

fun Long.isPastYear(): Boolean {
return LocalDate.now().year > toLocalDateTime().toLocalDate().year
}

fun Long.beforeCurrentTime(): Boolean {
return this < System.currentTimeMillis()
}

fun epoch(year: Int, month: Int, dayOfMonth: Int): Int {
return LocalDateTime.now().withYear(year).withMonth(month).withDayOfMonth(dayOfMonth)
.atZone(ZoneId.systemDefault()).toEpochSecond().toInt()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.teamtuna.emotionaldiary.db.EmotionalEntity
import com.teamtuna.emotionaldiary.entity.DailyEmotion
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.UniqId
import java.util.Date
import java.time.LocalDateTime

interface LocalDataSource {
suspend fun add(emotion: Emotion, reason: String): UniqId
suspend fun add(emotion: Emotion, date: Date, reason: String): UniqId
suspend fun add(emotion: Emotion, date: LocalDateTime, reason: String): UniqId
suspend fun get(id: UniqId): EmotionalEntity?

suspend fun replace(dailyEmotion: DailyEmotion): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.teamtuna.emotionaldiary.entity.DailyEmotion
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.UniqId
import com.teamtuna.emotionaldiary.toEntity
import java.util.Date
import java.time.LocalDateTime
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

Expand All @@ -19,7 +19,7 @@ class LocalDataSourceImpl(
return@withContext emotionDao.insertEmotional(entity)
}

override suspend fun add(emotion: Emotion, date: Date, reason: String): UniqId =
override suspend fun add(emotion: Emotion, date: LocalDateTime, reason: String): UniqId =
withContext(Dispatchers.IO) {
val entity = EmotionalEntity(emotion = emotion, date = date, reason = reason)
return@withContext emotionDao.insertEmotional(entity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import androidx.room.Entity
import androidx.room.PrimaryKey
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.UniqId
import java.util.Calendar
import java.util.Date
import java.time.LocalDateTime

@Entity
data class EmotionalEntity(
@PrimaryKey(autoGenerate = true)
val id: UniqId = 0,
val emotion: Emotion,
val date: Date = Calendar.getInstance().time,
val date: LocalDateTime = LocalDateTime.now(),
val reason: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.teamtuna.emotionaldiary.entity.ErrorModel
import com.teamtuna.emotionaldiary.entity.Result
import com.teamtuna.emotionaldiary.entity.UniqId
import com.teamtuna.emotionaldiary.toDailyEmotion
import java.util.Date
import java.time.LocalDateTime
import javax.inject.Inject

class EmotionRepositoryImpl @Inject constructor(
Expand All @@ -24,7 +24,7 @@ class EmotionRepositoryImpl @Inject constructor(
return Result.Success(localDataSource.replace(dailyEmotion))
}

override suspend fun add(emotion: Emotion, date: Date, reason: String): Result<Long> {
override suspend fun add(emotion: Emotion, date: LocalDateTime, reason: String): Result<Long> {
return Result.Success(localDataSource.add(emotion, date, reason))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.teamtuna.emotionaldiary.MainCoroutineRule
import com.teamtuna.emotionaldiary.db.EmotionRoomDatabase
import com.teamtuna.emotionaldiary.entity.DailyEmotion
import com.teamtuna.emotionaldiary.entity.Emotion
import java.util.Calendar
import java.time.LocalDateTime
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.After
Expand Down Expand Up @@ -69,7 +69,7 @@ class LocalDataSourceImplTest {
@Test
fun `replace 기존 데이터가 존재하면 데이터를 바꿔서 저장한다`() = runBlocking {
val actualReason = "test2"
val currentDate = Calendar.getInstance().time
val currentDate = LocalDateTime.now()
assertTrue(localDataSource.replace(
DailyEmotion(1L, Emotion.JOY, currentDate, actualReason))
)
Expand All @@ -84,7 +84,7 @@ class LocalDataSourceImplTest {
fun `replace 기존 데이터가 존재하지 않으면 새롭게 데이터를 넣어서 저장한다`() = runBlocking {
val reason = "test1"
val actualReason = "test2"
val currentDate = Calendar.getInstance().time
val currentDate = LocalDateTime.now()
val dbId = localDataSource.add(Emotion.FEAR, reason)
assertTrue(localDataSource.replace(
DailyEmotion(dbId, Emotion.JOY, currentDate, actualReason))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.teamtuna.emotionaldiary.db.EmotionalEntity
import com.teamtuna.emotionaldiary.entity.DailyEmotion
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.Result
import java.util.Calendar
import java.time.LocalDateTime
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
Expand Down Expand Up @@ -50,7 +50,7 @@ internal class EmotionalRepositoryTest {
@Test
fun addTest2() = runBlocking {
testEmotion = Emotion.JOY
val currentDate = Calendar.getInstance().time
val currentDate = LocalDateTime.now()
whenever(localDataSource.add(testEmotion, currentDate, "Test")).thenReturn(1)
val actual = repository.add(testEmotion, currentDate, "Test")

Expand All @@ -75,7 +75,7 @@ internal class EmotionalRepositoryTest {

assertTrue(actual is Result.Success)

val currentDate = Calendar.getInstance().time
val currentDate = LocalDateTime.now()
val expect = DailyEmotion(
id = 1,
emotion = Emotion.JOY,
Expand All @@ -89,7 +89,7 @@ internal class EmotionalRepositoryTest {
@DisplayName("Repository Replace")
@Test
fun replaceTest() = runBlocking {
val currentDate = Calendar.getInstance().time
val currentDate = LocalDateTime.now()
whenever(localDataSource.replace(
DailyEmotion(1, Emotion.FEAR, currentDate, "Yes"))
).thenReturn(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.teamtuna.emotionaldiary.entity

import java.util.Date
import java.time.LocalDateTime

data class DailyEmotion(val id: UniqId, val emotion: Emotion, val date: Date, val reason: String)
data class DailyEmotion(
val id: UniqId,
val emotion: Emotion,
val date: LocalDateTime,
val reason: String
)

typealias UniqId = Long
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.teamtuna.emotionaldiary.repository
import com.teamtuna.emotionaldiary.entity.DailyEmotion
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.Result
import java.util.Date
import java.time.LocalDateTime

interface EmotionRepository {
suspend fun add(emotion: Emotion, reason: String): Result<Long>
suspend fun replace(dailyEmotion: DailyEmotion): Result<Boolean>
suspend fun add(emotion: Emotion, date: Date, reason: String): Result<Long>
suspend fun add(emotion: Emotion, date: LocalDateTime, reason: String): Result<Long>
suspend fun get(id: Long): Result<DailyEmotion>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package com.teamtuna.emotionaldiary.usecase
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.Result
import com.teamtuna.emotionaldiary.repository.EmotionRepository
import java.util.Date
import java.time.LocalDateTime
import javax.inject.Inject

class EmotionAddByDateUseCase @Inject constructor(
private val emotionRepository: EmotionRepository
) {
suspend operator fun invoke(emotion: Emotion, date: Date, reason: String): Result<Long> {
suspend operator fun invoke(
emotion: Emotion,
date: LocalDateTime,
reason: String
): Result<Long> {
return emotionRepository.add(emotion, date, reason)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.nhaarman.mockitokotlin2.whenever
import com.teamtuna.emotionaldiary.entity.Emotion
import com.teamtuna.emotionaldiary.entity.Result
import com.teamtuna.emotionaldiary.repository.EmotionRepository
import java.util.Calendar
import java.time.LocalDateTime
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.hamcrest.CoreMatchers.equalTo
Expand Down Expand Up @@ -48,7 +48,7 @@ internal class EmotionAddUseCaseTest {
fun addUseCase2() = runBlockingTest {
// given
val answers = Result.Success(1L)
val currentDate = Calendar.getInstance().time
val currentDate = LocalDateTime.now()
whenever(repository.add(Emotion.JOY, currentDate, "기쁨이")).thenReturn(answers)
val addUseCase = EmotionAddByDateUseCase(repository)

Expand Down
3 changes: 3 additions & 0 deletions presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android {
}
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand Down Expand Up @@ -66,6 +67,8 @@ dependencies {
implementation "com.google.dagger:hilt-android:$versions.hiltVersion"
kapt "com.google.dagger:hilt-compiler:$versions.hiltVersion"

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

implementation project(":domain")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.teamtuna.emotionaldiary.entity.process
import com.teamtuna.emotionaldiary.usecase.EmotionAddByDateUseCase
import com.teamtuna.emotionaldiary.usecase.EmotionAddUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import java.util.Date
import java.time.LocalDateTime
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -34,7 +34,7 @@ class EmotionAddViewModel @Inject constructor(
}
}

fun add(emotion: Emotion, date: Date, reason: String) {
fun add(emotion: Emotion, date: LocalDateTime, reason: String) {
viewModelScope.launch(Dispatchers.IO) {
emotionAddByDateUseCase(emotion, date, reason).process({
// Cannot invoke setValue on a background thread
Expand Down

0 comments on commit a3a9349

Please sign in to comment.