Added superclass viewmodel
This commit is contained in:
		
							parent
							
								
									e3c54ce116
								
							
						
					
					
						commit
						30cfdd2c96
					
				
					 1 changed files with 23 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -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
 | 
			
		||||
        )
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue