Commit d094f0e3 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fix files save flow

parent 99cecf5f
......@@ -64,7 +64,7 @@ class PlanRepository @Inject constructor(
,sizes?:false
,walls?:false
,electric?:false)
.map{
.flatMap{
val sFile = getPlanFile(
estateId = estateId,
planId = planId,
......@@ -72,8 +72,8 @@ class PlanRepository @Inject constructor(
walls = walls,
sizes = sizes,
electric = electric)
file.saveFileToDisk(sFile,it)
sFile.path
file.saveFileToDiskObservable(sFile,it)
.map{sFile.path}
}
.subscribeOn(Schedulers.io())
......
......@@ -8,6 +8,7 @@ import com.google.gson.JsonElement
import dagger.Module
import io.reactivex.Observable
import kotlinx.io.IOException
import okio.Okio
import timber.log.Timber
import java.io.File
import javax.inject.Inject
......@@ -58,6 +59,31 @@ class FileModule @Inject constructor(val context: Application) {
file.writeText("[$jsonElement]") //to json array because core unity method parse data like TourData[] Estate[] etc..
}
fun saveFileToDiskObservable(file:File,content: String):Observable<Long> {
file.parentFile.mkdirs()
return Observable.create { emitter ->
val fileStorage = file
val sink = Okio.buffer(Okio.sink(fileStorage))
val buffer = sink.buffer()
var read = 0L
val step = 8192
val source =
content.byteInputStream()
var bytesRead = 0L
sink.use {
while ( source.read()>0) {
bytesRead += read
}
}
emitter.onNext(bytesRead)
emitter.onComplete()
}
}
fun saveFileToDisk(file:File,content: String){
Timber.d("write to : $file")
// file.createNewFile()
......
......@@ -7,6 +7,7 @@ import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.contract.FlatPlanContract
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject
/**
......@@ -59,16 +60,18 @@ class PlanTypesUseCase @Inject constructor(
, cancellationToken: DownloadUseCase.CancellationToken
)
: Observable<DownloadProgress> {
var completedThreads = 0
return Observable.fromIterable(list)
.filter { !cancellationToken.isCancelled }
.flatMap {
planContract.getPlan(it)
.map { 1 }
.subscribeOn(Schedulers.io())
.map {
completedThreads++
DownloadProgress(completedThreads, list.size)
.observeOn(Schedulers.computation())
.scan { t1: Int, t2: Int -> t1+t2 }
.map {completed ->
Timber.d("downaloadods ${completed}/${list.size}")
DownloadProgress(completed, list.size)
}
}
}
......
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