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