Skip to content

Commit

Permalink
Implement handler and action systems
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Sep 5, 2024
1 parent 3fe3214 commit 46b4883
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Designed and developed by 2024 skydoves (Jaewoong Eum)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.getstream.server.driven.core.designsystem.consumer

import androidx.compose.foundation.clickable
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import io.getstream.server.driven.core.model.Handler
import io.getstream.server.driven.core.model.HandlerAction
import io.getstream.server.driven.core.model.HandlerType

@Composable
fun Handler?.consumeHandler(
navigator: () -> Unit
): Modifier {
if (this == null) return Modifier

val action = if (this.actions[HandlerAction.NAVIGATION.value] == "to") {
{ navigator }
} else {
{}
}

return if (this.type == HandlerType.CLICK.value) {
Modifier.clickable { action.invoke() }
} else {
Modifier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ import io.getstream.server.driven.core.designsystem.extension.size
import io.getstream.server.driven.core.designsystem.preview.MockUtils
import io.getstream.server.driven.core.designsystem.theme.ServerDrivenTheme
import io.getstream.server.driven.core.model.ImageUi
import io.getstream.server.driven.core.model.UiComponent

@Composable
fun ConsumeImageUi(
imageUi: ImageUi,
modifier: Modifier = Modifier,
version: Int,
navigator: (UiComponent) -> Unit = {},
imageOptions: ImageOptions? = null
) {
val actionModifier = imageUi.handler.consumeHandler(
navigator = { navigator.invoke(imageUi) }
)

val newModifier = if (version == 1) {
modifier
.then(actionModifier)
.size(imageUi.size)
.clip(RoundedCornerShape(8.dp))
} else {
modifier
.then(actionModifier)
.size(imageUi.size)
.clip(RoundedCornerShape(16.dp))
.border(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ import io.getstream.server.driven.core.model.UiComponent
@Composable
fun UiComponent.Consume(
modifier: Modifier = Modifier,
version: Int = 0,
onListItemClicked: (UiComponent) -> Unit = {}
version: Int = 1,
navigator: (UiComponent) -> Unit = {}
) {
when (this) {
is TextUi -> ConsumeTextUi(textUi = this, modifier = modifier, version = version)
is ImageUi -> ConsumeImageUi(imageUi = this, modifier = modifier, version = version)
is ImageUi -> ConsumeImageUi(
imageUi = this,
modifier = modifier,
version = version,
navigator = navigator
)

is ListUi -> ConsumeList(
listUi = this,
modifier = modifier,
version = version,
onListItemClicked = onListItemClicked
onListItemClicked = navigator
)

else -> ConsumeDefaultUi(uiComponent = this, version = version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Designed and developed by 2024 skydoves (Jaewoong Eum)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.getstream.server.driven.core.model

import androidx.compose.runtime.Immutable
import kotlinx.serialization.Serializable

@Immutable
@Serializable
data class Handler(
val type: String,
val actions: Map<String, String>
)

enum class HandlerType(val value: String) {
CLICK("click")
}

enum class HandlerAction(val value: String) {
NAVIGATION("navigation")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ data class ImageUi(
val title: String = "",
val url: String,
val size: DpSizeUi = DpSizeUi(0, 0),
val scaleType: String
val scaleType: String,
val handler: Handler? = null
) : UiComponent {

fun toTextUi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data class ListUi(
val layout: String,
val itemSize: DpSizeUi,
val items: List<ImageUi>,
val handler: Handler? = null,
val extra: Map<String, JsonElement> = mapOf()
) : UiComponent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.getstream.server.driven.feature.timeline

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -36,7 +35,6 @@ import io.getstream.server.driven.core.designsystem.consumer.Consume
import io.getstream.server.driven.core.designsystem.preview.DefaultPreview
import io.getstream.server.driven.core.designsystem.preview.MockUtils
import io.getstream.server.driven.core.designsystem.theme.ServerDrivenTheme
import io.getstream.server.driven.core.model.ImageUi
import io.getstream.server.driven.core.model.ScreenUi
import io.getstream.server.driven.core.model.UiComponent

Expand Down Expand Up @@ -73,16 +71,9 @@ private fun ServerDrivenTimelineContent(
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
timelineUi.components.forEach { uiComponent ->
val modifier = if (uiComponent is ImageUi) {
Modifier.clickable { navigateToDetails.invoke(uiComponent) }
} else {
Modifier
}

uiComponent.Consume(
modifier = modifier,
version = timelineUi.version,
onListItemClicked = { clickedComponent ->
navigator = { clickedComponent ->
navigateToDetails.invoke(clickedComponent)
}
)
Expand Down

0 comments on commit 46b4883

Please sign in to comment.