Add sync method to repository defs

Change-Id: Idfe402dde440e8dce47a03fbbc1d827744997e1b
pull/2/head
Adetunji Dahunsi 3 years ago
parent b5ea108609
commit 9c83cb7412

@ -16,10 +16,10 @@
package com.google.samples.apps.nowinandroid.core.domain.di package com.google.samples.apps.nowinandroid.core.domain.di
import com.google.samples.apps.nowinandroid.core.domain.repository.FakeNewsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.FakeTopicsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.fake.FakeNewsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.fake.FakeTopicsRepository
import dagger.Binds import dagger.Binds
import dagger.Module import dagger.Module
import dagger.hilt.InstallIn import dagger.hilt.InstallIn

@ -32,4 +32,10 @@ interface NewsRepository {
* Returns available news resources as a stream filtered by the topic. * Returns available news resources as a stream filtered by the topic.
*/ */
fun getNewsResourcesStream(filterTopicIds: Set<Int>): Flow<List<NewsResource>> fun getNewsResourcesStream(filterTopicIds: Set<Int>): Flow<List<NewsResource>>
/**
* Synchronizes the local database in backing the repository with the network.
* Returns if the sync was successful or not.
*/
suspend fun sync(): Boolean
} }

@ -0,0 +1,66 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.domain.repository
import com.google.samples.apps.nowinandroid.core.database.dao.TopicDao
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
import com.google.samples.apps.nowinandroid.core.datastore.NiaPreferences
import com.google.samples.apps.nowinandroid.core.domain.model.asEntity
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.network.NiANetwork
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
import javax.inject.Inject
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
/**
* Room database backed implementation of the [TopicsRepository].
*/
class RoomTopicsRepository @Inject constructor(
private val dispatchers: NiaDispatchers,
private val topicDao: TopicDao,
private val network: NiANetwork,
private val niaPreferences: NiaPreferences
) : TopicsRepository {
override fun getTopicsStream(): Flow<List<Topic>> =
topicDao.getTopicEntitiesStream()
.map { it.map(TopicEntity::asExternalModel) }
override suspend fun setFollowedTopicIds(followedTopicIds: Set<Int>) =
niaPreferences.setFollowedTopicIds(followedTopicIds)
override suspend fun toggleFollowedTopicId(followedTopicId: Int, followed: Boolean) =
niaPreferences.toggleFollowedTopicId(followedTopicId, followed)
override fun getFollowedTopicIdsStream() = niaPreferences.followedTopicIds
override suspend fun sync(): Boolean = try {
topicDao.saveTopics(
network.getTopics()
.map(NetworkTopic::asEntity)
)
true
} catch (cancellationException: CancellationException) {
throw cancellationException
} catch (exception: Exception) {
false
}
}

@ -39,4 +39,10 @@ interface TopicsRepository {
* Returns the users currently followed topics * Returns the users currently followed topics
*/ */
fun getFollowedTopicIdsStream(): Flow<Set<Int>> fun getFollowedTopicIdsStream(): Flow<Set<Int>>
/**
* Synchronizes the local database in backing the repository with the network.
* Returns if the sync was successful or not.
*/
suspend fun sync(): Boolean
} }

@ -14,8 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.samples.apps.nowinandroid.core.domain.repository package com.google.samples.apps.nowinandroid.core.domain.repository.fake
import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository
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.NiaDispatchers import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers
import javax.inject.Inject import javax.inject.Inject
@ -39,4 +40,6 @@ class FakeNewsRepository @Inject constructor(
override fun getNewsResourcesStream(filterTopicIds: Set<Int>): Flow<List<NewsResource>> = override fun getNewsResourcesStream(filterTopicIds: Set<Int>): Flow<List<NewsResource>> =
flowOf(emptyList()) flowOf(emptyList())
override suspend fun sync() = true
} }

