Commit 1b342e91 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fix flow flatamapiterable;

complete plan types download
parent 6d527311
......@@ -8,7 +8,6 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.annotation.LayoutRes
import androidx.core.os.bundleOf
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.base.BaseRoomParkActivity
import com.biganto.visual.roompark.base.RoomParkApplication
......@@ -17,12 +16,8 @@ import com.biganto.visual.roompark.di.dagger.ActivityModule
import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.interactor.SettingsInteractor
import com.biganto.visual.roompark.domain.interactor.ToursInteractor
import com.biganto.visual.roompark.domain.model.TourModel
import com.biganto.visual.roompark.domain.model.fromEntity
import com.biganto.visual.roompark.domain.use_case.DownloadUseCase
import com.biganto.visual.roompark.util.view_utils.snackbar.ISnackBarProvider
import com.biganto.visual.roomparkvr.data.repository.db.requery.model.TourPreviewEntity
import com.bluelinelabs.conductor.Controller
import com.bumptech.glide.Glide
import com.google.android.material.textview.MaterialTextView
......@@ -60,7 +55,7 @@ class DownloadPlansDialogController : Controller {
override fun onContextAvailable(context: Context) {
super.onContextAvailable(context)
PlansDownloaderScreenComponent.factory()
DaggerPlansDownloaderScreenComponent.factory()
.create(RoomParkApplication.component,activity as RoomParkMainActivity)
.inject(this)
}
......@@ -100,7 +95,8 @@ class DownloadPlansDialogController : Controller {
Glide.with(view)
.load(R.drawable.ic_plan)
.transform(BlurTransformation(13, 8)
.fitCenter()
.transform(BlurTransformation(25, 12)
,ColorFilterTransformation(0x99000000.toInt()))
.into(downloaderBg)
......@@ -113,8 +109,10 @@ class DownloadPlansDialogController : Controller {
{ model ->
downloadTourTitleText.text = "${model.currentProgress}/${model.totalProgress}"
updateProgressBar(model.currentProgress, model.totalProgress)
snackbar.showSnackBar(R.string.plan_types_download_completed)
handleBack()
if (model.currentProgress == model.totalProgress){
snackbar.showSnackBar(R.string.plan_types_download_completed)
handleBack()
}
}
,{error ->
Timber.e(error)
......@@ -153,7 +151,7 @@ class DownloadPlansDialogController : Controller {
@PerScreen
@Component(
modules = [DownloaderScreenModule::class],
modules = [PlansDownloaderScreenModule::class],
dependencies = [AppComponent::class])
interface PlansDownloaderScreenComponent {
......@@ -171,11 +169,7 @@ interface PlansDownloaderScreenComponent {
}
@Module
abstract class DownloaderScreenModule{
@PerScreen
@Binds
abstract fun provideContext(activity: RoomParkMainActivity): Context
abstract class PlansDownloaderScreenModule{
@PerScreen
@Binds
......
......@@ -6,7 +6,6 @@ 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.FlatPlanContract
import io.reactivex.Observable
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
......@@ -28,49 +27,50 @@ class PlanTypesUseCase @Inject constructor(
list.addAll(user.estates?.map { it as EstateEntity } ?: arrayListOf())
list
}
fun fetchNotDownloadedPlansSizes(): Observable<Int> =
fetchAllPlanTypes
.flatMapIterable { it }
.flatMap {
planContract.getPlanTypes(it.id)
.map { plantypes -> plantypes.flatMap { plan -> plan.featuresVariants } }
}
fun fetchNotDownloadedPlansSizes(): Observable<Int> =
fetchAllPlanTypes
.map { list -> list.sumBy { if (planContract.getPlanFile(it).exists()) 0 else 1 } }
.scan { t1: Int, t2: Int -> t1 + t2 }
fun downloadAllPlanTypes(cancellationToken: DownloadUseCase.CancellationToken) =
fetchAllPlanTypes
.flatMapSingle {
Observable.fromIterable(it)
.flatMap { estateEntity ->
planContract.getPlanTypes(estateEntity.id)
.map { plantypes -> plantypes.flatMap { plan -> plan.featuresVariants } }
}
.toList()
}
.map { it.flatten() }
.filter { !cancellationToken.isCancelled }
.flatMap {downloadPlan(it,cancellationToken)}
.flatMap { downloadPlan(it, cancellationToken) }
fun downloadPlan(
private fun downloadPlan(
list: List<PlanFeaturesVariant>
, cancellationToken: DownloadUseCase.CancellationToken
)
: Observable<DownloadProgress> {
var runThreads = list.size
var completedThreads = 0
return Observable.create<DownloadProgress> { emitter ->
list.forEach {
return Observable.fromIterable(list)
.filter { !cancellationToken.isCancelled }
.flatMap {
planContract.getPlan(it)
.filter { !cancellationToken.isCancelled }
.subscribeOn(Schedulers.io())
.map { 1 }
.scan { t1: Int, t2: Int -> t1.plus(t2) }
.subscribe {
emitter.onNext(DownloadProgress(it, list.size))
.map {
completedThreads++
DownloadProgress(completedThreads, list.size)
}
}
while (completedThreads<runThreads && cancellationToken.isCancelled.not())
Thread.sleep(100)
if (cancellationToken.isCancelled)
emitter.onError(Throwable("Cacnelled by user"))
emitter.onComplete()
}.subscribeOn(Schedulers.computation())
}
}
......
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