Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas
Last active September 17, 2021 04:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save PatilShreyas/9934edf1cd121b478869b0030777bec4 to your computer and use it in GitHub Desktop.
@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