Commit 3214a4f8 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fetch size method

parent 140839e0
...@@ -233,7 +233,7 @@ class EstateRepository @Inject constructor( ...@@ -233,7 +233,7 @@ class EstateRepository @Inject constructor(
override fun getDeals(): Observable<List<DealModel>> { override fun getDeals(): Observable<List<DealModel>> {
return Observable.mergeDelayError( return Observable.mergeDelayError(
arrayListOf( arrayListOf(
// getDealsDb, getDealsDb,
getDealsApi getDealsApi
) )
) )
......
package com.biganto.visual.roompark.data.repository.api.biganto package com.biganto.visual.roompark.data.repository.api.biganto
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesDataRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesDataRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesSimpleDataRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw
import io.reactivex.* import io.reactivex.*
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
...@@ -53,6 +54,13 @@ class BigantoRetrofitRepository @Inject constructor(@Named("bigantoApi") retrof ...@@ -53,6 +54,13 @@ class BigantoRetrofitRepository @Inject constructor(@Named("bigantoApi") retrof
.doOnError { e(it) } .doOnError { e(it) }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
override fun getTourFilesSizes(ids: List<String>, resolution: String)
: Observable<List<TourFilesSimpleDataRaw>> = api
.getTourFilesSize(ids = ids, resolution = resolution)
.compose(RetrofitResponseValidation())
.doOnError { e(it) }
.subscribeOn(Schedulers.io())
override fun downloadFile(uri: String, headers: Map<String, String>?): Flowable<ResponseBody> = api override fun downloadFile(uri: String, headers: Map<String, String>?): Flowable<ResponseBody> = api
.requestFile(headers ?: HashMap<String, String>(), uri) .requestFile(headers ?: HashMap<String, String>(), uri)
......
...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.data.repository.api.biganto ...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.data.repository.api.biganto
import com.biganto.visual.roompark.data.repository.api.biganto.raw.AppVersionRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.AppVersionRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesDataRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesDataRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesSimpleDataRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.Observable import io.reactivex.Observable
...@@ -26,4 +27,8 @@ interface IBigantoApi { ...@@ -26,4 +27,8 @@ interface IBigantoApi {
fun getOfferTours(multiTourId:Int): Observable<List<TourPreviewRaw>> fun getOfferTours(multiTourId:Int): Observable<List<TourPreviewRaw>>
fun getOfferTours(multiTourIds: List<Int>): Observable<List<TourPreviewRaw>> fun getOfferTours(multiTourIds: List<Int>): Observable<List<TourPreviewRaw>>
fun getTourFilesSizes(
ids: List<String>,
resolution: String
): Observable<List<TourFilesSimpleDataRaw>>
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.data.repository.api.biganto ...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.data.repository.api.biganto
import com.biganto.visual.roompark.data.repository.api.biganto.raw.AppVersionRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.AppVersionRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesDataRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesDataRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourFilesSimpleDataRaw
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw
import com.google.gson.JsonArray import com.google.gson.JsonArray
import io.reactivex.Flowable import io.reactivex.Flowable
...@@ -129,6 +130,16 @@ interface IBigantoMobileApi { ...@@ -129,6 +130,16 @@ interface IBigantoMobileApi {
@Query(GET_TOURS_FILES_RESOLUTION) resolution: String @Query(GET_TOURS_FILES_RESOLUTION) resolution: String
): Observable<Response<List<TourFilesDataRaw>>> ): Observable<Response<List<TourFilesDataRaw>>>
@GET("$API_URL$GET_TOURS_FILES_METHOD$DELIMITER")
fun getTourFilesSize(
@Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE,
@Query(CLIENT_VERSION_PARAM) clientVersion: String = DEFAULT_CLIENT_VERSION,
@Query(API_VERSION_PARAM) apiVersion: String = DEFAULT_API_VERSION,
@Query(LANG_PARAM) languageCode: String = Locale.getDefault().language,
@Query(GET_TOURS_FILES_ID) ids: List<String>,
@Query(GET_TOURS_FILES_RESOLUTION) resolution: String
): Observable<Response<List<TourFilesSimpleDataRaw>>>
@GET("$API_URL$GET_APP_VERSION$DELIMITER") @GET("$API_URL$GET_APP_VERSION$DELIMITER")
fun getAppVersion( fun getAppVersion(
@Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE, @Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE,
......
...@@ -82,6 +82,13 @@ data class TourFileRaw( ...@@ -82,6 +82,13 @@ data class TourFileRaw(
) )
data class TourFilesSimpleDataRaw(
val id:Int,
val resolution:Int,
val total_size:Long,
val errors:List<ErrorRaw>?
)
data class TourFilesDataRaw( data class TourFilesDataRaw(
val files:List<TourFileRaw>, val files:List<TourFileRaw>,
val id:Int, val id:Int,
......
...@@ -73,4 +73,5 @@ interface IDb { ...@@ -73,4 +73,5 @@ interface IDb {
fun dropTourFileJuncTable(): Completable fun dropTourFileJuncTable(): Completable
fun dropTourTable(): Completable fun dropTourTable(): Completable
fun refreshUser(userEntity: UserEntity): Observable<UserEntity> fun refreshUser(userEntity: UserEntity): Observable<UserEntity>
fun refreshEntities(entities: List<Persistable>): Observable<Iterable<Persistable>>
} }
\ No newline at end of file
...@@ -56,6 +56,9 @@ class RequeryRepository @Inject constructor( ...@@ -56,6 +56,9 @@ class RequeryRepository @Inject constructor(
override fun refreshUser(userEntity: UserEntity): Observable<UserEntity> = override fun refreshUser(userEntity: UserEntity): Observable<UserEntity> =
store.refresh(userEntity).toObservable() store.refresh(userEntity).toObservable()
override fun refreshEntities(entities:List<Persistable>): Observable<Iterable<Persistable>>
= store.refresh(entities).toObservable()
override fun dropTourTable() = store.delete(TourPreviewEntity::class).get().toCompletable() override fun dropTourTable() = store.delete(TourPreviewEntity::class).get().toCompletable()
override fun dropTourFileJuncTable() = store.delete(TourFileJunctionEntity::class).get().toCompletable() override fun dropTourFileJuncTable() = store.delete(TourFileJunctionEntity::class).get().toCompletable()
......
...@@ -39,6 +39,7 @@ interface Estate : Persistable { ...@@ -39,6 +39,7 @@ interface Estate : Persistable {
@get:Nullable @get:Nullable
val multitourId: Int? val multitourId: Int?
@get:Nullable @get:Nullable
@get:OneToMany(mappedBy = "estate") @get:OneToMany(mappedBy = "estate")
val tours: Set<TourPreview>? val tours: Set<TourPreview>?
......
...@@ -4,7 +4,9 @@ import android.content.res.Resources ...@@ -4,7 +4,9 @@ import android.content.res.Resources
import com.biganto.visual.roompark.data.repository.api.biganto.IBigantoApi import com.biganto.visual.roompark.data.repository.api.biganto.IBigantoApi
import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw import com.biganto.visual.roompark.data.repository.api.biganto.raw.TourPreviewRaw
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 com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.contract.TourContract
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.TourModel import com.biganto.visual.roompark.domain.model.TourModel
import com.biganto.visual.roompark.domain.model.fromEntity import com.biganto.visual.roompark.domain.model.fromEntity
...@@ -28,6 +30,7 @@ class TourPreviewsUseCase @Inject constructor( ...@@ -28,6 +30,7 @@ class TourPreviewsUseCase @Inject constructor(
private val api:IBigantoApi private val api:IBigantoApi
,private val db: IDb ,private val db: IDb
,private val auth:AuthContract ,private val auth:AuthContract
,private val tours:TourContract
) { ) {
fun getTourById(tourId:String):Observable<DownloadTourByIdResult> { fun getTourById(tourId:String):Observable<DownloadTourByIdResult> {
...@@ -171,13 +174,86 @@ class TourPreviewsUseCase @Inject constructor( ...@@ -171,13 +174,86 @@ class TourPreviewsUseCase @Inject constructor(
return fromApi return fromApi
} }
private fun getUserEstates(user:UserEntity)
= user.deals?.map { it.estate }?.plus(
user.estates
?.asSequence()
?.filter { it.favorite }
?.asIterable()
?: arrayListOf()
)
fun fetchToursSizes() =
auth.currentUser()
.map (::getUserEstates)
.map { it.filter { estates -> estates.multitourId!=null }.toList() }
.map { estates ->
val toursOffersToLoad = mutableListOf<Int>()
estates.forEach{
if (it.tours.isNullOrEmpty())
toursOffersToLoad.add(it.multitourId!!)
}
Pair(estates,toursOffersToLoad)
}
.flatMap { pair ->
if (!pair.second.isEmpty())
return@flatMap Observable.fromIterable(pair.second)
.flatMap {
fetchTourOffer(
it, pair.first.first { estete -> estete.multitourId == it }.id
)
}
.doOnNext { db.refreshEntities(pair.first) }
.map { pair.first }
else return@flatMap Observable.just(pair.first)
}
.flatMap { estates ->
val r = (estates.first().user as UserEntity).targetResolution
var knownSize = estates.flatMap {
it.tours?.map {t -> t as TourPreviewEntity}
?.asIterable()?: arrayListOf()
}.filter{t -> t.overallSize > 0L }
.sumByLong { it.overallSize - it.downloadedSize }
val toursToLoadSize =
estates.flatMap {
it.tours?.map {t -> t as TourPreviewEntity}
?.asIterable()?: arrayListOf()
}.filter{t -> t.overallSize < 1L }
if (toursToLoadSize.isNullOrEmpty()) return@flatMap Observable.just(knownSize)
else return@flatMap api.getTourFilesSizes(
toursToLoadSize.map { it.id },r.toString()
)
.flatMapIterable { it }
.flatMap { rawSize ->
db.getTourPreview(rawSize.id.toString())
.observable()
.map {entity ->
knownSize += rawSize.total_size
entity.apply {
overallSize = rawSize.total_size
targetResolution = rawSize.resolution
}
entity
}
.doOnNext{ db.upsert(it).blockingGet() }
}.map { knownSize }
}
.subscribeOn(Schedulers.io())
private inline fun <T> Iterable<T>.sumByLong(selector: (T) -> Long): Long {
var sum = 0L
for (element in this) {
sum += selector(element)
}
return sum
}
fun downloadAllDeelsAndEstates(): Observable<Iterable<TourPreviewEntity>> = fun downloadAllDeelsAndEstates(): Observable<Iterable<TourPreviewEntity>> =
auth.currentUser() auth.currentUser()
.map { user -> .map { user ->
val estatesList = val estatesList = getUserEstates(user)
user.deals?.map { it.estate }?.plus(
user.estates?.asIterable()?: arrayListOf()
)
estatesList estatesList
?.asSequence() ?.asSequence()
......
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