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 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.api.IRoomParkApi
import com.biganto.visual.roompark.data.repository.db.IDb 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.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.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.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedModel import com.biganto.visual.roompark.domain.model.FeedModel
import com.biganto.visual.roompark.domain.model.fromEntity import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import timber.log.Timber import timber.log.Timber
import timber.log.Timber.e import timber.log.Timber.e
...@@ -23,23 +23,19 @@ import javax.inject.Inject ...@@ -23,23 +23,19 @@ import javax.inject.Inject
//@Singleton //@Singleton
class FeedsContractModule @Inject constructor( class FeedsContractModule @Inject constructor(
private val api: IRoomParkApi, private val api: IRoomParkApi,
private val db: IDb, private val db: IDb
private val local:ILocalStore,
private val session:IUserSession
): FeedsContract { ): FeedsContract {
override fun fetchFeeds(): Observable<List<FeedModel>> = override fun fetchFeeds(): Observable<List<FeedModel>> =
fetchAllFeeds(session.token) fetchAllFeeds().doOnError (::e)
.doOnError (::e)
override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> { override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> =
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. 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. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
...@@ -48,8 +44,42 @@ class FeedsContractModule @Inject constructor( ...@@ -48,8 +44,42 @@ class FeedsContractModule @Inject constructor(
} }
private fun fetchFeedsApi(token:String) = private fun fetchArticlessApi(feedId:Int): Observable<List<ArticleEntity>>? {
api.getFeeds(token) 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") } .doOnNext { Timber.d("raw0 $it") }
.map(::fromRaw) .map(::fromRaw)
.doOnNext(db::blockingUpsert) .doOnNext(db::blockingUpsert)
...@@ -61,9 +91,9 @@ class FeedsContractModule @Inject constructor( ...@@ -61,9 +91,9 @@ class FeedsContractModule @Inject constructor(
.toObservable() .toObservable()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
fun fetchAllFeeds(userToken:String): Observable<List<FeedModel>> = fun fetchAllFeeds(): Observable<List<FeedModel>> =
Observable.mergeDelayError( Observable.mergeDelayError(
arrayListOf(fetchFeedsApi(userToken),fetchFeedsDb) arrayListOf(fetchFeedsApi(),fetchFeedsDb)
).map { fromEntity(it,::fromEntity) } ).map { fromEntity(it,::fromEntity) }
} }
......
...@@ -27,7 +27,7 @@ interface IRoomParkApi { ...@@ -27,7 +27,7 @@ interface IRoomParkApi {
fun getDeals(userToken: String): Observable<List<DealRaw>> fun getDeals(userToken: String): Observable<List<DealRaw>>
fun getFavorites(userToken: String): Observable<List<EstateRaw>> 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 getArticle(id: Int): Observable<ArticleRaw>
fun getPhotos(parentId: Int): Observable<List<NewsPhotoRaw>> fun getPhotos(parentId: Int): Observable<List<NewsPhotoRaw>>
fun getEstatePlanTypes(estateId: Int): Observable<List<PlanTypeRaw>> fun getEstatePlanTypes(estateId: Int): Observable<List<PlanTypeRaw>>
......
...@@ -53,7 +53,7 @@ class RetrofitRepository @Inject constructor(retrofit: Retrofit) : IRoomParkApi ...@@ -53,7 +53,7 @@ class RetrofitRepository @Inject constructor(retrofit: Retrofit) : IRoomParkApi
api.getFavorites(token = userToken) api.getFavorites(token = userToken)
.compose(RetrofitResponseValidation()) .compose(RetrofitResponseValidation())
override fun getFeeds(userToken: String): Observable<List<FeedRaw>> = override fun getFeeds(): Observable<List<FeedRaw>> =
api.getFeeds() api.getFeeds()
.compose(RetrofitResponseValidation()) .compose(RetrofitResponseValidation())
......
package com.biganto.visual.roompark.data.repository.db 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.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import io.reactivex.Observable import io.reactivex.Observable
...@@ -18,4 +19,6 @@ interface IDb { ...@@ -18,4 +19,6 @@ interface IDb {
fun fetchUser(uuid: Int): Observable<UserEntity> fun fetchUser(uuid: Int): Observable<UserEntity>
fun <T : List<Persistable>> blockingUpsert(entity: T) fun <T : List<Persistable>> blockingUpsert(entity: T)
fun userObservableResult(uuid: Int): Observable<ReactiveResult<UserEntity>>? 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 ...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.data.repository.db.requrey
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.data.repository.db.IDb 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.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.Models import com.biganto.visual.roompark.data.repository.db.requrey.model.Models
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
...@@ -13,7 +14,9 @@ import io.reactivex.Single ...@@ -13,7 +14,9 @@ import io.reactivex.Single
import io.requery.Persistable import io.requery.Persistable
import io.requery.android.sqlite.DatabaseSource import io.requery.android.sqlite.DatabaseSource
import io.requery.reactivex.KotlinReactiveEntityStore import io.requery.reactivex.KotlinReactiveEntityStore
import io.requery.reactivex.ReactiveResult
import io.requery.sql.KotlinEntityDataStore import io.requery.sql.KotlinEntityDataStore
import io.requery.sql.TableCreationMode
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
...@@ -31,6 +34,8 @@ class DbModule{ ...@@ -31,6 +34,8 @@ class DbModule{
val source = DatabaseSource(context, Models.DEFAULT, "BigantoPerfect", DATABASE_VERSION) val source = DatabaseSource(context, Models.DEFAULT, "BigantoPerfect", DATABASE_VERSION)
source.setLoggingEnabled(true) source.setLoggingEnabled(true)
source.setWriteAheadLoggingEnabled(true)
source.setTableCreationMode(TableCreationMode.DROP_CREATE)
val store = KotlinEntityDataStore<Persistable>(source.configuration) val store = KotlinEntityDataStore<Persistable>(source.configuration)
Timber.d("Kotlin store %s",source) Timber.d("Kotlin store %s",source)
...@@ -61,6 +66,19 @@ class RequeryRepository @Inject constructor( ...@@ -61,6 +66,19 @@ class RequeryRepository @Inject constructor(
override fun fetchFeeds(): Observable<FeedEntity> = override fun fetchFeeds(): Observable<FeedEntity> =
fetchAll<FeedEntity>().get().observable() 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> = override fun fetchUser(uuid:Int): Observable<UserEntity> =
fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable() fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable()
......
...@@ -22,8 +22,8 @@ interface Article : Persistable { ...@@ -22,8 +22,8 @@ interface Article : Persistable {
val preview : String? val preview : String?
@get:Nullable @get:Nullable
val body : String? val body : String?
// @get:ForeignKey(references = Feed::class, referencedColumn = "alias") @get:ForeignKey(references = Feed::class, referencedColumn = "id")
@get:ManyToOne @get:ManyToOne(cascade = [CascadeAction.NONE])
val feed : Feed? val feed : Feed?
@get:Convert(TitledPhotoListConverter::class) @get:Convert(TitledPhotoListConverter::class)
val photo : List<TitledPhoto>? val photo : List<TitledPhoto>?
......
...@@ -13,7 +13,7 @@ interface Feed : Persistable { ...@@ -13,7 +13,7 @@ interface Feed : Persistable {
@get:Key @get:Key
val id: Int val id: Int
val title:String val title:String
// @get:Key @get:Key
val alias: String val alias: String
@get:JunctionTable(name= "FeedArticlesRule") @get:JunctionTable(name= "FeedArticlesRule")
@get:OneToMany( cascade = arrayOf(CascadeAction.DELETE)) @get:OneToMany( cascade = arrayOf(CascadeAction.DELETE))
......
package com.biganto.visual.roompark.data.repository.mapper package com.biganto.visual.roompark.data.repository.mapper
import android.content.res.Resources 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.AuthRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.FeedRaw 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.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import kotlin.math.max import kotlin.math.max
...@@ -31,8 +34,39 @@ fun fromRaw(raw:FeedRaw) : FeedEntity { ...@@ -31,8 +34,39 @@ fun fromRaw(raw:FeedRaw) : FeedEntity {
return entity 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<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) val calcTargetResolution = max(display.widthPixels,display.heightPixels)
package com.biganto.visual.roompark.domain.contract 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.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedModel import com.biganto.visual.roompark.domain.model.FeedModel
import com.biganto.visual.roompark.domain.model.FeedsHeaderModel
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single
/** /**
* Created by Vladislav Bogdashkin on 24.09.2019. * Created by Vladislav Bogdashkin on 24.09.2019.
...@@ -13,6 +12,6 @@ import io.reactivex.Single ...@@ -13,6 +12,6 @@ import io.reactivex.Single
interface FeedsContract{ interface FeedsContract{
fun fetchFeeds(): Observable<List<FeedModel>> fun fetchFeeds(): Observable<List<FeedModel>>
fun fetchFeedObservable(id:Int): Observable<ArticlesPreviewModel> fun getArticle(id:Int): Observable<ArticlePreviewModel>
fun getArticle(id:Int): Single<ArticlesPreviewModel> fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel>
} }
\ No newline at end of file
...@@ -16,19 +16,20 @@ class FeedsInteractor @Inject constructor( ...@@ -16,19 +16,20 @@ class FeedsInteractor @Inject constructor(
) { ) {
fun fetchTopFeeds(): Observable<FeedsHeaderModel> = fun fetchTopFeeds(): Observable<FeedsHeaderModel> =
feedsUseCase.getFeeds().map { feedsUseCase.getFeeds().map {FeedsHeaderModel(it)}
FeedsHeaderModel(it)
}
//Single.just(testFeeds) //Single.just(testFeeds)
fun fetchArticles(feedId: Int): Single<ArticlesPreviewModel> = Single.just( fun fetchArticles(feedId: Int): Observable<ArticlesPreviewModel> =
when (feedId) { feedsUseCase.getArticles(feedId)
1 -> testNewsArticles // Single.just(
2 -> testBlogsArticles // when (feedId) {
3 -> testDevArticles // 1 -> testNewsArticles
else -> error("unknown feedId") // 2 -> testBlogsArticles
} // 3 -> testDevArticles
) // else -> error("unknown feedId")
// }
// )
fun fetchAlbums(): Single<List<AlbumPreviewModel>> = Single.just(albumsPreviews) fun fetchAlbums(): Single<List<AlbumPreviewModel>> = Single.just(albumsPreviews)
......
package com.biganto.visual.roompark.domain.model 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 com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import java.util.* import java.util.*
...@@ -39,4 +40,19 @@ fun fromEntity(entity: FeedEntity):FeedModel = FeedModel( ...@@ -39,4 +40,19 @@ fun fromEntity(entity: FeedEntity):FeedModel = FeedModel(
alias = entity.alias 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]) } 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( ...@@ -13,4 +13,9 @@ class FeedUseCase @Inject constructor(
fun getFeeds() = contract.fetchFeeds() 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( ...@@ -56,7 +56,7 @@ class FeedsScreenPresenter @Inject constructor(
} }
val getFeedArticlesPreview = intent(FeedsScreen::feedsTabSelected) val getFeedArticlesPreview = intent(FeedsScreen::feedsTabSelected)
.flatMapSingle { interactor.fetchArticles(it) } .flatMap { interactor.fetchArticles(it) }
.map { .map {
restoreModel.articles = it.articles restoreModel.articles = it.articles
FeedsScreenViewState.GetFeedArticlesPreview(it.articles.toList()) 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