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