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

fix files save flow

parent 99cecf5f
...@@ -64,7 +64,7 @@ class PlanRepository @Inject constructor( ...@@ -64,7 +64,7 @@ class PlanRepository @Inject constructor(
,sizes?:false ,sizes?:false
,walls?:false ,walls?:false
,electric?:false) ,electric?:false)
.map{ .flatMap{
val sFile = getPlanFile( val sFile = getPlanFile(
estateId = estateId, estateId = estateId,
planId = planId, planId = planId,
...@@ -72,8 +72,8 @@ class PlanRepository @Inject constructor( ...@@ -72,8 +72,8 @@ class PlanRepository @Inject constructor(
walls = walls, walls = walls,
sizes = sizes, sizes = sizes,
electric = electric) electric = electric)
file.saveFileToDisk(sFile,it) file.saveFileToDiskObservable(sFile,it)
sFile.path .map{sFile.path}
} }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
......
...@@ -8,6 +8,7 @@ import com.google.gson.JsonElement ...@@ -8,6 +8,7 @@ import com.google.gson.JsonElement
import dagger.Module import dagger.Module
import io.reactivex.Observable import io.reactivex.Observable
import kotlinx.io.IOException import kotlinx.io.IOException
import okio.Okio
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
...@@ -58,6 +59,31 @@ class FileModule @Inject constructor(val context: Application) { ...@@ -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.. 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){ fun saveFileToDisk(file:File,content: String){
Timber.d("write to : $file") Timber.d("write to : $file")
// file.createNewFile() // file.createNewFile()
......
...@@ -7,6 +7,7 @@ import com.biganto.visual.roompark.domain.contract.AuthContract ...@@ -7,6 +7,7 @@ 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 io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
/** /**
...@@ -59,16 +60,18 @@ class PlanTypesUseCase @Inject constructor( ...@@ -59,16 +60,18 @@ class PlanTypesUseCase @Inject constructor(
, cancellationToken: DownloadUseCase.CancellationToken , cancellationToken: DownloadUseCase.CancellationToken
) )
: Observable<DownloadProgress> { : Observable<DownloadProgress> {
var completedThreads = 0
return Observable.fromIterable(list) return Observable.fromIterable(list)
.filter { !cancellationToken.isCancelled } .filter { !cancellationToken.isCancelled }
.flatMap { .flatMap {
planContract.getPlan(it) planContract.getPlan(it)
.map { 1 }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.map { .observeOn(Schedulers.computation())
completedThreads++ .scan { t1: Int, t2: Int -> t1+t2 }
DownloadProgress(completedThreads, list.size) .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