Commit 936097b5 authored by Vladislav's avatar Vladislav

subs move to titledsubs models;

fetch api and update states
parent 3d63a790
......@@ -6,6 +6,8 @@ import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.SubscriptionEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.domain.contract.SubscriptionContract
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Completable
import io.reactivex.Observable
import timber.log.Timber
......@@ -43,7 +45,7 @@ class SubscriptionRepository @Inject constructor(
val userSubs = userEntity.subscriptions
val sub = subInnerId?.let { id ->
userEntity.subscriptions?.firstOrNull { sub -> sub.id == id } as SubscriptionEntity?
userSubs?.firstOrNull { sub -> sub.id == id } as SubscriptionEntity?
}?:SubscriptionEntity()
.apply {
setOwner(userEntity)
......@@ -59,7 +61,7 @@ class SubscriptionRepository @Inject constructor(
fun saveSubscribtions(
userEntity: UserEntity,
apiSubs: List<SubscriptionStatusRaw>
): Observable<SubscriptionEntity> {
): Observable<List<SubscriptionModel>>? {
val userSubs = userEntity.subscriptions
val newSubList = mutableListOf<SubscriptionEntity>()
......@@ -77,10 +79,44 @@ class SubscriptionRepository @Inject constructor(
newSubList.add(cachedSub)
}
userSubs?.let { db.deleteSubscriptions(it) }
return db.upsert(newSubList).map { it as SubscriptionEntity }.toObservable()
return db.upsert(newSubList)
.map {list -> list.map {
fromEntity(
it as SubscriptionEntity)
}}
.toObservable()
}
override fun subscribeTopicResult(
user:UserEntity,
subInnerId:Int,
deviceToken: String,
topic: String,
topic_id: String?
): Observable<List<SubscriptionModel>> =
api.subscribeTopic(
userToken = user.authToken,
deviceToken = deviceToken
,topicName = topic
,topicId = topic_id)
.flatMap { saveSubscribtions(user,it.subscriptions?: arrayListOf()) }
override fun unSubscribeTopicResult(
user:UserEntity,
subInnerId:Int,
deviceToken: String,
topic: String,
topic_id: String?
): Observable<List<SubscriptionModel>> =
api.unSuubscribeTopic(
userToken = user.authToken,
deviceToken = deviceToken
,topicName = topic
,topicId = topic_id)
.flatMap { saveSubscribtions(user,it.subscriptions?: arrayListOf()) }
override fun subscribeTopic(
user:UserEntity,
subInnerId:Int,
......
......@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.data.repository.db.requrey.model.SubscriptionEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import io.reactivex.Completable
import io.reactivex.Observable
......@@ -36,4 +37,19 @@ interface SubscriptionContract{
): Observable<SubscriptionEntity>
fun saveSubscribeState(sub: SubscriptionEntity): Observable<SubscriptionEntity>
fun subscribeTopicResult(
user: UserEntity,
subInnerId: Int,
deviceToken: String,
topic: String,
topic_id: String?
): Observable<List<SubscriptionModel>>
fun unSubscribeTopicResult(
user: UserEntity,
subInnerId: Int,
deviceToken: String,
topic: String,
topic_id: String?
): Observable<List<SubscriptionModel>>
}
\ No newline at end of file
......@@ -5,7 +5,6 @@ import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.SubscriptionTopic
import com.biganto.visual.roompark.domain.use_case.AlbumsUseCase
import com.biganto.visual.roompark.domain.use_case.SubscriptionUseCase
import io.reactivex.Completable
import io.reactivex.Observable
import java.util.concurrent.TimeUnit
import javax.inject.Inject
......@@ -41,7 +40,7 @@ class AlbumsInteractor @Inject constructor(
fun getSubscriptions(topic: SubscriptionTopic) =
subUc.getSubscriptions(topic)
fun switchSubscription(model: SubscriptionModel, newState: Boolean): Completable =
fun switchSubscription(model: SubscriptionModel, newState: Boolean) =
when(newState){
true -> subUc.subscribeTopic(model.id,model.topic)
false -> subUc.unSubscribeTopic(model.id,model.topic)
......
......@@ -4,7 +4,6 @@ import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.SubscriptionTopic
import com.biganto.visual.roompark.domain.use_case.FeedUseCase
import com.biganto.visual.roompark.domain.use_case.SubscriptionUseCase
import io.reactivex.Completable
import javax.inject.Inject
/**
......@@ -25,7 +24,7 @@ class ArticlesInteractor @Inject constructor(
fun getSubscriptions(feed: String) =
subUc.getSubscriptions(feedSubType(feed))
fun switchSubscription(model:SubscriptionModel,newState: Boolean): Completable =
fun switchSubscription(model:SubscriptionModel,newState: Boolean) =
when(newState){
true -> subUc.subscribeTopic(model.id,model.topic)
false -> subUc.unSubscribeTopic(model.id,model.topic)
......
......@@ -34,7 +34,7 @@ class DealInteractor @Inject constructor(
fun getSubscriptions(dealId: String) =
subUc.getSubscriptions(SubscriptionTopic.Deals(dealId = dealId))
fun switchSubscription(model: SubscriptionModel, newState: Boolean): Completable =
fun switchSubscription(model: SubscriptionModel, newState: Boolean) =
when(newState){
true -> subUc.subscribeTopic(model.id,model.topic)
false -> subUc.unSubscribeTopic(model.id,model.topic)
......
......@@ -12,7 +12,6 @@ import com.biganto.visual.roompark.domain.model.PushSwitchModel
import com.biganto.visual.roompark.domain.model.SettingsModel
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.use_case.*
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
......@@ -85,7 +84,7 @@ class SettingsInteractor @Inject constructor(
fun getSubscriptions() =
subUc.getCurrentUserSubscriptions()
fun switchSubscription(model: SubscriptionModel, newState: Boolean): Completable =
fun switchSubscription(model: SubscriptionModel, newState: Boolean) =
when(newState){
true -> subUc.subscribeTopic(model.id,model.topic)
false -> subUc.unSubscribeTopic(model.id,model.topic)
......
......@@ -11,7 +11,6 @@ import com.biganto.visual.roompark.domain.model.SubscriptionModel
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.Completable
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
......@@ -28,55 +27,75 @@ class SubscriptionUseCase @Inject constructor(
private val utils: DeviceUtilsContract,
private val auth: AuthContract
) {
fun subscribeTopic(subId: Int, topic: SubscriptionTopic): Completable =
fun subscribeTopic(subId: Int, topic: SubscriptionTopic): Observable<List<TitledSubscriptionModel>> =
Observable.zip(auth.currentUser(), utils.getDeviceId()
, BiFunction<UserEntity,String,SubscribeRequestModel> {
user, token -> SubscribeRequestModel(user,token)
})
.flatMapCompletable {
subscription.subscribeTopic(
it.user
.flatMap {requestModel ->
subscription.subscribeTopicResult(
requestModel.user
, subId
, it.deviceToken
, topic = topic.topicName,
topic_id = topic.topicId
).subscribeOn(Schedulers.io())
, requestModel.deviceToken
, topic = topic.topicName
, topic_id = topic.topicId
)
.map {subs -> subsToTitle(requestModel.user,subs)}
.subscribeOn(Schedulers.io())
}
fun unSubscribeTopic(subId: Int, topic: SubscriptionTopic): Completable =
fun unSubscribeTopic(subId: Int, topic: SubscriptionTopic): Observable<List<TitledSubscriptionModel>> =
Observable.zip(auth.currentUser(), utils.getDeviceId()
, BiFunction<UserEntity,String,SubscribeRequestModel> {
user, token -> SubscribeRequestModel(user,token)
})
.flatMapCompletable {
subscription.unSubscribeTopic(
it.user
.flatMap {requestModel ->
subscription.unSubscribeTopicResult(
requestModel.user
, subId
, it.deviceToken
, requestModel.deviceToken
, topic = topic.topicName,
topic_id = topic.topicId
).subscribeOn(Schedulers.io())
)
.map {subs -> subsToTitle(requestModel.user,subs)}
.subscribeOn(Schedulers.io())
}
fun getCurrentUserSubscriptions(): Observable<List<TitledSubscriptionModel>> =
auth.currentUser()
.map {user ->
val subList = user.subscriptions?: arrayListOf()
val list = List<TitledSubscriptionModel>(subList.size){i ->
val sub:SubscriptionModel = fromEntity(subList[i] as SubscriptionEntity)
private fun subsToTitle(user:UserEntity,subs:List<SubscriptionModel>?)
: List<TitledSubscriptionModel> {
val subList =
subs
?: user.subscriptions
?.asSequence()?.map { it as SubscriptionEntity }?.map(::fromEntity)?.toList()
?: arrayListOf()
return List(subList.size){ i ->
subToTitledSub(user.deals?.map { it as DealEntity }?: arrayListOf(),subList[i])
}
}
private fun subToTitledSub(deals:List<DealEntity>,sub:SubscriptionModel)
: TitledSubscriptionModel {
var title = SubscriptionTopic.titleByTopic(sub.topic)
if (sub.topic is SubscriptionTopic.Deals){
if (sub.topic is SubscriptionTopic.Deals) {
val deal =
user.deals?.firstOrNull { d->d.estateCrmId == sub.topic.topicId } as DealEntity
title = "$title № ${deal.estate.number}"
deals.firstOrNull { d -> d.estateCrmId == sub.topic.topicId }
if (deal == null)
Timber.e("Deal not recognized: $sub ; $deals")
title = "$title № ${deal?.estate?.number}"
}
TitledSubscriptionModel(title,sub)
}
list
return TitledSubscriptionModel(title, sub)
}
fun getCurrentUserSubscriptions(): Observable<List<TitledSubscriptionModel>> =
auth.currentUser()
.map {user -> subsToTitle(user,null)}
fun getSubscriptions(topic: SubscriptionTopic): Observable<SubscriptionModel> =
auth.currentUser()
......
......@@ -48,13 +48,14 @@ class AlbumsScreenPresenter @Inject constructor(
.filter { restoreModel.sub != null }
.flatMap { newState ->
interactor.switchSubscription(restoreModel.sub!!, newState)
.andThen(
Observable.just<AlbumsScreenViewState>(
AlbumsScreenViewState.SubscriptionStatus(
newState
)
)
)
.map {subs ->
val s = subs.map { it.subModel }.firstOrNull {it.id == restoreModel.sub?.id}
restoreModel.sub = s
if (s!=null)
return@map AlbumsScreenViewState.SubscriptionStatus(s.state)
else
return@map AlbumsScreenViewState.SubscriptionError(!newState)
}
.onErrorReturn { AlbumsScreenViewState.SubscriptionError(!newState) }
}
......
......@@ -55,14 +55,18 @@ class DealScreenPresenter @Inject constructor(
.filter { restoreModel.sub != null }
.flatMap { newState ->
interactor.switchSubscription(restoreModel.sub!!, newState)
.andThen(
Observable.just<DealScreenViewState>(
DealScreenViewState.SubscriptionStatus(newState)
)
)
.map {subs ->
val s = subs.map { it.subModel }.firstOrNull {it.id == restoreModel.sub?.id}
restoreModel.sub = s
if (s!=null)
return@map DealScreenViewState.SubscriptionStatus(s.state)
else
return@map DealScreenViewState.SubscriptionError(!newState)
}
.onErrorReturn { DealScreenViewState.SubscriptionError(!newState) }
}
val fetchSubscription =
fetchDeal.flatMap { fetchedDealViewState ->
interactor.getSubscriptions(
......
......@@ -39,7 +39,6 @@ class DealsScreenPresenter @Inject constructor(
)
}
}
.doOnError { Timber.e(" ERORORO ") }
}
.map<DealsScreenViewState>(DealsScreenViewState::DealsLoaded)
.onErrorReturn(::parseError)
......
......@@ -43,13 +43,14 @@ class ArticlesScreenPresenter @Inject constructor(
.filter { restoreModel.sub != null }
.flatMap { newState ->
interactor.switchSubscription(restoreModel.sub!!, newState)
.andThen(
Observable.just<ArticlesScreenViewState>(
ArticlesScreenViewState.SubscriptionStatus(
newState
)
)
)
.map {subs ->
val s = subs.map { it.subModel }.firstOrNull {it.id == restoreModel.sub?.id}
restoreModel.sub = s
if (s!=null)
return@map ArticlesScreenViewState.SubscriptionStatus(s.state)
else
return@map ArticlesScreenViewState.SubscriptionError(!newState)
}
.onErrorReturn { ArticlesScreenViewState.SubscriptionError(!newState) }
}
......
......@@ -67,12 +67,12 @@ class SettingsScreenPresenter @Inject constructor(
val onSubChecked = intent(SettingsScreen::onSubscription)
.flatMap { sub ->
interactor.switchSubscription(sub, !sub.state)
.andThen(
Observable.just<SettingsScreenViewState>(
.map<SettingsScreenViewState> {
restoreModel.subs=it.toMutableList()
SettingsScreenViewState.SubscriptionStatus(
sub.id, !sub.state
)
)
}
.doOnNext {
val ind = restoreModel
.subs
......@@ -87,8 +87,6 @@ class SettingsScreenPresenter @Inject constructor(
)
)
}
)
.doOnError { Timber.e(it) }
.onErrorReturn { SettingsScreenViewState.SubscriptionError(sub.id, sub.state) }
}
......
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