From 9aafa361f3054515aca3095731a0ebe53768e027 Mon Sep 17 00:00:00 2001 From: Priyatanu Date: Fri, 4 Oct 2024 23:33:52 +0200 Subject: [PATCH] Fix Transactions copy bug (#3581) Co-authored-by: Priyatanu --- .../transaction/EditTransactionViewModel.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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 6574d4d36e..4d2adfa7c3 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 @@ -182,7 +182,6 @@ class EditTransactionViewModel @Inject constructor( transactionAssociatedTags = tagRepository.findByAssociatedId(AssociationId(loadedTransaction().id)).map(Tag::id) .toImmutableList() - display(loadedTransaction!!) } } @@ -622,17 +621,24 @@ class EditTransactionViewModel @Inject constructor( private fun duplicate() { viewModelScope.launch { ioThread { - loadedTransaction() + val id = UUID.randomUUID() + loadedTransaction() .copy( - id = UUID.randomUUID(), + id = id, dateTime = timeProvider.utcNow(), ) .toDomain(transactionMapper) ?.let { transactionRepo.save(it) } + + tagRepository.findByIds(transactionAssociatedTags).forEach { + associateTagToTransaction(it, id) + } + refreshWidget(WalletBalanceWidgetReceiver::class.java) } + closeScreen() } } @@ -930,6 +936,15 @@ class EditTransactionViewModel @Inject constructor( } } + private fun associateTagToTransaction(selectedTag: Tag, id: UUID) { + viewModelScope.launch(Dispatchers.IO) { + val associatedId = AssociationId(id) + tagRepository.associateTagToEntity(associatedId, selectedTag.id) + transactionAssociatedTags = + tagRepository.findByAssociatedId(associatedId).map(Tag::id).toImmutableList() + } + } + private fun removeTagAssociation(selectedTag: Tag) { viewModelScope.launch(Dispatchers.IO) { val associatedId = AssociationId(loadedTransaction().id)