-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathYellowBoxDelegate.kt
More file actions
50 lines (41 loc) · 1.59 KB
/
YellowBoxDelegate.kt
File metadata and controls
50 lines (41 loc) · 1.59 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
package com.reactnativenavigation.viewcontrollers.viewcontroller
import android.content.Context
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RestrictTo
import androidx.core.view.get
import com.reactnativenavigation.utils.isDebug
import java.util.*
open class YellowBoxDelegate(private val context: Context, private val yellowBoxHelper: YellowBoxHelper = YellowBoxHelper()) {
constructor(context: Context) : this(context, YellowBoxHelper())
var parent: ViewGroup? = null
private set
@get:RestrictTo(RestrictTo.Scope.TESTS)
val yellowBoxes: List<View>
get() = yellowBoxViews
private var isDestroyed = false
private val yellowBoxViews = ArrayList<View>()
private val tempViews = ArrayList<View>()
open fun onChildViewAdded(parent: View, child: View?) {
if (!context.isDebug()) return
onYellowBoxAdded(parent, child)
}
fun onYellowBoxAdded(parent: View?, child: View?) {
if (isDestroyed) return
this.parent = parent as ViewGroup
if (!yellowBoxHelper.isYellowBox(parent, child) && (isDestroyed || tempViews.contains(child))) return
parent as ViewGroup
for (i in 1 until parent.childCount) {
yellowBoxViews.add(parent[i])
parent.removeView(parent[i])
var tempView = View(context)
tempViews.add(tempView)
parent.addView(tempView, i)
}
}
fun destroy() {
isDestroyed = true
if (yellowBoxViews.isNotEmpty()) yellowBoxViews.forEach { parent?.addView(it) }
}
}