Search code examples
androidkotlindagger-2dagger-hilt

Hilt: Get Fragment LifecycleOwner and context automagically


I would like to automagically get the fragment context and lifecycleOwner (e.g here viewLifecycleOwner), when I inject my dependencies inside a fragment. My question is, how to do so, as there is no corresponding @FragmentContext like there is a @ActivityContext. Furthermore, I would like to dynamically assign a variable to this dependency.

My Dependency

class LoginDialog(
    context: Context, // get this from fragment
    private val mLifecycleOwner: LifecycleOwner, // get this from fragment
    private val loginTitle: LoginTitle, // define this when injecting
) : AlertDialog(context, R.style.LoginDialogTheme) {
    private var _binding: LoginLoadingScreenBinding? = null
    private val binding: LoginLoadingScreenBinding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        initDialog()
        super.onCreate(savedInstanceState)
    }

    override fun show() {
        if (isShowing) return
        _binding = LoginLoadingScreenBinding.inflate(layoutInflater).apply { lifecycleOwner = mLifecycleOwner }
        super.show()
    }

    override fun dismiss() {
        _binding = null
        super.dismiss()
    }

    /**
     * Calls show internally
     */
    fun onChangeIndicator(mText: String) {
        show()
        binding.indicator = mText
    }

    private fun initDialog() {
        setTitle(loginTitle.title)
        setCancelable(false)
        setView(binding.root)
    }

    sealed class LoginTitle(@StringRes val title: Int) {
        object Login : LoginTitle(R.string.loading_login_title)
        object Registration : LoginTitle(R.string.loading_registration_title)
        object ChangingEmail : LoginTitle(R.string.user_data_changing_email)
    }

}

Injection

@AndroidEntryPoint
class FragmentA : Fragment() {

    // here, context is requireContext(), 
    // lifecycleOwner is viewLifecylceOwner
    // and loginTitle is LoginDialog.LoginTitle.Login
    @Inject 
    @LoginTitleLogin
    lateinit var loginDialog: LoginDialog
}

Solution

  • Okay, I've managed to solve this problem by creating a hilt module and changing the constructor a bit, here is the solution:

    Dependency

    class LoginDialog(
        private val fragment: Fragment,
        private val loginTitle: LoginTitle,
    ) : AlertDialog(fragment.requireContext(), R.style.LoginDialogTheme) {
        private var _binding: LoginLoadingScreenBinding? = null
        private val binding: LoginLoadingScreenBinding get() = _binding!!
    
        override fun onCreate(savedInstanceState: Bundle?) {
            initDialog()
            super.onCreate(savedInstanceState)
        }
    
        override fun show() {
            if (isShowing) return
            _binding = LoginLoadingScreenBinding.inflate(layoutInflater).apply { lifecycleOwner = fragment.viewLifecycleOwner }
            super.show()
        }
    
        override fun dismiss() {
            _binding = null
            super.dismiss()
        }
    
        /**
         * Calls show internally
         */
        fun onChangeIndicator(mText: String) {
            show()
            binding.indicator = mText
        }
    
        private fun initDialog() {
            setTitle(loginTitle.title)
            setCancelable(false)
            setView(binding.root)
        }
    
        sealed class LoginTitle(@StringRes val title: Int) {
            object Login : LoginTitle(R.string.loading_login_title)
            object Registration : LoginTitle(R.string.loading_registration_title)
            object ChangingEmail : LoginTitle(R.string.user_data_changing_email)
        }
    
    }
    

    Module

    @Module
    @InstallIn(FragmentComponent::class)
    object FragmentModule {
    
        @LoginTitle
        @Provides
        fun provideLoginDialogWithLoginTitle(fragment: Fragment) = LoginDialog(
            fragment,
            LoginDialog.LoginTitle.Login
        )
    
        @RegistrationTitle
        @Provides
        fun provideLoginDialogWithRegistrationTitle(fragment: Fragment) = LoginDialog(
            fragment,
            LoginDialog.LoginTitle.Registration
        )
    
        @ChangingEmailTitle
        @Provides
        fun provideLoginDialogWithChangingEmailTitle(fragment: Fragment) = LoginDialog(
            fragment,
            LoginDialog.LoginTitle.ChangingEmail
        )
    
    }
    
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class LoginTitle
    
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class RegistrationTitle
    
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class ChangingEmailTitle
    

    Usage

    @AndroidEntryPoint
    class MyFragment : Fragment() {
    
    
        @Inject
        @LoginTitle
        lateinit var loginDialog: LoginDialog
    }