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.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<List<Author>> = flow {
emit(
assets.open(FakeDataSource.AUTHORS)
.use<InputStream, List<NetworkAuthor>>(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<Author> {
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.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
}

@ -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<List<Topic>> = flow {
emit(
assets.open(FakeDataSource.TOPICS)
.use<InputStream, List<NetworkTopic>>(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<Topic> {
return getTopicsStream().map { it.first { topic -> topic.id == id } }

Loading…
Cancel
Save