Skip to content

Commit

Permalink
Trying to inject dependencies from dynamic feature modules
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Apr 16, 2019
1 parent baef2e6 commit a3c35f3
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 26 deletions.
19 changes: 11 additions & 8 deletions core/src/main/java/io/plaidapp/core/dagger/DataSourcesModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ package io.plaidapp.core.dagger

import dagger.Module
import dagger.Provides
import io.plaidapp.core.interfaces.SearchDataSourceFactoriesRegistry
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchDataSourcesRegistry
import io.plaidapp.core.interfaces.designernews.DesignerNewsSearchDataSourceFactory
import io.plaidapp.core.interfaces.dribbble.DribbbleSearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchDataSourcesFactory

@Module
class DataSourcesModule {

@Provides
fun searchDataSourceFactoriesRegistry(): SearchDataSourceFactoriesRegistry {
return SearchDataSourceFactoriesRegistry()
}

@Provides
fun searchDataSourcesRegistry(
dnDataSourceFactory: DesignerNewsSearchDataSourceFactory,
dribbbleSourceFactory: DribbbleSearchDataSourceFactory
): SearchDataSourcesRegistry {
factoriesRegistry: SearchDataSourceFactoriesRegistry
): SearchDataSourcesFactory {
val factories: List<SearchDataSourceFactory> =
listOf(dnDataSourceFactory, dribbbleSourceFactory)
return SearchDataSourcesRegistry(factories)
factoriesRegistry.dataSourceFactories.value.orEmpty()
return SearchDataSourcesFactory(factories)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.core.interfaces

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

private const val designerNewsSearchDataSourceFactoryProviderClassName =
"io.plaidapp.designernews.domain.search.DesignerNewsSearchFactoryProvider"

// TODO add dribbble as well
private val factoryClassNames =
listOf(designerNewsSearchDataSourceFactoryProviderClassName)

class SearchDataSourceFactoriesRegistry {

private val _dataSourceFactories =
MutableLiveData<List<SearchDataSourceFactory>>(getAlreadyAvailableDataSourceFactories())

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

fun add(dataSourceFactory: SearchDataSourceFactory) {
val existingDataSources = _dataSourceFactories.value.orEmpty().toMutableList()
existingDataSources.add(dataSourceFactory)
_dataSourceFactories.postValue(existingDataSources)
}

fun remove(dataSourceFactory: SearchDataSourceFactory) {
val existingDataSources = _dataSourceFactories.value.orEmpty().toMutableList()
existingDataSources.remove(dataSourceFactory)
_dataSourceFactories.postValue(existingDataSources)
}

private fun getAlreadyAvailableDataSourceFactories(): List<SearchDataSourceFactory> {
return factoryClassNames.map {
val clazz = Class.forName(it)
val instance = clazz.newInstance() as SearchFactoryProvider
instance.getFactory()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.plaidapp.core.interfaces

class SearchDataSourcesRegistry(private val factories: List<SearchDataSourceFactory>) {
class SearchDataSourcesFactory(private val factories: List<SearchDataSourceFactory>) {

fun buildSearchDataSources(query: String): List<PlaidDataSource> =
factories.map { it.buildDataSource(query) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.core.interfaces

interface SearchFactoryProvider {

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

import dagger.Component

@Component(
modules = [SearchDataModule::class]
)
interface DesignerNewsSearchComponent
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import `in`.uncod.android.bypass.Bypass
import android.util.TypedValue
import androidx.core.content.ContextCompat
import io.plaidapp.core.dagger.MarkdownModule
import io.plaidapp.designernews.domain.search.DesignerNewsSearchFactoryProvider
import io.plaidapp.designernews.ui.login.LoginActivity
import io.plaidapp.designernews.ui.story.StoryActivity
import io.plaidapp.ui.coreComponent
Expand Down Expand Up @@ -62,3 +63,8 @@ fun inject(activity: LoginActivity) {
.build()
.inject(activity)
}

fun DesignerNewsSearchFactoryProvider.inject() {

DaggerDesignerNewsSearchComponent.create()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.dagger

import dagger.Module
import dagger.Provides
import io.plaidapp.core.dagger.DataSourcesModule
import io.plaidapp.core.designernews.data.stories.StoriesRepository
import io.plaidapp.core.interfaces.SearchDataSourceFactoriesRegistry
import io.plaidapp.designernews.domain.search.DesignerNewsSearchDataSourceFactory

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

@Provides
fun designerNewsSearchDataSourceFactory(
repository: StoriesRepository,
registry: SearchDataSourceFactoriesRegistry
): DesignerNewsSearchDataSourceFactory {
val factory = DesignerNewsSearchDataSourceFactory(repository)
registry.add(factory)
return factory
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.plaidapp.core.interfaces.designernews
package io.plaidapp.designernews.domain.search

import io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
* limitations under the License.
*/

package io.plaidapp.core.interfaces.designernews
package io.plaidapp.designernews.domain.search

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,37 @@
/*
* 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 io.plaidapp.core.designernews.data.stories.StoriesRepository
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchFactoryProvider
import io.plaidapp.designernews.dagger.inject
import javax.inject.Inject

class DesignerNewsSearchFactoryProvider : SearchFactoryProvider {

init {
inject()
}

@Inject
lateinit var repository: StoriesRepository

override fun getFactory(): SearchDataSourceFactory {
return DesignerNewsSearchDataSourceFactory(repository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.plaidapp.core.interfaces.dribbble
package io.plaidapp.dribbble.domain.search

import io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
* limitations under the License.
*/

package io.plaidapp.core.interfaces.dribbble
package io.plaidapp.dribbble.domain.search

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
Expand Up @@ -20,15 +20,15 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.Result
import io.plaidapp.core.interfaces.SearchDataSourcesRegistry
import io.plaidapp.core.interfaces.SearchDataSourcesFactory
import io.plaidapp.core.ui.getPlaidItemsForDisplay

class SearchUseCase(
dataSourcesRegistry: SearchDataSourcesRegistry,
dataSourcesFactory: SearchDataSourcesFactory,
query: String
) {

private val dataSources = dataSourcesRegistry.buildSearchDataSources(query)
private val dataSources = dataSourcesFactory.buildSearchDataSources(query)

private val _searchResult = MutableLiveData<List<PlaidItem>>()
val searchResult: LiveData<List<PlaidItem>>
Expand Down
6 changes: 3 additions & 3 deletions search/src/main/java/io/plaidapp/search/ui/SearchViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.lifecycle.viewModelScope
import io.plaidapp.core.data.CoroutinesDispatcherProvider
import io.plaidapp.core.feed.FeedProgressUiModel
import io.plaidapp.core.feed.FeedUiModel
import io.plaidapp.core.interfaces.SearchDataSourcesRegistry
import io.plaidapp.core.interfaces.SearchDataSourcesFactory
import io.plaidapp.search.domain.SearchUseCase
import kotlinx.coroutines.launch

Expand All @@ -33,7 +33,7 @@ import kotlinx.coroutines.launch
* for display in the [SearchActivity].
*/
class SearchViewModel(
private val dataSourcesRegistry: SearchDataSourcesRegistry,
private val dataSourcesFactory: SearchDataSourcesFactory,
private val dispatcherProvider: CoroutinesDispatcherProvider
) : ViewModel() {

Expand All @@ -42,7 +42,7 @@ class SearchViewModel(
private val searchQuery = MutableLiveData<String>()

private val results = Transformations.switchMap(searchQuery) {
searchUseCase = SearchUseCase(dataSourcesRegistry, it)
searchUseCase = SearchUseCase(dataSourcesFactory, it)
loadMore()
return@switchMap searchUseCase?.searchResult
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ package io.plaidapp.search.ui
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import io.plaidapp.core.data.CoroutinesDispatcherProvider
import io.plaidapp.core.interfaces.SearchDataSourcesRegistry
import io.plaidapp.core.interfaces.SearchDataSourcesFactory
import javax.inject.Inject

/**
* Factory to create [SearchViewModel]
*/
class SearchViewModelFactory @Inject constructor(
private val dataSourcesRegistry: SearchDataSourcesRegistry,
private val dataSourcesFactory: SearchDataSourcesFactory,
private val dispatcherProvider: CoroutinesDispatcherProvider
) : ViewModelProvider.Factory {

Expand All @@ -35,6 +35,6 @@ class SearchViewModelFactory @Inject constructor(
if (modelClass != SearchViewModel::class.java) {
throw IllegalArgumentException("Unknown ViewModel class")
}
return SearchViewModel(dataSourcesRegistry, dispatcherProvider) as T
return SearchViewModel(dataSourcesFactory, dispatcherProvider) as T
}
}

0 comments on commit a3c35f3

Please sign in to comment.