@ -14,9 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.samples.apps.nowinandroid.core.domain.repository package com.google.samples.apps.nowinandroid.core.domain.repository.fake
import com.google.samples.apps.nowinandroid.core.datastore.NiaPreferences import com.google.samples.apps.nowinandroid.core.datastore.NiaPreferences
import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository
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.NiaDispatchers import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers
import com.google.samples.apps.nowinandroid.core.network.fake.FakeDataSource import com.google.samples.apps.nowinandroid.core.network.fake.FakeDataSource
@ -60,4 +61,6 @@ class FakeTopicsRepository @Inject constructor(
niaPreferences.toggleFollowedTopicId(followedTopicId, followed) niaPreferences.toggleFollowedTopicId(followedTopicId, followed)
override fun getFollowedTopicIdsStream() = niaPreferences.followedTopicIds override fun getFollowedTopicIdsStream() = niaPreferences.followedTopicIds
override suspend fun sync() = true
} }

@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.core.domain.repository package com.google.samples.apps.nowinandroid.core.domain.repository
import com.google.samples.apps.nowinandroid.core.domain.repository.fake.FakeNewsRepository
import com.google.samples.apps.nowinandroid.core.network.DefaultNiaDispatchers import com.google.samples.apps.nowinandroid.core.network.DefaultNiaDispatchers
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.junit.Before import org.junit.Before

@ -46,6 +46,9 @@ dependencies {
implementation libs.kotlinx.serialization.json implementation libs.kotlinx.serialization.json
implementation libs.kotlinx.datetime implementation libs.kotlinx.datetime
implementation libs.okhttp.logging
implementation libs.retrofit
implementation libs.hilt.android implementation libs.hilt.android
kapt libs.hilt.compiler kapt libs.hilt.compiler
} }

@ -0,0 +1,29 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network.retrofit
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
import retrofit2.http.GET
interface RetrofitNiANetwork {
@GET(value = "/topics")
suspend fun getTopics(): List<NetworkTopic>
@GET(value = "/newsresources")
suspend fun getNewsResources(): List<NetworkNewsResource>
}

@ -46,4 +46,6 @@ class TestNewsRepository : NewsRepository {
fun sendNewsResources(newsResources: List<NewsResource>) { fun sendNewsResources(newsResources: List<NewsResource>) {
newsResourcesFlow.tryEmit(newsResources) newsResourcesFlow.tryEmit(newsResources)
} }
override suspend fun sync(): Boolean = true
} }

@ -63,4 +63,6 @@ class TestTopicsRepository : TopicsRepository {
* A test-only API to allow querying the current followed topics. * A test-only API to allow querying the current followed topics.
*/ */
fun getCurrentFollowedTopics(): Set<Int>? = _followedTopicIds.replayCache.firstOrNull() fun getCurrentFollowedTopics(): Set<Int>? = _followedTopicIds.replayCache.firstOrNull()
override suspend fun sync(): Boolean = true
} }

@ -26,8 +26,10 @@ ksp = "1.6.10-1.0.2"
ktlint = "0.43.0" ktlint = "0.43.0"
material3 = "1.5.0-alpha05" material3 = "1.5.0-alpha05"
mockk = "1.12.1" mockk = "1.12.1"
okhttp = "4.9.3"
protobuf = "3.19.1" protobuf = "3.19.1"
protobufPlugin = "0.8.18" protobufPlugin = "0.8.18"
retrofit = "2.9.0"
room = "2.4.1" room = "2.4.1"
spotless = "6.0.0" spotless = "6.0.0"
turbine = "0.7.0" turbine = "0.7.0"
@ -71,9 +73,11 @@ kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime",
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
material3 = { group = "com.google.android.material", name = "material", version.ref = "material3" } material3 = { group = "com.google.android.material", name = "material", version.ref = "material3" }
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" } mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
okhttp-logging = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp" }
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" } protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" } protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" } turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }
retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" } room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" } room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" } room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }

Loading…
Cancel
Save