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

Commit

Permalink
WIP: Rework time in the VM
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Aug 30, 2024
1 parent 4347ad2 commit 0a1e856
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,8 +24,8 @@ data class EditTransactionViewState(
val titleSuggestions: ImmutableSet<String>,
val currency: String,
val description: String?,
val dateTime: LocalDateTime?,
val dueDate: LocalDateTime?,
val dateTime: Instant?,
val dueDate: Instant?,
val accounts: ImmutableList<Account>,
val categories: ImmutableList<Category>,
val account: Account?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -116,11 +116,8 @@ class EditTransactionViewModel @Inject constructor(
private val titleSuggestions = mutableStateOf(persistentSetOf<String>())
private val currency = mutableStateOf("")
private val description = mutableStateOf<String?>(null)
private val dateTime = mutableStateOf<LocalDateTime?>(null)
private val dueDate = mutableStateOf<LocalDateTime?>(null)
private val paidHistory = mutableStateOf<LocalDateTime?>(null)
private val date = MutableStateFlow<LocalDate?>(null)
private val time = MutableStateFlow<LocalTime?>(null)
private val dateTime = mutableStateOf<Instant?>(null)
private val dueDate = mutableStateOf<Instant?>(null)
private val accounts = mutableStateOf<ImmutableList<Account>>(persistentListOf())
private val categories = mutableStateOf<ImmutableList<Category>>(persistentListOf())
private val tags = mutableStateOf<ImmutableList<Tag>>(persistentListOf())
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand All @@ -547,7 +548,6 @@ class EditTransactionViewModel @Inject constructor(
loadedTransaction = loadedTransaction().copy(
date = newDate
)
date.value = newDate
onSetDateTime(
getTrueDate(
loadedTransaction?.date ?: dateNowLocal(),
Expand Down Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions temp/legacy-code/src/main/java/com/ivy/legacy/utils/DateExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0a1e856

Please sign in to comment.