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

subscription model

parent c6514013
......@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.domain.interactor
import com.biganto.visual.roompark.domain.model.ArticleModel
import com.biganto.visual.roompark.domain.use_case.FeedUseCase
import com.biganto.visual.roompark.domain.use_case.SubscriptionUseCase
import io.reactivex.Observable
import java.util.*
import javax.inject.Inject
......
package com.biganto.visual.roompark.domain.interactor
import com.biganto.visual.roompark.domain.use_case.FeedUseCase
import com.biganto.visual.roompark.domain.use_case.SubscriptionTopic
import com.biganto.visual.roompark.domain.use_case.SubscriptionUseCase
import javax.inject.Inject
/**
......@@ -8,7 +10,8 @@ import javax.inject.Inject
*/
class ArticlesInteractor @Inject constructor(
private val uc: FeedUseCase
private val uc: FeedUseCase,
private val subUc: SubscriptionUseCase
) {
fun fetchArticles(feed:String) =
......@@ -17,6 +20,17 @@ class ArticlesInteractor @Inject constructor(
fun fetchArticlesPage(feed:String,pageSize:Int,current:Int) =
uc.fetchArticlesPage(feed,pageSize,current)
fun getSubscriptions(feed: String) =
subUc.getSubscriptions(feedSubType(feed))
private fun feedSubType(feed:String) =
when(feed){
"news" -> SubscriptionTopic.News()
"blog" -> SubscriptionTopic.Blog()
"construction-blog" -> SubscriptionTopic.Construction()
else -> error("Unknown feed type")
}
}
......
package com.biganto.visual.roompark.domain.model
import com.biganto.visual.roompark.data.repository.db.requrey.model.SubscriptionEntity
/**
* Created by Vladislav Bogdashkin on 26.03.2020.
*/
data class SubscriptionModel(
val topic:SubscriptionTopic,
val id: Int,
val state:Boolean
)
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 topicFromEntity(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()
else -> error("Unknown subscription type!")
}
fun fromEntity(sub: SubscriptionEntity):SubscriptionModel =
SubscriptionModel(topicFromEntity(sub),sub.id,sub.state)
......@@ -5,6 +5,10 @@ import com.biganto.visual.roompark.data.repository.db.requrey.model.Subscription
import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.contract.DeviceUtilsContract
import com.biganto.visual.roompark.domain.contract.SubscriptionContract
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.SubscriptionTopic
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable
import io.reactivex.rxkotlin.Observables
import javax.inject.Inject
//import io.reactivex.rxkotlin
......@@ -17,31 +21,34 @@ class SubscriptionUseCase @Inject constructor(
private val subscription: SubscriptionContract,
private val utils: DeviceUtilsContract,
private val auth: AuthContract
){
fun subscribeTopic(subId:Int,topic:SubscriptionTopic) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ user, token ->
) {
fun subscribeTopic(subId: Int, topic: SubscriptionTopic) =
Observables.zip(auth.currentUser(), utils.getDeviceId()) { user, token ->
subscription.subscribeTopic(
user
,subId
,token
,topic = topic.topicName,
, subId
, token
, topic = topic.topicName,
topic_id = topic.topicId
)
}
fun unSubscribeTopic(subId:Int,topic:SubscriptionTopic) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ user, token ->
subscription.unSubscribeTopic(user
,subId
,token
,topic = topic.topicName,
topic_id = topic.topicId)
fun unSubscribeTopic(subId: Int, topic: SubscriptionTopic) =
Observables.zip(auth.currentUser(), utils.getDeviceId()) { user, token ->
subscription.unSubscribeTopic(
user
, subId
, token
, topic = topic.topicName,
topic_id = topic.topicId
)
}
fun getSubscriptions(topic:SubscriptionTopic) = auth.currentUser()
fun getSubscriptions(topic: SubscriptionTopic): Observable<SubscriptionModel> =
auth.currentUser()
.map {
var sub = it.subscriptions
?.firstOrNull{it.topic == topic.topicName && it.number == topic.topicId}
?.firstOrNull { it.topic == topic.topicName && it.number == topic.topicId }
if (sub == null) {
sub = SubscriptionEntity()
sub.setOwner(it)
......@@ -51,59 +58,10 @@ class SubscriptionUseCase @Inject constructor(
}
subscription.saveSubscribeState(sub as SubscriptionEntity).blockingFirst()
}
// fun getSubscription()
.map(::fromEntity)
}
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