diff --git a/app/src/main/java/com/example/bondoman/data/viewmodels/transaction/TransactionViewModel.kt b/app/src/main/java/com/example/bondoman/data/viewmodels/transaction/TransactionViewModel.kt index 2f61bb3a0f89aea1e4afebf6f6fea085cb3fc91e..2341bf8e541e46ad42acef391a083c40f5bc15d4 100644 --- a/app/src/main/java/com/example/bondoman/data/viewmodels/transaction/TransactionViewModel.kt +++ b/app/src/main/java/com/example/bondoman/data/viewmodels/transaction/TransactionViewModel.kt @@ -56,7 +56,7 @@ class TransactionViewModel( Transaction( title = title, owner = currentUserNim, - amount = Long.MAX_VALUE, + amount = amount, category = category, location = location, ) diff --git a/app/src/main/java/com/example/bondoman/views/fragments/ScanReceiptResultFragment.kt b/app/src/main/java/com/example/bondoman/views/fragments/ScanReceiptResultFragment.kt index f62f7be1d7a5133a36edeea0752c933dbc37741a..ba7271757c353295495ca1a005416e0d14ad6813 100644 --- a/app/src/main/java/com/example/bondoman/views/fragments/ScanReceiptResultFragment.kt +++ b/app/src/main/java/com/example/bondoman/views/fragments/ScanReceiptResultFragment.kt @@ -145,7 +145,8 @@ class ScanReceiptResultFragment : Fragment() { val observer = Observer<ScanReceiptResponseItemsWrapper> { response -> if (response.error != null) { - Toast.makeText(this.requireContext(), response.error, Toast.LENGTH_SHORT).show() + Toast.makeText(this.requireContext(), response.error, Toast.LENGTH_SHORT) + .show() Navigation.findNavController(view) .navigate(R.id.action_scanReceiptResultFragment_to_scanReceiptFragment) } else { @@ -211,24 +212,32 @@ class ScanReceiptResultFragment : Fragment() { // Save button listener viewBinding.scanReceiptFormButtonSave.setOnClickListener { - val title = viewBinding.scanReceiptFormTitle.getText().trim() - val amount = viewBinding.scanReceiptFormAmount.getText().trim().toLong() - val location = viewBinding.scanReceiptFormLocationField.text.toString().trim() - val isDataValid = validateInput(title, amount, location) - - if (isDataValid == null) { - // Create new transaction - transactionViewModel.insertTransaction( - title, - TransactionCategory.EXPENSE, - amount, - location, - ) { _, message -> - Toast.makeText(this.requireContext(), message, Toast.LENGTH_SHORT).show() - findNavController().popBackStack(R.id.scanReceiptFragment, false) + try { + val title = viewBinding.scanReceiptFormTitle.getText().trim() + val amount = viewBinding.scanReceiptFormAmount.getText().trim().toLong() + val location = viewBinding.scanReceiptFormLocationField.text.toString().trim() + val isDataValid = validateInput(title, amount, location) + + if (isDataValid == null) { + // Create new transaction + transactionViewModel.insertTransaction( + title, + TransactionCategory.EXPENSE, + amount, + location + ) { _, message -> + Toast.makeText(this.requireContext(), message, Toast.LENGTH_SHORT).show() + findNavController().popBackStack(R.id.scanReceiptFragment, false) + } + } else { + Toast.makeText(this.requireContext(), isDataValid, Toast.LENGTH_SHORT).show() } - } else { - Toast.makeText(this.requireContext(), isDataValid, Toast.LENGTH_SHORT).show() + } catch (e: NumberFormatException) { + Toast.makeText( + this.requireContext(), + "Amount must be a positive number", + Toast.LENGTH_SHORT + ).show() } } } @@ -243,7 +252,7 @@ class ScanReceiptResultFragment : Fragment() { } if (amount < 0) { - return "Amount must be greater than zero" + return "Amount must be a positive number" } if (location == "") {