Commit 99e88e1d authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

subscription api fix

parent 0a690861
......@@ -54,6 +54,8 @@ class AuthContractModule @Inject constructor(
.flatMap{ when(it){
is UserState.NotAuthenticated -> throw CustomApiException.NotAuthorizedException()
is UserState.Authenticated -> db.fetchUser(it.uuid.toInt())
.doOnError { Timber.e(it) }
.doOnNext {u-> "got usr:$u" }
else -> throw CustomApiException.NotAuthorizedException()
} }
......
......@@ -18,6 +18,7 @@ class DeviceUtilsRepository @Inject constructor(
override fun getDeviceId(): Observable<String> =
Observable.create<String> {emitter ->
Timber.d("Creat observer")
FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
......
......@@ -7,6 +7,7 @@ import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.domain.contract.SubscriptionContract
import io.reactivex.Completable
import io.reactivex.Observable
import timber.log.Timber
import javax.inject.Inject
/**
......@@ -59,7 +60,8 @@ class SubscriptionRepository @Inject constructor(
topic: String,
topic_id: String?
): Completable = api.subscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken)
.switchMapCompletable {
.doOnNext { Timber.d("UUUUUUUU $it") }
.flatMapCompletable {
if (it.status == SUBSCRIPTION_RESULT_STATUS){
saveSubscribeState(user,subInnerId,topic,topic_id,true).ignoreElements()
}
......@@ -75,7 +77,8 @@ class SubscriptionRepository @Inject constructor(
topic_id: String?
): Completable =
api.unSuubscribeTopic(deviceToken = deviceToken ,topicName = topic,userToken = user.authToken)
.switchMapCompletable {
.doOnNext { Timber.d("$it") }
.flatMapCompletable {
if (it.status == SUBSCRIPTION_RESULT_STATUS){
saveSubscribeState(user,subInnerId,topic,topic_id,false).ignoreElements()
}
......
......@@ -141,24 +141,26 @@ interface IRoomParkMobileApi{
@Field(PASSWORD_AUTH_PARAM) pwd: String
): Observable<Response<AuthRaw>>
@POST("$API_URL${SUBSCRIBE_METHOD}OD$DELIMITER")
@POST("$API_URL${SUBSCRIBE_METHOD}$DELIMITER")
@FormUrlEncoded
fun subscribe(
@Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE,
@Query(CLIENT_VERSION_PARAM) clientVersion: String = DEFAULT_CLIENT_VERSION,
@Query(API_VERSION_PARAM) apiVersion: String = DEFAULT_API_VERSION,
@Query(AUTH_TOKEN) token: String,
@Query(DEVICE_TOKEN_SUBSCRIBTION_PARAM) deviceToken: String,
@Query(TOPIC_SUBSCRIBTION_PARAM) topic: String
@Field(DEVICE_TOKEN_SUBSCRIBTION_PARAM) deviceToken: String,
@Field(TOPIC_SUBSCRIBTION_PARAM) topic: String
): Observable<Response<StatusResponse>>
@POST("$API_URL${UNSUBSCRIBE_METHOD}OD$DELIMITER")
@POST("$API_URL${UNSUBSCRIBE_METHOD}$DELIMITER")
@FormUrlEncoded
fun unsubscribe(
@Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE,
@Query(CLIENT_VERSION_PARAM) clientVersion: String = DEFAULT_CLIENT_VERSION,
@Query(API_VERSION_PARAM) apiVersion: String = DEFAULT_API_VERSION,
@Query(AUTH_TOKEN) token: String,
@Query(DEVICE_TOKEN_SUBSCRIBTION_PARAM) deviceToken: String,
@Query(TOPIC_SUBSCRIBTION_PARAM) topic: String
@Field(DEVICE_TOKEN_SUBSCRIBTION_PARAM) deviceToken: String,
@Field(TOPIC_SUBSCRIBTION_PARAM) topic: String
): Observable<Response<StatusResponse>>
@GET("$API_URL$DEALS_METHOD$DELIMITER")
......
......@@ -38,6 +38,7 @@ class RetrofitRepository @Inject constructor(retrofit: Retrofit) : IRoomParkApi
topicName: String
): Observable<StatusResponse> =
api.subscribe(token = userToken, deviceToken = deviceToken, topic = topicName)
.doOnError { Timber.w(" WTFF ???") }
.compose(RetrofitResponseValidation())
override fun unSuubscribeTopic(
......
......@@ -34,7 +34,7 @@ private const val TIMEOUT_SECONDS=120L
private const val WRITE_SECONDS=120L
private const val READ_SECONDS=120L
val INTERCEPT_LOG_LEVEL = HttpLoggingInterceptor.Level.NONE
val INTERCEPT_LOG_LEVEL = HttpLoggingInterceptor.Level.BODY
@Module
class RetrofitModule{
......
......@@ -31,8 +31,8 @@ class DbModule{
Timber.d("Kotlin store creating..")
val source = DatabaseSource(context, Models.DEFAULT, "BigantoPerfect", DATABASE_VERSION)
// source.setLoggingEnabled(true)
// source.setWriteAheadLoggingEnabled(true)
source.setLoggingEnabled(true)
source.setWriteAheadLoggingEnabled(true)
source.setTableCreationMode(TableCreationMode.DROP_CREATE)
val store = KotlinEntityDataStore<Persistable>(source.configuration)
Timber.d("Kotlin store %s",source)
......@@ -51,7 +51,8 @@ class RequeryRepository @Inject constructor(
override fun upsertUser(entity: UserEntity): Observable<UserEntity> =
store.upsert(entity).toObservable()
override fun <T : Persistable> upsert(entity: T) = store.upsert(entity)
override fun <T : Persistable> upsert(entity: T): Single<T> = store.upsert(entity)
override fun <T : List<Persistable>> upsert(entity: T): Single<Iterable<Persistable>> =
store.upsert(entity)
......
......@@ -29,7 +29,7 @@ class ArticlesInteractor @Inject constructor(
when(newState){
true -> subUc.subscribeTopic(model.id,model.topic)
false -> subUc.unSubscribeTopic(model.id,model.topic)
}.ignoreElements()
}
private fun feedSubType(feed:String) =
when(feed){
......
......@@ -2,14 +2,17 @@ package com.biganto.visual.roompark.domain.use_case
//import io.reactivex.Observable
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.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.Completable
import io.reactivex.Observable
import io.reactivex.rxkotlin.Observables
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject
//import io.reactivex.rxkotlin
......@@ -23,27 +26,37 @@ class SubscriptionUseCase @Inject constructor(
private val utils: DeviceUtilsContract,
private val auth: AuthContract
) {
fun subscribeTopic(subId: Int, topic: SubscriptionTopic) =
Observables.zip(auth.currentUser(), utils.getDeviceId()) { user, token ->
subscription.subscribeTopic(
user
, subId
, token
, topic = topic.topicName,
topic_id = topic.topicId
)
}
fun subscribeTopic(subId: Int, topic: SubscriptionTopic): Completable =
Observable.zip(auth.currentUser(), utils.getDeviceId()
, BiFunction<UserEntity,String,SubscribeRequestModel> {
user, token -> SubscribeRequestModel(user,token)
})
.flatMapCompletable {
subscription.subscribeTopic(
it.user
, subId
, it.deviceToken
, topic = topic.topicName,
topic_id = topic.topicId
).subscribeOn(Schedulers.io())
}
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): Completable =
Observable.zip(auth.currentUser(), utils.getDeviceId()
, BiFunction<UserEntity,String,SubscribeRequestModel> {
user, token -> SubscribeRequestModel(user,token)
})
.flatMapCompletable {
subscription.unSubscribeTopic(
it.user
, subId
, it.deviceToken
, topic = topic.topicName,
topic_id = topic.topicId
).subscribeOn(Schedulers.io())
}
fun getSubscriptions(topic: SubscriptionTopic): Observable<SubscriptionModel> =
auth.currentUser()
......@@ -65,6 +78,11 @@ class SubscriptionUseCase @Inject constructor(
}
private data class SubscribeRequestModel(
val user:UserEntity,
val deviceToken:String
)
......@@ -38,6 +38,7 @@ class ArticlesScreenPresenter @Inject constructor(
Timber.d("feedId : $feedId")
val onSubChecked = intent(ArticlesScreen::feedSubscription)
.skip(1)
.filter { restoreModel.sub!=null }
.flatMap { newState ->
interactor.switchSubscription(restoreModel.sub!!,newState)
......
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