Commit 54b1a51a authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fetch articles for feed_id == 1

parent 09ccdde3
package com.biganto.visual.roompark.data.data_provider
import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.data.repository.api.IRoomParkApi
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.data.service.user_session.IUserSession
import com.biganto.visual.roompark.domain.contract.FeedsContract
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.model.ArticlePreviewModel
import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import timber.log.Timber.e
......@@ -23,23 +23,19 @@ import javax.inject.Inject
//@Singleton
class FeedsContractModule @Inject constructor(
private val api: IRoomParkApi,
private val db: IDb,
private val local:ILocalStore,
private val session:IUserSession
private val db: IDb
): FeedsContract {
override fun fetchFeeds(): Observable<List<FeedModel>> =
fetchAllFeeds(session.token)
.doOnError (::e)
fetchAllFeeds().doOnError (::e)
override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> =
fetchArticles(id)
override fun getArticle(id: Int): Single<ArticlesPreviewModel> {
override fun getArticle(id: Int): Observable<ArticlePreviewModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
......@@ -48,8 +44,42 @@ class FeedsContractModule @Inject constructor(
}
private fun fetchFeedsApi(token:String) =
api.getFeeds(token)
private fun fetchArticlessApi(feedId:Int): Observable<List<ArticleEntity>>? {
val feed = db.getFeed(feedId)?.firstOrNull()
if (feed == null) {
e("Unknown feedId: $feedId - cann't resolve feed_alias!")
throw CustomApiException.UnknownCustomApiException(-1,null,"Unknown Feed!")
}
else {
return api.getArticlesPage(feed.alias, 11, 1)
.doOnNext { Timber.d("raw0 $it") }
.map { it.items }
.map { fromRaw(it, feedId, feed.alias) }
.doOnNext(db::blockingUpsert)
.subscribeOn(Schedulers.io())
}
}
private fun fetchArticlesDb(feedId:Int): Observable<MutableList<ArticleEntity>>? {
val feed = db.getFeed(feedId)?.firstOrNull()
if (feed == null) {
e("Unknown feedId: $feedId - cann't resolve feed_alias!")
throw CustomApiException.UnknownCustomApiException(-1, null, "Unknown Feed!")
} else return db.fetchArticles(feed.alias, 11, 1)
.toList()
.toObservable()
.subscribeOn(Schedulers.io())
}
private fun fetchArticles(feedId:Int): Observable<ArticlesPreviewModel> =
Observable.mergeDelayError(
arrayListOf(fetchArticlessApi(feedId),fetchArticlesDb(feedId))
).map { fromEntity(feedId,it) }
private fun fetchFeedsApi() =
api.getFeeds()
.doOnNext { Timber.d("raw0 $it") }
.map(::fromRaw)
.doOnNext(db::blockingUpsert)
......@@ -61,9 +91,9 @@ class FeedsContractModule @Inject constructor(
.toObservable()
.subscribeOn(Schedulers.io())
fun fetchAllFeeds(userToken:String): Observable<List<FeedModel>> =
fun fetchAllFeeds(): Observable<List<FeedModel>> =
Observable.mergeDelayError(
arrayListOf(fetchFeedsApi(userToken),fetchFeedsDb)
arrayListOf(fetchFeedsApi(),fetchFeedsDb)
).map { fromEntity(it,::fromEntity) }
}
......
......@@ -27,7 +27,7 @@ interface IRoomParkApi {
fun getDeals(userToken: String): Observable<List<DealRaw>>
fun getFavorites(userToken: String): Observable<List<EstateRaw>>
fun getFeeds(userToken: String): Observable<List<FeedRaw>>
fun getFeeds(): Observable<List<FeedRaw>>
fun getArticle(id: Int): Observable<ArticleRaw>
fun getPhotos(parentId: Int): Observable<List<NewsPhotoRaw>>
fun getEstatePlanTypes(estateId: Int): Observable<List<PlanTypeRaw>>
......
......@@ -53,7 +53,7 @@ class RetrofitRepository @Inject constructor(retrofit: Retrofit) : IRoomParkApi
api.getFavorites(token = userToken)
.compose(RetrofitResponseValidation())
override fun getFeeds(userToken: String): Observable<List<FeedRaw>> =
override fun getFeeds(): Observable<List<FeedRaw>> =
api.getFeeds()
.compose(RetrofitResponseValidation())
......
package com.biganto.visual.roompark.data.repository.db
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import io.reactivex.Observable
......@@ -18,4 +19,6 @@ interface IDb {
fun fetchUser(uuid: Int): Observable<UserEntity>
fun <T : List<Persistable>> blockingUpsert(entity: T)
fun userObservableResult(uuid: Int): Observable<ReactiveResult<UserEntity>>?
fun fetchArticles(feedAlias: String, pageSize: Int, startIndex: Int): Observable<ArticleEntity>
fun getFeed(feedId: Int): ReactiveResult<FeedEntity>?
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.data.repository.db.requrey
import android.content.Context
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.Models
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
......@@ -13,7 +14,9 @@ import io.reactivex.Single
import io.requery.Persistable
import io.requery.android.sqlite.DatabaseSource
import io.requery.reactivex.KotlinReactiveEntityStore
import io.requery.reactivex.ReactiveResult
import io.requery.sql.KotlinEntityDataStore
import io.requery.sql.TableCreationMode
import timber.log.Timber
import javax.inject.Inject
......@@ -31,6 +34,8 @@ class DbModule{
val source = DatabaseSource(context, Models.DEFAULT, "BigantoPerfect", DATABASE_VERSION)
source.setLoggingEnabled(true)
source.setWriteAheadLoggingEnabled(true)
source.setTableCreationMode(TableCreationMode.DROP_CREATE)
val store = KotlinEntityDataStore<Persistable>(source.configuration)
Timber.d("Kotlin store %s",source)
......@@ -61,6 +66,19 @@ class RequeryRepository @Inject constructor(
override fun fetchFeeds(): Observable<FeedEntity> =
fetchAll<FeedEntity>().get().observable()
override fun getFeed(feedId:Int): ReactiveResult<FeedEntity>? =
fetchAll<FeedEntity>().where(FeedEntity.ID.eq(feedId))
.get()
override fun fetchArticles(feedAlias:String,pageSize:Int,startIndex:Int)
: Observable<ArticleEntity> =
fetchAll<ArticleEntity>()
.where(ArticleEntity.FEED_ID.eq(1))
.orderBy(ArticleEntity.PUBLISHED.desc())
.limit(pageSize)
.offset(startIndex)
.get().observable()
override fun fetchUser(uuid:Int): Observable<UserEntity> =
fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable()
......
......@@ -22,8 +22,8 @@ interface Article : Persistable {
val preview : String?
@get:Nullable
val body : String?
// @get:ForeignKey(references = Feed::class, referencedColumn = "alias")
@get:ManyToOne
@get:ForeignKey(references = Feed::class, referencedColumn = "id")
@get:ManyToOne(cascade = [CascadeAction.NONE])
val feed : Feed?
@get:Convert(TitledPhotoListConverter::class)
val photo : List<TitledPhoto>?
......
......@@ -13,7 +13,7 @@ interface Feed : Persistable {
@get:Key
val id: Int
val title:String
// @get:Key
@get:Key
val alias: String
@get:JunctionTable(name= "FeedArticlesRule")
@get:OneToMany( cascade = arrayOf(CascadeAction.DELETE))
......
package com.biganto.visual.roompark.data.repository.mapper
import android.content.res.Resources
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ArticleRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.AuthRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.FeedRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.NewsArticleRaw
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import kotlin.math.max
......@@ -31,8 +34,39 @@ fun fromRaw(raw:FeedRaw) : FeedEntity {
return entity
}
fun fromRaw(raw:ArticleRaw) : ArticleEntity {
val entity = ArticleEntity()
entity.setId(raw.id)
entity.setTitle(raw.title)
entity.setBody(raw.body)
entity.setPreview(raw.feed_alias)
val feed = FeedEntity()
feed.setAlias(raw.feed_alias)
entity.setFeed(feed)
return entity
}
fun fromRaw(raw:NewsArticleRaw,feedId:Int,feedAlias:String):ArticleEntity{
val entity = ArticleEntity()
entity.setId(raw.id)
entity.setPreview("https://room-park.ru${raw.preview}")
entity.setAnnounce(raw.announce)
entity.setPublished(raw.published)
entity.setTitle(raw.title)
val feed = FeedEntity()
feed.setId(feedId)
// feed.setAlias(feedAlias)
entity.setFeed(feed)
return entity
}
fun fromRaw(raw: List<FeedRaw>):List<FeedEntity> = List(raw.size) { index-> fromRaw(raw[index]) }
fun fromRaw(raw: List<NewsArticleRaw>,feedId:Int,feedAlias:String):List<ArticleEntity> =
List(raw.size) { index-> fromRaw(raw[index],feedId,feedAlias) }
val calcTargetResolution = max(display.widthPixels,display.heightPixels)
package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.domain.model.ArticlePreviewModel
import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedModel
import com.biganto.visual.roompark.domain.model.FeedsHeaderModel
import io.reactivex.Observable
import io.reactivex.Single
/**
* Created by Vladislav Bogdashkin on 24.09.2019.
......@@ -13,6 +12,6 @@ import io.reactivex.Single
interface FeedsContract{
fun fetchFeeds(): Observable<List<FeedModel>>
fun fetchFeedObservable(id:Int): Observable<ArticlesPreviewModel>
fun getArticle(id:Int): Single<ArticlesPreviewModel>
fun getArticle(id:Int): Observable<ArticlePreviewModel>
fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel>
}
\ No newline at end of file
......@@ -16,19 +16,20 @@ class FeedsInteractor @Inject constructor(
) {
fun fetchTopFeeds(): Observable<FeedsHeaderModel> =
feedsUseCase.getFeeds().map {
FeedsHeaderModel(it)
}
feedsUseCase.getFeeds().map {FeedsHeaderModel(it)}
//Single.just(testFeeds)
fun fetchArticles(feedId: Int): Single<ArticlesPreviewModel> = Single.just(
when (feedId) {
1 -> testNewsArticles
2 -> testBlogsArticles
3 -> testDevArticles
else -> error("unknown feedId")
}
)
fun fetchArticles(feedId: Int): Observable<ArticlesPreviewModel> =
feedsUseCase.getArticles(feedId)
// Single.just(
// when (feedId) {
// 1 -> testNewsArticles
// 2 -> testBlogsArticles
// 3 -> testDevArticles
// else -> error("unknown feedId")
// }
// )
fun fetchAlbums(): Single<List<AlbumPreviewModel>> = Single.just(albumsPreviews)
......
package com.biganto.visual.roompark.domain.model
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import java.util.*
......@@ -39,4 +40,19 @@ fun fromEntity(entity: FeedEntity):FeedModel = FeedModel(
alias = entity.alias
)
fun fromEntity(entity: ArticleEntity):ArticlePreviewModel = ArticlePreviewModel(
articleId = entity.id,
published = entity.published,
title = entity.title,
announce = entity.announce?:"",
previewUrl = entity.preview?:"",
isRead = false
)
fun fromEntity(parentId:Int,entity: List<ArticleEntity>):ArticlesPreviewModel =
ArticlesPreviewModel(
parentFeedId = parentId,
articles = fromEntity(entity,::fromEntity)
)
fun <E,M> fromEntity(raw: List<E>,block:(E)->M):List<M> = List(raw.size) { index-> block(raw[index]) }
......@@ -13,4 +13,9 @@ class FeedUseCase @Inject constructor(
fun getFeeds() = contract.fetchFeeds()
fun getArticles(feedId:Int) =
contract.fetchFeedObservable(feedId)
}
\ No newline at end of file
......@@ -56,7 +56,7 @@ class FeedsScreenPresenter @Inject constructor(
}
val getFeedArticlesPreview = intent(FeedsScreen::feedsTabSelected)
.flatMapSingle { interactor.fetchArticles(it) }
.flatMap { interactor.fetchArticles(it) }
.map {
restoreModel.articles = it.articles
FeedsScreenViewState.GetFeedArticlesPreview(it.articles.toList())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment