Skip to content

Commit

Permalink
Revert "Multibinding and Initializer"
Browse files Browse the repository at this point in the history
This reverts commit 348a023.
  • Loading branch information
florina-muntenescu committed Apr 16, 2019
1 parent ca4fdc3 commit e9741fc
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dagger.Component
import io.plaidapp.core.dagger.CoreDataModule
import io.plaidapp.core.dagger.designernews.DesignerNewsDataModule
import io.plaidapp.core.dagger.scope.FeatureScope
import io.plaidapp.core.interfaces.SearchDataSourceFactory

@Component(
modules = [
Expand All @@ -33,4 +34,6 @@ import io.plaidapp.core.dagger.scope.FeatureScope
@FeatureScope
interface DesignerNewsSearchComponent {

@FeatureScope
fun factory(): SearchDataSourceFactory
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@

package io.plaidapp.designernews.dagger

import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoSet
import dagger.Provides
import io.plaidapp.core.dagger.scope.FeatureScope
import io.plaidapp.core.designernews.data.stories.StoriesRepository
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.designernews.domain.search.DesignerNewsSearchDataSourceFactory

@Module(includes = [DesignerNewsPreferencesModule::class])
interface SearchDataModule {
class SearchDataModule {

@Binds
@IntoSet
fun searchDataSourceFactory(designerNewsFactory: DesignerNewsSearchDataSourceFactory): SearchDataSourceFactory
@Provides
@FeatureScope
fun searchDataSourceFactory(
repository: StoriesRepository
): SearchDataSourceFactory {
return DesignerNewsSearchDataSourceFactory(repository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import io.plaidapp.core.designernews.data.DesignerNewsSearchSource
import io.plaidapp.core.designernews.data.stories.StoriesRepository
import io.plaidapp.core.interfaces.PlaidDataSource
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import javax.inject.Inject

class DesignerNewsSearchDataSourceFactory @Inject constructor(
class DesignerNewsSearchDataSourceFactory(
private val repository: StoriesRepository
) : SearchDataSourceFactory {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 Google, Inc.
*
* 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.designernews.domain.search

import android.content.Context
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchFactoryProvider
import io.plaidapp.designernews.dagger.DaggerDesignerNewsSearchComponent
import io.plaidapp.designernews.dagger.DesignerNewsPreferencesModule

object DesignerNewsSearchFactoryProvider : SearchFactoryProvider {

override fun getFactory(context: Context): SearchDataSourceFactory {
return DaggerDesignerNewsSearchComponent.builder()
.designerNewsPreferencesModule(
DesignerNewsPreferencesModule(context)
)
.build().factory()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package io.plaidapp.dribbble.dagger
import dagger.Component
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule
import io.plaidapp.core.dagger.scope.FeatureScope
import io.plaidapp.core.interfaces.SearchDataSourceFactory

@Component(
modules = [
Expand All @@ -30,4 +31,6 @@ import io.plaidapp.core.dagger.scope.FeatureScope
@FeatureScope
interface DribbbleSearchComponent {

@FeatureScope
fun factory(): SearchDataSourceFactory
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@

package io.plaidapp.dribbble.dagger

import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoSet
import dagger.Provides
import io.plaidapp.core.dagger.scope.FeatureScope
import io.plaidapp.core.dribbble.data.ShotsRepository
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.dribbble.domain.search.DribbbleSearchDataSourceFactory

@Module
interface SearchDataModule {
class SearchDataModule {

@Binds
@IntoSet
fun searchDataSourceFactory(dribbbleFactory: DribbbleSearchDataSourceFactory): SearchDataSourceFactory
@Provides
@FeatureScope
fun searchDataSourceFactory(
repository: ShotsRepository
): SearchDataSourceFactory {
return DribbbleSearchDataSourceFactory(repository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import io.plaidapp.core.dribbble.data.DribbbleSourceItem
import io.plaidapp.core.dribbble.data.ShotsRepository
import io.plaidapp.core.interfaces.PlaidDataSource
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import javax.inject.Inject

class DribbbleSearchDataSourceFactory @Inject constructor(
class DribbbleSearchDataSourceFactory(
private val repository: ShotsRepository
) : SearchDataSourceFactory {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Google, Inc.
*
* 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.dribbble.domain.search

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

object DribbbleSearchFactoryProvider : SearchFactoryProvider {

override fun getFactory(context: Context): SearchDataSourceFactory {
return DaggerDribbbleSearchComponent.builder()
.coreComponent(coreComponent(context))
.build().factory()
}
}
5 changes: 0 additions & 5 deletions search/src/main/java/io/plaidapp/search/dagger/Injector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.plaidapp.search.dagger

import io.plaidapp.core.interfaces.ModuleInitializer
import io.plaidapp.search.ui.SearchActivity
import io.plaidapp.ui.coreComponent

Expand All @@ -29,10 +28,6 @@ object Injector {

@JvmStatic
fun inject(activity: SearchActivity) {
val initializer =
Class.forName("io.plaidapp.designernews.dagger.DesignerNewsSearchInitializer").kotlin.objectInstance as ModuleInitializer
initializer.init(activity)

DaggerSearchComponent.builder()
.coreComponent(activity.coreComponent())
.searchActivity(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import io.plaidapp.search.ui.SearchActivity
@FeatureScope
interface SearchComponent : BaseActivityComponent<SearchActivity> {

fun factories(): Set<SearchDataSourceFactory>
fun factories(): List<SearchDataSourceFactory>

@Component.Builder
interface Builder {
Expand Down
65 changes: 31 additions & 34 deletions search/src/main/java/io/plaidapp/search/dagger/SearchModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import androidx.lifecycle.ViewModelProviders
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.multibindings.Multibinds
import io.plaidapp.R
import io.plaidapp.core.dagger.qualifier.IsPocketInstalled
import io.plaidapp.core.data.pocket.PocketUtils
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchFactoryProvider
import io.plaidapp.search.ui.SearchActivity
import io.plaidapp.search.ui.SearchViewModel
import io.plaidapp.search.ui.SearchViewModelFactory
Expand All @@ -44,9 +44,6 @@ abstract class SearchModule {
@Binds
abstract fun context(activity: Activity): Context

@Multibinds
abstract fun factories(): Set<SearchDataSourceFactory>

@Module
companion object {

Expand All @@ -59,36 +56,36 @@ abstract class SearchModule {
@Provides
fun isPocketInstalled(activity: Activity): Boolean = PocketUtils.isPocketInstalled(activity)

// @JvmStatic
// @Provides
// fun factories(activity: Activity): Set<SearchDataSourceFactory> {
// val factories = mutableSetOf<SearchDataSourceFactory>()
//
// searchDataSourceFactory(
// activity,
// "io.plaidapp.designernews.domain.search.DesignerNewsSearchFactoryProvider"
// )?.apply { factories.add(this) }
//
// searchDataSourceFactory(
// activity,
// "io.plaidapp.dribbble.domain.search.DribbbleSearchFactoryProvider"
// )?.apply { factories.add(this) }
//
// return factories
// }
//
// private fun searchDataSourceFactory(
// activity: Activity,
// className: String
// ): SearchDataSourceFactory? {
// return try {
// val provider =
// Class.forName(className).kotlin.objectInstance as SearchFactoryProvider
// provider.getFactory(activity.applicationContext)
// } catch (e: ClassNotFoundException) {
// null
// }
// }
@JvmStatic
@Provides
fun factories(activity: Activity): List<SearchDataSourceFactory> {
val factories = mutableListOf<SearchDataSourceFactory>()

searchDataSourceFactory(
activity,
"io.plaidapp.designernews.domain.search.DesignerNewsSearchFactoryProvider"
)?.apply { factories.add(this) }

searchDataSourceFactory(
activity,
"io.plaidapp.dribbble.domain.search.DribbbleSearchFactoryProvider"
)?.apply { factories.add(this) }

return factories
}

private fun searchDataSourceFactory(
activity: Activity,
className: String
): SearchDataSourceFactory? {
return try {
val provider =
Class.forName(className).kotlin.objectInstance as SearchFactoryProvider
provider.getFactory(activity.applicationContext)
} catch (e: ClassNotFoundException) {
null
}
}

@JvmStatic
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import javax.inject.Inject
import javax.inject.Provider

class SearchDataSourceFactoriesRegistry @Inject constructor(
defaultFactories: Provider<Set<SearchDataSourceFactory>>
defaultFactories: Provider<List<SearchDataSourceFactory>>
) {
private val _dataSourceFactories = mutableSetOf<SearchDataSourceFactory>()
private val _dataSourceFactories = mutableListOf<SearchDataSourceFactory>()

init {
defaultFactories.get()?.apply { _dataSourceFactories.addAll(this) }
}

val dataSourceFactories: Set<SearchDataSourceFactory>
val dataSourceFactories: List<SearchDataSourceFactory>
get() = _dataSourceFactories

fun add(dataSourceFactory: SearchDataSourceFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SearchViewModel(
private val searchQuery = MutableLiveData<String>()

private val results = Transformations.switchMap(searchQuery) {
searchUseCase = SearchUseCase(factories.toList(), it)
searchUseCase = SearchUseCase(factories, it)
loadMore()
return@switchMap searchUseCase?.searchResult
}
Expand Down

0 comments on commit e9741fc

Please sign in to comment.