Hilt particular feature dependency

215 views Asked by At
@Module(includes = [AuthModule.ViewModel::class, AuthModule.UseCase::class, AuthModule.Api::class, AuthModule.Repository::class, AuthModule.Interactor::class])
@InstallIn(ActivityComponent::class)
class AuthModule {
    @Module
    @InstallIn(ActivityComponent::class)
    class ViewModel() {
        @Provides
        fun providesRegisterViewModel(activity:FragmentActivity): RegisterViewModel {
            val registerViewModel = ViewModelProvider(activity).get(RegisterViewModel::class.java)
            return registerViewModel
        }
    }

    @Module
    @InstallIn(ActivityComponent::class)
    class UseCase {
        @Provides
        fun providesRegisterationUseCase(registrationApi: RegistrationApi, userRepository: UserRepository): RegistrationUsecase {
            val registrationUsecase = RegistrationUsecase(registrationApi, userRepository)
            return registrationUsecase
        }
    }


    @Module
    @InstallIn(ActivityComponent::class)
    class Api {
        @Provides
        fun provideRegistrationApi(retrofit: Retrofit): RegistrationApi {
            val registrationService = retrofit.create(RegistrationService::class.java)
            return RegistrationApi(registrationService)
        }
    }


    @Module
    @InstallIn(ActivityComponent::class)
    class Repository {
        @Provides
        fun provideUserRepository(realm: Realm): UserRepository = UserRepository(realm)
    }

    @Module
    @InstallIn(ActivityComponent::class)
    class Interactor {
        @Provides
        fun provideSignInInteractor(registerViewModel: RegisterViewModel): SignInInteractor = SignInInteractor(registerViewModel)
    }
}

@Component(modules = [AuthModule::class])
interface AuthComponent {

    fun inject(signInFragment: SignInFragment)

    @Component.Builder
    interface Builder {
        fun activity(@BindsInstance fragmentActivity: FragmentActivity): Builder
        fun build(): AuthComponent
    }
}

class SignInFragment : Fragment() {
    @Inject lateinit var signInInteractor:SignInInteractor
    private lateinit var fragmentSignInBinding: FragmentSignInBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        DaggerAuthModuleComponent.builder()
                .activity(requireActivity()).build().inject(this)
        super.onCreate(savedInstanceState)
    }

i was trying to inject it in other fragments but it does not work. my aim is to make feature wise component so that only these fragment related to particular feature gets the dependency. Hilt requires every module to use install annotation if i want hilt to install to my custom component is it possible.

0

There are 0 answers