/UserViewModel.kt Secret
Last active
September 17, 2021 04:44
Star
You must be signed in to star a gist
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
@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