Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
WIP: Fix time problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Aug 30, 2024
1 parent 8b4d1b9 commit af17364
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import com.ivy.legacy.datamodel.temp.toDomain
import com.ivy.legacy.domain.deprecated.logic.AccountCreator
import com.ivy.legacy.utils.computationThread
import com.ivy.legacy.utils.convertUTCToLocal
import com.ivy.legacy.utils.dateNowLocal
import com.ivy.legacy.utils.ioThread
import com.ivy.legacy.utils.timeUTC
import com.ivy.legacy.utils.toLowerCaseLocal
import com.ivy.legacy.utils.uiThread
import com.ivy.navigation.EditTransactionScreen
Expand Down Expand Up @@ -547,26 +545,30 @@ class EditTransactionViewModel @Inject constructor(
loadedTransaction = loadedTransaction().copy(
date = newDate
)
val localDateTime = with(timeConverter) {
(dateTime.value ?: timeProvider.utcNow()).toLocalDateTime()
}
onSetDateTime(
getTrueDate(
loadedTransaction?.date ?: dateNowLocal(),
(dateTime.value?.toLocalTime() ?: timeUTC()),
true
)
localDateTime
.withDayOfMonth(newDate.dayOfMonth)
.withMonth(newDate.monthValue)
.withYear(newDate.year)
)
}

private fun onSetTime(newTime: LocalTime) {
loadedTransaction = loadedTransaction().copy(
time = newTime.convertUTCToLocal()
)
time.value = newTime
val localDateTime = with(timeConverter) {
(dateTime.value ?: timeProvider.utcNow()).toLocalDateTime()
}
onSetDateTime(
getTrueDate(
dateTime.value?.toLocalDate() ?: dateNowLocal(),
loadedTransaction?.time ?: timeUTC(),
true
)
localDateTime
.withHour(newTime.hour)
.withMinute(newTime.minute)
.withSecond(0)
.withNano(0)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.ivy.design.api.LocalTimeConverter
import com.ivy.legacy.ivyWalletCtx
import com.ivy.legacy.utils.convertLocalToUTC
import com.ivy.legacy.utils.convertUTCToLocal
import com.ivy.legacy.utils.convertUTCtoLocal
import com.ivy.legacy.utils.formatLocalTime
import com.ivy.legacy.utils.formatNicely
import com.ivy.legacy.utils.getTrueDate
import com.ivy.legacy.utils.timeNowUTC
import com.ivy.ui.R
import com.ivy.wallet.ui.theme.components.IvyOutlinedButton
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime

@Composable
fun DateTimeRow(
Expand All @@ -31,6 +36,7 @@ fun DateTimeRow(
) {
Spacer(Modifier.width(24.dp))

val timeConverter = LocalTimeConverter.current
IvyOutlinedButton(
text = dateTime.formatNicely(),
iconStart = R.drawable.ic_date
Expand All @@ -57,4 +63,25 @@ fun DateTimeRow(

Spacer(Modifier.width(24.dp))
}
}

// The timepicker returns time in UTC, but the date picker returns date in LocalTimeZone
// hence use this method to get both date & time in UTC
@Deprecated("Rework this to use the TimeConverter API")
fun getTrueDate(
date: LocalDate,
time: LocalTime,
convert: Boolean = true
): LocalDateTime {
val timeLocal = if (convert) time.convertUTCToLocal() else time

return timeNowUTC()
.withYear(date.year)
.withMonth(date.monthValue)
.withDayOfMonth(date.dayOfMonth)
.withHour(timeLocal.hour)
.withMinute(timeLocal.minute)
.withSecond(0)
.withNano(0)
.convertLocalToUTC()
}

0 comments on commit af17364

Please sign in to comment.