Commit ba6db1a2 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fix subscriptions

parent e8b55308
...@@ -44,7 +44,7 @@ class EstateRepository @Inject constructor( ...@@ -44,7 +44,7 @@ class EstateRepository @Inject constructor(
.doOnError(Timber::e) .doOnError(Timber::e)
.map { fromRawList(it, ::fromRaw) } .map { fromRawList(it, ::fromRaw) }
,db.getUserFavorites(user.uuid) ,db.getUserFavorites(user.uuid)
.defaultIfEmpty(null).toList().toObservable() .toList().toObservable()
.doOnError(Timber::e) .doOnError(Timber::e)
){apiList,dbList -> ){apiList,dbList ->
if (dbList.isEmpty()) if (dbList.isEmpty())
......
...@@ -66,8 +66,12 @@ class SubscriptionRepository @Inject constructor( ...@@ -66,8 +66,12 @@ class SubscriptionRepository @Inject constructor(
val newSubList = mutableListOf<SubscriptionEntity>() val newSubList = mutableListOf<SubscriptionEntity>()
apiSubs.forEach { apiSub -> apiSubs.forEach { apiSub ->
Timber.d("api sub state: ${apiSub.active}")
val cachedSub = val cachedSub =
userSubs?.firstOrNull { s -> s.topic == apiSub.topic && s.number == apiSub.estate_id } 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? as SubscriptionEntity?
?: SubscriptionEntity() ?: SubscriptionEntity()
.apply { .apply {
...@@ -75,10 +79,16 @@ class SubscriptionRepository @Inject constructor( ...@@ -75,10 +79,16 @@ class SubscriptionRepository @Inject constructor(
setTopic(apiSub.topic) setTopic(apiSub.topic)
setNumber(apiSub.estate_id) setNumber(apiSub.estate_id)
} }
cachedSub.setState(apiSub.state) cachedSub.setState(apiSub.active)
newSubList.add(cachedSub) newSubList.add(cachedSub)
} }
userSubs?.let { db.deleteSubscriptions(it) } userSubs?.filter { !newSubList.map { s ->s.id }.contains(it.id) }?.toList()
?.let {
Timber.w("to delete; ${it.size}")
db.deleteSubscriptions(it)
}
Timber.w("to newSubList; ${newSubList}")
return db.upsert(newSubList) return db.upsert(newSubList)
.map {list -> list.map { .map {list -> list.map {
fromEntity( fromEntity(
...@@ -101,7 +111,7 @@ class SubscriptionRepository @Inject constructor( ...@@ -101,7 +111,7 @@ class SubscriptionRepository @Inject constructor(
,topicName = topic ,topicName = topic
,topicId = topic_id) ,topicId = topic_id)
.flatMap { saveSubscribtions(user,it.subscriptions?: arrayListOf()) } .flatMap { saveSubscribtions(user,it.subscriptions?: arrayListOf()) }
.doOnNext { db.refreshUser(user) } .doOnNext { db.refreshUser(user).blockingFirst() }
override fun unSubscribeTopicResult( override fun unSubscribeTopicResult(
user:UserEntity, user:UserEntity,
......
...@@ -23,7 +23,7 @@ data class SubscriptionStatusRaw( ...@@ -23,7 +23,7 @@ data class SubscriptionStatusRaw(
val topic:String, val topic:String,
val estate_id: String, val estate_id: String,
@Expose @Expose
val state: Boolean = true val active: Boolean = true
) )
data class DealRaw( data class DealRaw(
......
...@@ -6,7 +6,6 @@ import com.biganto.visual.roompark.domain.model.SubscriptionModel ...@@ -6,7 +6,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.DealseUseCase import com.biganto.visual.roompark.domain.use_case.DealseUseCase
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 timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
...@@ -101,4 +100,4 @@ class DealInteractor @Inject constructor( ...@@ -101,4 +100,4 @@ class DealInteractor @Inject constructor(
) )
) )
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -56,8 +56,10 @@ class DealScreenPresenter @Inject constructor( ...@@ -56,8 +56,10 @@ class DealScreenPresenter @Inject constructor(
.flatMap { newState -> .flatMap { newState ->
interactor.switchSubscription(restoreModel.sub!!, newState) interactor.switchSubscription(restoreModel.sub!!, newState)
.map {subs -> .map {subs ->
Timber.d("Got subs: $subs")
val s = subs.map { it.subModel }.firstOrNull {it.id == restoreModel.sub?.id} val s = subs.map { it.subModel }.firstOrNull {it.id == restoreModel.sub?.id}
restoreModel.sub = s restoreModel.sub = s
Timber.d("Got s: $s")
if (s!=null) if (s!=null)
return@map DealScreenViewState.SubscriptionStatus(s.state) return@map DealScreenViewState.SubscriptionStatus(s.state)
else else
......
...@@ -4,7 +4,6 @@ import com.biganto.visual.roompark.base.BaseRoomParkActivity ...@@ -4,7 +4,6 @@ import com.biganto.visual.roompark.base.BaseRoomParkActivity
import com.biganto.visual.roompark.conductor.BigantoBasePresenter import com.biganto.visual.roompark.conductor.BigantoBasePresenter
import com.biganto.visual.roompark.domain.interactor.SettingsInteractor import com.biganto.visual.roompark.domain.interactor.SettingsInteractor
import com.biganto.visual.roompark.domain.model.CachedDataModel import com.biganto.visual.roompark.domain.model.CachedDataModel
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.TitledSubscriptionModel import com.biganto.visual.roompark.domain.model.TitledSubscriptionModel
import com.biganto.visual.roompark.util.monades.ExceptionString import com.biganto.visual.roompark.util.monades.ExceptionString
import com.jakewharton.rxrelay2.PublishRelay import com.jakewharton.rxrelay2.PublishRelay
...@@ -68,24 +67,8 @@ class SettingsScreenPresenter @Inject constructor( ...@@ -68,24 +67,8 @@ class SettingsScreenPresenter @Inject constructor(
.flatMap { sub -> .flatMap { sub ->
interactor.switchSubscription(sub, !sub.state) interactor.switchSubscription(sub, !sub.state)
.map<SettingsScreenViewState> { .map<SettingsScreenViewState> {
restoreModel.subs=it.toMutableList() restoreModel.subs = it.sortedBy { it.subModel.id }.toMutableList()
SettingsScreenViewState.SubscriptionStatus( SettingsScreenViewState.LoadSubscriptions(it)
sub.id, !sub.state
)
}
.doOnNext {
val ind = restoreModel
.subs
.indexOfFirst { it.subModel.id == sub.id }
restoreModel.subs[ind] =
TitledSubscriptionModel(
title = restoreModel.subs[ind].title,
subModel = SubscriptionModel(
topic = sub.topic,
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