From 67fea35e6db12993d4b577312649218d368d5fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Moczkowski?= Date: Fri, 18 Nov 2022 12:18:57 +0100 Subject: [PATCH] Replace networkJson and FakeAssetManager with FakeNiaDataSource in all repositories Change-Id: Ibeb9fd09bd862000794f41edcddd2cea434a9bb1 --- .../repository/fake/FakeAuthorsRepository.kt | 37 +++++++------------ .../repository/fake/FakeNewsRepository.kt | 37 +++++++------------ .../repository/fake/FakeTopicsRepository.kt | 37 +++++++------------ 3 files changed, 42 insertions(+), 69 deletions(-) diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeAuthorsRepository.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeAuthorsRepository.kt index 566214dd7..7518e5fca 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeAuthorsRepository.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeAuthorsRepository.kt @@ -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.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.model.NetworkAuthor -import java.io.InputStream -import javax.inject.Inject +import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromStream +import javax.inject.Inject /** * Fake implementation of the [AuthorsRepository] that returns hardcoded authors. @@ -42,27 +37,23 @@ import kotlinx.serialization.json.decodeFromStream */ class FakeAuthorsRepository @Inject constructor( @Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher, - private val networkJson: Json, - private val assets: FakeAssetManager, + private val datasource: FakeNiaNetworkDataSource ) : AuthorsRepository { override fun getAuthorsStream(): Flow> = flow { emit( - assets.open(FakeDataSource.AUTHORS) - .use>(networkJson::decodeFromStream) - .map { - Author( - id = it.id, - name = it.name, - imageUrl = it.imageUrl, - twitter = it.twitter, - mediumPage = it.mediumPage, - bio = it.bio, - ) - } + datasource.getAuthors().map { + Author( + id = it.id, + name = it.name, + imageUrl = it.imageUrl, + twitter = it.twitter, + mediumPage = it.mediumPage, + bio = it.bio, + ) + } ) - } - .flowOn(ioDispatcher) + }.flowOn(ioDispatcher) override fun getAuthorStream(id: String): Flow { return getAuthorsStream().map { it.first { author -> author.id == id } } diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeNewsRepository.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeNewsRepository.kt index e9d965ae6..99261a289 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeNewsRepository.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeNewsRepository.kt @@ -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> = flow { emit( - assets.open(FakeDataSource.DATA) - .use>(networkJson::decodeFromStream) + datasource.getNewsResources() .map(NetworkNewsResource::asEntity) .map(NewsResourceEntity::asExternalModel) ) - } - .flowOn(ioDispatcher) + }.flowOn(ioDispatcher) override fun getNewsResourcesStream( filterAuthorIds: Set, @@ -65,18 +58,16 @@ class FakeNewsRepository @Inject constructor( ): Flow> = flow { emit( - assets.open(FakeDataSource.DATA).use { stream -> - networkJson.decodeFromStream>(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 } diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt index d8cdaa51a..57d1e951c 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt @@ -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.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.model.NetworkTopic -import java.io.InputStream -import javax.inject.Inject +import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromStream +import javax.inject.Inject /** * Fake implementation of the [TopicsRepository] that retrieves the topics from a JSON String, and @@ -43,26 +38,22 @@ import kotlinx.serialization.json.decodeFromStream */ class FakeTopicsRepository @Inject constructor( @Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher, - private val networkJson: Json, - private val assets: FakeAssetManager, + private val datasource: FakeNiaNetworkDataSource ) : TopicsRepository { override fun getTopicsStream(): Flow> = flow { emit( - assets.open(FakeDataSource.TOPICS) - .use>(networkJson::decodeFromStream) - .map { - Topic( - id = it.id, - name = it.name, - shortDescription = it.shortDescription, - longDescription = it.longDescription, - url = it.url, - imageUrl = it.imageUrl - ) - } + datasource.getTopics().map { + Topic( + id = it.id, + name = it.name, + shortDescription = it.shortDescription, + longDescription = it.longDescription, + url = it.url, + imageUrl = it.imageUrl + ) + } ) - } - .flowOn(ioDispatcher) + }.flowOn(ioDispatcher) override fun getTopic(id: String): Flow { return getTopicsStream().map { it.first { topic -> topic.id == id } }