parent
b5ea108609
commit
9c83cb7412
@ -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
|
||||
}
|
||||
}
|
@ -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>
|
||||
}
|
Loading…
Reference in new issue