diff --git a/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt b/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt index e4ff4ce69b..f58c8a1c26 100644 --- a/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt +++ b/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt @@ -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 @@ -547,12 +545,14 @@ 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) ) } @@ -560,13 +560,15 @@ class EditTransactionViewModel @Inject constructor( 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) ) } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/components/DateTimeRow.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/components/DateTimeRow.kt index 7fc77fc86a..8438ff8647 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/components/DateTimeRow.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/components/DateTimeRow.kt @@ -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( @@ -31,6 +36,7 @@ fun DateTimeRow( ) { Spacer(Modifier.width(24.dp)) + val timeConverter = LocalTimeConverter.current IvyOutlinedButton( text = dateTime.formatNicely(), iconStart = R.drawable.ic_date @@ -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() } \ No newline at end of file