Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 221ea16

Browse files
authored
Add support for Transition object (#250)
also fix issue with composition before layout
1 parent 4450b7c commit 221ea16

File tree

8 files changed

+394
-121
lines changed

8 files changed

+394
-121
lines changed

constraintlayout/compose/src/main/java/androidx/constraintlayout/compose/ConstraintLayout.kt

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package androidx.constraintlayout.compose
1818

19+
import android.annotation.SuppressLint
1920
import android.util.Log
2021
import androidx.annotation.FloatRange
2122
import androidx.compose.foundation.layout.LayoutScopeMarker
@@ -36,6 +37,7 @@ import androidx.compose.ui.unit.dp
3637
import androidx.compose.ui.util.fastForEach
3738
import androidx.constraintlayout.core.state.ConstraintReference
3839
import androidx.constraintlayout.core.state.Dimension.*
40+
import androidx.constraintlayout.core.state.Transition
3941
import androidx.constraintlayout.core.state.WidgetFrame
4042
import androidx.constraintlayout.core.widgets.ConstraintWidget
4143
import androidx.constraintlayout.core.widgets.ConstraintWidget.DimensionBehaviour.FIXED
@@ -1242,31 +1244,51 @@ interface ConstraintSet {
12421244
fun applyTo(state: State, measurables: List<Measurable>)
12431245

12441246
fun override(name: String, value: Float) : ConstraintSet
1247+
fun applyTo(transition: Transition, type: Int) {
1248+
// nothing here, used in MotionLayout
1249+
}
12451250
}
12461251

1247-
fun ConstraintSet(@Language("json5") content : String) = object : ConstraintSet {
1248-
private val overridedVariables = HashMap<String, Float>()
1252+
@SuppressLint("ComposableNaming")
1253+
@Composable
1254+
fun ConstraintSet(@Language("json5") content : String) : ConstraintSet {
1255+
val constraintset = remember {
1256+
mutableStateOf(object : ConstraintSet {
1257+
private val overridedVariables = HashMap<String, Float>()
1258+
1259+
override fun applyTo(transition: Transition, type: Int) {
1260+
val layoutVariables = LayoutVariables()
1261+
for (name in overridedVariables.keys) {
1262+
layoutVariables.putOverride(name, overridedVariables[name]!!)
1263+
}
1264+
parseJSON(content, transition, type, layoutVariables)
1265+
}
12491266

1250-
override fun applyTo(state: State, measurables: List<Measurable>) {
1251-
measurables.forEach { measurable ->
1252-
var layoutId = measurable.layoutId ?: measurable.constraintLayoutId ?: createId()
1253-
state.map(layoutId, measurable)
1254-
var tag = measurable.constraintLayoutTag
1255-
if (tag != null && tag is String && layoutId is String) {
1256-
state.setTag(layoutId, tag)
1267+
override fun applyTo(state: State, measurables: List<Measurable>) {
1268+
measurables.forEach { measurable ->
1269+
val layoutId =
1270+
measurable.layoutId ?: measurable.constraintLayoutId ?: createId()
1271+
state.map(layoutId, measurable)
1272+
val tag = measurable.constraintLayoutTag
1273+
if (tag != null && tag is String && layoutId is String) {
1274+
state.setTag(layoutId, tag)
1275+
}
1276+
}
1277+
val layoutVariables = LayoutVariables()
1278+
for (name in overridedVariables.keys) {
1279+
layoutVariables.putOverride(name, overridedVariables[name]!!)
1280+
}
1281+
parseJSON(content, state, layoutVariables)
12571282
}
1258-
}
1259-
val layoutVariables = LayoutVariables()
1260-
for (name in overridedVariables.keys) {
1261-
layoutVariables.putOverride(name, overridedVariables[name]!!)
1262-
}
1263-
parseJSON(content, state, layoutVariables)
1264-
}
12651283

1266-
override fun override(name: String, value: Float) : ConstraintSet {
1267-
overridedVariables[name] = value
1268-
return this
1284+
override fun override(name: String, value: Float): ConstraintSet {
1285+
overridedVariables[name] = value
1286+
return this
1287+
}
1288+
})
12691289
}
1290+
1291+
return constraintset.value
12701292
}
12711293

12721294
/**

constraintlayout/compose/src/main/java/androidx/constraintlayout/compose/ConstraintSetParser.kt

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.constraintlayout.core.state.ConstraintReference
2020
import androidx.constraintlayout.core.state.Dimension
2121
import androidx.constraintlayout.core.state.Dimension.SPREAD_DIMENSION
2222
import androidx.constraintlayout.core.state.State.Chain.*
23+
import androidx.constraintlayout.core.state.Transition
2324
import androidx.constraintlayout.core.state.helpers.GuidelineReference
2425
import androidx.constraintlayout.core.widgets.ConstraintWidget
2526
import org.json.JSONArray
@@ -105,6 +106,51 @@ class OverrideValue(value: Float) : GeneratedValue {
105106
}
106107
}
107108

109+
internal fun parseJSON(content: String, transition: Transition,
110+
state: Int, layoutVariables: LayoutVariables) {
111+
val json = JSONObject(content)
112+
val elements = json.names() ?: return
113+
(0 until elements.length()).forEach { i ->
114+
val elementName = elements[i].toString()
115+
val element = json[elementName]
116+
if (element is JSONObject) {
117+
val customProperties = element.optJSONObject("custom")
118+
if (customProperties != null) {
119+
val properties = customProperties.names() ?: return
120+
(0 until properties.length()).forEach { i ->
121+
val property = properties[i].toString()
122+
val value = customProperties[property]
123+
if (value is Int) {
124+
transition.addCustomFloat(state, elementName, property, value.toFloat())
125+
} else if (value is Float) {
126+
transition.addCustomFloat(state, elementName, property, value)
127+
} else if (value is String) {
128+
if (value.startsWith('#')) {
129+
var r = 0f
130+
var g = 0f
131+
var b = 0f
132+
var a = 1f
133+
if (value.length == 7 || value.length == 9) {
134+
var hr = Integer.valueOf(value.substring(1, 3), 16)
135+
var hg = Integer.valueOf(value.substring(3, 5), 16)
136+
var hb = Integer.valueOf(value.substring(5, 7), 16)
137+
r = hr.toFloat() / 255f
138+
g = hg.toFloat() / 255f
139+
b = hb.toFloat() / 255f
140+
}
141+
if (value.length == 9) {
142+
var ha = Integer.valueOf(value.substring(5, 7), 16)
143+
a = ha.toFloat() / 255f
144+
}
145+
transition.addCustomColor(state, elementName, property, r, g, b, a)
146+
}
147+
}
148+
}
149+
}
150+
}
151+
}
152+
}
153+
108154
internal fun parseJSON(content: String, state: State, layoutVariables: LayoutVariables) {
109155
val json = JSONObject(content)
110156
val elements = json.names() ?: return
@@ -418,7 +464,7 @@ fun parseWidget(
418464
reference.rotationZ(value) // element.getDouble(constraintName).toFloat())
419465
}
420466
"custom" -> {
421-
parseCustomProperties(state, layoutVariables, element, reference, constraintName)
467+
parseCustomProperties(element, reference, constraintName)
422468
}
423469
else -> {
424470
parseConstraint(state, layoutVariables, element, reference, constraintName)
@@ -428,8 +474,6 @@ fun parseWidget(
428474
}
429475

430476
private fun parseCustomProperties(
431-
state: State,
432-
layoutVariables: LayoutVariables,
433477
element: JSONObject,
434478
reference: ConstraintReference,
435479
constraintName: String

0 commit comments

Comments
 (0)