Replace networkJson and FakeAssetManager with FakeNiaDataSource in all repositories

Change-Id: Ibeb9fd09bd862000794f41edcddd2cea434a9bb1
pull/441/head
Miłosz Moczkowski 2 years ago
parent e653b738d2
commit 67fea35e6d

@ -21,18 +21,13 @@ import com.google.samples.apps.nowinandroid.core.data.repository.AuthorsReposito
import com.google.samples.apps.nowinandroid.core.model.data.Author import com.google.samples.apps.nowinandroid.core.model.data.Author
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.NetworkAuthor
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.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.serialization.json.Json import javax.inject.Inject
import kotlinx.serialization.json.decodeFromStream
/** /**
* Fake implementation of the [AuthorsRepository] that returns hardcoded authors. * Fake implementation of the [AuthorsRepository] that returns hardcoded authors.
@ -42,15 +37,12 @@ import kotlinx.serialization.json.decodeFromStream
*/ */
class FakeAuthorsRepository @Inject constructor( class FakeAuthorsRepository @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,
) : AuthorsRepository { ) : AuthorsRepository {
override fun getAuthorsStream(): Flow<List<Author>> = flow { override fun getAuthorsStream(): Flow<List<Author>> = flow {
emit( emit(
assets.open(FakeDataSource.AUTHORS) datasource.getAuthors().map {
.use<InputStream, List<NetworkAuthor>>(networkJson::decodeFromStream)
.map {
Author( Author(
id = it.id, id = it.id,
name = it.name, name = it.name,
@ -61,8 +53,7 @@ class FakeAuthorsRepository @Inject constructor(
) )
} }
) )
} }.flowOn(ioDispatcher)
.flowOn(ioDispatcher)
override fun getAuthorStream(id: String): Flow<Author> { override fun getAuthorStream(id: String): Flow<Author> {
return getAuthorsStream().map { it.first { author -> author.id == id } } return getAuthorsStream().map { it.first { author -> author.id == id } }

@ -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
} }

@ -21,18 +21,13 @@ import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepositor
import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.Topic
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.NetworkTopic
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.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.serialization.json.Json import javax.inject.Inject
import kotlinx.serialization.json.decodeFromStream
/** /**
* Fake implementation of the [TopicsRepository] that retrieves the topics from a JSON String, and * Fake implementation of the [TopicsRepository] that retrieves the topics from a JSON String, and
@ -43,14 +38,11 @@ import kotlinx.serialization.json.decodeFromStream
*/ */
class FakeTopicsRepository @Inject constructor( class FakeTopicsRepository @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,
) : TopicsRepository { ) : TopicsRepository {
override fun getTopicsStream(): Flow<List<Topic>> = flow { override fun getTopicsStream(): Flow<List<Topic>> = flow {
emit( emit(
assets.open(FakeDataSource.TOPICS) datasource.getTopics().map {
.use<InputStream, List<NetworkTopic>>(networkJson::decodeFromStream)
.map {
Topic( Topic(
id = it.id, id = it.id,
name = it.name, name = it.name,
@ -61,8 +53,7 @@ class FakeTopicsRepository @Inject constructor(
) )
} }
) )
} }.flowOn(ioDispatcher)
.flowOn(ioDispatcher)
override fun getTopic(id: String): Flow<Topic> { override fun getTopic(id: String): Flow<Topic> {
return getTopicsStream().map { it.first { topic -> topic.id == id } } return getTopicsStream().map { it.first { topic -> topic.id == id } }

Loading…
Cancel
Save