Skip to content

Commit

Permalink
Fix issue 3342 (Ivy-Apps#3346)
Browse files Browse the repository at this point in the history
* add duplicate button to the edit transaction screen

* duplicate transaction on click the duplicate button and close the transaction screen

---------

Co-authored-by: lexleontiev <[email protected]>
  • Loading branch information
LexLeontiev and lexleontiev authored Jul 17, 2024
1 parent a1a5828 commit 1a46521
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ compose-animation = { module = "androidx.compose.animation:animation", version.r
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }
compose-material3-windowsize = { module = "androidx.compose.material3:material3-window-size-class", version.ref = "compose-material3" }
compose-material-icons-extended = { module = "androidx.compose.material:material-icons-extended", version.ref = "compose" }
compose-runtime = { module = "androidx.compose.runtime:runtime", version.ref = "compose" }
compose-runtime-livedate-temp = { module = "androidx.compose.runtime:runtime-livedata", version.ref = "compose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
Expand Down Expand Up @@ -190,6 +191,7 @@ compose = [
"compose-foundation",
"compose-material3",
"compose-material3-windowsize",
"compose-material-icons-extended",
"compose-runtime",
"compose-ui",
"compose-activity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sealed interface 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ fun BoxWithConstraintsScope.EditTransactionScreen(screen: EditTransactionScreen)
onDelete = {
viewModel.onEvent(EditTransactionEvent.Delete)
},
onDuplicate = {
viewModel.onEvent(EditTransactionEvent.Duplicate)
},
onCreateAccount = {
viewModel.onEvent(EditTransactionEvent.CreateAccount(it))
},
Expand Down Expand Up @@ -223,6 +226,7 @@ private fun BoxWithConstraintsScope.UI(
onSave: (closeScreen: Boolean) -> Unit,
onSetHasChanges: (hasChanges: Boolean) -> Unit,
onDelete: () -> Unit,
onDuplicate: () -> Unit,
onCreateAccount: (CreateAccountData) -> Unit,
onExchangeRateChange: (Double?) -> Unit = { },
onTagOperation: (EditTransactionEvent.TagEvent) -> Unit = {},
Expand Down Expand Up @@ -290,7 +294,9 @@ private fun BoxWithConstraintsScope.UI(
},
onChangeTransactionTypeModal = {
changeTransactionTypeModalVisible = true
}
},
showDuplicateButton = true,
onDuplicate = onDuplicate
)

Spacer(Modifier.height(32.dp))
Expand Down Expand Up @@ -696,6 +702,7 @@ private fun BoxWithConstraintsScope.Preview(isDark: Boolean = false) {
onSave = {},
onSetHasChanges = {},
onDelete = {},
onDuplicate = {},
onCreateAccount = { },
onSetDate = {},
onSetTime = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class EditTransactionViewModel @Inject constructor(
is EditTransactionEvent.CreateAccount -> createAccount(event.data)
is EditTransactionEvent.CreateCategory -> createCategory(event.data)
EditTransactionEvent.Delete -> delete()
EditTransactionEvent.Duplicate -> duplicate()
is EditTransactionEvent.EditCategory -> editCategory(event.updatedCategory)
is EditTransactionEvent.OnAccountChanged -> onAccountChanged(event.newAccount)
is EditTransactionEvent.OnAmountChanged -> onAmountChanged(event.newAmount)
Expand Down Expand Up @@ -605,6 +606,24 @@ class EditTransactionViewModel @Inject constructor(
}
}

private fun duplicate() {
viewModelScope.launch {
ioThread {
loadedTransaction()
.copy(
id = UUID.randomUUID(),
dateTime = timeNowLocal()
)
.toDomain(transactionMapper)
?.let {
transactionRepo.save(it)
}
refreshWidget(WalletBalanceWidgetReceiver::class.java)
}
closeScreen()
}
}

private fun createCategory(data: CreateCategoryData) {
viewModelScope.launch {
categoryCreator.createCategory(data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ private fun BoxWithConstraintsScope.UI(
},
onChangeTransactionTypeModal = {
onEvent(EditPlannedScreenEvent.OnTransactionTypeModalVisible(true))
}
},
showDuplicateButton = false,
onDuplicate = {}
)

Spacer(Modifier.height(32.dp))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.ivy.wallet.ui.edit.core

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.sharp.CopyAll
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.OutlinedIconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.ivy.base.model.TransactionType
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.asBrush
import com.ivy.navigation.navigation
import com.ivy.ui.R
import com.ivy.wallet.ui.theme.components.CloseButton
Expand All @@ -24,6 +37,9 @@ fun Toolbar(

onDeleteTrnModal: () -> Unit,
onChangeTransactionTypeModal: () -> Unit,

showDuplicateButton: Boolean,
onDuplicate: () -> Unit,
) {
Row(
verticalAlignment = Alignment.CenterVertically
Expand Down Expand Up @@ -66,6 +82,30 @@ fun Toolbar(
}

if (initialTransactionId != null) {
if (showDuplicateButton) {
OutlinedIconButton(
modifier = Modifier
.size(48.dp)
.background(Color.Transparent, CircleShape)
.testTag("duplicate_button"),
shape = CircleShape,
colors = IconButtonDefaults.outlinedIconButtonColors()
.copy(contentColor = UI.colors.medium),
border = IconButtonDefaults.outlinedIconButtonBorder(enabled = true)
.copy(width = 2.dp, brush = UI.colors.medium.asBrush()),
onClick = onDuplicate
) {
Icon(
modifier = Modifier.padding(6.dp),
imageVector = Icons.Sharp.CopyAll,
contentDescription = "duplicate_button",
tint = UI.colors.pureInverse
)
}

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

DeleteButton(
hasShadow = false
) {
Expand Down

0 comments on commit 1a46521

Please sign in to comment.