|
|
@ -21,42 +21,36 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.SearchResult
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.SearchResult
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.Topic
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.Topic
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
import kotlinx.coroutines.flow.flow
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
import kotlinx.coroutines.flow.flowOf
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.update
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.TestOnly
|
|
|
|
|
|
|
|
|
|
|
|
class TestSearchContentsRepository : SearchContentsRepository {
|
|
|
|
class TestSearchContentsRepository : SearchContentsRepository {
|
|
|
|
|
|
|
|
|
|
|
|
private val cachedTopics: MutableList<Topic> = mutableListOf()
|
|
|
|
private val cachedTopics = MutableStateFlow(emptyList<Topic>())
|
|
|
|
private val cachedNewsResources: MutableList<NewsResource> = mutableListOf()
|
|
|
|
private val cachedNewsResources = MutableStateFlow(emptyList<NewsResource>())
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun populateFtsData() = Unit
|
|
|
|
override suspend fun populateFtsData() = Unit
|
|
|
|
|
|
|
|
|
|
|
|
override fun searchContents(searchQuery: String): Flow<SearchResult> = flowOf(
|
|
|
|
override fun searchContents(searchQuery: String): Flow<SearchResult> =
|
|
|
|
SearchResult(
|
|
|
|
combine(cachedTopics, cachedNewsResources) { topics, news ->
|
|
|
|
topics = cachedTopics.filter {
|
|
|
|
SearchResult(
|
|
|
|
searchQuery in it.name || searchQuery in it.shortDescription || searchQuery in it.longDescription
|
|
|
|
topics = topics.filter {
|
|
|
|
},
|
|
|
|
searchQuery in it.name || searchQuery in it.shortDescription || searchQuery in it.longDescription
|
|
|
|
newsResources = cachedNewsResources.filter {
|
|
|
|
},
|
|
|
|
searchQuery in it.content || searchQuery in it.title
|
|
|
|
newsResources = news.filter {
|
|
|
|
},
|
|
|
|
searchQuery in it.content || searchQuery in it.title
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
override fun getSearchContentsCount(): Flow<Int> = flow {
|
|
|
|
|
|
|
|
emit(cachedTopics.size + cachedNewsResources.size)
|
|
|
|
override fun getSearchContentsCount(): Flow<Int> = combine(cachedTopics, cachedNewsResources) { topics, news -> topics.size + news.size }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@TestOnly
|
|
|
|
/**
|
|
|
|
fun addTopics(topics: List<Topic>) = cachedTopics.update { it + topics }
|
|
|
|
* Test only method to add the topics to the stored list in memory
|
|
|
|
|
|
|
|
*/
|
|
|
|
@TestOnly
|
|
|
|
fun addTopics(topics: List<Topic>) {
|
|
|
|
fun addNewsResources(newsResources: List<NewsResource>) =
|
|
|
|
cachedTopics.addAll(topics)
|
|
|
|
cachedNewsResources.update { it + newsResources }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Test only method to add the news resources to the stored list in memory
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
fun addNewsResources(newsResources: List<NewsResource>) {
|
|
|
|
|
|
|
|
cachedNewsResources.addAll(newsResources)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|