Skip to content

Commit

Permalink
Implementing a SearchFactoryProvider for Dribbble as well. Now search…
Browse files Browse the repository at this point in the history
… uses both dibbble and designer news
  • Loading branch information
florina-muntenescu committed Apr 16, 2019
1 parent 9d01634 commit 8951a60
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SearchDataSourceFactoriesRegistry @Inject constructor(
private val _dataSourceFactories = mutableListOf<SearchDataSourceFactory>()

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

val dataSourceFactories: List<SearchDataSourceFactory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import android.content.Context

interface SearchFactoryProvider {

fun getFactory(context: Context): SearchDataSourceFactory?
fun getFactory(context: Context): SearchDataSourceFactory
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.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 = [
DribbbleDataModule::class,
SearchDataModule::class
],
dependencies = [io.plaidapp.core.dagger.CoreComponent::class]
)
@FeatureScope
interface DribbbleSearchComponent {

@FeatureScope
fun factory(): SearchDataSourceFactory
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.dagger

import dagger.Module
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
class SearchDataModule {

@Provides
@FeatureScope
fun searchDataSourceFactory(
repository: ShotsRepository
): SearchDataSourceFactory {
return DribbbleSearchDataSourceFactory(repository)
}
}
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()
}
}
34 changes: 23 additions & 11 deletions search/src/main/java/io/plaidapp/search/dagger/SearchModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,33 @@ abstract class SearchModule {
@Provides
fun factories(activity: Activity): List<SearchDataSourceFactory> {
val factories = mutableListOf<SearchDataSourceFactory>()
try {
val provider = Class
.forName("io.plaidapp.designernews.domain.search.DesignerNewsSearchFactoryProvider")
.kotlin.objectInstance as SearchFactoryProvider
val factory = provider.getFactory(activity.applicationContext)
factory?.apply {
factories.add(this)
}
} catch (e: ClassNotFoundException) {
// nothing to do
}

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 searchViewModel(
Expand Down

0 comments on commit 8951a60

Please sign in to comment.