From 0a1e85631015091a3c0952c89a90f8322c80c334 Mon Sep 17 00:00:00 2001 From: iliyangermanov Date: Fri, 30 Aug 2024 22:50:55 +0300 Subject: [PATCH] WIP: Rework time in the VM --- .../transaction/EditTransactionViewEvent.kt | 5 ++- .../transaction/EditTransactionViewModel.kt | 39 +++++++++---------- .../main/java/com/ivy/legacy/utils/DateExt.kt | 16 -------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewEvent.kt b/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewEvent.kt index 5a5d273318..3a5357aa4f 100644 --- a/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewEvent.kt +++ b/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewEvent.kt @@ -12,6 +12,7 @@ import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData import com.ivy.wallet.domain.deprecated.logic.model.CreateCategoryData import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableSet +import java.time.Instant import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime @@ -23,8 +24,8 @@ data class EditTransactionViewState( val titleSuggestions: ImmutableSet, val currency: String, val description: String?, - val dateTime: LocalDateTime?, - val dueDate: LocalDateTime?, + val dateTime: Instant?, + val dueDate: Instant?, val accounts: ImmutableList, val categories: ImmutableList, val account: Account?, 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 7dcd74d404..06e5747de4 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 @@ -71,9 +71,9 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.async import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch import java.math.BigDecimal +import java.time.Instant import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime @@ -116,11 +116,8 @@ class EditTransactionViewModel @Inject constructor( private val titleSuggestions = mutableStateOf(persistentSetOf()) private val currency = mutableStateOf("") private val description = mutableStateOf(null) - private val dateTime = mutableStateOf(null) - private val dueDate = mutableStateOf(null) - private val paidHistory = mutableStateOf(null) - private val date = MutableStateFlow(null) - private val time = MutableStateFlow(null) + private val dateTime = mutableStateOf(null) + private val dueDate = mutableStateOf(null) private val accounts = mutableStateOf>(persistentListOf()) private val categories = mutableStateOf>(persistentListOf()) private val tags = mutableStateOf>(persistentListOf()) @@ -132,6 +129,8 @@ class EditTransactionViewModel @Inject constructor( private val hasChanges = mutableStateOf(false) private val displayLoanHelper = mutableStateOf(EditTransactionDisplayLoan()) + private var paidHistory: Instant? = null + // This is used to when the transaction is associated with a loan/loan record, // used to indicate the background updating of loan/loanRecord data private val backgroundProcessingStarted = mutableStateOf(false) @@ -241,12 +240,12 @@ class EditTransactionViewModel @Inject constructor( } @Composable - private fun getDateTime(): LocalDateTime? { + private fun getDateTime(): Instant? { return dateTime.value } @Composable - private fun getDueDate(): LocalDateTime? { + private fun getDueDate(): Instant? { return dueDate.value } @@ -377,10 +376,10 @@ class EditTransactionViewModel @Inject constructor( transactionType.value = transaction.type initialTitle.value = transaction.title - dateTime.value = with(timeConverter) { transaction.dateTime?.toLocalDateTime() } + dateTime.value = transaction.dateTime description.value = transaction.description - dueDate.value = with(timeConverter) { transaction.dueDate?.toLocalDateTime() } - paidHistory.value = with(timeConverter) { transaction.paidFor?.toLocalDateTime() } + dueDate.value = transaction.dueDate + paidHistory = transaction.paidFor val selectedAccount = accountByIdAct(transaction.accountId)!! account.value = selectedAccount toAccount.value = transaction.toAccountId?.let { @@ -526,19 +525,21 @@ class EditTransactionViewModel @Inject constructor( } private fun onDueDateChanged(newDueDate: LocalDateTime?) { + val newDueDateUtc = with(timeConverter) { newDueDate?.toUTC() } loadedTransaction = loadedTransaction().copy( - dueDate = with(timeConverter) { newDueDate?.toUTC() } + dueDate = newDueDateUtc ) - dueDate.value = newDueDate + dueDate.value = newDueDateUtc saveIfEditMode() } private fun onSetDateTime(newDateTime: LocalDateTime) { + val newDateTimeUtc = with(timeConverter) { newDateTime.toUTC() } loadedTransaction = loadedTransaction().copy( - dateTime = with(timeConverter) { newDateTime.toUTC() } + dateTime = newDateTimeUtc ) - dateTime.value = newDateTime + dateTime.value = newDateTimeUtc saveIfEditMode() } @@ -547,7 +548,6 @@ class EditTransactionViewModel @Inject constructor( loadedTransaction = loadedTransaction().copy( date = newDate ) - date.value = newDate onSetDateTime( getTrueDate( loadedTransaction?.date ?: dateNowLocal(), @@ -586,10 +586,9 @@ class EditTransactionViewModel @Inject constructor( syncTransaction = false ) { paidTransaction -> loadedTransaction = paidTransaction - paidHistory.value = - with(timeConverter) { paidTransaction.paidFor?.toLocalDateTime() } - dueDate.value = with(timeConverter) { paidTransaction.dueDate?.toLocalDateTime() } - dateTime.value = with(timeConverter) { paidTransaction.dateTime?.toLocalDateTime() } + paidHistory = paidTransaction.paidFor + dueDate.value = paidTransaction.dueDate + dateTime.value = paidTransaction.dateTime saveIfEditMode( closeScreen = true diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/utils/DateExt.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/utils/DateExt.kt index ce6a0e79bc..d7f65340e3 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/utils/DateExt.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/utils/DateExt.kt @@ -229,22 +229,6 @@ fun LocalDateTime.convertLocalToUTC(): LocalDateTime { return this.minusSeconds(offset) } -// 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 -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() -} - fun LocalDate.formatLocal( pattern: String = "dd MMM yyyy", zone: ZoneId = ZoneOffset.systemDefault()