From 773b514e4259b8f8692fe7600bc15c1ec24605dd Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sat, 22 Aug 2020 19:12:26 -0700 Subject: [PATCH] Only show relevant expense/income categories for transaction editing/creation --- .../ui/transactions/TransactionFormActivity.kt | 11 ++++++++--- .../ui/transactions/TransactionFormViewModel.kt | 13 +++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormActivity.kt b/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormActivity.kt index ffb37d7..9368488 100644 --- a/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormActivity.kt +++ b/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormActivity.kt @@ -55,15 +55,20 @@ class TransactionFormActivity : AppCompatActivity(), CoroutineScope { android.R.layout.simple_list_item_1, accounts ) - + container_edit_transaction_type.setOnCheckedChangeListener { _, _ -> + this@TransactionFormActivity.launch { + val budget = budgetSpinner.selectedItem as Budget + setCategories(viewModel.getCategories(budget.id!!, edit_transaction_type_expense.isChecked)) + } + } budgetSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { this@TransactionFormActivity.launch { - val account = budgetSpinner.selectedItem as Budget - setCategories(viewModel.getCategories(account.id!!)) + val budget = budgetSpinner.selectedItem as Budget + setCategories(viewModel.getCategories(budget.id!!, edit_transaction_type_expense.isChecked)) } } } diff --git a/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormViewModel.kt b/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormViewModel.kt index e413278..c039c9d 100644 --- a/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormViewModel.kt +++ b/android/src/main/java/com/wbrawner/budget/ui/transactions/TransactionFormViewModel.kt @@ -8,11 +8,16 @@ import com.wbrawner.budget.common.transaction.TransactionRepository import javax.inject.Inject class TransactionFormViewModel : ViewModel() { - @Inject lateinit var budgetRepository: BudgetRepository - @Inject lateinit var categoryRepository: CategoryRepository - @Inject lateinit var transactionRepository: TransactionRepository + @Inject + lateinit var budgetRepository: BudgetRepository + @Inject + lateinit var categoryRepository: CategoryRepository + @Inject + lateinit var transactionRepository: TransactionRepository - suspend fun getCategories(budgetId: Long) = categoryRepository.findAll(arrayOf(budgetId)) + suspend fun getCategories(budgetId: Long, expense: Boolean) = categoryRepository.findAll(arrayOf(budgetId)).filter { + it.expense == expense + } suspend fun getTransaction(id: Long) = transactionRepository.findById(id)