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

Merge branch 'feature/userSession' into develop

parents b12f7c40 95fa0705
...@@ -140,6 +140,9 @@ dependencies { ...@@ -140,6 +140,9 @@ dependencies {
implementation "io.requery:requery-kotlin:$requeryVersion" implementation "io.requery:requery-kotlin:$requeryVersion"
kapt "io.requery:requery-processor:$requeryVersion" kapt "io.requery:requery-processor:$requeryVersion"
//RxSharedPreferences
implementation "com.afollestad:rxkprefs:1.2.5"
//Tests //Tests
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:runner:1.2.0'
......
package com.biganto.visual.androidplayer.data.repository.local
import com.biganto.visual.roompark.data.local.UserState
import io.reactivex.Completable
import io.reactivex.Observable
/**
* Created by Vladislav Bogdashkin on 14.06.2018.
*/
interface ILocalStore {
fun setRecentUser(uuid: String?): Completable
fun recentUser(): Observable<in UserState>
fun setDownloadListDestinationTourId(tourId: String): Observable<String>?
fun getDownloadListDestinationTourId(): Observable<String>?
}
\ No newline at end of file
package com.biganto.visual.roompark.data.local
import android.content.Context
import com.afollestad.rxkprefs.rxkPrefs
import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import io.reactivex.Completable
import io.reactivex.Observable
import javax.inject.Inject
import javax.inject.Singleton
/**
* Created by Vladislav Bogdashkin on 14.06.2018.
*/
@Singleton
class UserHolder @Inject constructor(val context : Context) : ILocalStore
{
private val prefs = rxkPrefs(context)
companion object {
const val RECENT_UUID_KEY = "com.biganto.visual.androidplayer.LAST_USER_UUD"
const val EMPTY_PREF_VALUE_KEY = "NO_ACTIVE_SESSION"
const val SCROLL_LIST_TO_TOUR_KEY="SCROLL_LIST_TO_TOUR_ID"
}
override fun setDownloadListDestinationTourId(tourId:String) =
prefs.string(SCROLL_LIST_TO_TOUR_KEY,tourId)
.observe()
override fun getDownloadListDestinationTourId() =
prefs.string(SCROLL_LIST_TO_TOUR_KEY,"")
.observe()
override fun recentUser(): Observable<in UserState> =
prefs.string(RECENT_UUID_KEY, EMPTY_PREF_VALUE_KEY)
.observe()
.map {
if (it == EMPTY_PREF_VALUE_KEY) return@map UserState.NotAuthenticated()
else return@map UserState.Authenticated(it)
}
override fun setRecentUser(uuid: String?) =
Completable.fromObservable(prefs.string(RECENT_UUID_KEY, uuid ?: EMPTY_PREF_VALUE_KEY).observe())
}
sealed class UserState{
class NotAuthenticated
data class Authenticated(val uuid: String)
}
data class UserPrefModel(
val uuid:String
,val name:String
,val email:String
,val timeZone:String)
package com.biganto.visual.roompark.data.repository.db package com.biganto.visual.roompark.data.repository.db
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import io.reactivex.Observable
/** /**
* Created by Vladislav Bogdashkin on 29.10.2019. * Created by Vladislav Bogdashkin on 29.10.2019.
*/ */
interface IDb { interface IDb {
fun upsertUser(entity: UserEntity): Observable<UserEntity>?
} }
\ No newline at end of file
package com.biganto.visual.roompark.data.repository.db.requrey package com.biganto.visual.roompark.data.repository.db.requrey
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.db.requrey.model.UserEntity
import io.requery.Persistable import io.requery.Persistable
import io.requery.reactivex.KotlinReactiveEntityStore import io.requery.reactivex.KotlinReactiveEntityStore
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
...@@ -17,4 +17,6 @@ class RequeryRepository @Inject constructor( ...@@ -17,4 +17,6 @@ class RequeryRepository @Inject constructor(
) )
: IDb { : IDb {
override fun upsertUser(entity:UserEntity) = store.upsert(entity).toObservable()
} }
\ No newline at end of file
...@@ -21,4 +21,5 @@ interface User : Persistable { ...@@ -21,4 +21,5 @@ interface User : Persistable {
val authToken: String val authToken: String
val targetResolution: Int val targetResolution: Int
} }
\ No newline at end of file
package com.biganto.visual.roompark.data.repository.mapper
import android.content.res.Resources
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.AuthRaw
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import kotlin.math.max
/**
* Created by Vladislav Bogdashkin on 06.11.2019.
*/
private val display = Resources.getSystem().displayMetrics
fun fromRaw(raw: AuthRaw) : UserEntity {
val user = UserEntity()
user.setEmail(raw.email)
user.setUuid(raw.id)
user.setAuthToken(raw.token)
user.setName(raw.name)
user.setTargetResolution(calcTargetResolution)
return user
}
val calcTargetResolution = max(display.widthPixels,display.heightPixels)
package com.biganto.visual.roompark.di.dagger package com.biganto.visual.roompark.di.dagger
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.mapper.fromRaw
import com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.model.AuthInfoModel import com.biganto.visual.roompark.domain.model.AuthInfoModel
import com.biganto.visual.roompark.domain.model.fromEntity
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import io.reactivex.Completable import io.reactivex.Completable
...@@ -33,20 +39,31 @@ class AppModule() { ...@@ -33,20 +39,31 @@ class AppModule() {
} }
class AuthContractModule @Inject constructor():AuthContract{ class AuthContractModule @Inject constructor(
val local:ILocalStore,
val api:IRoomParkApi,
val db:IDb
):AuthContract{
init { init {
Timber.d("Auth Repository Created") Timber.d("Auth Repository Created")
} }
override fun signOut(): Completable = Completable.complete()
override fun signIn(email: String, password: String): Observable<AuthInfoModel> { override fun signOut(): Completable = local.setRecentUser(null)
Timber.d("Gonna signIIn") override fun signIn(email: String, password: String): Observable<AuthInfoModel> =
return Observable.just(AuthInfoModel("some tokan zzaza",1488, "hi4", "biganto@demo.ru")) api.authenticate(email,password)
} .map ( ::fromRaw )
.flatMap{ db.upsertUser(it) }
fun validateAuthState(): Observable<Boolean> = Observable.just(false) .doOnNext { local.setRecentUser(it.uuid.toString()) }
.map(::fromEntity)
override fun validateAuthState(): Observable<Boolean> = local.recentUser()
.map { when(it){
is UserState.NotAuthenticated -> false
is UserState.Authenticated -> true
else ->false
} }
} }
@Module() @Module()
......
...@@ -11,4 +11,5 @@ import io.reactivex.Observable ...@@ -11,4 +11,5 @@ import io.reactivex.Observable
interface AuthContract { interface AuthContract {
fun signIn(email:String,password:String) : Observable<AuthInfoModel> fun signIn(email:String,password:String) : Observable<AuthInfoModel>
fun signOut() : Completable fun signOut() : Completable
fun validateAuthState(): Observable<Boolean>
} }
\ No newline at end of file
package com.biganto.visual.roompark.domain.model package com.biganto.visual.roompark.domain.model
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
/** /**
* Created by Vladislav Bogdashkin on 24.09.2019. * Created by Vladislav Bogdashkin on 24.09.2019.
*/ */
...@@ -9,4 +11,11 @@ data class AuthInfoModel( ...@@ -9,4 +11,11 @@ data class AuthInfoModel(
val id:Int, val id:Int,
val name:String, val name:String,
val email:String val email:String
)
fun fromEntity(entity:UserEntity) = AuthInfoModel(
entity.authToken,
entity.uuid,
entity.name,
entity.email
) )
\ 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