commonMain.org.jetbrains.letsPlot.livemap.canvascontrols.LiveMapPresenter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of livemap-jvm Show documentation
Show all versions of livemap-jvm Show documentation
A part of the Lets-Plot library.
The newest version!
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package org.jetbrains.letsPlot.livemap.canvascontrols
import org.jetbrains.letsPlot.commons.intern.async.Async
import org.jetbrains.letsPlot.commons.intern.observable.property.Properties
import org.jetbrains.letsPlot.commons.intern.observable.property.PropertyBinding
import org.jetbrains.letsPlot.commons.intern.observable.property.ValueProperty
import org.jetbrains.letsPlot.commons.registration.Disposable
import org.jetbrains.letsPlot.commons.registration.Registration
import org.jetbrains.letsPlot.core.canvas.CanvasControl
import org.jetbrains.letsPlot.core.canvas.scheduleAsync
import org.jetbrains.letsPlot.livemap.LiveMap
class LiveMapPresenter : Disposable {
private val contentPresenter: CanvasContentPresenter
private var errorHandlerRegistration = Registration.EMPTY
private var isLoadingLiveMapRegistration = Registration.EMPTY
private var removed = false
private val initializing = ValueProperty(true)
private val liveMapIsLoading = ValueProperty(true)
val isLoading = Properties.map(Properties.or(initializing, liveMapIsLoading)) { it == true }
constructor() {
contentPresenter = CanvasContentPresenter()
}
// for tests
internal constructor(presenter: CanvasContentPresenter) {
contentPresenter = presenter
}
fun render(canvasControl: CanvasControl, liveMap: Async) {
contentPresenter.canvasControl = canvasControl
showSpinner()
canvasControl.scheduleAsync(liveMap).onResult(::showLiveMap, ::showError)
}
private fun showLiveMap(liveMap: LiveMap) {
check(isLoadingLiveMapRegistration === Registration.EMPTY) { "Unexpected" }
initializing.set(false)
isLoadingLiveMapRegistration = PropertyBinding.bindOneWay(liveMap.isLoading, liveMapIsLoading)
setContent {
LiveMapContent(liveMap).also { liveMapContent ->
errorHandlerRegistration = liveMapContent.addErrorHandler(::showError)
}
}
}
private fun showSpinner() {
initializing.set(true)
setContent(::SpinnerContent)
}
private fun showError(throwable: Throwable) {
initializing.set(false)
liveMapIsLoading.set(false)
val message = throwable.message
setContent { MessageContent(message ?: "Undefined exception") }
}
private fun setContent(canvasContentSupplier: () -> CanvasContent) {
if (removed) {
return
}
contentPresenter.show(canvasContentSupplier())
}
override fun dispose() {
removed = true
errorHandlerRegistration.dispose()
isLoadingLiveMapRegistration.dispose()
contentPresenter.clear()
}
}