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

Commit

Permalink
Edit transaction time fixes (part 1) (#3446)
Browse files Browse the repository at this point in the history
* Minor refactor

* Minor refactor

* WIP: Rework time in the VM

* WIP: Rework

* WIP: Fix time problems

* Fix Detekt
  • Loading branch information
ILIYANGERMANOV authored Aug 30, 2024
1 parent 8ef3841 commit f461c4f
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInParent
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
Expand All @@ -34,6 +35,7 @@ import com.ivy.base.model.TransactionType
import com.ivy.data.model.Category
import com.ivy.data.model.Tag
import com.ivy.data.model.TagId
import com.ivy.design.api.LocalTimeConverter
import com.ivy.design.l0_system.Orange
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.style
Expand All @@ -42,11 +44,10 @@ import com.ivy.legacy.IvyWalletPreview
import com.ivy.legacy.data.EditTransactionDisplayLoan
import com.ivy.legacy.datamodel.Account
import com.ivy.legacy.ivyWalletCtx
import com.ivy.legacy.rootView
import com.ivy.legacy.ui.component.edit.TransactionDateTime
import com.ivy.legacy.ui.component.edit.core.Description
import com.ivy.legacy.ui.component.tags.AddTagButton
import com.ivy.legacy.ui.component.tags.ShowTagModal
import com.ivy.legacy.utils.convertUTCtoLocal
import com.ivy.legacy.utils.onScreenStart
import com.ivy.navigation.EditPlannedScreen
import com.ivy.navigation.EditTransactionScreen
Expand All @@ -55,11 +56,10 @@ import com.ivy.navigation.navigation
import com.ivy.navigation.screenScopedViewModel
import com.ivy.ui.R
import com.ivy.wallet.domain.data.CustomExchangeRateState
import com.ivy.wallet.domain.data.IvyCurrency
import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData
import com.ivy.wallet.domain.deprecated.logic.model.CreateCategoryData
import com.ivy.wallet.ui.edit.core.Category
import com.ivy.legacy.ui.component.edit.core.Description
import com.ivy.wallet.domain.data.IvyCurrency
import com.ivy.wallet.ui.edit.core.DueDate
import com.ivy.wallet.ui.edit.core.EditBottomSheet
import com.ivy.wallet.ui.edit.core.Title
Expand All @@ -83,9 +83,11 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentSetOf
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneOffset
import java.util.UUID
import kotlin.math.roundToInt

Expand All @@ -99,7 +101,7 @@ fun BoxWithConstraintsScope.EditTransactionScreen(screen: EditTransactionScreen)
viewModel.start(screen)
}

val view = rootView()
val view = LocalView.current

