简体   繁体   English

如何使用 Dagger2 提供 GoogleSignInOptions?

[英]How to provide GoogleSignInOptions using Dagger2?

I am trying to make small Firebase login app using Dagger2, but I have a problem with GoogleSignInOptions.我正在尝试使用 Dagger2 制作小型 Firebase 登录应用程序,但 GoogleSignInOptions 有问题。 I want to set it in the AppModule, but.requestIdToken have an error with: (getString(R.string.default_web_client_id)) Is my approach with GoogleSignInOptions appropriate?我想在 AppModule 中设置它,但是.requestIdToken 有一个错误: (getString(R.string.default_web_client_id)) 我使用 GoogleSignInOptions 的方法是否合适? Maybe it's the wrong way?也许这是错误的方式? Sorry for that, but I am little confused about injection of the Firebase stuff.很抱歉,但我对注入 Firebase 的东西有点困惑。

@Module
class AppModule {

@Provides
fun getApp(application: Application?): Boolean {
    return application == null
}

@Singleton
@Provides
fun provideFirebaseAuthInstance(): FirebaseAuth {
    return FirebaseAuth.getInstance()
}

@Singleton
@Provides
fun provideGoogleSignInClient() : GoogleSignInOptions {

   return GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id)) //here is a problem
        .requestEmail()
        .build()
}

}

Pass application object as parameter and use it to get required string传递应用程序 object 作为参数并使用它来获取所需的字符串

@Singleton
@Provides
fun provideGoogleSignInClient(application: Application) : GoogleSignInOptions {

   return GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(application.applicationContext.getString(R.string.default_web_client_id))
        .requestEmail()
        .build()
    }

You need to get a Context in order to use the getString() method.您需要获取 Context 才能使用getString()方法。

One way to fix it would be to make the application context available there.解决它的一种方法是使应用程序上下文可用。

You would modify your Application subclass to add a application context reference:您将修改您的 Application 子类以添加应用程序上下文引用:

class App : Application(),
        HasSupportFragmentInjector,
        HasActivityInjector {

    @Inject
    lateinit var activityInjector: DispatchingAndroidInjector<Activity>

    @Inject
    lateinit var supportfragmentInjector: DispatchingAndroidInjector<Fragment>

    override fun onCreate() {
        super.onCreate()
        appContext = this
        DaggerApplicationComponent
                .create()
                .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity> = activityInjector

    override fun supportFragmentInjector(): AndroidInjector<Fragment> = supportfragmentInjector
}

lateinit var appContext: Context
    internal set

Then you can use that appContext reference in your dependency injection module:然后,您可以在依赖注入模块中使用该appContext引用:

@Singleton
@Provides
fun provideGoogleSignInClient() : GoogleSignInOptions {

    return GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(appContext.getString(R.string.default_web_client_id))
            .requestEmail()
            .build()
}

Another option would be to use dependency injection to provide the application context, see here for more info: https://github.com/google/dagger/issues/832另一种选择是使用依赖注入来提供应用程序上下文,请参阅此处了解更多信息: https://github.com/google/dagger/issues/832

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM