From 0566fddcc2e9dfe32879ae58a587a2035a56ea35 Mon Sep 17 00:00:00 2001 From: Dias Mashikov <50723693+diasmashikov@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:50:42 -0500 Subject: [PATCH] [FEATURE] Fix issue #2987: Add User Feedback for Unselected 'To' Account in Transfers (#2988) * Fix issue #2987: Added Toast to notify a user that he hasn't chosen an account to send his money when doing Account Transfer Transaction * Fix issue #2987: Created Toaster class in shared.base to utilize it in EditTransactionViewModel. Added SharedModule to be able to inject Toaster into the ViewModel. Did all of this to make sure that ViewModel is not reliant on ApplicationContext or any other UI dependencies, but to our shared Toaster which we can utilize further in other sections of the app. * Fix issue #2987: Test push * Added annotation fix * Removed SharedModule because @Inject const is sufficient to let Hilt to build Toaster instances --- .../transaction/EditTransactionViewModel.kt | 31 ++++++++++++------- .../src/main/java/com/ivy/base/Toaster.kt | 14 +++++++++ .../resources/src/main/res/values/strings.xml | 1 + 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 shared/base/src/main/java/com/ivy/base/Toaster.kt 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 4a3e9c13ab..b04ddf294e 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 @@ -5,6 +5,7 @@ import androidx.compose.runtime.Stable import androidx.compose.runtime.mutableDoubleStateOf import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.viewModelScope +import com.ivy.base.Toaster import com.ivy.base.legacy.Transaction import com.ivy.base.legacy.refreshWidget import com.ivy.base.model.TransactionType @@ -75,6 +76,7 @@ import javax.inject.Inject @Stable @HiltViewModel class EditTransactionViewModel @Inject constructor( + private val toaster: Toaster, private val loanDao: LoanDao, private val settingsDao: SettingsDao, private val nav: Navigation, @@ -404,15 +406,15 @@ class EditTransactionViewModel @Inject constructor( val loanWarningDescription = if (isLoanRecord) { "Note: This transaction is associated with a Loan Record of Loan : ${loan.name}\n" + - "You are trying to change the account associated with the loan record to an " + - "account of different currency" + - "\n The Loan Record will be re-calculated based on today's currency exchanges" + - " rates" + "You are trying to change the account associated with the loan record to an " + + "account of different currency" + + "\n The Loan Record will be re-calculated based on today's currency exchanges" + + " rates" } else { "Note: You are trying to change the account associated with the loan: ${loan.name} " + - "with an account of different currency, " + - "\nAll the loan records will be re-calculated based on today's currency " + - "exchanges rates " + "with an account of different currency, " + + "\nAll the loan records will be re-calculated based on today's currency " + + "exchanges rates " } val loanCaption = @@ -636,7 +638,7 @@ class EditTransactionViewModel @Inject constructor( } private fun save(closeScreen: Boolean = true) { - if (!validateTransaction()) { + if (!validTransaction()) { return } @@ -662,7 +664,7 @@ class EditTransactionViewModel @Inject constructor( dueDate = dueDate.value, dateTime = when { loadedTransaction().dateTime == null && - dueDate.value == null -> { + dueDate.value == null -> { timeNowUTC() } @@ -730,8 +732,11 @@ class EditTransactionViewModel @Inject constructor( } } - private fun validateTransaction(): Boolean { - if (transactionType.value == TransactionType.TRANSFER && toAccount.value == null) { + // Comment for push + @Suppress("ReturnCount") + private fun validTransaction(): Boolean { + if (hasNotChosenAccountToTransfer()) { + toaster.show(com.ivy.resources.R.string.msg_select_account_to_transfer) return false } @@ -742,6 +747,10 @@ class EditTransactionViewModel @Inject constructor( return true } + private fun hasNotChosenAccountToTransfer(): Boolean { + return transactionType.value == TransactionType.TRANSFER && toAccount.value == null + } + private fun reset() { loadedTransaction = null diff --git a/shared/base/src/main/java/com/ivy/base/Toaster.kt b/shared/base/src/main/java/com/ivy/base/Toaster.kt new file mode 100644 index 0000000000..056f2f54fc --- /dev/null +++ b/shared/base/src/main/java/com/ivy/base/Toaster.kt @@ -0,0 +1,14 @@ +package com.ivy.base + +import android.content.Context +import android.widget.Toast +import dagger.hilt.android.qualifiers.ApplicationContext +import javax.inject.Inject + +class Toaster @Inject constructor( + @ApplicationContext private val context: Context, +) { + fun show(messageId: Int, duration: Int = Toast.LENGTH_SHORT) { + Toast.makeText(context, context.getString(messageId), duration).show() + } +} \ No newline at end of file diff --git a/shared/resources/src/main/res/values/strings.xml b/shared/resources/src/main/res/values/strings.xml index 1cdd51fb84..b835efcc23 100644 --- a/shared/resources/src/main/res/values/strings.xml +++ b/shared/resources/src/main/res/values/strings.xml @@ -447,4 +447,5 @@ Record type Increase Decrease + Please select an account to transfer to