Commit 631c032e authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fix csancellation

parent d094f0e3
...@@ -12,6 +12,7 @@ import com.biganto.visual.roompark.domain.contract.FlatPlanContract ...@@ -12,6 +12,7 @@ import com.biganto.visual.roompark.domain.contract.FlatPlanContract
import com.biganto.visual.roompark.domain.model.FeatureModel import com.biganto.visual.roompark.domain.model.FeatureModel
import com.biganto.visual.roompark.domain.model.PlanPresetModel import com.biganto.visual.roompark.domain.model.PlanPresetModel
import com.biganto.visual.roompark.domain.model.fromEntity import com.biganto.visual.roompark.domain.model.fromEntity
import com.biganto.visual.roompark.domain.use_case.DownloadUseCase
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import timber.log.Timber import timber.log.Timber
...@@ -47,6 +48,55 @@ class PlanRepository @Inject constructor( ...@@ -47,6 +48,55 @@ class PlanRepository @Inject constructor(
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
override fun getPlansObservable(list:List<PlanFeaturesVariant>
,cancellationToken: DownloadUseCase.CancellationToken)
: Observable<String> {
return Observable.create { emitter ->
list
.asSequence()
.chunked(12)
.also { System.gc() }
.flatten()
.filter { !cancellationToken.isCancelled }
.forEach { variant->
val file = FileModule.getDirectory(
file.context
, FileModule.FileDirectory.PlanTypeDir(
estateId = variant.estateId,
planId = variant.planId,
furniture = variant.furniture,
walls = variant.walls,
sizes = variant.sizes,
electric = variant.electric
))
if (file.exists())
emitter.onNext(file.absolutePath)
else{
try {
Timber.w("Cancellation is: ${cancellationToken.isCancelled}")
if (cancellationToken.isCancelled){
emitter.onComplete()
}
file.parentFile.mkdirs()
file.writeText(
api.getDirectPlan(variant.estateId, variant.planId,
variant.furniture?:false
,variant.sizes?:false
,variant.walls?:false
,variant.electric?:false).blockingFirst()
)
emitter.onNext(file.absolutePath)
}
catch (e:Throwable){emitter.onError(e)}
}
}
emitter.onComplete()
}
}
override fun getPlanTypes(estateId: Int): Observable<List<PlanPresetModel>> = override fun getPlanTypes(estateId: Int): Observable<List<PlanPresetModel>> =
Observable.mergeDelayError( Observable.mergeDelayError(
arrayListOf(getPlanTypesApi(estateId)) arrayListOf(getPlanTypesApi(estateId))
...@@ -64,16 +114,17 @@ class PlanRepository @Inject constructor( ...@@ -64,16 +114,17 @@ class PlanRepository @Inject constructor(
,sizes?:false ,sizes?:false
,walls?:false ,walls?:false
,electric?:false) ,electric?:false)
.flatMap{ .map {
val sFile = getPlanFile( val sFile = getPlanFile(
estateId = estateId, estateId = estateId,
planId = planId, planId = planId,
furniture = furniture, furniture = furniture,
walls = walls, walls = walls,
sizes = sizes, sizes = sizes,
electric = electric) electric = electric
file.saveFileToDiskObservable(sFile,it) )
.map{sFile.path} file.saveFileToDisk(sFile, it)
sFile.absolutePath
} }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
...@@ -88,7 +139,7 @@ class PlanRepository @Inject constructor( ...@@ -88,7 +139,7 @@ class PlanRepository @Inject constructor(
electric = featuresVariant.electric electric = featuresVariant.electric
) )
fun getPlanFile(estateId: Int private fun getPlanFile(estateId: Int
, planId:Int , planId:Int
, furniture:Boolean? = null , furniture:Boolean? = null
, sizes:Boolean? = null , sizes:Boolean? = null
...@@ -133,7 +184,7 @@ class PlanRepository @Inject constructor( ...@@ -133,7 +184,7 @@ class PlanRepository @Inject constructor(
furniture = furniture, furniture = furniture,
walls = walls, walls = walls,
sizes = sizes, sizes = sizes,
electric = electric) }.switchMap { electric = electric) }.flatMap {
if (it.exists()) Observable.just(it.path) if (it.exists()) Observable.just(it.path)
else getPlanApi(estateId else getPlanApi(estateId
,planId ,planId
......
...@@ -72,14 +72,14 @@ class FileModule @Inject constructor(val context: Application) { ...@@ -72,14 +72,14 @@ class FileModule @Inject constructor(val context: Application) {
val source = val source =
content.byteInputStream() content.byteInputStream()
var bytesRead = 0L var bytesRead = 0L
sink.use { Timber.w("start read ")
while ( source.read()>0) { file.writeText(content)
bytesRead += read Timber.w("butes read ")
} // sink.flush()
} System.gc()
emitter.onNext(bytesRead) emitter.onNext(bytesRead)
emitter.onComplete() // emitter.onComplete()
} }
} }
......
...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.domain.contract ...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.data.data_provider.PlanFeaturesVariant import com.biganto.visual.roompark.data.data_provider.PlanFeaturesVariant
import com.biganto.visual.roompark.domain.model.PlanPresetModel import com.biganto.visual.roompark.domain.model.PlanPresetModel
import com.biganto.visual.roompark.domain.use_case.DownloadUseCase
import io.reactivex.Observable import io.reactivex.Observable
import java.io.File import java.io.File
...@@ -22,4 +23,8 @@ interface FlatPlanContract{ ...@@ -22,4 +23,8 @@ interface FlatPlanContract{
fun getPlanFile(featuresVariant: PlanFeaturesVariant): File fun getPlanFile(featuresVariant: PlanFeaturesVariant): File
fun getPlan(featuresVariant: PlanFeaturesVariant): Observable<String> fun getPlan(featuresVariant: PlanFeaturesVariant): Observable<String>
fun getPlansObservable(
list: List<PlanFeaturesVariant>,
cancellationToken: DownloadUseCase.CancellationToken
): Observable<String>
} }
\ No newline at end of file
package com.biganto.visual.roompark.domain.use_case package com.biganto.visual.roompark.domain.use_case
import com.biganto.visual.roompark.data.data_provider.PlanFeaturesVariant
import com.biganto.visual.roompark.data.data_provider.featuresVariants import com.biganto.visual.roompark.data.data_provider.featuresVariants
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.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.contract.FlatPlanContract import com.biganto.visual.roompark.domain.contract.FlatPlanContract
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
/** /**
...@@ -40,8 +37,9 @@ class PlanTypesUseCase @Inject constructor( ...@@ -40,8 +37,9 @@ class PlanTypesUseCase @Inject constructor(
.map { list -> list.sumBy { if (planContract.getPlanFile(it).exists()) 0 else 1 } } .map { list -> list.sumBy { if (planContract.getPlanFile(it).exists()) 0 else 1 } }
.scan { t1: Int, t2: Int -> t1 + t2 } .scan { t1: Int, t2: Int -> t1 + t2 }
fun downloadAllPlanTypes(cancellationToken: DownloadUseCase.CancellationToken) = fun downloadAllPlanTypes(cancellationToken: DownloadUseCase.CancellationToken): Observable<DownloadProgress> {
fetchAllPlanTypes var listSize = 0
return fetchAllPlanTypes
.flatMapSingle { .flatMapSingle {
Observable.fromIterable(it) Observable.fromIterable(it)
.flatMap { estateEntity -> .flatMap { estateEntity ->
...@@ -52,31 +50,38 @@ class PlanTypesUseCase @Inject constructor( ...@@ -52,31 +50,38 @@ class PlanTypesUseCase @Inject constructor(
} }
.map { it.flatten() } .map { it.flatten() }
.filter { !cancellationToken.isCancelled } .filter { !cancellationToken.isCancelled }
.flatMap { downloadPlan(it, cancellationToken) } .flatMap { list ->
planContract.getPlansObservable(list,cancellationToken)
private fun downloadPlan(
list: List<PlanFeaturesVariant>
, cancellationToken: DownloadUseCase.CancellationToken
)
: Observable<DownloadProgress> {
return Observable.fromIterable(list)
.filter { !cancellationToken.isCancelled }
.flatMap {
planContract.getPlan(it)
.map { 1 } .map { 1 }
.subscribeOn(Schedulers.io()) .scan(0,
.observeOn(Schedulers.computation()) { t1, t2 -> t1 + t2 })
.scan { t1: Int, t2: Int -> t1+t2 } .map { completed ->
.map {completed ->
Timber.d("downaloadods ${completed}/${list.size}")
DownloadProgress(completed, list.size) DownloadProgress(completed, list.size)
} }
} }
} }
} }
// private fun downloadPlan(
// list: PlanFeaturesVariant
// , cancellationToken: DownloadUseCase.CancellationToken
// )
// : Observable<DownloadProgress> {
//
// val avaliablethread = true
//
// return Observable.fromIterable(list)
//
//
// .delay(100,TimeUnit.MILLISECONDS)
// .doOnNext { Timber.w("emmited") }
// .delay { variant ->
// planContract.getPlan(variant)}
// .filter { !cancellationToken.isCancelled }
//
// }
......
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