This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginActivity : AppCompatActivity() { | |
@Inject | |
lateinit var loginAnalyticsAdapter: LoginAnalyticsAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
DaggerLoginComponent.builder() | |
.context(this) | |
.appDependencies( | |
EntryPointAccessors.fromApplication( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LoginAnalyticsAdapter.kt - File in the login module. | |
class LoginAnalyticsAdapter @Inject constructor( | |
@AuthInterceptorOkHttpClient okHttpClient: OkHttpClient | |
) { ... } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(dependencies = [LoginModuleDependencies::class]) | |
interface LoginComponent { | |
fun inject(activity: LoginActivity) | |
@Component.Builder | |
interface Builder { | |
fun context(@BindsInstance context: Context): Builder | |
fun appDependencies(loginModuleDependencies: LoginModuleDependencies): Builder | |
fun build(): LoginComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LoginModuleDependencies.kt - File in the app module. | |
@EntryPoint | |
@InstallIn(SingletonComponent::class) | |
interface LoginModuleDependencies { | |
@AuthInterceptorOkHttpClient | |
fun okHttpClient(): OkHttpClient | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
val viewModel: MainActivityViewModel by viewModels() | |
………… | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HiltViewModel | |
class MainActivityViewModel @Inject constructor(private val appDatabase: AppDatabase): ViewModel(){ | |
fun getDatabase(): List<Readings?>? { | |
return appDatabase.readingDao()!!.filterReadings("%%") | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleContentProvider: ContentProvider() { | |
... | |
override fun query(...): Cursor { | |
val appContext = context?.applicationContext ?: throw IllegalStateException() | |
val hiltEntryPoint = | |
EntryPointAccessors.fromApplication(appContext, ExampleContentProviderEntryPoint::class.java) | |
val analyticsService = hiltEntryPoint.analyticsService() | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleContentProvider : ContentProvider() { | |
@EntryPoint | |
@InstallIn(SingletonComponent::class) | |
interface ExampleContentProviderEntryPoint { | |
fun analyticsService(): AnalyticsService | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ActivityScoped | |
class AnalyticsAdapter @Inject constructor( | |
private val service: AnalyticsService | |
) { ... } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android class ------------------ Components ------------------------------------ Scope | |
Application---- SingletonComponent -------------------------- @Singleton | |
Activity ---ActivityRetainedComponents -------- @ActivityRetainedScoped | |
ViewModel ------ ViewModelComponents -------- @ViewModelScoped | |
Activity ----- ActivityComponents ------------------ @ActivityScoped | |
Fragment ---- FragmentComponents ------- @FragmentScoped | |
View --------- ViewComponents ------------------ @ViewScoped | |
View annotated @WithFragmentBindings -- ViewWithFragmentComponents -- @ViewScoped | |
Service --------- ServiceComponent ------------ @ServiceScoped |