Skip to content

Commit

Permalink
Merge pull request #31 from plaidev/android-1
Browse files Browse the repository at this point in the history
[android] bug fixes - support data reference for grid, fix img url, fix json path util
  • Loading branch information
RyosukeCla authored Apr 2, 2024
2 parents 9af9715 + f3c4782 commit a1f1f64
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
12 changes: 6 additions & 6 deletions android/nativebrik/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.nativebrik"
version = "0.1.1"
version = "0.1.2"

android {
namespace = "com.nativebrik.sdk"
Expand Down Expand Up @@ -59,13 +59,13 @@ android {

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
implementation("androidx.compose.ui:ui-tooling:1.6.2")
implementation("androidx.compose.ui:ui:1.6.2")
implementation("androidx.compose.foundation:foundation:1.6.2")
implementation("androidx.compose.runtime:runtime:1.6.2")
implementation("androidx.compose.ui:ui-tooling:1.6.4")
implementation("androidx.compose.ui:ui:1.6.4")
implementation("androidx.compose.foundation:foundation:1.6.4")
implementation("androidx.compose.runtime:runtime:1.6.4")
implementation("io.coil-kt:coil:2.5.0")
implementation("io.coil-kt:coil-compose:2.5.0")
implementation("androidx.compose.material3:material3:1.2.0")
implementation("androidx.compose.material3:material3:1.2.1")
implementation("androidx.navigation:navigation-compose:2.7.7")

implementation("androidx.core:core-ktx:1.12.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal fun Carousel(block: UICollectionBlock, modifier: Modifier = Modifier) {
var children = block.data?.children ?: emptyList()
var arrayData: JsonArray? = null
if (reference != null) {
var data = variableByPath(reference, dataState.data)
val data = variableByPath(reference, dataState.data)
if (data is JsonArray && children.isNotEmpty()) {
arrayData = data.jsonArray
children = arrayData.map { children[0] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.nativebrik.sdk.component.provider.data.DataContext
import com.nativebrik.sdk.component.provider.data.NestedDataProvider
import com.nativebrik.sdk.schema.FlexDirection
import com.nativebrik.sdk.schema.UICollectionBlock
import com.nativebrik.sdk.template.variableByPath
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.jsonArray

@Composable
internal fun Grid(block: UICollectionBlock, modifier: Modifier = Modifier) {
val children = block.data?.children ?: emptyList()
val state = rememberLazyGridState(0, 0)
val padding = parseFramePadding(block.data?.frame)
val gridSize = block.data?.gridSize ?: 1
Expand All @@ -27,7 +31,19 @@ internal fun Grid(block: UICollectionBlock, modifier: Modifier = Modifier) {
val size = DpSize((block.data?.itemWidth ?: 0).dp, (block.data?.itemHeight ?: 0).dp)
val gridHeight = (block.data?.frame?.paddingTop ?: 0) + (block.data?.frame?.paddingBottom ?: 0) + (gridSize - 1) * (block.data?.gap ?: 0) + (gridSize * (block.data?.itemHeight ?: 0))
val gridWidth = (block.data?.frame?.paddingLeft ?: 0) + (block.data?.frame?.paddingRight ?: 0) + (gridSize - 1) * (block.data?.gap ?: 0) + (gridSize * (block.data?.itemWidth ?: 0))


val dataState = DataContext.state
val reference = block.data?.reference
var children = block.data?.children ?: emptyList()
var arrayData: JsonArray? = null
if (reference != null) {
val data = variableByPath(reference, dataState.data)
if (data is JsonArray && children.isNotEmpty()) {
arrayData = data.jsonArray
children = arrayData.map { children[0] }
}
}

if (direction == FlexDirection.ROW) {
LazyHorizontalGrid(
contentPadding = padding,
Expand All @@ -39,7 +55,9 @@ internal fun Grid(block: UICollectionBlock, modifier: Modifier = Modifier) {
) {
items(children.size) {
Box(Modifier.size(size)) {
Block(block = children[it])
NestedDataProvider(data = if (arrayData != null) arrayData[it] else dataState.data) {
Block(block = children[it])
}
}
}
}
Expand All @@ -54,7 +72,9 @@ internal fun Grid(block: UICollectionBlock, modifier: Modifier = Modifier) {
) {
items(children.size) {
Box(Modifier.size(size)) {
Block(block = children[it])
NestedDataProvider(data = if (arrayData != null) arrayData[it] else dataState.data) {
Block(block = children[it])
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal fun Image(block: UIImageBlock, modifier: Modifier = Modifier) {
AsyncImage(
modifier = modifier,
model = ImageRequest.Builder(LocalContext.current)
.data(block.data?.src ?: "https://example.com/image.jpg",)
.data(src)
.crossfade(true)
.build(),
contentDescription = null,
Expand Down
10 changes: 8 additions & 2 deletions android/nativebrik/src/main/java/com/nativebrik/sdk/sdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.nativebrik.sdk.data.user.NativebrikUser
import com.nativebrik.sdk.remoteconfig.RemoteConfigLoadingState
import com.nativebrik.sdk.schema.UIBlock

const val VERSION = "0.1.1"
const val VERSION = "0.1.2"

data class Endpoint(
val cdn: String = "https://cdn.nativebrik.com",
Expand Down Expand Up @@ -117,7 +117,13 @@ public class NativebrikExperiment {

internal constructor(config: Config, user: NativebrikUser, db: SQLiteDatabase, context: Context) {
this.container = ContainerImpl(
config = config,
config = config.copy(onEvent = { event ->
val name = event.name ?: ""
if (name.isNotEmpty()) {
this.dispatch(NativebrikEvent(name))
}
config.onEvent?.let { it(event) }
}),
user = user,
db = db,
context = context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fun variableByPath(path: String, variable: JsonElement?): JsonElement? {
if (keys.isEmpty()) return null
var current = variable
keys.forEach { key ->
if (key.isEmpty()) return@forEach
if (key == "$") {
current = variable
} else {
Expand Down

0 comments on commit a1f1f64

Please sign in to comment.