Commit 80becc0d authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

deals list merge

parent 0c141bec
......@@ -11,7 +11,6 @@ import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.data.repository.mapper.fromRawList
import com.biganto.visual.roompark.domain.contract.DealContract
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.model.DealModel
import com.biganto.visual.roompark.domain.model.EstateModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Completable
......@@ -51,7 +50,6 @@ class EstateRepository @Inject constructor(
estate.setFavorite(true)
estate.user = user
}
dbList
.filterNotNull()
.filter { dbFav ->!apiList.map{it.id}.contains(dbFav.id) }
......@@ -63,13 +61,8 @@ class EstateRepository @Inject constructor(
.map { it.toList() }
.doOnNext{ db.refreshUser(user) }
override fun fetchDeals(user:UserEntity): Observable<List<EstateEntity>> =
api.getDeals(user.authToken)//api.getDeals(user.authToken)
.doOnError(Timber::e)
.map { List(it.size){index -> fromRaw(it[index],user)} }
.flatMap(db::upsertDeals)
.map { it.map {deal -> deal.estate as EstateEntity } }
.doOnNext { db.refreshUser(user) }
override fun fetchDeals(user:UserEntity): Observable<List<DealEntity>> =
getDealsApi(user)
private fun fetchEstateDb(id: Int) = db.getEstate(id)
......@@ -80,23 +73,29 @@ class EstateRepository @Inject constructor(
private val getDealsApi: Observable<List<DealEntity>> =
local.recentUser()
.doOnError (Timber::e)
.flatMap {
when (it) {
is UserState.Authenticated -> db.fetchUser(it.uuid.toInt())
else -> throw CustomApiException.NotAuthorizedException()
}
}
.doOnError (Timber::e)
.flatMap { user ->
api.getDeals(user.authToken)//api.getDeals(user.authToken)
override fun getDealsApi(user: UserEntity): Observable<List<DealEntity>> =
Observables.zip(
api.getDeals(user.authToken)
.doOnError(Timber::e)
.map { List(it.size){index -> fromRaw(it[index],user)} }
,db.getUserDeals(user.uuid)
.toList().toObservable()
.doOnError(Timber::e)
){apiList,dbList ->
apiList.forEach { deal ->
dbList?.firstOrNull { dbDeal-> dbDeal?.id == deal.id }?.let {
deal.setRead(it.read)
}
deal.user = user
}
dbList
.filterNotNull()
.filter { dbFav ->!apiList.map{it.id}.contains(dbFav.id) }
.let{ db.deleteDeal(it) }
return@zip apiList
}
.doOnNext(db::blockingUpsert)
.doOnNext { db.refreshUser(user) }
}
private val getDealsDb: Observable<List<DealEntity>> =
local.recentUser()
......@@ -109,21 +108,11 @@ class EstateRepository @Inject constructor(
.map { it.deals?.map {deal -> deal as DealEntity } }
override fun getDeals(): Observable<List<DealModel>> {
return Observable.mergeDelayError(
arrayListOf(
getDealsDb,
getDealsApi
)
)
.map { fromEntity(it, ::fromEntity) }
.doOnError(Timber::e)
.subscribeOn (Schedulers.io())
}
override fun setDealRead(dealId: String): Completable =
db.setDealReadState(dealId,true)
.doOnError { Timber.e(it) }
......
......@@ -66,10 +66,8 @@ class SubscriptionRepository @Inject constructor(
val newSubList = mutableListOf<SubscriptionEntity>()
apiSubs.forEach { apiSub ->
Timber.d("api sub state: ${apiSub.active}")
val cachedSub =
userSubs?.firstOrNull { s ->
Timber.w("hehe ${s.topic} == ${apiSub.topic} && ${s.number} == ${apiSub.estate_id}")
s.topic == apiSub.topic && s.number == apiSub.estate_id
}
as SubscriptionEntity?
......@@ -88,7 +86,6 @@ class SubscriptionRepository @Inject constructor(
db.deleteSubscriptions(it)
}
Timber.w("to newSubList; ${newSubList}")
return db.upsert(newSubList)
.map {list -> list.map {
fromEntity(
......
......@@ -80,4 +80,7 @@ interface IDb {
fun deleteSubscriptions(entities: List<Subscription>)
fun deleteEstate(entity: List<EstateEntity>)
fun deleteEstate(entity: EstateEntity)
fun getUserDeals(uuid: Int): Observable<DealEntity?>
fun deleteDeal(entity: DealEntity)
fun deleteDeal(entity: List<DealEntity>)
}
\ No newline at end of file
......@@ -196,6 +196,11 @@ class RequeryRepository @Inject constructor(
.and(EstateEntity.FAVORITE.eq(true))
.get().observable()
override fun getUserDeals(uuid: Int): Observable<DealEntity?> =
store.select(DealEntity::class)
.where(DealEntity.USER_ID.eq(uuid))
.get().observable()
override fun setArticleReadState(id:Int,state:Boolean): Completable =
store.update(ArticleEntity::class)
.set(ArticleEntity.READ,state)
......@@ -411,8 +416,12 @@ class RequeryRepository @Inject constructor(
override fun deleteEstate(entity:EstateEntity) = deleteBlocking(entity)
override fun deleteDeal(entity:DealEntity) = deleteBlocking(entity)
override fun deleteEstate(entity:List<EstateEntity>) = deleteBlocking(entity)
override fun deleteDeal(entity:List<DealEntity>) = deleteBlocking(entity)
override fun deleteFile(entity:FileEntity) = deleteBlocking(entity)
override fun deleteFiles(entity:List<FileEntity>) = deleteBlocking(entity)
......
package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.data.repository.db.requrey.model.DealEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.EstateEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.domain.model.DealModel
import com.biganto.visual.roompark.domain.model.EstateModel
import io.reactivex.Completable
import io.reactivex.Observable
......@@ -16,9 +16,9 @@ interface DealContract{
// fun getFavorites() : Observable<List<EstateModel>>
fun getEstate(estateId: Int): Observable<EstateModel>
fun getDeals(): Observable<List<DealModel>>
fun fetchEstate(building: Int, number: Int): Observable<EstateModel>
fun setDealRead(dealId: String): Completable
fun fetchDeals(user: UserEntity): Observable<List<EstateEntity>>
fun fetchDeals(user: UserEntity): Observable<List<DealEntity>>
fun fetchFavorites(user: UserEntity): Observable<List<EstateEntity>>
fun getDealsApi(user: UserEntity): Observable<List<DealEntity>>
}
\ No newline at end of file
......@@ -20,8 +20,7 @@ class DealInteractor @Inject constructor(
){
fun getDeal(id:String): Observable<DealModel> =
useCase.getDeals()
.doOnNext { Timber.d("$it") }
useCase.prefetchDeal()
.map {deals -> deals.first { it.id==id } }
fun getStatusList() = Observable.just(statusList.sortedBy{ it.orderId })
......
package com.biganto.visual.roompark.domain.interactor
import com.biganto.visual.roompark.domain.model.DealModel
import com.biganto.visual.roompark.domain.model.StatusModel
import com.biganto.visual.roompark.domain.use_case.DealseUseCase
import io.reactivex.Observable
......@@ -13,7 +14,9 @@ class DealsInteractor @Inject constructor(
val useCase:DealseUseCase
){
fun fetchDeals() = useCase.getDeals()
fun fetchDeals(): Observable<List<DealModel>> = useCase.prefetchDeal()
fun getDealsApi(): Observable<List<DealModel>> = useCase.getDeals()
// Single.just(arrayListOf(dealFlat, dealParkign))
fun getStatusList() = Observable.just(statusList.sortedBy{ it.orderId })
......
package com.biganto.visual.roompark.domain.use_case
import com.biganto.visual.roompark.data.repository.db.requrey.model.DealEntity
import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.contract.DealContract
import com.biganto.visual.roompark.domain.model.DealModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable
import timber.log.Timber
import javax.inject.Inject
/**
......@@ -8,10 +14,23 @@ import javax.inject.Inject
*/
class DealseUseCase @Inject constructor(
private val contract: DealContract
private val contract: DealContract,
private val authContract: AuthContract
) {
fun getDeals() = contract.getDeals()
fun prefetchDeal(): Observable<List<DealModel>> =
authContract.currentUser()
.map {user ->
user.deals?.asSequence()
?.map { it as DealEntity }
?.filterNotNull()?.toList()
}
.map { fromEntity(it, ::fromEntity) }
fun getDeals(): Observable<List<DealModel>> =
authContract.currentUser()
.flatMap(contract::getDealsApi)
.map { fromEntity(it, ::fromEntity) }
fun setDealRead(id:String) = contract.setDealRead(id)
......
......@@ -21,7 +21,10 @@ class PlanTypesUseCase @Inject constructor(
) {
private fun fetchUserEstates(user:UserEntity): Observable<List<EstateEntity>> =
Observables.zip(dealContract.fetchDeals(user),dealContract.fetchFavorites(user))
Observables.zip(
dealContract.fetchDeals(user)
.map { it.map {deal -> deal.estate as EstateEntity } }
,dealContract.fetchFavorites(user))
{t1,t2 -> t1+t2}
private val fetchAllPlanTypes =
......
......@@ -12,7 +12,7 @@ import com.biganto.visual.roompark.domain.model.SubscriptionTopic
import com.biganto.visual.roompark.domain.model.TitledSubscriptionModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import io.reactivex.rxkotlin.Observables
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject
......@@ -31,10 +31,9 @@ class SubscriptionUseCase @Inject constructor(
fun subscribeTopic(subId: Int, topic: SubscriptionTopic): Observable<List<TitledSubscriptionModel>> =
Observable.zip(auth.currentUser(), utils.getDeviceId()
, BiFunction<UserEntity,String,SubscribeRequestModel> {
Observables.zip(auth.currentUser(), utils.getDeviceId()){
user, token -> SubscribeRequestModel(user,token)
})
}
.flatMap {requestModel ->
subscription.subscribeTopicResult(
requestModel.user
......@@ -50,10 +49,9 @@ class SubscriptionUseCase @Inject constructor(
fun unSubscribeTopic(subId: Int, topic: SubscriptionTopic): Observable<List<TitledSubscriptionModel>> =
Observable.zip(auth.currentUser(), utils.getDeviceId()
, BiFunction<UserEntity,String,SubscribeRequestModel> {
Observables.zip(auth.currentUser(), utils.getDeviceId()){
user, token -> SubscribeRequestModel(user,token)
})
}
.flatMap {requestModel ->
subscription.unSubscribeTopicResult(
requestModel.user
......@@ -102,7 +100,6 @@ class SubscriptionUseCase @Inject constructor(
.map {user ->
var sub = user.subscriptions
?.firstOrNull { it.topic == topic.topicName && it.number == topic.topicId }
Timber.d("fetched topic: $sub")
if (sub == null) {
sub = SubscriptionEntity()
sub.setOwner(user)
......@@ -110,9 +107,7 @@ class SubscriptionUseCase @Inject constructor(
sub.setNumber(topic.topicId)
sub.setState(false)
}
Timber.w("sub is : $sub")
subscription.saveSubscribeState(sub as SubscriptionEntity)
.doOnNext { Timber.d("zzz blocking $it") }
.blockingFirst()
}
.map(::fromEntity)
......
......@@ -38,28 +38,29 @@ class DealScreenPresenter @Inject constructor(
restoreStateObservable.accept(DealScreenViewState.RestoreView(restoreModel))
}
override fun bindIntents() {
val fetchDeal = interactor.getDeal(dealId)
private val fetchDeal = interactor.getDeal(dealId)
.doOnNext { restoreModel.deal = it }
.map<DealScreenViewState>{ deal ->
DealScreenViewState.LoadDeal(deal ,interactor.getStatusListSync())
}.share()
}
override fun bindIntents() {
val setRead = interactor.setDealRead(dealId)
.andThen(Observable.just(DealScreenViewState.Idle()))
val onSubChecked = intent(DealScreen::onSubscription)
.filter { restoreModel.sub != null }
.flatMap { newState ->
interactor.switchSubscription(restoreModel.sub!!, newState)
.map {subs ->
Timber.d("Got subs: $subs")
val s = subs.map { it.subModel }.firstOrNull {it.id == restoreModel.sub?.id}
restoreModel.sub = s
Timber.d("Got s: $s")
if (s!=null)
return@map DealScreenViewState.SubscriptionStatus(s.state)
else
......@@ -74,17 +75,19 @@ class DealScreenPresenter @Inject constructor(
interactor.getSubscriptions(
(fetchedDealViewState as DealScreenViewState.LoadDeal).estate.estate_id
)
.doAfterNext { restoreModel.sub = it }
.doOnNext { restoreModel.sub = it }
.map<DealScreenViewState> { DealScreenViewState.SubscriptionStatus(it.state) }
.startWith(Observable.just<DealScreenViewState>(DealScreenViewState.Idle()))
}
}
.onErrorReturn (::parseError)
val onStartTours = intent(DealScreen::tourCardClicked)
.map { restoreModel.deal }
.map{ it.estate }
.flatMap {estate -> toursInteractor.getEstateTourList(estate)
.map { DealScreenViewState.ToursLoaded(it) }
.map<DealScreenViewState> { DealScreenViewState.ToursLoaded(it) }
.onErrorReturn (::parseError)
}
......
......@@ -53,6 +53,22 @@ class DealsScreenPresenter @Inject constructor(
.onErrorReturn(::parseError)
val getDeals = interactor.getDealsApi()
.flatMap { deals ->
getStatusList
.map{
List(deals.size) { index ->
DealPreviewModel(
Pair(deals[index], it)
)
}
}
}
.doOnNext { restoreModel.list = it.toMutableList() }
.map<DealsScreenViewState>(DealsScreenViewState::DealsLoaded)
.onErrorReturn(::parseError)
val onStartTours = intent(DealsScreen::tourCardClicked)
.flatMap {dealPreview ->
toursInteractor.getEstateTourList(dealPreview.tourId,dealPreview.estateId)
......@@ -63,6 +79,7 @@ class DealsScreenPresenter @Inject constructor(
arrayListOf(
restoreStateObservable,
fetchDeals,
getDeals,
onStartTours
)
)
......
......@@ -37,6 +37,7 @@ class EstateScreenPresenter @Inject constructor(
private fun getPlan(plan: PlanPresetModel): Observable<EstateScreenViewState> =
interactor.getPlan(plan)
.map<EstateScreenViewState> { EstateScreenViewState.LoadPlan(it) }
.onErrorReturn (::parseError)
private var restoreModel = RestoreModel(null,null)
......@@ -50,11 +51,13 @@ class EstateScreenPresenter @Inject constructor(
val prefetchCards = interactor.getEstate(estateId)
.doOnNext { restoreModel.estate = it.copy() }
.map { EstateScreenViewState.LoadEstate(it) }
.map<EstateScreenViewState> { EstateScreenViewState.LoadEstate(it) }
.onErrorReturn (::parseError)
val fetchPlans = interactor.getPlanTypes(estateId)
.doOnNext {restoreModel.planList = it.toList() }
.map { EstateScreenViewState.LoadPlanTypes(it) }
.map<EstateScreenViewState> { EstateScreenViewState.LoadPlanTypes(it) }
.onErrorReturn (::parseError)
val fetchPlan = intent(EstateScreen::planTypesTabSelected)
.map { restoreModel.planList?.get(it) }
......@@ -77,6 +80,7 @@ class EstateScreenPresenter @Inject constructor(
)
)
)
.onErrorReturn (::parseError)
}
val switchSizes = intent(EstateScreen::switchSizes)
......
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