Skip to content

Commit

Permalink
Wire up dagger components
Browse files Browse the repository at this point in the history
CoreComponent gets injected into HomeComponent and DribbbleComponent.
This makes it provide a true singleton of the modules declared there.

Several @provides functions are now provision functions (without
parameter) so component dependency resolution works without
subcomponents.
  • Loading branch information
keyboardsurfer authored and florina-muntenescu committed Oct 11, 2018
1 parent bafeda0 commit 3684f80
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 264 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
android:supportsRtl="true"
android:extractNativeLibs="false"
android:appCategory="image"
android:name=".ui.PlaidApplication"
android:theme="@style/Plaid">

<activity
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/io/plaidapp/dagger/HomeComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package io.plaidapp.dagger

import android.content.Context
import dagger.BindsInstance
import dagger.Component
import io.plaidapp.core.dagger.CoreComponent
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
Expand All @@ -27,21 +26,19 @@ import io.plaidapp.ui.HomeActivity
/**
* Dagger component for the [HomeActivity].
*/
@Component(modules = [HomeModule::class])
@Component(modules = [HomeModule::class], dependencies = [CoreComponent::class])
interface HomeComponent {

fun inject(activity: HomeActivity)

@Component.Builder
interface Builder {
fun build(): HomeComponent

@BindsInstance
fun context(context: Context): Builder

fun dataLoadedModule(module: OnDataLoadedModule): Builder
fun build(): HomeComponent
fun coreComponent(module: CoreComponent): Builder
fun dataManagerModule(module: DataManagerModule): Builder
fun homeModule(module: HomeModule): Builder
fun dataLoadedModule(module: OnDataLoadedModule): Builder
fun filterAdapterModule(module: FilterAdapterModule): Builder
fun homeModule(module: HomeModule): Builder
}
}
29 changes: 23 additions & 6 deletions app/src/main/java/io/plaidapp/dagger/HomeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,37 @@ import dagger.Module
import dagger.Provides
import io.plaidapp.R
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule
import io.plaidapp.core.data.pocket.PocketUtils
import io.plaidapp.core.dribbble.data.api.model.Shot

@Module(includes = [DataManagerModule::class, OnDataLoadedModule::class])
/**
* Dagger module for [io.plaidapp.ui.HomeActivity].
*/
@Module(
includes = [
DataManagerModule::class,
DribbbleDataModule::class,
FilterAdapterModule::class,
OnDataLoadedModule::class
]
)
class HomeModule(private val activity: Activity) {

@Provides fun context(): Context = activity
@Provides
fun context(): Context = activity

@Provides fun activity(): Activity = activity
@Provides
fun activity(): Activity = activity

@Provides fun columns(): Int = activity.resources.getInteger(R.integer.num_columns)
@Provides
fun columns(): Int = activity.resources.getInteger(R.integer.num_columns)

@Provides fun viewPreloadSizeProvider() = ViewPreloadSizeProvider<Shot>()
@Provides
fun viewPreloadSizeProvider() = ViewPreloadSizeProvider<Shot>()

@Provides fun isPocketInstalled() = PocketUtils.isPocketInstalled(activity)
@Provides
fun isPocketInstalled() = PocketUtils.isPocketInstalled(activity)
}
7 changes: 4 additions & 3 deletions app/src/main/java/io/plaidapp/dagger/Injector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.data.BaseDataManager
import io.plaidapp.core.data.PlaidItem
import io.plaidapp.ui.HomeActivity
import io.plaidapp.ui.PlaidApplication

