Skip to content

Commit

Permalink
centralize CoreComponent access method
Browse files Browse the repository at this point in the history
  • Loading branch information
nlgtuankiet committed Apr 14, 2020
1 parent e703957 commit dd3e4c7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/io/plaidapp/dagger/Injector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
package io.plaidapp.dagger

import io.plaidapp.core.dagger.SharedPreferencesModule
import io.plaidapp.core.dagger.coreComponent
import io.plaidapp.core.designernews.data.login.LoginLocalDataSource
import io.plaidapp.ui.HomeActivity
import io.plaidapp.ui.coreComponent

/**
* Injector for HomeActivity.
*/
fun inject(activity: HomeActivity) {
DaggerHomeComponent.builder()
.coreComponent(activity.coreComponent())
.coreComponent(activity.coreComponent)
.sharedPreferencesModule(
SharedPreferencesModule(activity, LoginLocalDataSource.DESIGNER_NEWS_PREF)
)
Expand Down
15 changes: 3 additions & 12 deletions app/src/main/java/io/plaidapp/ui/PlaidApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@

package io.plaidapp.ui

import android.app.Activity
import android.app.Application
import android.content.Context
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
import androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode
import androidx.core.os.BuildCompat
import io.plaidapp.core.dagger.CoreComponent
import io.plaidapp.core.dagger.DaggerCoreComponent
import io.plaidapp.core.dagger.HasCoreComponent

/**
* Io and Behold
*/
class PlaidApplication : Application() {
class PlaidApplication : Application(), HasCoreComponent {

override fun onCreate() {
super.onCreate()
Expand All @@ -41,15 +40,7 @@ class PlaidApplication : Application() {
setDefaultNightMode(nightMode)
}

private val coreComponent: CoreComponent by lazy {
override val coreComponent: CoreComponent by lazy(LazyThreadSafetyMode.NONE) {
DaggerCoreComponent.create()
}

companion object {
@JvmStatic
fun coreComponent(context: Context) =
(context.applicationContext as PlaidApplication).coreComponent
}
}

fun Activity.coreComponent() = PlaidApplication.coreComponent(this)
4 changes: 2 additions & 2 deletions core/src/main/java/io/plaidapp/core/dagger/CoreComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import retrofit2.converter.gson.GsonConverterFactory

/**
* Component providing application wide singletons.
* To call this make use of PlaidApplication.coreComponent or the
* Activity.coreComponent extension function.
* Access [CoreComponent] via any [android.content.Context] instance
* using [io.plaidapp.core.dagger.HasCoreComponent.coreComponent]
*/
@Component(modules = [CoreDataModule::class])
@Singleton
Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/io/plaidapp/core/dagger/HasCoreComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.dagger

import android.content.Context

interface HasCoreComponent {
val coreComponent: CoreComponent
}

val Context.coreComponent
get() = (this.applicationContext as HasCoreComponent).coreComponent
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package io.plaidapp.designernews.dagger
import `in`.uncod.android.bypass.Bypass
import android.util.TypedValue
import io.plaidapp.core.dagger.MarkdownModule
import io.plaidapp.core.dagger.coreComponent
import io.plaidapp.core.util.ColorUtils
import io.plaidapp.designernews.R
import io.plaidapp.designernews.ui.login.LoginActivity
import io.plaidapp.designernews.ui.story.StoryActivity
import io.plaidapp.ui.coreComponent

/**
* Inject [StoryActivity].
Expand All @@ -46,7 +46,7 @@ fun inject(storyId: Long, activity: StoryActivity) {
)

DaggerStoryComponent.builder()
.coreComponent(activity.coreComponent())
.coreComponent(activity.coreComponent)
.designerNewsModule(StoryModule(storyId, activity))
.markdownModule(MarkdownModule(activity.resources.displayMetrics, bypassOptions))
.sharedPreferencesModule(
Expand All @@ -59,7 +59,7 @@ fun inject(storyId: Long, activity: StoryActivity) {
fun inject(activity: LoginActivity) {

DaggerLoginComponent.builder()
.coreComponent(activity.coreComponent())
.coreComponent(activity.coreComponent)
.sharedPreferencesModule(DesignerNewsPreferencesModule(activity))
.build()
.inject(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package io.plaidapp.designernews.domain.search

import android.content.Context
import io.plaidapp.core.dagger.coreComponent
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchDataSourceFactoryProvider
import io.plaidapp.designernews.dagger.DaggerDesignerNewsSearchComponent
import io.plaidapp.designernews.dagger.DesignerNewsPreferencesModule
import io.plaidapp.ui.PlaidApplication.Companion.coreComponent

/**
* Provider for DesignerNews implementations of [SearchDataSourceFactory]
Expand All @@ -34,7 +34,7 @@ class DesignerNewsSearchDataSourceFactoryProvider : SearchDataSourceFactoryProvi
*/
override fun getFactory(context: Context): SearchDataSourceFactory {
return DaggerDesignerNewsSearchComponent.builder()
.coreComponent(coreComponent(context))
.coreComponent(context.coreComponent)
.designerNewsPreferencesModule(
DesignerNewsPreferencesModule(context)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package io.plaidapp.dribbble.dagger

import io.plaidapp.core.dagger.coreComponent
import io.plaidapp.dribbble.ui.shot.ShotActivity
import io.plaidapp.ui.coreComponent

/**
* Inject dependencies into [ShotActivity]
*/
fun ShotActivity.inject(shotId: Long) {
DaggerDribbbleComponent.builder()
.coreComponent(coreComponent())
.coreComponent(coreComponent)
.dribbbleModule(DribbbleModule(this, shotId))
.build()
.inject(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package io.plaidapp.dribbble.domain.search

import android.content.Context
import io.plaidapp.core.dagger.coreComponent
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchDataSourceFactoryProvider
import io.plaidapp.dribbble.dagger.DaggerDribbbleSearchComponent
import io.plaidapp.ui.PlaidApplication.Companion.coreComponent

/**
* Provider for Dribbble implementations of [SearchDataSourceFactory]
Expand All @@ -33,7 +33,7 @@ class DribbbleSearchDataSourceFactoryProvider : SearchDataSourceFactoryProvider
*/
override fun getFactory(context: Context): SearchDataSourceFactory {
return DaggerDribbbleSearchComponent.builder()
.coreComponent(coreComponent(context))
.coreComponent(context.coreComponent)
.build().factory()
}
}
4 changes: 2 additions & 2 deletions search/src/main/java/io/plaidapp/search/dagger/Injector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package io.plaidapp.search.dagger

import io.plaidapp.core.dagger.coreComponent
import io.plaidapp.search.ui.SearchActivity
import io.plaidapp.ui.coreComponent

/**
* Injector for SearchActivity.
Expand All @@ -29,7 +29,7 @@ object Injector {
@JvmStatic
fun inject(activity: SearchActivity) {
DaggerSearchComponent.builder()
.coreComponent(activity.coreComponent())
.coreComponent(activity.coreComponent)
.searchActivity(activity)
.build()
.inject(activity)
Expand Down

0 comments on commit dd3e4c7

Please sign in to comment.