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 ...@@ -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.model.ArticleModel
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 io.reactivex.Observable import io.reactivex.Observable
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
......
package com.biganto.visual.roompark.domain.interactor package com.biganto.visual.roompark.domain.interactor
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.SubscriptionTopic
import com.biganto.visual.roompark.domain.use_case.SubscriptionUseCase
import javax.inject.Inject import javax.inject.Inject
/** /**
...@@ -8,7 +10,8 @@ import javax.inject.Inject ...@@ -8,7 +10,8 @@ import javax.inject.Inject
*/ */
class ArticlesInteractor @Inject constructor( class ArticlesInteractor @Inject constructor(
private val uc: FeedUseCase private val uc: FeedUseCase,
private val subUc: SubscriptionUseCase
) { ) {
fun fetchArticles(feed:String) = fun fetchArticles(feed:String) =
...@@ -17,6 +20,17 @@ class ArticlesInteractor @Inject constructor( ...@@ -17,6 +20,17 @@ class ArticlesInteractor @Inject constructor(
fun fetchArticlesPage(feed:String,pageSize:Int,current:Int) = fun fetchArticlesPage(feed:String,pageSize:Int,current:Int) =
uc.fetchArticlesPage(feed,pageSize,current) 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 ...@@ -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.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
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 io.reactivex.rxkotlin.Observables
import javax.inject.Inject import javax.inject.Inject
//import io.reactivex.rxkotlin //import io.reactivex.rxkotlin
...@@ -17,92 +21,46 @@ class SubscriptionUseCase @Inject constructor( ...@@ -17,92 +21,46 @@ class SubscriptionUseCase @Inject constructor(
private val subscription: SubscriptionContract, private val subscription: SubscriptionContract,
private val utils: DeviceUtilsContract, private val utils: DeviceUtilsContract,
private val auth: AuthContract private val auth: AuthContract
){ ) {
fun subscribeTopic(subId:Int,topic:SubscriptionTopic) = fun subscribeTopic(subId: Int, topic: SubscriptionTopic) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ user, token -> Observables.zip(auth.currentUser(), utils.getDeviceId()) { user, token ->
subscription.subscribeTopic( subscription.subscribeTopic(
user user
,subId , subId
,token , token
,topic = topic.topicName, , topic = topic.topicName,
topic_id = topic.topicId topic_id = topic.topicId
) )
} }
fun unSubscribeTopic(subId:Int,topic:SubscriptionTopic) = fun unSubscribeTopic(subId: Int, topic: SubscriptionTopic) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ user, token -> Observables.zip(auth.currentUser(), utils.getDeviceId()) { user, token ->
subscription.unSubscribeTopic(user subscription.unSubscribeTopic(
,subId user
,token , subId
,topic = topic.topicName, , token
topic_id = topic.topicId) , topic = topic.topicName,
topic_id = topic.topicId
)
} }
fun getSubscriptions(topic:SubscriptionTopic) = auth.currentUser() fun getSubscriptions(topic: SubscriptionTopic): Observable<SubscriptionModel> =
.map { auth.currentUser()
var sub = it.subscriptions .map {
?.firstOrNull{it.topic == topic.topicName && it.number == topic.topicId} var sub = it.subscriptions
if (sub == null) { ?.firstOrNull { it.topic == topic.topicName && it.number == topic.topicId }
sub = SubscriptionEntity() if (sub == null) {
sub.setOwner(it) sub = SubscriptionEntity()
sub.setTopic(topic.topicName) sub.setOwner(it)
sub.setNumber(topic.topicId) sub.setTopic(topic.topicName)
sub.setState(false) sub.setNumber(topic.topicId)
sub.setState(false)
}
subscription.saveSubscribeState(sub as SubscriptionEntity).blockingFirst()
} }
subscription.saveSubscribeState(sub as SubscriptionEntity).blockingFirst() .map(::fromEntity)
}
// 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