Skip to content

Commit

Permalink
Merge pull request #149 from TimerTiTi/147-daily-automation
Browse files Browse the repository at this point in the history
close #147 daily automation
  • Loading branch information
koreatlwls authored Jun 27, 2024
2 parents b302df3 + 6a1d415 commit bc4b3ef
Show file tree
Hide file tree
Showing 53 changed files with 1,281 additions and 347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
implementation(project(":core:ui"))
implementation(project(":core:util"))
implementation(project(":core:designsystem"))
implementation(project(":tds"))

implementation(libs.findLibrary("androidx.compose.navigation").get())
implementation(libs.findLibrary("androidx.hilt.navigation.compose").get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density

val LocalTiTiColors =
staticCompositionLocalOf {
TdsColorsPalette()
}
val LocalTiTiColors = staticCompositionLocalOf {
TdsColorsPalette()
}

val LocalTiTiTypography =
staticCompositionLocalOf {
TdsTypography()
}
val LocalTiTiTypography = staticCompositionLocalOf {
TdsTypography()
}

object TiTiTheme {
val colors: TdsColorsPalette
Expand All @@ -33,12 +31,11 @@ fun TiTiTheme(
tdsTypography: TdsTypography = TiTiTheme.textStyle,
content: @Composable () -> Unit,
) {
val tdsColorsPalette =
if (darkTheme) {
TdsDarkColorsPalette
} else {
TdsLightColorsPalette
}
val tdsColorsPalette = if (darkTheme) {
TdsDarkColorsPalette
} else {
TdsLightColorsPalette
}

CompositionLocalProvider(
LocalTiTiColors provides tdsColorsPalette,
Expand Down
9 changes: 9 additions & 0 deletions core/ui/src/main/kotlin/com/titi/app/core/ui/ScreenUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.titi.app.core.ui

import android.content.res.Configuration

fun Configuration.isTablet(): Boolean {
val screenLayout = this.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK
return screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE ||
screenLayout == Configuration.SCREENLAYOUT_SIZE_XLARGE
}
60 changes: 41 additions & 19 deletions core/util/src/main/kotlin/com/titi/app/core/util/JavaTimeUtil.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.titi.app.core.util

import java.time.DayOfWeek
import java.time.Duration
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
Expand Down Expand Up @@ -47,25 +46,48 @@ fun LocalDateTime.toOnlyTime(): String {
return this.format(formatter)
}

fun addTimeLine(
startTime: ZonedDateTime,
endTime: ZonedDateTime,
timeLine: List<Long>,
): List<Long> {
var current = startTime
val updateTimeLine = timeLine.toMutableList()
fun getDailyDayWithHour(hour: Int): Pair<String, String> {
val currentDateTime = LocalDateTime.now()

while (current.isBefore(endTime)) {
val diffSeconds = if (current.hour == endTime.hour) {
Duration.between(current, endTime).seconds
} else {
val nextTime = current.plusHours(1).withMinute(0).withSecond(0)
Duration.between(current, nextTime).seconds
}
return if (currentDateTime.hour < hour) {
val startDateTime = currentDateTime
.minusDays(1)
.withHour(hour)
.withMinute(0)
.withSecond(0)
.withNano(0)
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneOffset.UTC)
.toString()
val endDateTime = currentDateTime
.withHour(hour)
.withMinute(0)
.withSecond(0)
.withNano(0)
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneOffset.UTC)
.toString()

updateTimeLine[current.hour] += diffSeconds
current = current.plusHours(1).withMinute(0).withSecond(0)
}
startDateTime to endDateTime
} else {
val startDateTime = currentDateTime
.withHour(hour)
.withMinute(0)
.withSecond(0)
.withNano(0)
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneOffset.UTC)
.toString()
val endDateTime = currentDateTime
.plusDays(1)
.withHour(hour)
.withMinute(0)
.withSecond(0)
.withNano(0)
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneOffset.UTC)
.toString()

return updateTimeLine.toList()
startDateTime to endDateTime
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.titi.app.core.util

import java.time.ZoneId
import java.time.ZonedDateTime

fun String.isAfterH(hour: Int): Boolean {
val inputDateTime = ZonedDateTime.parse(this).withZoneSameInstant(ZoneId.systemDefault())
val currentDateTime = ZonedDateTime.now()

return inputDateTime.dayOfMonth != currentDateTime.dayOfMonth && currentDateTime.hour >= hour
}
22 changes: 0 additions & 22 deletions core/util/src/main/kotlin/com/titi/app/core/util/TimeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ fun String.parseZoneDateTime(): String {
return inputDateTime.format(DateTimeFormatter.ofPattern("uuuu.MM.dd"))
}

fun getTodayDate(): String {
val now = ZonedDateTime.now()
return now.format(DateTimeFormatter.ofPattern("uuuu.MM.dd"))
}

fun addTimeToNow(time: Long): String {
val now = ZonedDateTime.now()
val interval = Duration.ofSeconds(time)
Expand All @@ -31,23 +26,6 @@ fun getTimeToLong(hour: String, minutes: String, seconds: String): Long {
return hourLong * 3600 + minutesLong * 60 + secondsLong
}

fun isAfterSixAM(dateTime: String?): Boolean {
return if (dateTime.isNullOrBlank()) {
false
} else {
val inputDateTime =
ZonedDateTime.parse(dateTime).withZoneSameInstant(ZoneId.systemDefault())
val currentDateTime = ZonedDateTime.now()

if (inputDateTime.dayOfMonth == currentDateTime.dayOfMonth) {
true
} else {
val plusDays = inputDateTime.plusDays(1).dayOfMonth
currentDateTime.hour <= 6 && plusDays == currentDateTime.dayOfMonth
}
}
}

fun getMeasureTime(dateTime: String): Long {
val inputDateTime = ZonedDateTime.parse(dateTime)
val currentDateTime = ZonedDateTime.now(ZoneOffset.UTC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ interface DailyRepository {

suspend fun getDailies(startDateTime: String, endDateTime: String): List<DailyRepositoryModel>?

fun getLastDailyFlow(): Flow<DailyRepositoryModel?>

suspend fun getAllDailies(): List<DailyRepositoryModel>?

suspend fun upsert(dailyRepositoryModel: DailyRepositoryModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ internal interface DailyDao {
@Query(
"SELECT * FROM dailies " +
" WHERE datetime(day) " +
"BETWEEN datetime(:startDateTime) AND datetime(:endDateTime)",
"BETWEEN datetime(:startDateTime) AND datetime(:endDateTime) LIMIT 1",
)
suspend fun getDateDaily(startDateTime: String, endDateTime: String): DailyEntity?

@Query(
"SELECT * FROM dailies " +
" WHERE datetime(day) " +
"BETWEEN datetime(:startDateTime) AND datetime(:endDateTime)",
"BETWEEN datetime(:startDateTime) AND datetime(:endDateTime) LIMIT 1",
)
fun getDateDailyFlow(startDateTime: String, endDateTime: String): Flow<DailyEntity?>

Expand All @@ -28,10 +28,7 @@ internal interface DailyDao {
" WHERE datetime(day) " +
"BETWEEN datetime(:startDateTime) AND datetime(:endDateTime)",
)
suspend fun getWeekDaily(startDateTime: String, endDateTime: String): List<DailyEntity>?

@Query("SELECT * FROM dailies ORDER BY id desc LIMIT 1")
fun getLastDailyFlow(): Flow<DailyEntity?>
suspend fun getDailies(startDateTime: String, endDateTime: String): List<DailyEntity>?

@Query("SELECT * FROM dailies")
suspend fun getAllDailies(): List<DailyEntity>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,12 @@ internal class DailyRepositoryImpl @Inject constructor(
startDateTime: String,
endDateTime: String,
): List<DailyRepositoryModel>? {
return dailyDao.getWeekDaily(
return dailyDao.getDailies(
startDateTime = startDateTime,
endDateTime = endDateTime,
)?.map { it.toRepositoryModel() }
}

override fun getLastDailyFlow(): Flow<DailyRepositoryModel?> {
return dailyDao.getLastDailyFlow().map { it?.toRepositoryModel() }
}

override suspend fun getAllDailies(): List<DailyRepositoryModel>? {
return dailyDao.getAllDailies()?.map { it.toRepositoryModel() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package com.titi.app.doamin.daily.model

import android.os.Parcelable
import com.titi.app.core.util.addTimeLine
import com.titi.app.core.util.getDailyDayWithHour
import kotlin.math.max
import kotlinx.parcelize.Parcelize
import org.threeten.bp.Duration
import org.threeten.bp.ZoneOffset
import org.threeten.bp.ZonedDateTime

@Parcelize
data class Daily(
val id: Long = 0,
val status: String? = null,
val day: String = ZonedDateTime.now(ZoneOffset.UTC).toString(),
val day: String = getDailyDayWithHour(6).first,
val timeLine: List<Long> = LongArray(24) { 0 }.toList(),
val maxTime: Long = 0,
val tasks: Map<String, Long>? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
package com.titi.app.doamin.daily.usecase

import com.titi.app.core.util.getDailyDayWithHour
import com.titi.app.data.daily.api.DailyRepository
import com.titi.app.doamin.daily.mapper.toDomainModel
import com.titi.app.doamin.daily.mapper.toRepositoryModel
import com.titi.app.doamin.daily.model.Daily
import com.titi.app.doamin.daily.model.TaskHistory
import com.titi.app.doamin.daily.model.toUpdateDaily
import java.time.LocalDate
import java.time.ZoneId
import java.time.ZoneOffset
import javax.inject.Inject

class AddMeasureTimeAtDailyUseCase @Inject constructor(
private val dailyRepository: DailyRepository,
) {
suspend operator fun invoke(taskName: String, startTime: String, endTime: String) {
val recentDaily = dailyRepository
.getDateDaily(
startDateTime = LocalDate.now()
.atStartOfDay(ZoneId.systemDefault())
.withZoneSameInstant(ZoneOffset.UTC)
.toString(),
)?.toDomainModel()
val timePair = getDailyDayWithHour(6)
val recentDaily = dailyRepository.getDateDaily(
startDateTime = timePair.first,
endDateTime = timePair.second,
)?.toDomainModel() ?: Daily()

recentDaily?.let { daily ->
val taskHistory = TaskHistory(
startDate = startTime,
endDate = endTime,
)
val taskHistory = TaskHistory(
startDate = startTime,
endDate = endTime,
)

val updateTaskHistories = daily.taskHistories?.toMutableMap()?.apply {
this[taskName] = this[taskName]
?.toMutableList()
?.apply { add(taskHistory) }
?: listOf(taskHistory)
}?.toMap()
?: mapOf(taskName to listOf(taskHistory))
val updateTaskHistories = recentDaily.taskHistories?.toMutableMap()?.apply {
this[taskName] = this[taskName]
?.toMutableList()
?.apply { add(taskHistory) }
?: listOf(taskHistory)
}?.toMap()
?: mapOf(taskName to listOf(taskHistory))

dailyRepository.upsert(
daily.toUpdateDaily(updateTaskHistories).toRepositoryModel(),
)
}
dailyRepository.upsert(
recentDaily.toUpdateDaily(updateTaskHistories).toRepositoryModel(),
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.titi.app.doamin.daily.usecase

import com.titi.app.core.util.getDailyDayWithHour
import com.titi.app.data.daily.api.DailyRepository
import com.titi.app.doamin.daily.mapper.toDomainModel
import com.titi.app.doamin.daily.model.Daily
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class GetTodayDailyFlowUseCase @Inject constructor(
private val dailyRepository: DailyRepository,
) {
operator fun invoke(): Flow<Daily> {
val timePair = getDailyDayWithHour(6)

return dailyRepository.getDateDailyFlow(
startDateTime = timePair.first,
endDateTime = timePair.second,
).map { it?.toDomainModel() ?: Daily() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.titi.app.domain.time.mapper.toRepositoryModel
import com.titi.app.domain.time.model.RecordTimes
import javax.inject.Inject

class UpdateMeasuringStateUseCase @Inject constructor(
class UpdateRecordTimesUseCase @Inject constructor(
private val recordTimesRepository: RecordTimesRepository,
) {
suspend operator fun invoke(recordTimes: RecordTimes) {
Expand Down
1 change: 1 addition & 0 deletions feature/main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ dependencies {

implementation(libs.androidx.splashscreen)
implementation(libs.androidx.material3.window.size)
implementation(libs.lottie)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.titi.app.domain.time.model.RecordTimes
data class SplashResultState(
val recordTimes: RecordTimes = RecordTimes(),
val timeColor: TimeColor = TimeColor(),
val daily: Daily? = null,
val daily: Daily = Daily(),
)

fun SplashResultState.toFeatureTimeModel() = com.titi.app.feature.time.model.SplashResultState(
Expand Down
Loading

0 comments on commit bc4b3ef

Please sign in to comment.