-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathOverlayManager.kt
More file actions
76 lines (64 loc) · 2.6 KB
/
OverlayManager.kt
File metadata and controls
76 lines (64 loc) · 2.6 KB
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
package com.reactnativenavigation.viewcontrollers.overlay
import android.content.res.Configuration
import android.view.View
import android.view.ViewGroup
import com.reactnativenavigation.react.CommandListener
import com.reactnativenavigation.utils.CoordinatorLayoutUtils
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
import com.reactnativenavigation.views.BehaviourDelegate
class OverlayManager {
private val overlayRegistry = mutableMapOf<String, ViewController<*>>()
fun show(overlaysContainer: ViewGroup, overlay: ViewController<*>, listener: CommandListener) {
overlaysContainer.visibility = View.VISIBLE
overlayRegistry[overlay.id] = overlay
overlay.addOnAppearedListener {
overlay.onViewDidAppear()
listener.onSuccess(overlay.id)
}
overlaysContainer.addView(
overlay.view,
CoordinatorLayoutUtils.matchParentWithBehaviour(BehaviourDelegate(overlay))
)
}
fun onConfigurationChanged(configuration: Configuration?) {
overlayRegistry.values.forEach { controller -> controller.onConfigurationChanged(configuration) }
}
fun dismiss(overlaysContainer: ViewGroup, componentId: String, listener: CommandListener) {
val overlay = overlayRegistry.remove(componentId)
if (overlay == null) {
listener.onError("Could not dismiss Overlay. Overlay with id $componentId was not found.")
} else {
destroyOverlay(overlaysContainer, overlay)
listener.onSuccess(componentId)
}
}
fun dismissAll(overlaysContainer: ViewGroup, listener: CommandListener) {
destroy(overlaysContainer)
listener.onSuccess("")
}
fun destroy(overlaysContainer: ViewGroup) {
val removedOverlays = overlayRegistry.values.map { overlay ->
destroyOverlay(overlaysContainer, overlay)
overlay.id
}.toList()
removedOverlays.forEach {
overlayRegistry.remove(it)
}
}
fun size() = overlayRegistry.size
fun findControllerById(id: String?): ViewController<*>? {
return overlayRegistry[id]
}
private fun destroyOverlay(overlaysContainer: ViewGroup, overlay: ViewController<*>) {
overlay.destroy()
if (isEmpty) overlaysContainer.visibility = View.GONE
}
private val isEmpty: Boolean
get() = size() == 0
fun onHostPause() {
overlayRegistry.values.forEach(ViewController<*>::onViewDisappear)
}
fun onHostResume() {
overlayRegistry.values.forEach(ViewController<*>::onViewDidAppear)
}
}