Commit 501b0ea3 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fetch favorites complete

parent 8c5e77c4
...@@ -4,9 +4,14 @@ import com.biganto.visual.androidplayer.data.repository.local.ILocalStore ...@@ -4,9 +4,14 @@ import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.data.local.UserState import com.biganto.visual.roompark.data.local.UserState
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.db.requrey.model.EstateEntity
import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.data.repository.mapper.fromRawList
import com.biganto.visual.roompark.domain.contract.DealContract import com.biganto.visual.roompark.domain.contract.DealContract
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.model.EstateModel import com.biganto.visual.roompark.domain.model.EstateModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Observable
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
...@@ -20,23 +25,54 @@ class EstateRepository @Inject constructor( ...@@ -20,23 +25,54 @@ class EstateRepository @Inject constructor(
private val local: ILocalStore, private val local: ILocalStore,
private val api: IRoomParkApi, private val api: IRoomParkApi,
private val db: IDb private val db: IDb
): DealContract{ ): DealContract {
init { init {
Timber.d("Estate Repository Created") Timber.d("Estate Repository Created")
} }
val getFavoritesApi = private val getFavoritesApi: Observable<List<EstateEntity>> =
local.recentUser() local.recentUser()
.flatMap{ when(it){ .flatMap {
is UserState.Authenticated -> db.fetchUser(it.uuid.toInt()) when (it) {
else -> throw CustomApiException.NotAuthorizedException() is UserState.Authenticated -> db.fetchUser(it.uuid.toInt())
} } else -> throw CustomApiException.NotAuthorizedException()
.flatMap { api.getFavorites(it.authToken) }
// .map(::fromRaw)
} }
.flatMap { user ->
api.getFavorites(user.authToken)
.doOnError(Timber::e)
.map { fromRawList(it, ::fromRaw) }
.doOnNext {
it.forEach { estate ->
estate.setFavorite(true)
estate.user = user
}
}
.doOnNext(db::blockingUpsert)
}
private val getFavoritesDb: Observable<List<EstateEntity>> =
local.recentUser()
.flatMap {
when (it) {
is UserState.Authenticated -> db.fetchUser(it.uuid.toInt()).take(1)
else -> throw CustomApiException.NotAuthorizedException()
}
}
.flatMap {
db.getUserFavorites(it.uuid)
.doOnError(Timber::e)
.toList().toObservable()
}
override fun getFavorites(): io.reactivex.rxjava3.core.Observable<List<EstateModel>> { override fun getFavorites(): Observable<List<EstateModel>> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return Observable.mergeDelayError(
arrayListOf(
getFavoritesApi, getFavoritesDb
)
).map { fromEntity(it, ::fromEntity) }
.doOnError(Timber::e)
} }
......
...@@ -62,6 +62,7 @@ class UserHolder @Inject constructor(val context : Application) : ILocalStore ...@@ -62,6 +62,7 @@ class UserHolder @Inject constructor(val context : Application) : ILocalStore
.observe() .observe()
.map { .map {
Timber.d(" AUTH VALUE: $it") Timber.d(" AUTH VALUE: $it")
Timber.d(" EMPTY_PREF_VALUE_KEY: $EMPTY_PREF_VALUE_KEY")
if (it == EMPTY_PREF_VALUE_KEY) return@map UserState.NotAuthenticated() if (it == EMPTY_PREF_VALUE_KEY) return@map UserState.NotAuthenticated()
else return@map UserState.Authenticated(it) else return@map UserState.Authenticated(it)
} }
......
...@@ -26,4 +26,6 @@ interface IDb { ...@@ -26,4 +26,6 @@ interface IDb {
fun getPhotos(albumId: Int): Observable<GalleryPhotoEntity> fun getPhotos(albumId: Int): Observable<GalleryPhotoEntity>
fun getPhoto(photoId: Int): Observable<GalleryPhotoEntity> fun getPhoto(photoId: Int): Observable<GalleryPhotoEntity>
fun getAlbum(albumId: Int): Observable<ImageAlbumEntity> fun getAlbum(albumId: Int): Observable<ImageAlbumEntity>
fun getUserFavorites(uuid: Int): Observable<EstateEntity>
fun fetchAllUsers(): Observable<List<UserEntity>>
} }
\ No newline at end of file
...@@ -33,7 +33,6 @@ class DbModule{ ...@@ -33,7 +33,6 @@ class DbModule{
// 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)
return KotlinReactiveEntityStore(store) return KotlinReactiveEntityStore(store)
...@@ -123,8 +122,12 @@ class RequeryRepository @Inject constructor( ...@@ -123,8 +122,12 @@ class RequeryRepository @Inject constructor(
.firstOrNull() .firstOrNull()
override fun fetchUser(uuid: Int): Observable<UserEntity> = override fun fetchUser(uuid: Int): Observable<UserEntity> =
fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable() store.select(UserEntity::class).where(UserEntity.UUID.eq(uuid)).get().observable()
// fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable()
override fun fetchAllUsers(): Observable<List<UserEntity>> =
store.select(UserEntity::class).get().observable().toList().toObservable()
fun upsertFeeds(entity: List<FeedEntity>) = fun upsertFeeds(entity: List<FeedEntity>) =
store.upsert(entity) store.upsert(entity)
...@@ -134,4 +137,9 @@ class RequeryRepository @Inject constructor( ...@@ -134,4 +137,9 @@ class RequeryRepository @Inject constructor(
.where(UserEntity.UUID.eq(uuid)) .where(UserEntity.UUID.eq(uuid))
.get() .get()
.observableResult() .observableResult()
override fun getUserFavorites(uuid: Int): Observable<EstateEntity> =
store.select(EstateEntity::class)
.where(EstateEntity.USER_ID.eq(uuid))
.get().observable()
} }
\ No newline at end of file
...@@ -20,4 +20,9 @@ interface Deal : Persistable { ...@@ -20,4 +20,9 @@ interface Deal : Persistable {
@get:ForeignKey(references = Estate::class ) @get:ForeignKey(references = Estate::class )
@get:OneToOne(cascade = [CascadeAction.NONE]) @get:OneToOne(cascade = [CascadeAction.NONE])
var estate:Estate var estate:Estate
@get:Nullable
@get:Column(name = "UserContainer")
@get:ForeignKey(references = User::class )
@get:OneToOne(mappedBy = "uuid",cascade = [CascadeAction.NONE])
var user:User?
} }
\ No newline at end of file
...@@ -67,4 +67,12 @@ interface Estate : Persistable { ...@@ -67,4 +67,12 @@ interface Estate : Persistable {
@get:OneToMany(mappedBy = "id", cascade = [CascadeAction.SAVE, CascadeAction.DELETE]) @get:OneToMany(mappedBy = "id", cascade = [CascadeAction.SAVE, CascadeAction.DELETE])
val planPreset: MutableList<PlanPreset> val planPreset: MutableList<PlanPreset>
val favorite : Boolean
@get:Nullable
@get:Column(name = "UserContainer")
@get:ForeignKey(references = User::class )
@get:OneToOne(mappedBy = "uuid",cascade = [CascadeAction.NONE])
var user:User?
} }
\ No newline at end of file
...@@ -127,6 +127,7 @@ fun fromRaw(raw:EstateRaw):EstateEntity{ ...@@ -127,6 +127,7 @@ fun fromRaw(raw:EstateRaw):EstateEntity{
entity.setInfo_price_meter(raw.common_info.price_meter) entity.setInfo_price_meter(raw.common_info.price_meter)
entity.setInfo_ceiling(raw.common_info.ceiling) entity.setInfo_ceiling(raw.common_info.ceiling)
entity.setInfo_direction(raw.common_info.direction) entity.setInfo_direction(raw.common_info.direction)
entity.setFavorite(false)
return entity return entity
} }
......
...@@ -21,7 +21,7 @@ const val ESTATES_CACHE_LIMIT_SECONDS_INACTIVE = 200L ...@@ -21,7 +21,7 @@ const val ESTATES_CACHE_LIMIT_SECONDS_INACTIVE = 200L
const val FILES_CACHE_LIMIT_SIZE = 10000 const val FILES_CACHE_LIMIT_SIZE = 10000
const val FILES_CACHE_LIMIT_SECONDS_INACTIVE = 60L const val FILES_CACHE_LIMIT_SECONDS_INACTIVE = 60L
const val DATABASE_VERSION = 8 const val DATABASE_VERSION = 9
@Module @Module
abstract class AppModule{ abstract class AppModule{
......
...@@ -10,5 +10,5 @@ import io.reactivex.rxjava3.core.Observable ...@@ -10,5 +10,5 @@ import io.reactivex.rxjava3.core.Observable
interface DealContract{ interface DealContract{
fun getFavorites() : Observable<List<EstateModel>> fun getFavorites() : io.reactivex.Observable<List<EstateModel>>
} }
\ 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.api.retrofit.raw.EstateRaw
import com.biganto.visual.roompark.data.repository.db.requrey.model.EstateEntity import com.biganto.visual.roompark.data.repository.db.requrey.model.EstateEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.ExplicationEntity
/** /**
* Created by Vladislav Bogdashkin on 23.09.2019. * Created by Vladislav Bogdashkin on 23.09.2019.
...@@ -41,10 +39,45 @@ data class EstateModel( ...@@ -41,10 +39,45 @@ data class EstateModel(
val albumId:Int?=null, val albumId:Int?=null,
val multitourId:Int?=null, val multitourId:Int?=null,
val commonInfo:CommonInfoModel? = null, val commonInfo:CommonInfoModel? = null,
val explications:List<ExplicationListModel>? = null,
val url:String? val url:String?
) )
fun fromEntity(entity:EstateEntity): EstateModel {
val model = EstateModel(
id = entity.id,
type = entity.type,
number = entity.number,
sectionBegin = entity.sectionBegin,
sectionEnd = entity.sectionEnd,
planPNG = null,
planJPG = null,
rooms = entity.rooms,
albumId = entity.albumId,
multitourId = entity.multitourId,
commonInfo = CommonInfoModel(
building = entity.info_building,
section_begin = entity.sectionBegin,
floor = entity.info_floor,
floor_max = entity.info_floor_max,
area = entity.info_area,
area_living = entity.info_area_living,
kind = entity.info_kind,
dependent = entity.info_dependent,
decoration = entity.info_decoration,
ceiling = entity.info_ceiling,
windows_face = entity.info_window_face,
direction = entity.info_direction,
price_meter = entity.info_price_meter,
price = entity.info_price,
// discount = entity.di
// discount_amount = entity
rooms = entity.rooms
),
url = entity.url
)
return model
}
data class PlanModel( data class PlanModel(
val url:String, val url:String,
val width:Int, val width:Int,
...@@ -54,7 +87,7 @@ data class PlanModel( ...@@ -54,7 +87,7 @@ data class PlanModel(
data class CommonInfoModel( data class CommonInfoModel(
val building: Int, val building: Int,
val section_begin: Int, val section_begin: Int?,
val floor: Int, val floor: Int,
val floor_max: Int, val floor_max: Int,
val area: Float?, val area: Float?,
...@@ -67,8 +100,8 @@ data class CommonInfoModel( ...@@ -67,8 +100,8 @@ data class CommonInfoModel(
val direction: String?, val direction: String?,
val price_meter: Int?, val price_meter: Int?,
val price: Int?, val price: Int?,
val discount: Float?, val discount: Float? = null,
val discount_amount: Int?, val discount_amount: Int? = null,
val rooms: Int? val rooms: Int?
) )
......
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