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