79802224

Date: 2025-10-27 20:31:10
Score: 0.5
Natty:
Report link

I would recommend move this code:

var newEntryUuid = Uuid.random()
val newEntryUuidClone = newEntryUuid
coroutineScope.launch(Dispatchers.IO) {
    if (newEntryViewModel.selectedEntryType == EntryTypes.Card)
       newEntryUuid = newEntryViewModel.pushNewEntry(card = newEntryViewModel.createCard(), context =  localctx)
    if (newEntryViewModel.selectedEntryType == EntryTypes.Account)
        newEntryUuid = newEntryViewModel.pushNewEntry(account = newEntryViewModel.createAccount(), context = localctx)
    newEntryViewModel.entryCreated.value = newEntryUuid != newEntryUuidClone
}

to a new method at your viewmodel do to you already have one.
And because you're already updating this value:

newEntryViewModel.entryCreated.value

doing it at your VM will be easier, consistent and testeable, because your logic will be separated from your view.

then on your button now you'll only need to pass the method as parameter:

Button(
    onClick = newEntryViewModel::yourMethodToPushEntry
)

therefore your composable doesn't need to worry about manage coroutines.

you can launch it at your viewmodel using viewmodelScope.launch {} yes without the dispatcher because your method:

suspend fun pushNewEntry(

is already a suspend fun and its handling the need of move the context to IO Dispatchers.

Cheers!

Reasons:
  • Blacklisted phrase (1): Cheers
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Gapps