fixup! Fix user loading when returning from background

This commit is contained in:
William Brawner 2020-08-31 19:12:30 -07:00
parent 5b2dd61556
commit bf67966be8
4 changed files with 23 additions and 9 deletions

View file

@ -1,10 +1,8 @@
package com.wbrawner.budget
import android.app.Application
import com.wbrawner.budget.common.user.User
class AllowanceApplication : Application() {
var currentUser: User? = null
lateinit var appComponent: AppComponent
private set

View file

@ -93,7 +93,7 @@ class TransactionFormActivity : AppCompatActivity(), CoroutineScope {
.parse(transactionTime.text.toString()) ?: Date()
TimePickerDialog(
this@TransactionFormActivity,
TimePickerDialog.OnTimeSetListener { _, hourOfDay, minute ->
{ _, hourOfDay, minute ->
val newTime = Date().apply {
hours = hourOfDay
minutes = minute
@ -203,7 +203,7 @@ class TransactionFormActivity : AppCompatActivity(), CoroutineScope {
amount = (BigDecimal(edit_transaction_amount.text.toString()) * 100.toBigDecimal()).toLong(),
expense = edit_transaction_type_expense.isChecked,
categoryId = categoryId,
createdBy = (application as AllowanceApplication).currentUser!!.id!!
createdBy = viewModel.currentUserId!!
))
onNavigateUp()
}

View file

@ -5,16 +5,30 @@ import com.wbrawner.budget.common.budget.BudgetRepository
import com.wbrawner.budget.common.category.CategoryRepository
import com.wbrawner.budget.common.transaction.Transaction
import com.wbrawner.budget.common.transaction.TransactionRepository
import com.wbrawner.budget.common.user.UserRepository
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 userRepository: UserRepository
var currentUserId: Long? = null
private set
init {
userRepository.currentUser.observeForever {
currentUserId = it?.id
}
}
suspend fun getCategories(budgetId: Long, expense: Boolean) = categoryRepository.findAll(arrayOf(budgetId)).filter {
it.expense == expense
}

View file

@ -20,15 +20,17 @@ class TransactionListViewModel : ViewModel(), AsyncViewModel<List<Transaction>>
override val state: MutableLiveData<AsyncState<List<Transaction>>> = MutableLiveData(AsyncState.Loading)
fun getTransactions(
budgetId: Long? = budgetRepository.currentBudget.value?.id,
categoryId: Long? = null,
start: Calendar? = null,
end: Calendar? = null
) {
val budgets = budgetId?.let { listOf(it) }
val categories = categoryId?.let { listOf(it) }
launch {
transactionRepo.findAll(budgets, categories, start, end).toList()
budgetRepository.currentBudget.observeForever { budget ->
val budgets = budget?.id?.let { listOf(it) }
val categories = categoryId?.let { listOf(it) }
launch {
transactionRepo.findAll(budgets, categories, start, end).toList()
}
}
}
}