Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Fix/recomposition (#29)
Browse files Browse the repository at this point in the history
* removes heavy logic

* unfinished

* seems to be working for basic cases

* ParentName + Computed

* fixes

* fixes

* undoes unnecessary modifications

* fixes tests and other validations

* undoes unnecessary modifications

* upgrades nimbus-core from alpha5 to alpha6

* fixes extra recompositions

* fix for detekt
  • Loading branch information
Tiagoperes authored Aug 8, 2022
1 parent 370130f commit d9bc063
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
6 changes: 3 additions & 3 deletions compose-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ dependencies {
implementation("com.google.android.material:material:1.6.1")
implementation("androidx.appcompat:appcompat:1.4.2")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation ("androidx.navigation:navigation-compose:2.5.0")
implementation ("androidx.navigation:navigation-compose:2.5.1")
implementation("androidx.compose.ui:ui:${rootProject.extra["compose_version"]}")
implementation("androidx.compose.material:material:${rootProject.extra["compose_version"]}")
implementation("androidx.compose.ui:ui-tooling-preview:${rootProject.extra["compose_version"]}")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.5.0")
implementation("androidx.activity:activity-compose:1.5.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.5.1")
implementation("androidx.activity:activity-compose:1.5.1")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:${rootProject.extra["compose_version"]}")
debugImplementation("androidx.compose.ui:ui-tooling:${rootProject.extra["compose_version"]}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,73 @@ val json1 = """
]
}
""".trimIndent()

const val UPDATE_TEST = """{
"_:component": "layout:container",
"children": [
{
"_:component": "layout:container",
"properties": {
"backgroundColor": "@{global.backgroundColor}",
"padding": 10
},
"children": [
{
"_:component": "layout:container",
"children": [
{
"_:component": "material:text",
"properties": {
"text": "Row 1"
}
}
]
},
{
"_:component": "material:button",
"properties": {
"text": "Change BG to red",
"onPress": [
{
"_:action": "setState",
"properties": {
"path": "global.backgroundColor",
"value": "#FF0000"
}
}
]
}
}
]
},
{
"_:component": "layout:container",
"properties": {
"padding": 10
},
"children": [
{
"_:component": "material:text",
"properties": {
"text": "Row 2"
}
}
]
},
{
"_:component": "layout:container",
"properties": {
"padding": 10
},
"children": [
{
"_:component": "material:text",
"properties": {
"text": "Row 3"
}
}
]
}
]
}
"""
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package br.com.zup.nimbus.compose.sample.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import br.zup.com.nimbus.compose.ComponentData
import br.zup.com.nimbus.compose.NimbusTheme
import com.zup.nimbus.core.deserialization.ComponentDeserializer

private fun getColor(colorString: String): Color {
return Color(android.graphics.Color.parseColor(colorString))
}

@Composable
fun NimbusContainer(children: @Composable () -> Unit) {
Column {
children()
fun NimbusContainer(it: ComponentData) {
val logger = NimbusTheme.nimbus.logger
val deserializer = ComponentDeserializer(logger, it.node)
deserializer.start()
val background = deserializer.asStringOrNull("backgroundColor")
val padding = deserializer.asDoubleOrNull("padding")
var modifier = Modifier.padding((padding ?: 0.0).dp)
if (background != null) modifier = modifier.background(color = getColor(background))
Column(modifier = modifier) {
it.children()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.runtime.Composable
import br.zup.com.nimbus.compose.ComponentLibrary

val layoutLib = ComponentLibrary("layout")
.add("container") @Composable { NimbusContainer(it.children) }
.add("container") @Composable { NimbusContainer(it) }

val customLib = ComponentLibrary("custom")
.add("text") @Composable { NimbusText(it) }
Expand Down
1 change: 1 addition & 0 deletions compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
implementation("com.google.android.material:material:1.6.1")
implementation("androidx.appcompat:appcompat:1.4.2")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.compose.runtime:runtime:${rootProject.extra["compose_version"]}")
implementation("androidx.compose.ui:ui:${rootProject.extra["compose_version"]}")
implementation("androidx.compose.material:material:${rootProject.extra["compose_version"]}")
implementation("androidx.compose.ui:ui-tooling-preview:${rootProject.extra["compose_version"]}")
Expand Down
14 changes: 13 additions & 1 deletion compose/src/main/java/br/zup/com/nimbus/compose/ComponentData.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package br.zup.com.nimbus.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import com.zup.nimbus.core.tree.ServerDrivenNode

@Immutable
class ComponentData(
val node: ServerDrivenNode,
val parent: ServerDrivenNode?,
val children: @Composable () -> Unit,
)
) {
private val hash: Int = node.properties?.hashCode() ?: 0

override fun hashCode(): Int {
return hash
}

override fun equals(other: Any?): Boolean {
return if (other is ComponentData) other.hashCode() == hash else false
}
}

0 comments on commit d9bc063

Please sign in to comment.