|
|
|
@ -24,17 +24,13 @@ import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.fake.FakeDataSource
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
|
|
|
|
|
import java.io.InputStream
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
import kotlinx.coroutines.CoroutineDispatcher
|
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
|
import kotlinx.coroutines.flow.flow
|
|
|
|
|
import kotlinx.coroutines.flow.flowOn
|
|
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
import kotlinx.serialization.json.decodeFromStream
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fake implementation of the [NewsRepository] that retrieves the news resources from a JSON String.
|
|
|
|
@ -44,20 +40,17 @@ import kotlinx.serialization.json.decodeFromStream
|
|
|
|
|
*/
|
|
|
|
|
class FakeNewsRepository @Inject constructor(
|
|
|
|
|
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
|
|
|
|
|
private val networkJson: Json,
|
|
|
|
|
private val assets: FakeAssetManager,
|
|
|
|
|
private val datasource: FakeNiaNetworkDataSource
|
|
|
|
|
) : NewsRepository {
|
|
|
|
|
|
|
|
|
|
override fun getNewsResourcesStream(): Flow<List<NewsResource>> =
|
|
|
|
|
flow {
|
|
|
|
|
emit(
|
|
|
|
|
assets.open(FakeDataSource.DATA)
|
|
|
|
|
.use<InputStream, List<NetworkNewsResource>>(networkJson::decodeFromStream)
|
|
|
|
|
datasource.getNewsResources()
|
|
|
|
|
.map(NetworkNewsResource::asEntity)
|
|
|
|
|
.map(NewsResourceEntity::asExternalModel)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
.flowOn(ioDispatcher)
|
|
|
|
|
}.flowOn(ioDispatcher)
|
|
|
|
|
|
|
|
|
|
override fun getNewsResourcesStream(
|
|
|
|
|
filterAuthorIds: Set<String>,
|
|
|
|
@ -65,18 +58,16 @@ class FakeNewsRepository @Inject constructor(
|
|
|
|
|
): Flow<List<NewsResource>> =
|
|
|
|
|
flow {
|
|
|
|
|
emit(
|
|
|
|
|
assets.open(FakeDataSource.DATA).use { stream ->
|
|
|
|
|
networkJson.decodeFromStream<List<NetworkNewsResource>>(stream)
|
|
|
|
|
.filter {
|
|
|
|
|
it.authors.intersect(filterAuthorIds).isNotEmpty() ||
|
|
|
|
|
it.topics.intersect(filterTopicIds).isNotEmpty()
|
|
|
|
|
}
|
|
|
|
|
.map(NetworkNewsResource::asEntity)
|
|
|
|
|
.map(NewsResourceEntity::asExternalModel)
|
|
|
|
|
}
|
|
|
|
|
datasource
|
|
|
|
|
.getNewsResources()
|
|
|
|
|
.filter {
|
|
|
|
|
it.authors.intersect(filterAuthorIds).isNotEmpty() ||
|
|
|
|
|
it.topics.intersect(filterTopicIds).isNotEmpty()
|
|
|
|
|
}
|
|
|
|
|
.map(NetworkNewsResource::asEntity)
|
|
|
|
|
.map(NewsResourceEntity::asExternalModel)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
.flowOn(ioDispatcher)
|
|
|
|
|
}.flowOn(ioDispatcher)
|
|
|
|
|
|
|
|
|
|
override suspend fun syncWith(synchronizer: Synchronizer) = true
|
|
|
|
|
}
|
|
|
|
|