-
Notifications
You must be signed in to change notification settings - Fork 47
/
LiveMapPresenter.kt
79 lines (64 loc) · 2.44 KB
/
LiveMapPresenter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package jetbrains.livemap.canvascontrols
import jetbrains.datalore.base.async.Async
import jetbrains.datalore.base.observable.property.Properties
import jetbrains.datalore.base.observable.property.PropertyBinding
import jetbrains.datalore.base.observable.property.ValueProperty
import jetbrains.datalore.base.registration.Disposable
import jetbrains.datalore.base.registration.Registration
import jetbrains.datalore.visualization.base.canvas.CanvasControl
import jetbrains.livemap.BaseLiveMap
class LiveMapPresenter : Disposable {
private val contentPresenter: CanvasContentPresenter
private var registration = Registration.EMPTY
private var isLoadingLiveMapRegistration = Registration.EMPTY
private var removed = false
private val initializing = ValueProperty(true)
private val liveMapIsLoading = ValueProperty(true)
val isLoading = Properties.or(initializing, liveMapIsLoading)
constructor() {
contentPresenter = CanvasContentPresenter()
}
// for tests
internal constructor(presenter: CanvasContentPresenter) {
contentPresenter = presenter
}
fun render(canvasControl: CanvasControl, liveMap: Async<BaseLiveMap>) {
contentPresenter.canvasControl = canvasControl
showSpinner()
liveMap.onResult(::showLiveMap, ::showError)
}
private fun showLiveMap(liveMap: BaseLiveMap) {
if (isLoadingLiveMapRegistration !== Registration.EMPTY) {
throw IllegalStateException("Unexpected")
}
initializing.set(false)
isLoadingLiveMapRegistration = PropertyBinding.bindOneWay(liveMap.isLoading, liveMapIsLoading)
setContent {
LiveMapContent(liveMap).also {
registration = it.addHandler(::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
registration.dispose()
isLoadingLiveMapRegistration.dispose()
contentPresenter.clear()
}
}