Commit 6ca8c416 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

subscription contract;

import rxKotlin
parent 1fa0544c
......@@ -165,7 +165,10 @@ dependencies {
implementation "com.google.firebase:firebase-analytics:$fireBaseVersion"
//CrshLytics
implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta02'
implementation "com.google.firebase:firebase-crashlytics:$firebaseCrashlyticsVersion"
//RxKotlin
implementation("io.reactivex.rxjava2:rxkotlin:$rxKotlinVersion")
//Tests
testImplementation 'junit:junit:4.12'
......
......@@ -4,8 +4,10 @@ import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.data.local.UserState
import com.biganto.visual.roompark.data.repository.api.IRoomParkApi
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.model.AuthInfoModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Completable
......@@ -46,4 +48,12 @@ class AuthContractModule @Inject constructor(
else ->false
} }
override fun currentUser(): Observable<UserEntity> =
local.recentUser()
.flatMap{ when(it){
is UserState.NotAuthenticated -> throw CustomApiException.NotAuthorizedException()
is UserState.Authenticated -> db.fetchUser(it.uuid.toInt())
else -> throw CustomApiException.NotAuthorizedException()
} }
}
package com.biganto.visual.roompark.data.data_provider
import com.biganto.visual.roompark.data.repository.api.IRoomParkApi
import com.biganto.visual.roompark.data.repository.db.IDb
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.domain.contract.SubscriptionContract
import io.reactivex.Completable
import javax.inject.Inject
/**
* Created by Vladislav Bogdashkin on 25.03.2020.
*/
private const val SUBSCRIPTION_RESULT_STATUS = "OK"
class SubscriptionRepository @Inject constructor(
private val api: IRoomParkApi,
private val db: IDb
): SubscriptionContract{
private fun saveSubscribeState(
userEntity: UserEntity,
subInnerId:Int,
topic: String,
topic_id: String?,
nuewState:Boolean
): Completable {
var sub = userEntity.subscriptions?.firstOrNull { sub -> sub.id == subInnerId }
if (sub == null) {
sub = SubscriptionEntity()
sub.setOwner(userEntity)
sub.setTopic(topic)
sub.setNumber(topic_id)
}
(sub as SubscriptionEntity).setState(nuewState)
return db.saveSubscription(sub).ignoreElement()
}
override fun subscribeTopic(
user:UserEntity,
subInnerId:Int,
deviceToken: String,
topic: String,
topic_id: String?
): Completable = api.subscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken)
.switchMapCompletable {
if (it.status == SUBSCRIPTION_RESULT_STATUS){
saveSubscribeState(user,subInnerId,topic,topic_id,true)
}
else error("Error subscription state!")
}
override fun unSubscribeTopic(
user:UserEntity,
subInnerId:Int,
deviceToken: String,
topic: String,
topic_id: String?
): Completable =
api.unSuubscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken)
.switchMapCompletable {
if (it.status == SUBSCRIPTION_RESULT_STATUS){
saveSubscribeState(user,subInnerId,topic,topic_id,false)
}
else error("Error subscription state!")
}
}
\ No newline at end of file
......@@ -34,4 +34,6 @@ interface IDb {
fun fetchEstateByNumber(building: Int, number: String): ReactiveResult<EstateEntity>
fun setArticleReadState(id: Int, state: Boolean): Completable
fun setDealReadState(id: String, state: Boolean): Completable
fun saveSubscription(subscription: SubscriptionEntity): Single<SubscriptionEntity>
fun getSubscription(id: Int): ReactiveResult<SubscriptionEntity>
}
\ No newline at end of file
......@@ -147,7 +147,7 @@ class RequeryRepository @Inject constructor(
fun upsertFeeds(entity: List<FeedEntity>) =
store.upsert(entity)
override fun userObservableResult(uuid: Int) =
override fun userObservableResult(uuid: Int): Observable<ReactiveResult<UserEntity>> =
store.select(UserEntity::class)
.where(UserEntity.UUID.eq(uuid))
.get()
......@@ -175,4 +175,12 @@ class RequeryRepository @Inject constructor(
.get()
.single()
.ignoreElement()
override fun saveSubscription(subscription:SubscriptionEntity) =
store.upsert(subscription)
override fun getSubscription(id:Int): ReactiveResult<SubscriptionEntity> =
store.select(SubscriptionEntity::class)
.where(SubscriptionEntity.ID.eq(id))
.get()
}
......@@ -15,8 +15,9 @@ interface Subscription : Persistable {
@get:ForeignKey(references = User::class, referencedColumn = "uuid")
@get:ManyToOne
val owner: User
val deviceToken: String
val topic: String
val number: String
@get:Nullable
val number: String? //estateId or smth same, depends on subscription case
val state: Boolean
}
\ No newline at end of file
......@@ -24,5 +24,10 @@ interface User : Persistable {
@get:Nullable
@get:OneToMany(cascade = [CascadeAction.DELETE])
val deals:List<Deal>?
@get:Nullable
@get:OneToMany(cascade = [CascadeAction.DELETE])
val subscriptions:List<Subscription>?
}
package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.domain.model.AuthInfoModel
import io.reactivex.Completable
import io.reactivex.Observable
......@@ -12,4 +13,5 @@ interface AuthContract {
fun signIn(email:String,password:String) : Observable<AuthInfoModel>
fun signOut() : Completable
fun validateAuthState(): Observable<Boolean>
fun currentUser(): Observable<UserEntity>
}
\ No newline at end of file
package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.StatusResponse
import io.reactivex.Observable
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import io.reactivex.Completable
/**
* Created by Vladislav Bogdashkin on 24.09.2019.
*/
interface SubscriptionContract{
fun subscribeTopic(
deviceToken:String,
topic:String,
topic_id:String? = null
) : Observable<StatusResponse>
user: UserEntity,
subInnerId: Int,
deviceToken: String,
topic: String,
topic_id: String?
): Completable
fun unSubscribeTopic(
deviceToken:String,
topic:String,
topic_id:String? = null
) : Observable<StatusResponse>
user: UserEntity,
subInnerId: Int,
deviceToken: String,
topic: String,
topic_id: String?
): Completable
}
\ No newline at end of file
package com.biganto.visual.roompark.domain.use_case
//import io.reactivex.Observable
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 io.reactivex.rxkotlin.Observables
import javax.inject.Inject
//import io.reactivex.rxkotlin
/**
* Created by Vladislav Bogdashkin on 01.11.2019.
......@@ -10,25 +14,17 @@ import javax.inject.Inject
class SubscriptionUseCase @Inject constructor(
private val subscription: SubscriptionContract,
private val utils: DeviceUtilsContract
private val utils: DeviceUtilsContract,
private val auth: AuthContract
){
fun subscribeTopic(topic:String, topicId:String?=null) =
utils.getDeviceId()
.flatMap{deviceToken -> subscription.subscribeTopic(
deviceToken,
topic,
topicId
) }
fun unSubscribeTopic(topic:String, topicId:String?=null) =
utils.getDeviceId()
.flatMap{deviceToken -> subscription.unSubscribeTopic(
deviceToken,
topic,
topicId
) }
fun subscribeTopic(subId:Int,topic:String, topicId:String?=null) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ u, t ->
subscription.subscribeTopic(u,subId,t,topic = topic,topic_id = topicId)
}
fun unSubscribeTopic(subId:Int,topic:String, topicId:String?=null) =
Observables.zip(auth.currentUser(),utils.getDeviceId()){ u, t ->
subscription.subscribeTopic(u,subId,t,topic = topic,topic_id = topicId)
}
}
\ No newline at end of file
......@@ -28,4 +28,6 @@ ext {
viewPager2Version = "1.0.0"
glideVersion = "4.11.0"
fireBaseVersion = "17.2.3"
firebaseCrashlyticsVersion = "17.0.0-beta02"
rxKotlinVersion = "2.4.0"
}
\ No newline at end of file
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