Skip to content

Commit

Permalink
Addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Apr 16, 2019
1 parent 26fb1a2 commit 29d6a74
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/io/plaidapp/core/dagger/CoreDataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.plaidapp.core.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

/**
* Dagger module to provide core data functionality.
Expand All @@ -49,9 +50,11 @@ class CoreDataModule {
fun provideCallAdapterFactory(): CoroutineCallAdapterFactory = CoroutineCallAdapterFactory()

@Provides
@Singleton
fun provideGson(): Gson = Gson()

@Provides
@Singleton
fun provideGsonConverterFactory(gson: Gson): GsonConverterFactory =
GsonConverterFactory.create(gson)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
package io.plaidapp.designernews.dagger

import dagger.Component
import io.plaidapp.core.dagger.CoreDataModule
import io.plaidapp.core.dagger.CoreComponent
import io.plaidapp.core.dagger.designernews.DesignerNewsDataModule
import io.plaidapp.core.dagger.scope.FeatureScope
import io.plaidapp.core.interfaces.SearchDataSourceFactory

@Component(
modules = [
CoreDataModule::class,
DataModule::class,
DesignerNewsDataModule::class,
SearchDataModule::class,
DesignerNewsPreferencesModule::class
]
],
dependencies = [CoreComponent::class]
)
@FeatureScope
interface DesignerNewsSearchComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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])
@Module
class SearchDataModule {

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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 @@ -33,6 +34,7 @@ class DesignerNewsSearchDataSourceFactoryProvider : SearchDataSourceFactoryProvi
*/
override fun getFactory(context: Context): SearchDataSourceFactory {
return DaggerDesignerNewsSearchComponent.builder()
.coreComponent(coreComponent(context))
.designerNewsPreferencesModule(
DesignerNewsPreferencesModule(context)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.plaidapp.dribbble.dagger

import dagger.Component
import io.plaidapp.core.dagger.CoreComponent
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule
import io.plaidapp.core.dagger.scope.FeatureScope
import io.plaidapp.core.interfaces.SearchDataSourceFactory
Expand All @@ -26,7 +27,7 @@ import io.plaidapp.core.interfaces.SearchDataSourceFactory
DribbbleDataModule::class,
SearchDataModule::class
],
dependencies = [io.plaidapp.core.dagger.CoreComponent::class]
dependencies = [CoreComponent::class]
)
@FeatureScope
interface DribbbleSearchComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.Result
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.ui.getPlaidItemsForDisplay
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlin.coroutines.coroutineContext

/**
* Searches for a query in a list of data sources. Exposes the results of the search in a LiveData,
Expand All @@ -40,13 +46,20 @@ class LoadSearchDataUseCase(
get() = _searchResult

suspend operator fun invoke() {
val job = SupervisorJob(coroutineContext[Job])
val scope = CoroutineScope(job)
val deferredJobs = mutableListOf<Deferred<Unit>>()

dataSources.forEach {
val result = it.loadMore()
if (result is Result.Success) {
val oldItems = _searchResult.value.orEmpty().toMutableList()
val searchResult = getPlaidItemsForDisplay(oldItems, result.data)
_searchResult.postValue(searchResult)
}
deferredJobs.add(scope.async {
val result = it.loadMore()
if (result is Result.Success) {
val oldItems = _searchResult.value.orEmpty().toMutableList()
val searchResult = getPlaidItemsForDisplay(oldItems, result.data)
_searchResult.postValue(searchResult)
}
})
}
deferredJobs.forEach { it.await() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,4 @@ class SearchDataSourceFactoriesRegistry @Inject constructor(

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

fun add(dataSourceFactory: SearchDataSourceFactory) {
_dataSourceFactories.add(dataSourceFactory)
}

fun remove(dataSourceFactory: SearchDataSourceFactory) {
_dataSourceFactories.remove(dataSourceFactory)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SearchViewModel(
get() = _searchProgress

fun searchFor(query: String) {
searchQuery.postValue(query)
searchQuery.value = query
}

fun loadMore() = viewModelScope.launch(dispatcherProvider.computation) {
Expand Down

0 comments on commit 29d6a74

Please sign in to comment.