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

Merge branch 'feature/userSession' into develop

parents b12f7c40 95fa0705
......@@ -140,6 +140,9 @@ dependencies {
implementation "io.requery:requery-kotlin:$requeryVersion"
kapt "io.requery:requery-processor:$requeryVersion"
//RxSharedPreferences
implementation "com.afollestad:rxkprefs:1.2.5"
//Tests
testImplementation 'junit:junit:4.12'
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
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import io.reactivex.Observable
/**
* Created by Vladislav Bogdashkin on 29.10.2019.
*/
interface IDb {
fun upsertUser(entity: UserEntity): Observable<UserEntity>?
}
\ No newline at end of file
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.requrey.model.UserEntity
import io.requery.Persistable
import io.requery.reactivex.KotlinReactiveEntityStore
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
......@@ -17,4 +17,6 @@ class RequeryRepository @Inject constructor(
)
: IDb {
override fun upsertUser(entity:UserEntity) = store.upsert(entity).toObservable()
}
\ No newline at end of file
......@@ -22,3 +22,4 @@ interface User : Persistable {
val targetResolution: Int
}
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
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.model.AuthInfoModel
import com.biganto.visual.roompark.domain.model.fromEntity
import dagger.Module
import dagger.Provides
import io.reactivex.Completable
......@@ -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 {
Timber.d("Auth Repository Created")
}
override fun signOut(): Completable = Completable.complete()
override fun signIn(email: String, password: String): Observable<AuthInfoModel> {
Timber.d("Gonna signIIn")
return Observable.just(AuthInfoModel("some tokan zzaza",1488, "hi4", "biganto@demo.ru"))
}
fun validateAuthState(): Observable<Boolean> = Observable.just(false)
override fun signOut(): Completable = local.setRecentUser(null)
override fun signIn(email: String, password: String): Observable<AuthInfoModel> =
api.authenticate(email,password)
.map ( ::fromRaw )
.flatMap{ db.upsertUser(it) }
.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()
......
......@@ -11,4 +11,5 @@ import io.reactivex.Observable
interface AuthContract {
fun signIn(email:String,password:String) : Observable<AuthInfoModel>
fun signOut() : Completable
fun validateAuthState(): Observable<Boolean>
}
\ No newline at end of file
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.
*/
......@@ -10,3 +12,10 @@ data class AuthInfoModel(
val name: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