Commit 2ca86258 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

user session and feeds contract/uc/interactor

parent 4c5a7cc6
...@@ -53,11 +53,6 @@ abstract class BigantoBasePresenter<V : MvpView, VS> ...@@ -53,11 +53,6 @@ abstract class BigantoBasePresenter<V : MvpView, VS>
ExceptionString(R.string.no_network_error, null) ExceptionString(R.string.no_network_error, null)
) )
private fun onCodeReturn(e: CustomApiException): VS { private fun onCodeReturn(e: CustomApiException): VS =
Timber.d("2 CustomApiException ${e.messageStringId} / ${e.customMessage}") vsByCode(e.code).invoke(ExceptionString(e.messageStringId,e.customMessage))
val errst = ExceptionString(e.messageStringId,e.customMessage)
Timber.d("ExceptionString ${errst} / ${errst}")
return vsByCode(e.code).invoke(errst)
}
} }
\ No newline at end of file
package com.biganto.visual.roompark.data.data_provider package com.biganto.visual.roompark.data.data_provider
import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.data.repository.api.IRoomParkApi 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.IDb
import com.biganto.visual.roompark.data.repository.mapper.fromRaw import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.data.service.user_session.IUserSession
import com.biganto.visual.roompark.domain.contract.FeedsContract import com.biganto.visual.roompark.domain.contract.FeedsContract
import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedModel import com.biganto.visual.roompark.domain.model.FeedModel
import com.biganto.visual.roompark.domain.model.FeedsHeaderModel
import com.biganto.visual.roompark.domain.model.fromEntity import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single import io.reactivex.Single
...@@ -18,15 +19,19 @@ import javax.inject.Inject ...@@ -18,15 +19,19 @@ import javax.inject.Inject
* Created by Vladislav Bogdashkin on 29.10.2019. * Created by Vladislav Bogdashkin on 29.10.2019.
*/ */
//@Singleton
class FeedsContractModule @Inject constructor( class FeedsContractModule @Inject constructor(
private val api: IRoomParkApi, private val api: IRoomParkApi,
private val db: IDb private val db: IDb,
private val local:ILocalStore,
private val session:IUserSession
): FeedsContract { ): FeedsContract {
override fun fetchFeeds(): Single<FeedsHeaderModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} override fun fetchFeeds(): Observable<List<FeedModel>> =
fetchAllFeeds(session.token)
override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> { override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
...@@ -60,3 +65,4 @@ class FeedsContractModule @Inject constructor( ...@@ -60,3 +65,4 @@ class FeedsContractModule @Inject constructor(
).map { fromEntity(it,::fromEntity) } ).map { fromEntity(it,::fromEntity) }
} }
...@@ -12,4 +12,5 @@ interface ILocalStore { ...@@ -12,4 +12,5 @@ interface ILocalStore {
fun recentUser(): Observable<in UserState> fun recentUser(): Observable<in UserState>
fun setDownloadListDestinationTourId(tourId: String): Observable<String>? fun setDownloadListDestinationTourId(tourId: String): Observable<String>?
fun getDownloadListDestinationTourId(): Observable<String>? fun getDownloadListDestinationTourId(): Observable<String>?
fun recentUserObs(): Observable<String>
} }
\ No newline at end of file
...@@ -48,6 +48,8 @@ class UserHolder @Inject constructor(val context : Context) : ILocalStore ...@@ -48,6 +48,8 @@ class UserHolder @Inject constructor(val context : Context) : ILocalStore
prefs.string(SCROLL_LIST_TO_TOUR_KEY,"") prefs.string(SCROLL_LIST_TO_TOUR_KEY,"")
.observe() .observe()
override fun recentUserObs() = userSession.observe()
override fun recentUser(): Observable<in UserState> = override fun recentUser(): Observable<in UserState> =
userSession userSession
.observe() .observe()
......
...@@ -5,6 +5,7 @@ import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity ...@@ -5,6 +5,7 @@ import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single import io.reactivex.Single
import io.requery.Persistable import io.requery.Persistable
import io.requery.reactivex.ReactiveResult
/** /**
* Created by Vladislav Bogdashkin on 29.10.2019. * Created by Vladislav Bogdashkin on 29.10.2019.
...@@ -16,4 +17,5 @@ interface IDb { ...@@ -16,4 +17,5 @@ interface IDb {
fun fetchFeeds(): Observable<FeedEntity> fun fetchFeeds(): Observable<FeedEntity>
fun fetchUser(uuid: Int): Observable<UserEntity> fun fetchUser(uuid: Int): Observable<UserEntity>
fun <T : List<Persistable>> blockingUpsert(entity: T) fun <T : List<Persistable>> blockingUpsert(entity: T)
fun userObservableResult(uuid: Int): Observable<ReactiveResult<UserEntity>>?
} }
\ No newline at end of file
...@@ -68,4 +68,11 @@ class RequeryRepository @Inject constructor( ...@@ -68,4 +68,11 @@ class RequeryRepository @Inject constructor(
fun upsertFeeds(entity:List<FeedEntity>) = fun upsertFeeds(entity:List<FeedEntity>) =
store.upsert(entity) store.upsert(entity)
override fun userObservableResult(uuid:Int) =
store.select(UserEntity::class)
.where(UserEntity.UUID.eq(uuid))
.get()
.observableResult()
} }
\ No newline at end of file
package com.biganto.visual.roompark.data.service.user_session
import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.data.local.UserState
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import timber.log.Timber
import javax.inject.Inject
/**
* Created by Vladislav Bogdashkin on 19.11.2019.
*/
//@Singleton
class UserSessionService @Inject constructor(
private val local:ILocalStore,
private val db:IDb
):IUserSession{
private var _token:String? = null
override val token = _token?: throw CustomApiException
.UnknownCustomApiException(-1, R.string.unauthorized_user_request)
val disposable =
local.recentUserObs()
.flatMap { db.fetchUser(it.toInt()) }
.subscribe { _token = it.authToken }
val disposable2 =
local.recentUserObs()
.doOnNext { Timber.w("got new user $it") }
.flatMap { db.userObservableResult(it.toInt())
?.doOnNext { Timber.w("obs db user $it") }
?.flatMap{ reactiveResult -> reactiveResult.observable()
?.doOnNext { Timber.w("flatmapUser db user $it") } }
}
.doOnNext { Timber.w("subscribe user $it") }
.subscribe { _token = it.authToken }
fun currentUser() =
local.recentUser().map {
when(it){
is UserState.Authenticated ->it.uuid
is UserState.NotAuthenticated ->
CustomApiException
.UnknownCustomApiException(-1, R.string.unauthorized_user_request)
else -> CustomApiException
.UnknownCustomApiException(-1, R.string.unauthorized_user_request)
}
}
fun getUser(uuid:Int) = db.fetchUser(uuid).map { it.authToken }
}
class UnauthorizedUser(override var message:String,val userId:String): Exception(message)
interface IUserSession{
val token :String
}
sealed class UserSessionState(){
class Active(uuid:String):UserSessionState()
class Inactive():UserSessionState()
}
\ No newline at end of file
package com.biganto.visual.roompark.domain.contract package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedModel
import com.biganto.visual.roompark.domain.model.FeedsHeaderModel import com.biganto.visual.roompark.domain.model.FeedsHeaderModel
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single import io.reactivex.Single
...@@ -11,7 +12,7 @@ import io.reactivex.Single ...@@ -11,7 +12,7 @@ import io.reactivex.Single
interface FeedsContract{ interface FeedsContract{
fun fetchFeeds(): Single<FeedsHeaderModel> fun fetchFeeds(): Observable<List<FeedModel>>
fun fetchFeedObservable(id:Int): Observable<ArticlesPreviewModel> fun fetchFeedObservable(id:Int): Observable<ArticlesPreviewModel>
fun getArticle(id:Int): Single<ArticlesPreviewModel> fun getArticle(id:Int): Single<ArticlesPreviewModel>
} }
\ No newline at end of file
...@@ -128,14 +128,14 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In ...@@ -128,14 +128,14 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In
class TourByIdNotFoundException() : CustomApiException( class TourByIdNotFoundException() : CustomApiException(
CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND
,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID) ,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID)
class UnknownCustomApiException(code: Int, @StringRes messageStringId:Int?, apiMessage:String?) class UnknownCustomApiException(code: Int, @StringRes messageStringId: Int?)
: CustomApiException(code,messageStringId,apiMessage) : CustomApiException(code,messageStringId,apiMessage)
} }
//as an agreement error message should be correct for user (and localized, if needed) on server-side //as an agreement error message should be correct for user (and localized, if needed) on server-side
fun parseException(err: ErrorRaw) = fun parseException(err: ErrorRaw) =
CustomApiException.UnknownCustomApiException(err.code, null, err.message) CustomApiException.UnknownCustomApiException(err.code, null)
package com.biganto.visual.roompark.domain.interactor package com.biganto.visual.roompark.domain.interactor
import com.biganto.visual.roompark.domain.model.* import com.biganto.visual.roompark.domain.model.*
import com.biganto.visual.roompark.domain.use_case.FeedUseCase
import io.reactivex.Single import io.reactivex.Single
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
...@@ -10,9 +11,14 @@ import javax.inject.Inject ...@@ -10,9 +11,14 @@ import javax.inject.Inject
*/ */
class FeedsInteractor @Inject constructor( class FeedsInteractor @Inject constructor(
private val feedsUseCase:FeedUseCase
) { ) {
fun fetchTopFeeds(): Single<FeedsHeaderModel> = Single.just(testFeeds) fun fetchTopFeeds(): Single<FeedsHeaderModel> =
feedsUseCase.getFeeds().map {
FeedsHeaderModel(it)
}.single(FeedsHeaderModel(arrayListOf()))
//Single.just(testFeeds)
fun fetchArticles(feedId: Int): Single<ArticlesPreviewModel> = Single.just( fun fetchArticles(feedId: Int): Single<ArticlesPreviewModel> = Single.just(
when (feedId) { when (feedId) {
......
...@@ -9,6 +9,7 @@ import javax.inject.Inject ...@@ -9,6 +9,7 @@ import javax.inject.Inject
class AuthUseCase @Inject constructor( class AuthUseCase @Inject constructor(
private val authContract: AuthContract private val authContract: AuthContract
){ ){
fun validateAuth() = authContract.validateAuthState() fun validateAuth() = authContract.validateAuthState()
......
package com.biganto.visual.roompark.domain.use_case
import com.biganto.visual.roompark.domain.contract.FeedsContract
import javax.inject.Inject
/**
* Created by Vladislav Bogdashkin on 24.09.2019.
*/
class FeedUseCase @Inject constructor(
private val contract: FeedsContract
){
fun getFeeds() = contract.fetchFeeds()
}
\ No newline at end of file
...@@ -2,8 +2,16 @@ package com.biganto.visual.roompark.presentation.screen.feeds ...@@ -2,8 +2,16 @@ package com.biganto.visual.roompark.presentation.screen.feeds
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.base.RoomParkMainActivity import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.data.data_provider.FeedsContractModule
import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule
import com.biganto.visual.roompark.data.repository.db.requrey.DbModule
import com.biganto.visual.roompark.data.service.user_session.IUserSession
import com.biganto.visual.roompark.data.service.user_session.UserSessionService
import com.biganto.visual.roompark.di.dagger.AppComponent import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.AppModule
import com.biganto.visual.roompark.di.dagger.DataModule
import com.biganto.visual.roompark.di.dagger.PerScreen import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.contract.FeedsContract
import dagger.Binds import dagger.Binds
import dagger.BindsInstance import dagger.BindsInstance
import dagger.Component import dagger.Component
...@@ -12,7 +20,11 @@ import dagger.Module ...@@ -12,7 +20,11 @@ import dagger.Module
@PerScreen @PerScreen
@Component( @Component(
modules = [FeedsScreenModule::class], modules = [FeedsScreenModule::class
, DataModule::class
, RetrofitModule::class
, AppModule::class
, DbModule::class],
dependencies = [AppComponent::class]) dependencies = [AppComponent::class])
interface FeedsScreenComponent { interface FeedsScreenComponent {
...@@ -36,4 +48,13 @@ abstract class FeedsScreenModule{ ...@@ -36,4 +48,13 @@ abstract class FeedsScreenModule{
@Binds @Binds
abstract fun provideContext(activity: RoomParkMainActivity): Context abstract fun provideContext(activity: RoomParkMainActivity): Context
@PerScreen
@Binds
abstract fun provideContract(impl: FeedsContractModule): FeedsContract
@PerScreen
@Binds
abstract fun provideSession(impl: UserSessionService): IUserSession
} }
...@@ -4,8 +4,8 @@ import android.content.Context ...@@ -4,8 +4,8 @@ import android.content.Context
import com.biganto.visual.roompark.base.IBottomNavigation import com.biganto.visual.roompark.base.IBottomNavigation
import com.biganto.visual.roompark.base.ICollapsingToolBar import com.biganto.visual.roompark.base.ICollapsingToolBar
import com.biganto.visual.roompark.base.RoomParkMainActivity import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.data.data_provider.AuthContractModule
import com.biganto.visual.roompark.di.dagger.AppComponent import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.AuthContractModule
import com.biganto.visual.roompark.di.dagger.PerScreen import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.presentation.screen.home.home_routing.HomeBottomNavigationController import com.biganto.visual.roompark.presentation.screen.home.home_routing.HomeBottomNavigationController
......
...@@ -3,9 +3,13 @@ package com.biganto.visual.roompark.presentation.screen.splash ...@@ -3,9 +3,13 @@ package com.biganto.visual.roompark.presentation.screen.splash
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.base.IBottomNavigation import com.biganto.visual.roompark.base.IBottomNavigation
import com.biganto.visual.roompark.base.RoomParkMainActivity import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.data.data_provider.AuthContractModule
import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule
import com.biganto.visual.roompark.data.repository.db.requrey.DbModule import com.biganto.visual.roompark.data.repository.db.requrey.DbModule
import com.biganto.visual.roompark.di.dagger.* import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.AppModule
import com.biganto.visual.roompark.di.dagger.DataModule
import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.presentation.screen.auth.AuthScreenModule import com.biganto.visual.roompark.presentation.screen.auth.AuthScreenModule
import dagger.Binds import dagger.Binds
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
<string name="auth_data_requirments_failed">Login at least %d symbols <string name="auth_data_requirments_failed">Login at least %d symbols
\nPassword at least %d symbols</string> \nPassword at least %d symbols</string>
<string name="feeds_tab_divider">/</string> <string name="feeds_tab_divider">/</string>
<string name="unauthorized_user_request">Пользователь не авторизован</string>
<!--endregion--> <!--endregion-->
</resources> </resources>
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