fix(#21): move DB I/O to Dispatchers.IO in AddEdit screen models #22
+10
-2
@@ -8,12 +8,14 @@ import dev.achmad.ledgerr.domain.category.model.Category
|
|||||||
import dev.achmad.ledgerr.domain.expense.interactor.GetExpenses
|
import dev.achmad.ledgerr.domain.expense.interactor.GetExpenses
|
||||||
import dev.achmad.ledgerr.domain.expense.interactor.UpsertExpense
|
import dev.achmad.ledgerr.domain.expense.interactor.UpsertExpense
|
||||||
import dev.achmad.ledgerr.domain.expense.model.Expense
|
import dev.achmad.ledgerr.domain.expense.model.Expense
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
|
|
||||||
class AddEditExpenseScreenModel(
|
class AddEditExpenseScreenModel(
|
||||||
@@ -52,7 +54,11 @@ class AddEditExpenseScreenModel(
|
|||||||
init {
|
init {
|
||||||
if (isEditMode) {
|
if (isEditMode) {
|
||||||
screenModelScope.launch {
|
screenModelScope.launch {
|
||||||
val existing = expenseId?.let { getExpenses.awaitOne(it) }
|
val existing = expenseId?.let {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
getExpenses.awaitOne(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
_amount.value = "%.2f".format(existing.expense.amount)
|
_amount.value = "%.2f".format(existing.expense.amount)
|
||||||
_categoryId.value = existing.expense.categoryId
|
_categoryId.value = existing.expense.categoryId
|
||||||
@@ -97,7 +103,9 @@ class AddEditExpenseScreenModel(
|
|||||||
recurringExpenseId = null,
|
recurringExpenseId = null,
|
||||||
)
|
)
|
||||||
screenModelScope.launch {
|
screenModelScope.launch {
|
||||||
upsertExpense.await(expense)
|
withContext(Dispatchers.IO) {
|
||||||
|
upsertExpense.await(expense)
|
||||||
|
}
|
||||||
onSuccess()
|
onSuccess()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-2
@@ -9,12 +9,14 @@ import dev.achmad.ledgerr.domain.recurring.interactor.GetRecurringExpenses
|
|||||||
import dev.achmad.ledgerr.domain.recurring.interactor.UpsertRecurringExpense
|
import dev.achmad.ledgerr.domain.recurring.interactor.UpsertRecurringExpense
|
||||||
import dev.achmad.ledgerr.domain.recurring.model.RecurringExpense
|
import dev.achmad.ledgerr.domain.recurring.model.RecurringExpense
|
||||||
import dev.achmad.ledgerr.domain.recurring.model.RecurringInterval
|
import dev.achmad.ledgerr.domain.recurring.model.RecurringInterval
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
|
|
||||||
class AddEditRecurringScreenModel(
|
class AddEditRecurringScreenModel(
|
||||||
@@ -61,7 +63,11 @@ class AddEditRecurringScreenModel(
|
|||||||
init {
|
init {
|
||||||
if (isEditMode) {
|
if (isEditMode) {
|
||||||
screenModelScope.launch {
|
screenModelScope.launch {
|
||||||
val existing = recurringId?.let { getRecurringExpenses.awaitOne(it) }
|
val existing = recurringId?.let {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
getRecurringExpenses.awaitOne(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
_amount.value = "%.2f".format(existing.amount)
|
_amount.value = "%.2f".format(existing.amount)
|
||||||
_categoryId.value = existing.categoryId
|
_categoryId.value = existing.categoryId
|
||||||
@@ -121,7 +127,9 @@ class AddEditRecurringScreenModel(
|
|||||||
isActive = _isActive.value,
|
isActive = _isActive.value,
|
||||||
)
|
)
|
||||||
screenModelScope.launch {
|
screenModelScope.launch {
|
||||||
upsertRecurringExpense.await(recurring)
|
withContext(Dispatchers.IO) {
|
||||||
|
upsertRecurringExpense.await(recurring)
|
||||||
|
}
|
||||||
onSuccess()
|
onSuccess()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user