-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathColumnView.kt
More file actions
96 lines (85 loc) · 3.01 KB
/
Copy pathColumnView.kt
File metadata and controls
96 lines (85 loc) · 3.01 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package expo.modules.jetpackcomposereactnative.views.column
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.viewinterop.AndroidView
import expo.modules.jetpackcomposereactnative.common.ModifierProp
import expo.modules.jetpackcomposereactnative.common.toModifier
import expo.modules.jetpackcomposereactnative.views.carousel.CarouselProps
import expo.modules.jetpackcomposereactnative.views.carousel.CarouselView
import expo.modules.kotlin.AppContext
import expo.modules.kotlin.views.ExpoView
data class ColumnProps(
var children: List<View> = emptyList(),
var modifier: ModifierProp = emptyList(),
var lazy: Boolean = false,
var lastItem: View? = null,
)
class ColumnView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
private var props = mutableStateOf(ColumnProps())
override fun addView(child: View?, index: Int) {
if (child is ComposeView) {
super.addView(child, index)
} else {
if (child != null) {
props.value = props.value.copy(children = props.value.children + child)
}
}
}
init {
ComposeView(context).also {
it.layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT) // Allow the content to wrap
it.setContent {
if (props.value.lazy) {
LazyColumnComposable(props = props.value)
} else {
ColumnComposable(props = props.value)
}
}
addView(it)
}
}
fun updateLazy(lazy: Boolean) {
props.value = props.value.copy(lazy = lazy)
}
fun updateModifier(modifier: ModifierProp) {
props.value = props.value.copy(modifier = modifier)
}
}
@Composable
fun ColumnComposable(props: ColumnProps) {
Column(modifier = props.modifier.toModifier()) {
props.children.map { child ->
AndroidView(
modifier = Modifier.fillMaxWidth(),
factory = { child },
)
}
}
}
@Composable
fun LazyColumnComposable(props: ColumnProps) {
val totalItems = props.children.size
LazyColumn(modifier = props.modifier.toModifier()) {
items(totalItems) { index ->
AndroidView(
modifier = Modifier.fillMaxWidth(),
factory = { context ->
props.children[index].apply {
layoutParams = ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
}
},
)
}
// To be added soon...
item {}
}
}