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

fix subscriptions

parent e8b55308
......@@ -44,7 +44,7 @@ class EstateRepository @Inject constructor(
.doOnError(Timber::e)
.map { fromRawList(it, ::fromRaw) }
,db.getUserFavorites(user.uuid)
.defaultIfEmpty(null).toList().toObservable()
.toList().toObservable()
.doOnError(Timber::e)
){apiList,dbList ->
if (dbList.isEmpty())
......
......@@ -66,8 +66,12 @@ class SubscriptionRepository @Inject constructor(
val newSubList = mutableListOf<SubscriptionEntity>()
apiSubs.forEach { apiSub ->
Timber.d("api sub state: ${apiSub.active}")
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?
?: SubscriptionEntity()
.apply {
......@@ -75,10 +79,16 @@ class SubscriptionRepository @Inject constructor(
setTopic(apiSub.topic)
setNumber(apiSub.estate_id)
}
cachedSub.setState(apiSub.state)
cachedSub.setState(apiSub.active)
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)
.map {list -> list.map {
fromEntity(
......@@ -101,7 +111,7 @@ class SubscriptionRepository @Inject constructor(
,topicName = topic
,topicId = topic_id)
.flatMap { saveSubscribtions(user,it.subscriptions?: arrayListOf()) }
.doOnNext { db.refreshUser(user) }
.doOnNext { db.refreshUser(user).blockingFirst() }
override fun unSubscribeTopicResult(
user:UserEntity,
......
......@@ -23,7 +23,7 @@ data class SubscriptionStatusRaw(
val topic:String,
val estate_id: String,
@Expose
val state: Boolean = true
val active: Boolean = true
)
data class DealRaw(
......
......@@ -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.use_case.DealseUseCase
import com.biganto.visual.roompark.domain.use_case.SubscriptionUseCase
import io.reactivex.Completable
import io.reactivex.Observable
import timber.log.Timber
import javax.inject.Inject
......@@ -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(
.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
......
......@@ -4,7 +4,6 @@ import com.biganto.visual.roompark.base.BaseRoomParkActivity
import com.biganto.visual.roompark.conductor.BigantoBasePresenter
import com.biganto.visual.roompark.domain.interactor.SettingsInteractor
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.util.monades.ExceptionString
import com.jakewharton.rxrelay2.PublishRelay
......@@ -68,24 +67,8 @@ class SettingsScreenPresenter @Inject constructor(
.flatMap { sub ->
interactor.switchSubscription(sub, !sub.state)
.map<SettingsScreenViewState> {
restoreModel.subs=it.toMutableList()
SettingsScreenViewState.SubscriptionStatus(
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
)
)
restoreModel.subs = it.sortedBy { it.subModel.id }.toMutableList()
SettingsScreenViewState.LoadSubscriptions(it)
}
.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