Skip to content

Instantly share code, notes, and snippets.

@edujtm
Created July 16, 2020 00:36
Show Gist options
  • Save edujtm/0b8894b1545d40fb0de2f3c5ee748c97 to your computer and use it in GitHub Desktop.
Save edujtm/0b8894b1545d40fb0de2f3c5ee748c97 to your computer and use it in GitHub Desktop.
Gonna try implementing this later, might have errors
// The primary component has the dependency graph for the authentication classes
// and the login activity
@Component(modules = [
LoginModule::class,
AuthModule::class,
LoggedInComponent.InstallModule::class
])
interface ApplicationComponent {
val userLoggedInComponent: LoggedInComponent.Factory
fun inject(activity: LoginActivity)
@Component.Factory
interface Factory {
fun create(
@BindsInstance appContext: Context
)
}
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UserEmail {}
// The logged in component extends the object graph with the YouTube classes
@Subcomponent(modules = [YouTubeModule::class])
interface LoggedInComponent {
@Subcomponent.Factory
interface Factory {
// Allows me to receive the user email when extending the dependency graph
fun create(
@BindsInstance @UserEmail userEmail: String
)
}
@Module(subcomponents = [LoggedInComponent::class])
interface InstallModule {}
}
class MyApp : Application() {
val injector: ApplicationComponent by lazy {
// Will instantiate the root dependency graph lazily
DaggerApplicationComponent
.factory()
.create(appContext = this)
}
}
class LoginActivity : AppCompatActivity() {
@Inject private lateinit var authManager: AuthManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(application as MyApp).injector.inject(this)
login_btn.onClickListener {
val signInIntent = authManager.getSignInIntent()
startActivityForResult(signInIntent, REQUEST_SIGN_IN)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, arguments: Bundle) {
if (requestCode == REQUEST_SIGN_IN) {
val result = authManager.processResultIntent(intent)
when (result) {
is Auth.Success -> {
val intent = Intent(this, YouTubeActivty::class.java).apply {
putExtra(USER_EMAIL, result.account.userEmail)
}
startActivity(intent)
}
is Auth.Error -> {
/* Show snackbar */
}
}
}
}
companion object {
const val REQUEST_SIGN_IN = 1000
}
}
class YouTubeActivity : AppCompatActivity() {
private lateinit var youtubeViewModel: YouTubeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// The Intent extras are delivered in both configuration changes and process death
val email = intent.extras!!.get(USER_EMAIL)
val loggedInInjector = (application as MyApp).injector
.userLoggedInComponent
.factory()
.create(userEmail = email)
// There's probably a better way to do this
youtubeViewModel = ViewModelProvider(this, object: ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T = loggedInInjector.youtubeViewModel as T
}).get(YoutubeViewModel::class.java)
}
// Do stuff with the ViewModel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment