Skip to content

Instantly share code, notes, and snippets.

@jossiwolf
Last active May 19, 2021 23:53
Show Gist options
  • Save jossiwolf/b018bd39b5e88bbb55ef02552a5cb67e to your computer and use it in GitHub Desktop.
Save jossiwolf/b018bd39b5e88bbb55ef02552a5cb67e to your computer and use it in GitHub Desktop.
/**
* Merges two [ConstraintSet]s. If a Constraint exists in the first [ConstraintSet] and the second one, the original value will be replaced by the new value.
*/
operator fun ConstraintSet.plus(other: ConstraintSet): ConstraintSet {
return this.copy().apply { updateWith(other) }
}
/**
* Clears the existing constraints and sets them to those of the [constraintSet]
*/
fun ConstraintSet.setConstraints(constraintSet: ConstraintSet) {
val field = ConstraintSet::class.java.getDeclaredField("mConstraints")
field.isAccessible = true
val theseConstraints: HashMap<Int, Any> = field.get(this) as HashMap<Int, Any>
val newConstraints: HashMap<Int, Any> = field.get(constraintSet) as HashMap<Int, Any>
theseConstraints.clear()
theseConstraints.putAll(newConstraints)
}
/**
* Copies the [ConstraintSet] into a new [ConstraintSet]
*/
fun ConstraintSet.copy(): ConstraintSet = ConstraintSet().apply { clone(this@copy) }
/**
* Clears the constraints of this [ConstraintSet] using reflection
*/
@Suppress("UNCHECKED_CAST")
fun ConstraintSet.clearConstraints() {
val field = ConstraintSet::class.java.getDeclaredField("mConstraints")
field.isAccessible = true
val theseConstraints: HashMap<Int, Any> = field.get(this) as HashMap<Int, Any>
theseConstraints.clear()
}
/**
* Mutates the [ConstraintSet] with the values from the [other] [ConstraintSet]
* @see https://github.com/tristanvda/ConstraintSet-UpdateWith
*/
@Suppress("UNCHECKED_CAST")
fun ConstraintSet.updateWith(other: ConstraintSet) {
val field = ConstraintSet::class.java.getDeclaredField("mConstraints")
field.isAccessible = true
val theseConstraints: HashMap<Int, Any> = field.get(this) as HashMap<Int, Any>
val newConstraints: HashMap<Int, Any> = field.get(other) as HashMap<Int, Any>
theseConstraints.putAll(newConstraints)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment