Skip to content

Instantly share code, notes, and snippets.

@Zhuinden
Last active May 21, 2020 04:02
Show Gist options
  • Save Zhuinden/d76565909387200f68e202084bd8ee20 to your computer and use it in GitHub Desktop.
Save Zhuinden/d76565909387200f68e202084bd8ee20 to your computer and use it in GitHub Desktop.
Using EventEmitter to move Navigation commands into ViewModels
typealias NavigationCommand = (NavController, Context) -> Unit
class RegistrationViewModel @AssistedInject constructor(
private val authenticationManager: AuthenticationManager,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
@AssistedInject.Factory
interface Factory {
fun create(savedStateHandle: SavedStateHandle): RegistrationViewModel
}
private val navigationEmitter: EventEmitter<NavigationCommand> = EventEmitter()
val navigationCommands: EventSource<NavigationCommand> get() = navigationEmitter
fun onEnterProfileNextClicked() {
if (fullName.value!!.isNotBlank() && bio.value!!.isNotBlank()) {
currentState.value = RegistrationState.COLLECT_USER_PASSWORD
navigationEmitter.emit { navController, context ->
navController.navigate(R.id.enter_profile_data_to_create_login_credentials)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment