Skip to content

Commit

Permalink
[FEATURE] Fix issue Ivy-Apps#2987: Add User Feedback for Unselected '…
Browse files Browse the repository at this point in the history
…To' Account in Transfers (Ivy-Apps#2988)

* Fix issue Ivy-Apps#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 Ivy-Apps#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 Ivy-Apps#2987: Test push

* Added annotation fix

* Removed SharedModule because @Inject const is sufficient to let Hilt to build Toaster instances
  • Loading branch information
diasmashikov authored Feb 26, 2024
1 parent 65e9f23 commit 0566fdd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -636,7 +638,7 @@ class EditTransactionViewModel @Inject constructor(
}

private fun save(closeScreen: Boolean = true) {
if (!validateTransaction()) {
if (!validTransaction()) {
return
}

Expand All @@ -662,7 +664,7 @@ class EditTransactionViewModel @Inject constructor(
dueDate = dueDate.value,
dateTime = when {
loadedTransaction().dateTime == null &&
dueDate.value == null -> {
dueDate.value == null -> {
timeNowUTC()
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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

Expand Down
14 changes: 14 additions & 0 deletions shared/base/src/main/java/com/ivy/base/Toaster.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
1 change: 1 addition & 0 deletions shared/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,5 @@
<string name="loan_record_type">Record type</string>
<string name="increase_loan">Increase</string>
<string name="decrease_loan">Decrease</string>
<string name="msg_select_account_to_transfer">Please select an account to transfer to</string>
</resources>

0 comments on commit 0566fdd

Please sign in to comment.