Skip to content
Permalink
Browse files

Data storage logic updated

  • Loading branch information...
Naveentp committed Jun 2, 2019
1 parent 6b79dd0 commit 85eea77ef16845ab03808cc679436c2992258c79
@@ -29,6 +29,10 @@ class NewsCacheImpl(

}

override fun deleteTopHeadlines(): Completable {
return newsDatabase.articlesDao().deleteTopHeadlines()
}

override fun areArticlesCached(): Single<Boolean> {
return newsDatabase.articlesDao().getTopHeadlines().isEmpty
.map { !it }
@@ -1,9 +1,6 @@
package com.naveentp.cache.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.*
import com.naveentp.cache.model.ArticleEntity
import io.reactivex.Completable
import io.reactivex.Flowable
@@ -20,4 +17,7 @@ interface ArticlesDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveTopHeadlines(newsList: List<ArticleEntity>): Completable

@Query("Delete from Articles")
fun deleteTopHeadlines(): Completable
}
@@ -2,6 +2,7 @@ package com.naveentp.data

import com.naveentp.data.repository.NewsCache
import com.naveentp.data.store.NewsDataStoreFactory
import com.naveentp.data.store.NewsRemoteDataStore
import com.naveentp.domain.repository.NewsRepository
import com.naveentp.shared.NewsDetails
import io.reactivex.Observable
@@ -24,10 +25,19 @@ class NewsDataRepository(
Pair(network, areCached)
})
.flatMap { pair ->
newsDataStoreFactory.getDataStore(pair.first, pair.second).getTopHeadlines()
}.flatMap { newsDetails ->
newsDataStoreFactory.getCacheDataStore().saveTopHeadlines(newsDetails)
.andThen(Observable.just(newsDetails))
when (val store = newsDataStoreFactory.getDataStore(pair.first, pair.second)) {
is NewsRemoteDataStore -> {
store.getTopHeadlines()
.flatMap { newsDetails ->
newsDataStoreFactory.getCacheDataStore().deleteTopHeadlines()
.andThen(newsDataStoreFactory.getCacheDataStore().saveTopHeadlines(newsDetails))
.andThen(Observable.just(newsDetails))
}
}
else -> {
store.getTopHeadlines()
}
}
}
}
}
@@ -15,5 +15,7 @@ interface NewsCache {

fun saveTopHeadlines(newsDetails: NewsDetails): Completable

fun deleteTopHeadlines(): Completable

fun areArticlesCached(): Single<Boolean>
}
@@ -20,4 +20,8 @@ class NewsCacheDataStore(
override fun saveTopHeadlines(newsDetails: NewsDetails): Completable {
return newsCache.saveTopHeadlines(newsDetails)
}

override fun deleteTopHeadlines(): Completable {
return newsCache.deleteTopHeadlines()
}
}
@@ -13,4 +13,6 @@ interface NewsDataStore {
fun getTopHeadlines(): Observable<NewsDetails>

fun saveTopHeadlines(newsDetails: NewsDetails) : Completable

fun deleteTopHeadlines() : Completable
}
@@ -20,4 +20,8 @@ class NewsRemoteDataStore(
override fun saveTopHeadlines(newsDetails: NewsDetails): Completable {
throw UnsupportedOperationException("Not Supported")
}

override fun deleteTopHeadlines(): Completable {
throw UnsupportedOperationException("Not Supported")
}
}

0 comments on commit 85eea77

Please sign in to comment.
You can’t perform that action at this time.