UI(
screen = screen,
Expand All @@ -124,62 +126,62 @@ fun BoxWithConstraintsScope.EditTransactionScreen(screen: EditTransactionScreen)
transactionAssociatedTags = uiState.transactionAssociatedTags,
hasChanges = uiState.hasChanges,
onSetDate = {
viewModel.onEvent(EditTransactionEvent.OnSetDate(it))
viewModel.onEvent(EditTransactionViewEvent.OnSetDate(it))
},
onSetTime = {
viewModel.onEvent(EditTransactionEvent.OnSetTime(it))
viewModel.onEvent(EditTransactionViewEvent.OnSetTime(it))
},
onTitleChange = {
viewModel.onEvent(EditTransactionEvent.OnTitleChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnTitleChanged(it))
},
onDescriptionChange = {
viewModel.onEvent(EditTransactionEvent.OnDescriptionChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnDescriptionChanged(it))
},
onAmountChange = {
viewModel.onEvent(EditTransactionEvent.OnAmountChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnAmountChanged(it))
},
onCategoryChange = {
viewModel.onEvent(EditTransactionEvent.OnCategoryChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnCategoryChanged(it))
},
onAccountChange = {
viewModel.onEvent(EditTransactionEvent.OnAccountChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnAccountChanged(it))
},
onToAccountChange = {
viewModel.onEvent(EditTransactionEvent.OnToAccountChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnToAccountChanged(it))
},
onDueDateChange = {
viewModel.onEvent(EditTransactionEvent.OnDueDateChanged(it))
viewModel.onEvent(EditTransactionViewEvent.OnDueDateChanged(it))
},
onSetTransactionType = {
viewModel.onEvent(EditTransactionEvent.OnSetTransactionType(it))
viewModel.onEvent(EditTransactionViewEvent.OnSetTransactionType(it))
},
onCreateCategory = {
viewModel.onEvent(EditTransactionEvent.CreateCategory(it))
viewModel.onEvent(EditTransactionViewEvent.CreateCategory(it))
},
onEditCategory = {
viewModel.onEvent(EditTransactionEvent.EditCategory(it))
viewModel.onEvent(EditTransactionViewEvent.EditCategory(it))
},
onPayPlannedPayment = {
viewModel.onEvent(EditTransactionEvent.OnPayPlannedPayment)
viewModel.onEvent(EditTransactionViewEvent.OnPayPlannedPayment)
},
onSave = {
view.hideKeyboard()
viewModel.onEvent(EditTransactionEvent.Save(it))
viewModel.onEvent(EditTransactionViewEvent.Save(it))
},
onSetHasChanges = {
viewModel.onEvent(EditTransactionEvent.SetHasChanges(it))
viewModel.onEvent(EditTransactionViewEvent.SetHasChanges(it))
},
onDelete = {
viewModel.onEvent(EditTransactionEvent.Delete)
viewModel.onEvent(EditTransactionViewEvent.Delete)
},
onDuplicate = {
viewModel.onEvent(EditTransactionEvent.Duplicate)
viewModel.onEvent(EditTransactionViewEvent.Duplicate)
},
onCreateAccount = {
viewModel.onEvent(EditTransactionEvent.CreateAccount(it))
viewModel.onEvent(EditTransactionViewEvent.CreateAccount(it))
},
onExchangeRateChange = {
viewModel.onEvent(EditTransactionEvent.UpdateExchangeRate(it))
viewModel.onEvent(EditTransactionViewEvent.UpdateExchangeRate(it))
},
onTagOperation = {
viewModel.onEvent(it)
Expand All @@ -198,10 +200,10 @@ private fun BoxWithConstraintsScope.UI(
titleSuggestions: ImmutableSet<String>,
description: String?,
category: Category?,
dateTime: LocalDateTime?,
dateTime: Instant?,
account: Account?,
toAccount: Account?,
dueDate: LocalDateTime?,
dueDate: Instant?,
amount: Double,

customExchangeRateState: CustomExchangeRateState,
Expand Down Expand Up @@ -229,7 +231,7 @@ private fun BoxWithConstraintsScope.UI(
onDuplicate: () -> Unit,
onCreateAccount: (CreateAccountData) -> Unit,
onExchangeRateChange: (Double?) -> Unit = { },
onTagOperation: (EditTransactionEvent.TagEvent) -> Unit = {},
onTagOperation: (EditTransactionViewEvent.TagEvent) -> Unit = {},
loanData: EditTransactionDisplayLoan = EditTransactionDisplayLoan(),
backgroundProcessing: Boolean = false,
hasChanges: Boolean = false,
Expand Down Expand Up @@ -356,10 +358,13 @@ private fun BoxWithConstraintsScope.UI(

val ivyContext = ivyWalletCtx()

val timeConverter = LocalTimeConverter.current
if (dueDate != null) {
DueDate(dueDate = dueDate) {
ivyContext.datePicker(
initialDate = dueDate.toLocalDate()
initialDate = with(timeConverter) {
dueDate.toLocalDate()
}
) {
onDueDateChange(it.atTime(12, 0))
}
Expand All @@ -379,14 +384,18 @@ private fun BoxWithConstraintsScope.UI(
dueDateTime = dueDate,
onEditDate = {
ivyContext.datePicker(
initialDate = dateTime?.convertUTCtoLocal()?.toLocalDate()
initialDate = with(timeConverter) {
dateTime?.toLocalDate()
}
) { date ->
onSetDate((date))
}
},
onEditTime = {
ivyContext.timePicker(
initialTime = dateTime?.toLocalTime()
initialTime = with(timeConverter) {
dateTime?.toLocalTime()
}
) { time ->
onSetTime(time)
}
Expand Down Expand Up @@ -626,27 +635,27 @@ private fun BoxWithConstraintsScope.UI(
onDismiss = {
tagModelVisible = false
// Reset TagList, avoids showing incorrect tag list when user has searched for a tag
onTagOperation(EditTransactionEvent.TagEvent.OnTagSearch(""))
onTagOperation(EditTransactionViewEvent.TagEvent.OnTagSearch(""))
},
allTagList = tags,
selectedTagList = transactionAssociatedTags,
onTagAdd = {
onTagOperation(EditTransactionEvent.TagEvent.SaveTag(name = it))
onTagOperation(EditTransactionViewEvent.TagEvent.SaveTag(name = it))
},
onTagEdit = { oldTag, newTag ->
onTagOperation(EditTransactionEvent.TagEvent.OnTagEdit(oldTag, newTag))
onTagOperation(EditTransactionViewEvent.TagEvent.OnTagEdit(oldTag, newTag))
},
onTagDelete = {
onTagOperation(EditTransactionEvent.TagEvent.OnTagDelete(it))
onTagOperation(EditTransactionViewEvent.TagEvent.OnTagDelete(it))
},
onTagSelected = {
onTagOperation(EditTransactionEvent.TagEvent.OnTagSelect(it))
onTagOperation(EditTransactionViewEvent.TagEvent.OnTagSelect(it))
},
onTagDeSelected = {
onTagOperation(EditTransactionEvent.TagEvent.OnTagDeSelect(it))
onTagOperation(EditTransactionViewEvent.TagEvent.OnTagDeSelect(it))
},
onTagSearch = {
onTagOperation(EditTransactionEvent.TagEvent.OnTagSearch(it))
onTagOperation(EditTransactionViewEvent.TagEvent.OnTagSearch(it))
}
)
}
Expand All @@ -664,6 +673,7 @@ private fun shouldFocusAmount(amount: Double) = amount == 0.0

/** For Preview purpose **/
private val testDateTime = LocalDateTime.of(2023, 4, 27, 0, 35)
.toInstant(ZoneOffset.UTC)

@ExperimentalFoundationApi
@Preview
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,68 @@
package com.ivy.transaction

import androidx.compose.runtime.Immutable
import com.ivy.base.model.TransactionType
import com.ivy.data.model.Category
import com.ivy.data.model.Tag
import com.ivy.data.model.TagId
import com.ivy.legacy.data.EditTransactionDisplayLoan
import com.ivy.legacy.datamodel.Account
import com.ivy.wallet.domain.data.CustomExchangeRateState
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

sealed interface EditTransactionEvent {
data class OnAmountChanged(val newAmount: Double) : EditTransactionEvent
data class OnTitleChanged(val newTitle: String?) : EditTransactionEvent
data class OnDescriptionChanged(val newDescription: String?) : EditTransactionEvent
data class OnCategoryChanged(val newCategory: Category?) : EditTransactionEvent
data class OnAccountChanged(val newAccount: Account) : EditTransactionEvent
data class OnToAccountChanged(val newAccount: Account) : EditTransactionEvent
data class OnDueDateChanged(val newDueDate: LocalDateTime?) : EditTransactionEvent
data class OnSetDateTime(val newDateTime: LocalDateTime) : EditTransactionEvent
data class OnSetDate(val newDate: LocalDate) : EditTransactionEvent
data class OnSetTime(val newTime: LocalTime) : EditTransactionEvent
data class OnSetTransactionType(val newTransactionType: TransactionType) : EditTransactionEvent
data object OnPayPlannedPayment : EditTransactionEvent
data object Delete : EditTransactionEvent
data object Duplicate : EditTransactionEvent
data class CreateCategory(val data: CreateCategoryData) : EditTransactionEvent
data class EditCategory(val updatedCategory: Category) : EditTransactionEvent
data class CreateAccount(val data: CreateAccountData) : EditTransactionEvent
data class Save(val closeScreen: Boolean) : EditTransactionEvent
data class SetHasChanges(val hasChangesValue: Boolean) : EditTransactionEvent
data class UpdateExchangeRate(val exRate: Double?) : EditTransactionEvent
@Immutable
data class EditTransactionViewState(
val transactionType: TransactionType,
val initialTitle: String?,
val titleSuggestions: ImmutableSet<String>,
val currency: String,
val description: String?,
val dateTime: Instant?,
val dueDate: Instant?,
val accounts: ImmutableList<Account>,
val categories: ImmutableList<Category>,
val account: Account?,
val toAccount: Account?,
val category: Category?,
val amount: Double,
val hasChanges: Boolean,
val displayLoanHelper: EditTransactionDisplayLoan,
val backgroundProcessingStarted: Boolean,
val customExchangeRateState: CustomExchangeRateState,
val tags: ImmutableList<Tag>,
val transactionAssociatedTags: ImmutableList<TagId>
)

sealed interface TagEvent : EditTransactionEvent {
sealed interface EditTransactionViewEvent {
data class OnAmountChanged(val newAmount: Double) : EditTransactionViewEvent
data class OnTitleChanged(val newTitle: String?) : EditTransactionViewEvent
data class OnDescriptionChanged(val newDescription: String?) : EditTransactionViewEvent
data class OnCategoryChanged(val newCategory: Category?) : EditTransactionViewEvent
data class OnAccountChanged(val newAccount: Account) : EditTransactionViewEvent
data class OnToAccountChanged(val newAccount: Account) : EditTransactionViewEvent
data class OnDueDateChanged(val newDueDate: LocalDateTime?) : EditTransactionViewEvent
data class OnSetDateTime(val newDateTime: LocalDateTime) : EditTransactionViewEvent
data class OnSetDate(val newDate: LocalDate) : EditTransactionViewEvent
data class OnSetTime(val newTime: LocalTime) : EditTransactionViewEvent
data class OnSetTransactionType(val newTransactionType: TransactionType) : EditTransactionViewEvent
data object OnPayPlannedPayment : EditTransactionViewEvent
data object Delete : EditTransactionViewEvent
data object Duplicate : EditTransactionViewEvent
data class CreateCategory(val data: CreateCategoryData) : EditTransactionViewEvent
data class EditCategory(val updatedCategory: Category) : EditTransactionViewEvent
data class CreateAccount(val data: CreateAccountData) : EditTransactionViewEvent
data class Save(val closeScreen: Boolean) : EditTransactionViewEvent
data class SetHasChanges(val hasChangesValue: Boolean) : EditTransactionViewEvent
data class UpdateExchangeRate(val exRate: Double?) : EditTransactionViewEvent

sealed interface TagEvent : EditTransactionViewEvent {
data class SaveTag(val name: String) : TagEvent
data class OnTagSelect(val selectedTag: Tag) : TagEvent
data class OnTagDeSelect(val selectedTag: Tag) : TagEvent
Expand Down
Loading

0 comments on commit f461c4f

Please sign in to comment.