Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active July 9, 2020 22:11
Show Gist options
  • Save manuelvicnt/9dccbf282d9a4e2d52e988ebfae76356 to your computer and use it in GitHub Desktop.
Save manuelvicnt/9dccbf282d9a4e2d52e988ebfae76356 to your computer and use it in GitHub Desktop.
Adding components to the Hilt hierarchy - 3
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@Singleton
class UserManager @Inject constructor(
// Since UserManager will be in charge of managing the UserComponent's
// lifecycle, it needs to know how to create instances of it. We use the
// provider (i.e. factory) Dagger generates for us to create instances of UserComponent.
private val userComponentProvider: Provider<UserComponent.Builder>
) {
/**
* UserComponent is specific to a logged in user. Holds an instance of
* UserComponent. This determines if the user is logged in or not, when the
* user logs in, a new Component will be created.
* When the user logs out, this will be null.
*/
var userComponent: UserComponent? = null
private set
...
private fun userLoggedIn(userId: String) {
// When the user logs in, we create a new instance of UserComponent
val user = createUser(userId)
userComponent = userComponentProvider.get().setUser(user).build()
}
private fun logout() {
// When the user logs out, we remove the instance of UserComponent from memory
userComponent = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment