Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas
Last active Sep 17, 2021
Embed
What would you like to do?
@HiltViewModel
class UserViewModel @Inject constructor(
private val sessionManager: SessionManager,
private val userRepository: UserRepository,
@DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher // Injected by Hilt module. Used in testing
) : ViewModel() {
// ...
// OTHER CODE HERE
// ...
/**
* Creates a new user with [name] and [email] and sets active session of that created user.
*/
fun setUserSession(name: String, email: String) {
viewModelScope.launch(defaultDispatcher) {
val user = userRepository.add(name, email)
sessionManager.setUserSession(user)
// Update LiveData/StateFlow/Rx or any other stream
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment