Skip to content

Commit

Permalink
Merge pull request #40 from plaidev/flutter
Browse files Browse the repository at this point in the history
[android] support auto scroll / full item width options for carousel
  • Loading branch information
RyosukeCla authored Jun 4, 2024
2 parents 5e5a13d + 945e447 commit 3c3f813
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 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.2"
version = "0.1.3"

android {
namespace = "com.nativebrik.sdk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
Expand All @@ -18,9 +21,23 @@ 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.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.jsonArray

@Composable
internal fun Modifier.horizontalCollectionItemSize(block: UICollectionBlock): Modifier {
val isFullWidth = block.data?.fullItemWidth ?: false
return if (isFullWidth) {
this
.height((block.data?.itemHeight ?: 0).dp)
.fillMaxWidth()
} else {
this.size(DpSize((block.data?.itemWidth ?: 0).dp, (block.data?.itemHeight ?: 0).dp))
}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
internal fun Carousel(block: UICollectionBlock, modifier: Modifier = Modifier) {
Expand All @@ -39,6 +56,18 @@ internal fun Carousel(block: UICollectionBlock, modifier: Modifier = Modifier) {
val state = rememberPagerState {
children.size
}
val scope = rememberCoroutineScope()
LaunchedEffect(state.currentPage, children.size) {
if (block.data?.fullItemWidth == true && block.data.autoScroll == true) {
delay((block.data.autoScrollInterval?.toLong() ?: 3) * 1000)
scope.launch {
if (!state.isScrollInProgress) {
state.animateScrollToPage((state.currentPage + 1) % children.size)
}
}
}
}

val padding = parseFramePadding(block.data?.frame)
val gap = (block.data?.gap ?: 0).dp
val direction: FlexDirection = block.data?.direction ?: FlexDirection.ROW
Expand All @@ -48,10 +77,10 @@ internal fun Carousel(block: UICollectionBlock, modifier: Modifier = Modifier) {
contentPadding = padding,
pageSpacing = gap,
state = state,
pageSize = PageSize.Fixed(size.width),
pageSize = if (block.data?.fullItemWidth == true) PageSize.Fill else PageSize.Fixed(size.width),
modifier = modifier.fillMaxWidth()
) {
Box(Modifier.size(size)) {
Box(Modifier.horizontalCollectionItemSize(block)) {
NestedDataProvider(data = if (arrayData != null) arrayData[it] else dataState.data) {
Block(block = children[it])
}
Expand Down
2 changes: 1 addition & 1 deletion 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.2"
const val VERSION = "0.1.3"

data class Endpoint(
val cdn: String = "https://cdn.nativebrik.com",
Expand Down

0 comments on commit 3c3f813

Please sign in to comment.