Last active
May 19, 2023 12:18
-
-
Save Matikano/2eeea28f76a823aaf6893e7505c7a69d to your computer and use it in GitHub Desktop.
Android Studio usefull MVVM LiveTemplates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. HiltViewModel (hiltvm) - it assumes you are using State and Event classes for ViewModel, as well as UiEvent class for handling one time UI events: | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.viewModelScope | |
import com.mssmk.cleanarchitecturetemplate.ui.utils.UiEvent | |
import kotlinx.coroutines.channels.Channel | |
import dagger.hilt.android.lifecycle.HiltViewModel | |
import kotlinx.coroutines.flow.MutableStateFlow | |
import kotlinx.coroutines.flow.receiveAsFlow | |
import kotlinx.coroutines.launch | |
import javax.inject.Inject | |
@HiltViewModel | |
class $SCREEN_NAME$ViewModel @Inject constructor( | |
private val useCases: $SCREEN_NAME$UseCases | |
): ViewModel() { | |
private val _stateFlow = MutableStateFlow($SCREEN_NAME$State()) | |
val stateFlow = _stateFLow.asStateFlow() | |
private var _uiEventChannel = Channel<UiEvent>() | |
val uiEvents = _uiEventChannel.receiveAsFlow() | |
fun onEvent(event: $SCREEN_NAME$Event) { | |
when(event) { | |
} | |
} | |
fun sendUiEvent(event: UiEvent) = viewModelScope.launch { | |
_uiEventChannel.send(event) | |
} | |
suspend fun collectUiEvents( | |
onUiEvent: (UiEvent) -> Unit | |
) = uiEvents.collect { onUiEvent(it) } | |
} | |
2. UseCase (usecase): | |
import javax.inject.Inject | |
class $NAME$UseCase @Inject constructor( | |
private val $DEPENDENCY$: $DEPENDENCY_TYPE$ | |
) { | |
suspend operator fun invoke($ARG$: $ARG_TYPE$): $RETURN_TYPE$ { | |
$FUNCTION_BODY$ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment