Auth Component subcomponent dependency
This commit is contained in:
parent
9b85bcf562
commit
7f51e68f46
|
|
@ -10,7 +10,7 @@ class App : Application(), SharedComponentProvider {
|
|||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
DI.init(applicationContext)
|
||||
DI.initAppComponent(applicationContext)
|
||||
DI.getComponent().inject(this)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,19 +4,11 @@ import android.content.Context
|
|||
import dagger.BindsInstance
|
||||
import dagger.Component
|
||||
import ru.touchin.template.App
|
||||
import ru.touchin.template.di.auth.AuthComponent
|
||||
import ru.touchin.template.di.modules.AppModule
|
||||
import ru.touchin.template.di.modules.NavigationModule
|
||||
import ru.touchin.template.di.modules.ViewModelModule
|
||||
import ru.touchin.template.feature.SingleActivity
|
||||
import ru.touchin.template.feature.second.di.AuthComponent
|
||||
|
||||
@Component(
|
||||
modules = [
|
||||
AppModule::class,
|
||||
ViewModelModule::class,
|
||||
NavigationModule::class
|
||||
]
|
||||
)
|
||||
@Component(modules = [AppModule::class])
|
||||
@AppScope
|
||||
interface AppComponent : SharedComponent {
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ object DI {
|
|||
|
||||
private lateinit var appComponent: AppComponent
|
||||
|
||||
fun init(context: Context) {
|
||||
fun initAppComponent(context: Context) {
|
||||
appComponent = DaggerAppComponent.builder()
|
||||
.appContext(context)
|
||||
.build()
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
package ru.touchin.template.di.auth
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import ru.touchin.template.feature.second.SecondRepository
|
||||
import ru.touchin.template.feature.second.SecondRepositoryImpl
|
||||
|
||||
@Module
|
||||
class AuthModule {
|
||||
|
||||
@Provides
|
||||
@AuthScope
|
||||
internal fun providesSecondRepository(): SecondRepository {
|
||||
return SecondRepositoryImpl()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
package ru.touchin.template.di.auth
|
||||
|
||||
import javax.inject.Scope
|
||||
|
||||
@Scope
|
||||
annotation class AuthScope
|
||||
|
|
@ -1,12 +1,14 @@
|
|||
package ru.touchin.template.di.modules
|
||||
|
||||
import dagger.Module
|
||||
import ru.touchin.template.di.auth.AuthComponent
|
||||
import ru.touchin.template.feature.second.di.AuthComponent
|
||||
|
||||
@Module(
|
||||
includes = [
|
||||
RepositoryModule::class,
|
||||
NetworkModule::class
|
||||
NetworkModule::class,
|
||||
ViewModelModule::class,
|
||||
NavigationModule::class
|
||||
],
|
||||
subcomponents = [AuthComponent::class]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import com.github.terrakok.cicerone.NavigatorHolder
|
|||
import com.github.terrakok.cicerone.Router
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import javax.inject.Singleton
|
||||
import ru.touchin.template.di.AppScope
|
||||
|
||||
@Module
|
||||
class NavigationModule {
|
||||
|
|
@ -13,13 +13,13 @@ class NavigationModule {
|
|||
private val cicerone: Cicerone<Router> = Cicerone.create()
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@AppScope
|
||||
fun provideRouter(): Router {
|
||||
return cicerone.router
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@AppScope
|
||||
fun provideNavigatorHolder(): NavigatorHolder {
|
||||
return cicerone.getNavigatorHolder()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,5 +3,9 @@ package ru.touchin.template.di.modules
|
|||
import dagger.Module
|
||||
|
||||
@Module
|
||||
class RepositoryModule {
|
||||
interface RepositoryModule {
|
||||
|
||||
// @Binds
|
||||
// @AppScope
|
||||
// fun bindSecondRepository(secondRepositoryImpl: SecondRepositoryImpl): SecondRepository
|
||||
}
|
||||
|
|
@ -1,20 +1,32 @@
|
|||
package ru.touchin.template.feature.first
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.viewModels
|
||||
import javax.inject.Inject
|
||||
import ru.touchin.template.base.fragment.BaseFragment
|
||||
import ru.touchin.template.databinding.FragmentFirstBinding
|
||||
import ru.touchin.template.di.DI
|
||||
import ru.touchin.template.feature.second.SecondRepository
|
||||
|
||||
class FirstFragment : BaseFragment<FirstViewModel>() {
|
||||
|
||||
@Inject
|
||||
lateinit var secondRepository: SecondRepository
|
||||
|
||||
private var _binding: FragmentFirstBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun createViewModelLazy() = viewModels<FirstViewModel> { viewModelFactory }
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
DI.getComponent().authComponent().build().inject(this)
|
||||
super.onAttach(context)
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
_binding = FragmentFirstBinding.inflate(layoutInflater, container, false)
|
||||
return binding.root
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ import javax.inject.Inject
|
|||
import ru.touchin.template.navigation.Screens
|
||||
|
||||
class FirstViewModel @Inject constructor(
|
||||
private val router: Router
|
||||
private val router: Router,
|
||||
// private val secondRepository: SecondRepository
|
||||
) : ViewModel() {
|
||||
|
||||
fun onNextButtonClicked(from: String) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package ru.touchin.template.feature.second
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
|
|
@ -8,9 +9,11 @@ import androidx.core.os.bundleOf
|
|||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.flowWithLifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.touchin.template.base.fragment.BaseFragment
|
||||
import ru.touchin.template.databinding.FragmentSecondBinding
|
||||
import ru.touchin.template.di.DI
|
||||
import ru.touchin.template.di.viewmodel.assistedViewModel
|
||||
|
||||
class SecondFragment : BaseFragment<SecondViewModel>() {
|
||||
|
|
@ -31,6 +34,9 @@ class SecondFragment : BaseFragment<SecondViewModel>() {
|
|||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
lateinit var secondRepository: SecondRepository
|
||||
|
||||
private var _binding: FragmentSecondBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
|
|
@ -38,11 +44,16 @@ class SecondFragment : BaseFragment<SecondViewModel>() {
|
|||
|
||||
override fun createViewModelLazy() = assistedViewModel {
|
||||
secondViewModelFactory.create(
|
||||
arguments?.getString(FROM_KEY) ?: "Unkown Fragment",
|
||||
arguments?.getString(FROM_KEY) ?: "Unknown Fragment",
|
||||
arguments?.getString(SCREEN_NAME_KEY) ?: "Unknown"
|
||||
)
|
||||
}
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
DI.getComponent().authComponent().build().inject(this)
|
||||
super.onAttach(context)
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
_binding = FragmentSecondBinding.inflate(layoutInflater, container, false)
|
||||
return binding.root
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
package ru.touchin.template.feature.second
|
||||
|
||||
import javax.inject.Inject
|
||||
|
||||
interface SecondRepository {
|
||||
|
||||
}
|
||||
|
||||
class SecondRepositoryImpl @Inject constructor() : SecondRepository {
|
||||
|
||||
}
|
||||
|
|
@ -13,7 +13,8 @@ import ru.touchin.template.base.viewmodel.BaseController
|
|||
class SecondViewModel @AssistedInject constructor(
|
||||
@Assisted("from") from: String,
|
||||
@Assisted("screenName") screenName: String,
|
||||
private val rootRouter: Router
|
||||
private val rootRouter: Router,
|
||||
// private val secondRepository: SecondRepository
|
||||
) : ViewModel(), BaseController {
|
||||
|
||||
private val _state = MutableStateFlow("$from to $screenName")
|
||||
|
|
@ -21,9 +22,10 @@ class SecondViewModel @AssistedInject constructor(
|
|||
|
||||
@AssistedFactory
|
||||
interface Factory {
|
||||
|
||||
fun create(
|
||||
@Assisted("from") from: String,
|
||||
@Assisted("screenName") screenName: String
|
||||
@Assisted("screenName") screenName: String,
|
||||
): SecondViewModel
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
package ru.touchin.template.di.auth
|
||||
package ru.touchin.template.feature.second.di
|
||||
|
||||
import dagger.Subcomponent
|
||||
import ru.touchin.template.feature.first.FirstFragment
|
||||
import ru.touchin.template.feature.second.SecondFragment
|
||||
|
||||
@Subcomponent(modules = [AuthModule::class])
|
||||
@AuthScope
|
||||
|
|
@ -13,5 +14,7 @@ interface AuthComponent {
|
|||
fun build(): AuthComponent
|
||||
}
|
||||
|
||||
fun inject(entry: SecondFragment)
|
||||
|
||||
fun inject(entry: FirstFragment)
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package ru.touchin.template.feature.second.di
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import ru.touchin.template.feature.second.SecondRepository
|
||||
import ru.touchin.template.feature.second.SecondRepositoryImpl
|
||||
|
||||
@Module
|
||||
interface AuthModule {
|
||||
|
||||
@Binds
|
||||
@AuthScope
|
||||
fun bindSecondRepository(secondRepositoryImpl: SecondRepositoryImpl): SecondRepository
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package ru.touchin.template.feature.second.di
|
||||
|
||||
import javax.inject.Scope
|
||||
|
||||
@Scope
|
||||
annotation class AuthScope
|
||||
Loading…
Reference in New Issue