Added superclass viewmodel

This commit is contained in:
lbarraga 2023-04-08 17:45:46 +02:00
parent e3c54ce116
commit 30cfdd2c96

View file

@ -0,0 +1,23 @@
package be.ugent.sel.studeez.screens
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
import be.ugent.sel.studeez.common.snackbar.SnackbarMessage.Companion.toSnackbarMessage
import be.ugent.sel.studeez.domain.LogService
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
open class StudeezViewModel(private val logService: LogService) : ViewModel() {
fun launchCatching(snackbar: Boolean = true, block: suspend CoroutineScope.() -> Unit) =
viewModelScope.launch(
CoroutineExceptionHandler { _, throwable ->
if (snackbar) {
SnackbarManager.showMessage(throwable.toSnackbarMessage())
}
logService.logNonFatalCrash(throwable)
},
block = block
)
}