Skip to content

Commit

Permalink
Breaking data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Apr 16, 2019
1 parent 3df639c commit a48e0f4
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 42 deletions.
33 changes: 33 additions & 0 deletions core/src/main/java/io/plaidapp/core/dagger/DataSourcesModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.dagger

import dagger.Module
import dagger.Provides
import io.plaidapp.core.interfaces.SearchDataSourceFactory
import io.plaidapp.core.interfaces.SearchDataSourcesRegistry
import io.plaidapp.core.interfaces.designernews.DesignerNewsSearchDataSourceFactory

@Module
class DataSourcesModule {

@Provides
fun searchDataSourcesRegistry(dnDataSourceFactory: DesignerNewsSearchDataSourceFactory): SearchDataSourcesRegistry {
val factories: List<SearchDataSourceFactory> = listOf(dnDataSourceFactory)
return SearchDataSourcesRegistry(factories)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class DesignerNewsSourceItem(

data class DesignerNewsSearchSource(
val query: String,
override var active: Boolean
override var active: Boolean = true
) : DesignerNewsSourceItem(
DESIGNER_NEWS_QUERY_PREFIX + query,
SEARCH_SORT_ORDER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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

class DataSourcesRegistry(defaultDataSources: List<PlaidDataSource>) {

private val _dataSources = MutableLiveData<List<PlaidDataSource>>(defaultDataSources)

val dataSources: LiveData<List<PlaidDataSource>>
get() = _dataSources

fun add(dataSource: PlaidDataSource) {
val existingDataSources = _dataSources.value.orEmpty().toMutableList()
existingDataSources.add(dataSource)
_dataSources.postValue(existingDataSources)
}

fun remove(dataSource: PlaidDataSource) {
val existingDataSources = _dataSources.value.orEmpty().toMutableList()
existingDataSources.remove(dataSource)
_dataSources.postValue(existingDataSources)
}
}
26 changes: 26 additions & 0 deletions core/src/main/java/io/plaidapp/core/interfaces/PlaidDataSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.Result
import io.plaidapp.core.data.SourceItem

abstract class PlaidDataSource(val sourceItem: SourceItem) {

abstract suspend fun loadMore(): Result<List<PlaidItem>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 SearchDataSourceFactory {
fun buildDataSource(query: String): PlaidDataSource
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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

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

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

import io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.Result
import io.plaidapp.core.data.SourceItem
import io.plaidapp.core.designernews.data.stories.StoriesRepository
import io.plaidapp.core.designernews.data.stories.model.toStory
import io.plaidapp.core.interfaces.PlaidDataSource
import io.plaidapp.core.util.exhaustive

class DesignerNewsDataSource(
sourceItem: SourceItem,
private val repository: StoriesRepository
) : PlaidDataSource(sourceItem) {

private var page = 0

override suspend fun loadMore(): Result<List<PlaidItem>> {
val result = repository.search(sourceItem.key, page)
(return when (result) {
is Result.Success -> {
page++
val stories = result.data.map { it.toStory() }
Result.Success(stories)
}
is Result.Error -> result
}).exhaustive
}
}
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.core.interfaces.designernews

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(
private val repository: StoriesRepository
) : SearchDataSourceFactory {

override fun buildDataSource(query: String): PlaidDataSource {
val sourceItem = DesignerNewsSearchSource(query)
return DesignerNewsDataSource(
sourceItem,
repository
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dagger.Binds
import dagger.Module
import dagger.Provides
import io.plaidapp.R
import io.plaidapp.core.dagger.DataSourcesModule
import io.plaidapp.core.dagger.SharedPreferencesModule
import io.plaidapp.core.dagger.designernews.DesignerNewsDataModule
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule
Expand All @@ -39,7 +40,8 @@ import io.plaidapp.search.ui.SearchViewModelFactory
includes = [
DribbbleDataModule::class,
DesignerNewsDataModule::class,
SharedPreferencesModule::class
SharedPreferencesModule::class,
DataSourcesModule::class
]
)
abstract class SearchModule {
Expand Down
46 changes: 46 additions & 0 deletions search/src/main/java/io/plaidapp/search/domain/SearchUseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.search.domain

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

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

private val dataSources = dataSourcesRegistry.buildSearchDataSources(query)

private val _searchResult = MutableLiveData<List<PlaidItem>>()
val searchResult: LiveData<List<PlaidItem>>
get() = _searchResult

suspend fun loadMore() {
dataSources.forEach {
val result = it.loadMore()
if (result is Result.Success) {
val lastResult = _searchResult.value.orEmpty().toMutableList()
lastResult.addAll(result.data)
_searchResult.postValue(lastResult)
}
}
}
}
5 changes: 2 additions & 3 deletions search/src/main/java/io/plaidapp/search/ui/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import io.plaidapp.core.util.Activities
import io.plaidapp.core.util.ImeUtils
import io.plaidapp.core.util.ShortcutHelper
import io.plaidapp.core.util.TransitionUtils
import io.plaidapp.core.util.event.EventObserver
import io.plaidapp.search.R
import io.plaidapp.search.dagger.Injector
import io.plaidapp.search.ui.transitions.CircularReveal
Expand Down Expand Up @@ -109,7 +108,7 @@ class SearchActivity : AppCompatActivity() {

feedAdapter = FeedAdapter(this, columns, pocketInstalled)

viewModel.searchResults.observe(this, EventObserver { searchUiModel ->
viewModel.searchResults.observe(this, Observer { searchUiModel ->
if (searchUiModel.items.isNotEmpty()) {
if (results.visibility != View.VISIBLE) {
TransitionManager.beginDelayedTransition(
Expand All @@ -120,7 +119,7 @@ class SearchActivity : AppCompatActivity() {
results.visibility = View.VISIBLE
fab.visibility = View.VISIBLE
}
val feedItems = feedAdapter.getItems().orEmpty()
val feedItems = feedAdapter.getItems()
val items = getPlaidItemsForDisplayExpanded(feedItems, searchUiModel.items, columns)
feedAdapter.setItems(items)
} else {
Expand Down
Loading

0 comments on commit a48e0f4

Please sign in to comment.