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

subscriptions fetching and inittializing by default

parent 6ca8c416
...@@ -6,6 +6,7 @@ import com.biganto.visual.roompark.data.repository.db.requrey.model.Subscription ...@@ -6,6 +6,7 @@ import com.biganto.visual.roompark.data.repository.db.requrey.model.Subscription
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 io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Observable
import javax.inject.Inject import javax.inject.Inject
/** /**
...@@ -19,14 +20,27 @@ class SubscriptionRepository @Inject constructor( ...@@ -19,14 +20,27 @@ class SubscriptionRepository @Inject constructor(
private val db: IDb private val db: IDb
): SubscriptionContract{ ): SubscriptionContract{
private fun saveSubscribeState(
override fun saveSubscribeState(sub:SubscriptionEntity): Observable<SubscriptionEntity> {
return saveSubscribeState(
sub.owner as UserEntity,
sub.id,
sub.topic,
sub.number,
sub.state
)
}
override fun saveSubscribeState(
userEntity: UserEntity, userEntity: UserEntity,
subInnerId:Int, subInnerId:Int?,
topic: String, topic: String,
topic_id: String?, topic_id: String?,
nuewState:Boolean nuewState:Boolean
): Completable { ): Observable<SubscriptionEntity> {
var sub = userEntity.subscriptions?.firstOrNull { sub -> sub.id == subInnerId } var sub = subInnerId?.let {id ->
userEntity.subscriptions?.firstOrNull { sub -> sub.id == id }
}
if (sub == null) { if (sub == null) {
sub = SubscriptionEntity() sub = SubscriptionEntity()
sub.setOwner(userEntity) sub.setOwner(userEntity)
...@@ -35,7 +49,7 @@ class SubscriptionRepository @Inject constructor( ...@@ -35,7 +49,7 @@ class SubscriptionRepository @Inject constructor(
} }
(sub as SubscriptionEntity).setState(nuewState) (sub as SubscriptionEntity).setState(nuewState)
return db.saveSubscription(sub).ignoreElement() return db.saveSubscription(sub).toObservable()
} }
override fun subscribeTopic( override fun subscribeTopic(
...@@ -47,7 +61,7 @@ class SubscriptionRepository @Inject constructor( ...@@ -47,7 +61,7 @@ class SubscriptionRepository @Inject constructor(
): Completable = api.subscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken) ): Completable = api.subscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken)
.switchMapCompletable { .switchMapCompletable {
if (it.status == SUBSCRIPTION_RESULT_STATUS){ if (it.status == SUBSCRIPTION_RESULT_STATUS){
saveSubscribeState(user,subInnerId,topic,topic_id,true) saveSubscribeState(user,subInnerId,topic,topic_id,true).ignoreElements()
} }
else error("Error subscription state!") else error("Error subscription state!")
} }
...@@ -63,9 +77,12 @@ class SubscriptionRepository @Inject constructor( ...@@ -63,9 +77,12 @@ class SubscriptionRepository @Inject constructor(
api.unSuubscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken) api.unSuubscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken)
.switchMapCompletable { .switchMapCompletable {
if (it.status == SUBSCRIPTION_RESULT_STATUS){ if (it.status == SUBSCRIPTION_RESULT_STATUS){
saveSubscribeState(user,subInnerId,topic,topic_id,false) saveSubscribeState(user,subInnerId,topic,topic_id,false).ignoreElements()
} }
else error("Error subscription state!") else error("Error subscription state!")
} }
} }
\ No newline at end of file
...@@ -183,4 +183,5 @@ class RequeryRepository @Inject constructor( ...@@ -183,4 +183,5 @@ class RequeryRepository @Inject constructor(
store.select(SubscriptionEntity::class) store.select(SubscriptionEntity::class)
.where(SubscriptionEntity.ID.eq(id)) .where(SubscriptionEntity.ID.eq(id))
.get() .get()
} }
package com.biganto.visual.roompark.domain.contract 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.data.repository.db.requrey.model.UserEntity
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Observable
/** /**
* Created by Vladislav Bogdashkin on 24.09.2019. * Created by Vladislav Bogdashkin on 24.09.2019.
...@@ -24,4 +26,14 @@ interface SubscriptionContract{ ...@@ -24,4 +26,14 @@ interface SubscriptionContract{
topic: String, topic: String,
topic_id: String? topic_id: String?
): Completable ): Completable
fun saveSubscribeState(
userEntity: UserEntity,
subInnerId: Int?,
topic: String,
topic_id: String?,
nuewState: Boolean
): Observable<SubscriptionEntity>
fun saveSubscribeState(sub: SubscriptionEntity): Observable<SubscriptionEntity>
} }
\ No newline at end of file
package com.biganto.visual.roompark.domain.use_case package com.biganto.visual.roompark.domain.use_case
//import io.reactivex.Observable //import io.reactivex.Observable
import com.biganto.visual.roompark.data.repository.db.requrey.model.SubscriptionEntity
import com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.contract.DeviceUtilsContract import com.biganto.visual.roompark.domain.contract.DeviceUtilsContract
import com.biganto.visual.roompark.domain.contract.SubscriptionContract import com.biganto.visual.roompark.domain.contract.SubscriptionContract
...@@ -17,14 +18,92 @@ class SubscriptionUseCase @Inject constructor( ...@@ -17,14 +18,92 @@ 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:String, topicId:String?=null) = fun subscribeTopic(subId:Int,topic:SubscriptionTopic) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ u, t -> Observables.zip(auth.currentUser(),utils.getDeviceId()){ user, token ->
subscription.subscribeTopic(u,subId,t,topic = topic,topic_id = topicId) subscription.subscribeTopic(
user
,subId
,token
,topic = topic.topicName,
topic_id = topic.topicId
)
} }
fun unSubscribeTopic(subId:Int,topic:String, topicId:String?=null) = fun unSubscribeTopic(subId:Int,topic:SubscriptionTopic) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ u, t -> Observables.zip(auth.currentUser(),utils.getDeviceId()){ user, token ->
subscription.subscribeTopic(u,subId,t,topic = topic,topic_id = topicId) subscription.unSubscribeTopic(user
,subId
,token
,topic = topic.topicName,
topic_id = topic.topicId)
} }
fun getSubscriptions(topic:SubscriptionTopic) = auth.currentUser()
.map {
var sub = it.subscriptions
?.firstOrNull{it.topic == topic.topicName && it.number == topic.topicId}
if (sub == null) {
sub = SubscriptionEntity()
sub.setOwner(it)
sub.setTopic(topic.topicName)
sub.setNumber(topic.topicId)
sub.setState(false)
}
subscription.saveSubscribeState(sub as SubscriptionEntity).blockingFirst()
}
// fun getSubscription()
}
sealed class SubscriptionTopic(val topicName:String,val topicId:String? = null) {
class Deals(topicName: String =TOPIC_API_NAME_DEALS,dealId:String?) : SubscriptionTopic(topicName,dealId)
class Progress_1(topicName: String =TOPIC_API_NAME_PROGRESS_1) : SubscriptionTopic(topicName)
class Progress_2(topicName: String =TOPIC_API_NAME_PROGRESS_2) : SubscriptionTopic(topicName)
class Progress_3(topicName: String =TOPIC_API_NAME_PROGRESS_3) : SubscriptionTopic(topicName)
class Progress_kindergarden(topicName: String =TOPIC_API_NAME_PROGRESS_KINDERGARDEN) : SubscriptionTopic(topicName)
class Progress_school(topicName: String =TOPIC_API_NAME_PROGRESS_SCHOOL) : SubscriptionTopic(topicName)
class Progress_landscaping(topicName: String =TOPIC_API_NAME_PROGRESS_LANDSCAPING) : SubscriptionTopic(topicName)
class News(topicName: String =TOPIC_API_NAME_NEWS) : SubscriptionTopic(topicName)
class Blog(topicName: String =TOPIC_API_NAME_BLOG) : SubscriptionTopic(topicName)
class Construction(topicName: String = TOPIC_API_NAME_CONSCTRUCTION) : SubscriptionTopic(topicName)
} }
private const val TOPIC_API_NAME_DEALS = "deals"
private const val TOPIC_API_NAME_PROGRESS_1 = "progress-1"
private const val TOPIC_API_NAME_PROGRESS_2 = "progress-2"
private const val TOPIC_API_NAME_PROGRESS_3 = "progress-3"
private const val TOPIC_API_NAME_PROGRESS_KINDERGARDEN = "progress-kindergarden"
private const val TOPIC_API_NAME_PROGRESS_SCHOOL = "progress-school"
private const val TOPIC_API_NAME_PROGRESS_LANDSCAPING = "progress-landscaping"
private const val TOPIC_API_NAME_NEWS = "news"
private const val TOPIC_API_NAME_BLOG = "blog"
private const val TOPIC_API_NAME_CONSCTRUCTION = "construction-blog"
private fun fromEntity(sub:SubscriptionEntity):SubscriptionTopic{
when(sub.topic){
TOPIC_API_NAME_DEALS -> SubscriptionTopic.Deals(dealId = sub.number)
TOPIC_API_NAME_PROGRESS_1 -> SubscriptionTopic.Progress_1()
TOPIC_API_NAME_PROGRESS_2 -> SubscriptionTopic.Progress_2()
TOPIC_API_NAME_PROGRESS_3 -> SubscriptionTopic.Progress_3()
TOPIC_API_NAME_PROGRESS_KINDERGARDEN -> SubscriptionTopic.Progress_kindergarden()
TOPIC_API_NAME_PROGRESS_SCHOOL -> SubscriptionTopic.Progress_school()
TOPIC_API_NAME_PROGRESS_LANDSCAPING -> SubscriptionTopic.Progress_landscaping()
TOPIC_API_NAME_NEWS -> SubscriptionTopic.News()
TOPIC_API_NAME_BLOG -> SubscriptionTopic.Blog()
TOPIC_API_NAME_CONSCTRUCTION -> SubscriptionTopic.Construction()
}
}
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