/**
* Injector for HomeActivity.
Expand All @@ -36,11 +37,11 @@ object Injector {
dataLoadedCallback: BaseDataManager.OnDataLoadedCallback<List<PlaidItem>>
) {
DaggerHomeComponent.builder()
.context(activity)
.dataLoadedModule(OnDataLoadedModule(dataLoadedCallback))
.coreComponent(PlaidApplication.coreComponent(activity))
.dataManagerModule(DataManagerModule(activity))
.homeModule(HomeModule(activity))
.dataLoadedModule(OnDataLoadedModule(dataLoadedCallback))
.filterAdapterModule(FilterAdapterModule(activity))
.homeModule(HomeModule(activity))
.build()
.inject(activity)
}
Expand Down
9 changes: 2 additions & 7 deletions app/src/main/java/io/plaidapp/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import io.plaidapp.core.dribbble.data.api.model.Shot;
import io.plaidapp.core.ui.FeedAdapter;
import io.plaidapp.core.ui.FilterAdapter;
import io.plaidapp.core.ui.FilterAnimator;
import io.plaidapp.core.ui.HomeGridItemAnimator;
import io.plaidapp.core.ui.recyclerview.InfiniteScrollListener;
import io.plaidapp.core.ui.transitions.FabTransform;
Expand Down Expand Up @@ -121,13 +122,7 @@ protected void onCreate(Bundle savedInstanceState) {
setExitSharedElementCallback(FeedAdapter.createSharedElementReenterCallback(this));

loginRepository = Injection.provideLoginRepository(this);
// filtersAdapter = new FilterAdapter(this, SourceManager.getSources(this));
// dataManager = new DataManager(this, data -> {
// adapter.addAndResort(data);
// checkEmptyState();
// });
ViewPreloadSizeProvider<Shot> shotPreloadSizeProvider = new ViewPreloadSizeProvider<>();
// adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this), shotPreloadSizeProvider);

grid.setAdapter(adapter);
layoutManager = new GridLayoutManager(this, columns);
Expand Down Expand Up @@ -211,7 +206,7 @@ public void onLoadMore() {
setupTaskDescription();

filtersList.setAdapter(filtersAdapter);
filtersList.setItemAnimator(new FilterAdapter.FilterAnimator());
filtersList.setItemAnimator(new FilterAnimator());
filtersAdapter.registerFilterChangedCallback(filtersChangedCallbacks);
dataManager.loadAllDataSources();
ItemTouchHelper.Callback callback = new FilterTouchHelperCallback(filtersAdapter, this);
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/io/plaidapp/ui/PlaidApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ import android.app.Application
import android.content.Context
import io.plaidapp.core.dagger.CoreComponent
import io.plaidapp.core.dagger.DaggerCoreComponent
import io.plaidapp.core.dagger.MarkdownModule
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule

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

val coreComponent: CoreComponent by lazy {
DaggerCoreComponent.create()
DaggerCoreComponent
.builder()
.dribbbleDataModule(DribbbleDataModule())
.markdownModule(MarkdownModule(resources.displayMetrics))
.build()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import android.support.v7.widget.RecyclerView.ViewHolder
import android.support.v7.widget.helper.ItemTouchHelper
import android.support.v7.widget.helper.ItemTouchHelper.START
import io.plaidapp.R
import io.plaidapp.core.ui.FilterAdapter
import io.plaidapp.core.ui.FilterViewHolder
import io.plaidapp.core.ui.recyclerview.FilterSwipeDismissListener
import io.plaidapp.util.setTranslation

Expand Down Expand Up @@ -82,7 +82,7 @@ class FilterTouchHelperCallback(

override fun getSwipeDirs(rv: RecyclerView, viewHolder: ViewHolder): Int {
// can only swipe-dismiss certain sources
val swipeDir = if ((viewHolder as FilterAdapter.FilterViewHolder).isSwipeable) START else 0
val swipeDir = if ((viewHolder as FilterViewHolder).isSwipeable) START else 0
return makeMovementFlags(0, swipeDir)
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/io/plaidapp/core/AppInjection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package io.plaidapp.core
import android.content.Context
import android.content.SharedPreferences
import io.plaidapp.core.data.CoroutinesContextProvider
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.android.UI
import okhttp3.logging.HttpLoggingInterceptor

/**
Expand All @@ -45,4 +47,4 @@ fun provideSharedPreferences(context: Context, name: String): SharedPreferences
}

@Deprecated("Use Dagger CoroutinesContextProviderModule instead")
fun provideCoroutinesContextProvider(): CoroutinesContextProvider = CoroutinesContextProvider()
fun provideCoroutinesContextProvider() = CoroutinesContextProvider(UI, CommonPool)
15 changes: 11 additions & 4 deletions core/src/main/java/io/plaidapp/core/dagger/CoreComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ import dagger.Component
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule

/**
* Component providing application wide singletons
* Component providing application wide singletons.
* To call this make use of PlaidApplication.coreComponent.
*/
@Component(
modules = [
CoroutinesContextProviderModule::class,
DribbbleDataModule::class,
MarkdownModule::class,
OnDataLoadedModule::class,
SharedPreferencesModule::class
]
)
interface CoreComponent
interface CoreComponent {

@Component.Builder interface Builder {
fun build(): CoreComponent
fun dribbbleDataModule(module: DribbbleDataModule): Builder
fun markdownModule(module: MarkdownModule): Builder
fun sharedPreferencesModuleModule(module: SharedPreferencesModule): Builder
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package io.plaidapp.core.dagger
import dagger.Module
import dagger.Provides
import io.plaidapp.core.data.CoroutinesContextProvider
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.android.UI

/**
* Provide [CoroutinesContextProvider] to this app's components.
*/
@Module class CoroutinesContextProviderModule {

@Provides fun provideCoroutinesContextProvider() = CoroutinesContextProvider()
@Provides fun provideCoroutinesContextProvider() = CoroutinesContextProvider(UI, CommonPool)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package io.plaidapp.core.dagger
import android.content.Context
import dagger.Module
import dagger.Provides
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule
import io.plaidapp.core.data.BaseDataManager
import io.plaidapp.core.data.DataLoadingSubject
import io.plaidapp.core.data.DataManager
Expand All @@ -29,7 +30,7 @@ import io.plaidapp.core.ui.FilterAdapter
/**
* Module to provide [DataManager].
*/
@Module(includes = [FilterAdapterModule::class, ShotsRepositoryModule::class])
@Module(includes = [DribbbleDataModule::class])
class DataManagerModule(val context: Context) {

private lateinit var manager: DataManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package io.plaidapp.core.dagger
import android.content.Context
import dagger.Module
import dagger.Provides
import io.plaidapp.core.data.Source
import io.plaidapp.core.data.prefs.SourceManager
import io.plaidapp.core.ui.FilterAdapter

Expand All @@ -27,5 +28,7 @@ import io.plaidapp.core.ui.FilterAdapter
*/
@Module class FilterAdapterModule(val context: Context) {

@Provides fun provideFilterAdapter() = FilterAdapter(context, SourceManager.getSources(context))
@Provides fun provideFilterAdapter() = FilterAdapter(context, provideSources())

@Provides fun provideSources(): MutableList<Source> = SourceManager.getSources(context)
}

This file was deleted.

42 changes: 0 additions & 42 deletions core/src/main/java/io/plaidapp/core/dagger/OkHttpModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import io.plaidapp.core.data.PlaidItem
/**
* Module to provide [BaseDataManager.OnDataLoadedCallback].
*/
@Module class OnDataLoadedModule(private val callback: BaseDataManager.OnDataLoadedCallback<List<PlaidItem>>) {
@Module class OnDataLoadedModule(
private val callback: BaseDataManager.OnDataLoadedCallback<List<PlaidItem>>
) {

@Provides fun provideOnDataLoadedCallback() = callback
@Provides
fun provideOnDataLoadedCallback() = callback
}
Loading

0 comments on commit 3684f80

Please sign in to comment.