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

subscription api fix

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