Merge pull request 'fix(#21): move DB I/O to Dispatchers.IO in AddEdit screen models' (#22) from feat/21-move-db-io-to-dispatchers-io into main

Reviewed-on: #22
This commit was merged in pull request #22.
This commit is contained in:
2026-06-28 13:30:20 +00:00
2 changed files with 20 additions and 4 deletions
@@ -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.UpsertExpense
import dev.achmad.ledgerr.domain.expense.model.Expense
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDate
class AddEditExpenseScreenModel(
@@ -52,7 +54,11 @@ class AddEditExpenseScreenModel(
init {
if (isEditMode) {
screenModelScope.launch {
val existing = expenseId?.let { getExpenses.awaitOne(it) }
val existing = expenseId?.let {
withContext(Dispatchers.IO) {
getExpenses.awaitOne(it)
}
}
if (existing != null) {
_amount.value = "%.2f".format(existing.expense.amount)
_categoryId.value = existing.expense.categoryId
@@ -97,7 +103,9 @@ class AddEditExpenseScreenModel(
recurringExpenseId = null,
)
screenModelScope.launch {
upsertExpense.await(expense)
withContext(Dispatchers.IO) {
upsertExpense.await(expense)
}
onSuccess()
}
}
@@ -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.model.RecurringExpense
import dev.achmad.ledgerr.domain.recurring.model.RecurringInterval
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDate
class AddEditRecurringScreenModel(
@@ -61,7 +63,11 @@ class AddEditRecurringScreenModel(
init {
if (isEditMode) {
screenModelScope.launch {
val existing = recurringId?.let { getRecurringExpenses.awaitOne(it) }
val existing = recurringId?.let {
withContext(Dispatchers.IO) {
getRecurringExpenses.awaitOne(it)
}
}
if (existing != null) {
_amount.value = "%.2f".format(existing.amount)
_categoryId.value = existing.categoryId
@@ -121,7 +127,9 @@ class AddEditRecurringScreenModel(
isActive = _isActive.value,
)
screenModelScope.launch {
upsertRecurringExpense.await(recurring)
withContext(Dispatchers.IO) {
upsertRecurringExpense.await(recurring)
}
onSuccess()
}
}