Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let package = Package(
dependencies: [
.package(name: "ReactNative", path: "../../../../xcframeworks"),
.package(name: "React-GeneratedCode", path: "../../../ios"),
.package(url: "https://github.com/urbanairship/airship-mobile-framework-proxy.git", exact: "16.0.1"),
.package(url: "https://github.com/urbanairship/airship-mobile-framework-proxy.git", exact: "16.0.2"),
.package(url: "https://github.com/urbanairship/ios-library.git", exact: "21.0.2"),
],
targets: [
Expand Down Expand Up @@ -63,6 +63,7 @@ let package = Package(
],
path: "ios",
sources: [
"AirshipEmbeddedCarouselWrapper.swift",
"AirshipEmbeddedViewWrapper.swift",
"AirshipPluginLoader.swift",
"AirshipReactNative.swift",
Expand All @@ -77,6 +78,8 @@ let package = Package(
sources: [
"RNAirship.mm",
"RNAirshipBootloader.m",
"RNAirshipEmbeddedCarousel.mm",
"RNAirshipEmbeddedCarouselViewManager.mm",
"RNAirshipEmbeddedView.mm",
"RNAirshipEmbeddedViewViewManager.mm",
"RNAirshipMessageView.mm",
Expand Down
9 changes: 9 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ buildscript {
classpath "com.android.tools.build:gradle:8.13.0"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
classpath "org.jetbrains.kotlin:compose-compiler-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
}
}

Expand All @@ -24,6 +25,7 @@ apply plugin: "com.android.library"
if (project.extensions.findByName('kotlin') == null) {
apply plugin: "kotlin-android"
}
apply plugin: "org.jetbrains.kotlin.plugin.compose"
apply plugin: "com.facebook.react"

def getExtOrIntegerDefault(name) {
Expand Down Expand Up @@ -84,13 +86,20 @@ def getModuleVersion() {

def kotlin_version = getExtOrDefault("kotlinVersion")
def proxy_version = getExtOrDefault("airshipProxyVersion")
def compose_bom_version = getExtOrDefault("composeBomVersion")

dependencies {
implementation "com.facebook.react:react-android"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

api "com.urbanairship.android:airship-framework-proxy:$proxy_version"

// Compose, for hosting the AirshipEmbeddedCarousel provided by the proxy.
implementation platform("androidx.compose:compose-bom:$compose_bom_version")
implementation "androidx.compose.runtime:runtime"
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.foundation:foundation"

if (isHmsEnabled()) {
implementation "com.urbanairship.android:airship-framework-proxy-hms:$proxy_version"
}
Expand Down
3 changes: 2 additions & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Airship_minSdkVersion=26
Airship_targetSdkVersion=36
Airship_compileSdkVersion=36
Airship_ndkversion=26.1.10909125
Airship_airshipProxyVersion=16.0.1
Airship_airshipProxyVersion=16.0.2
Airship_composeBomVersion=2025.10.01
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class AirshipPackage : BaseReactPackage() {
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return listOf<ViewManager<*, *>>(ReactMessageViewManager(), ReactEmbeddedViewManager())
return listOf<ViewManager<*, *>>(ReactMessageViewManager(), ReactEmbeddedViewManager(), ReactEmbeddedCarouselManager())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* Copyright Airship and Contributors */

package com.urbanairship.reactnative

import android.content.Context
import android.widget.FrameLayout
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.urbanairship.automation.compose.AirshipEmbeddedCarousel
import com.urbanairship.automation.compose.AirshipEmbeddedCarouselDefaults
import com.urbanairship.automation.compose.rememberAirshipEmbeddedCarouselState
import com.urbanairship.embedded.AirshipEmbeddedSelection
import com.urbanairship.json.JsonException
import com.urbanairship.json.JsonValue
import kotlinx.coroutines.flow.distinctUntilChanged

class ReactEmbeddedCarousel(context: Context) : FrameLayout(context) {

private data class Config(val embeddedId: String, val selection: AirshipEmbeddedSelection)

private var renderedConfig: String? = null
private var config by mutableStateOf<Config?>(null)
private var requestedPage by mutableIntStateOf(-1)

private val composeView = ComposeView(context).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnDetachedFromWindowOrReleasedFromPool)
setContent {
val current = config ?: return@setContent
val state = rememberAirshipEmbeddedCarouselState(current.embeddedId, current.selection)

// Navigate when JS requests a new page. A request made before any
// content is available is held until pages exist.
LaunchedEffect(state) {
var lastRequested = -1
var pending: Int? = null
snapshotFlow { requestedPage to state.pageCount }.collect { (requested, count) ->
if (requested != lastRequested) {
lastRequested = requested
pending = requested.takeIf { it >= 0 }
}
val target = pending
if (target != null && count > 0) {
pending = null
val page = minOf(target, count - 1)
if (page != state.pagerState.currentPage) {
state.pagerState.animateScrollToPage(page)
}
}
}
}

// Report page state to JS.
LaunchedEffect(state) {
snapshotFlow {
val count = state.pageCount
Triple(if (count > 0) state.currentPage else 0, count, state.isAvailable)
}.distinctUntilChanged().collect { (page, count, available) ->
notifyState(page, count, available)
}
}

AirshipEmbeddedCarousel(
state = state,
modifier = Modifier,
indicator = AirshipEmbeddedCarouselDefaults.dotsIndicator,
previousArrow = AirshipEmbeddedCarouselDefaults.previousArrow(),
nextArrow = AirshipEmbeddedCarouselDefaults.nextArrow(),
)
}
}

init {
addView(composeView)
}

fun setConfig(config: String?) {
if (config == null || config == renderedConfig) {
return
}

val json = try {
JsonValue.parseString(config).optMap()
} catch (e: JsonException) {
return
}

val embeddedId = json.opt("embeddedId").optString()
if (embeddedId.isEmpty()) {
return
}

val selectionJson = json.opt("selection").optMap()
val instanceId = selectionJson.opt("instanceId").optString()
val selection = if (selectionJson.opt("type").optString() == "instance_id" && instanceId.isNotEmpty()) {
AirshipEmbeddedSelection.ByInstanceId(instanceId)
} else {
AirshipEmbeddedSelection.Priority
}

renderedConfig = config
this.config = Config(embeddedId, selection)
}

fun setPage(page: Int) {
requestedPage = page
}

private fun notifyState(currentPage: Int, pageCount: Int, isAvailable: Boolean) {
val event = Arguments.createMap()
event.putInt(CURRENT_PAGE_KEY, currentPage)
event.putInt(PAGE_COUNT_KEY, pageCount)
event.putBoolean(IS_AVAILABLE_KEY, isAvailable)
(context as ReactContext).airshipDispatchEvent(id, EVENT_STATE_CHANGE, event)
}

override fun requestLayout() {
super.requestLayout()

// This view relies on a measure + layout pass happening after it calls requestLayout().
// https://github.com/facebook/react-native/issues/4990#issuecomment-180415510
// https://stackoverflow.com/questions/39836356/react-native-resize-custom-ui-component
post(measureAndLayout)
}

private val measureAndLayout = Runnable {
measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY))
layout(left, top, right, bottom)
}

companion object {
const val EVENT_STATE_CHANGE = "topAirshipEmbeddedCarouselStateChange"
const val EVENT_STATE_CHANGE_HANDLER_NAME = "onStateChange"

private const val CURRENT_PAGE_KEY = "currentPage"
private const val PAGE_COUNT_KEY = "pageCount"
private const val IS_AVAILABLE_KEY = "isAvailable"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright Airship and Contributors */

package com.urbanairship.reactnative

import com.facebook.react.bridge.ReadableArray
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNAirshipEmbeddedCarouselManagerInterface

class ReactEmbeddedCarouselManager : SimpleViewManager<ReactEmbeddedCarousel>(),
RNAirshipEmbeddedCarouselManagerInterface<ReactEmbeddedCarousel> {

private val manualDelegate = object : ViewManagerDelegate<ReactEmbeddedCarousel> {

@Suppress("ACCIDENTAL_OVERRIDE")
override fun setProperty(view: ReactEmbeddedCarousel, propName: String, value: Any?) {
when (propName) {
"config" -> setConfig(view, value as? String)
"page" -> setPage(view, (value as? Number)?.toInt() ?: -1)
else -> {}
}
}

@Suppress("ACCIDENTAL_OVERRIDE")
override fun receiveCommand(
view: ReactEmbeddedCarousel,
commandName: String,
args: ReadableArray
) {
// No commands supported
}
}

override fun getName(): String {
return REACT_CLASS
}

override fun getDelegate(): ViewManagerDelegate<ReactEmbeddedCarousel> {
return manualDelegate
}

override fun createViewInstance(reactContext: ThemedReactContext): ReactEmbeddedCarousel {
return ReactEmbeddedCarousel(reactContext)
}

@ReactProp(name = "config")
override fun setConfig(view: ReactEmbeddedCarousel, config: String?) {
view.setConfig(config)
}

@ReactProp(name = "page", defaultInt = -1)
override fun setPage(view: ReactEmbeddedCarousel, page: Int) {
view.setPage(page)
}

override fun getExportedCustomBubblingEventTypeConstants(): Map<String, Any> {
return mapOf(
ReactEmbeddedCarousel.EVENT_STATE_CHANGE to mapOf(
"phasedRegistrationNames" to mapOf(
"bubbled" to ReactEmbeddedCarousel.EVENT_STATE_CHANGE_HANDLER_NAME
)
)
)
}

companion object {
const val REACT_CLASS = "RNAirshipEmbeddedCarousel"
}
}
5 changes: 4 additions & 1 deletion example/src/Styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,9 @@ export default StyleSheet.create({
evFlexGrow: {
flex: 1,
},
evCarouselContainer: {
minHeight: 300,
},
evAspectRatio: {
width: '100%',
aspectRatio: 16 / 9,
Expand All @@ -1075,4 +1078,4 @@ export default StyleSheet.create({
textAlign: 'center',
lineHeight: 18,
},
});
});
38 changes: 37 additions & 1 deletion example/src/screens/EmbeddedViewsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
ScrollView,
Dimensions,
Platform,
Button,
} from 'react-native';
import Airship, { AirshipEmbeddedView } from '@ua/react-native-airship';
import Airship, { AirshipEmbeddedCarousel, AirshipEmbeddedView } from '@ua/react-native-airship';
import styles from '../Styles';

const { width: SCREEN_WIDTH } = Dimensions.get('window');
Expand All @@ -25,6 +26,7 @@ type SizeConfig = {
};

const EMBEDDED_ID = 'test';
const CAROUSEL_EMBEDDED_ID = 'embedded-1';
const SCREEN_NAME = 'embedded_views_screen';

const SIZE_CONFIGS: SizeConfig[] = [
Expand Down Expand Up @@ -68,6 +70,8 @@ const SIZE_CONFIGS: SizeConfig[] = [

export default function EmbeddedViewsScreen(_props: EmbeddedViewsScreenProps) {
const [isEmbeddedReady, setEmbeddedReady] = useState(false);
const [carouselPage, setCarouselPage] = useState(0);
const [carouselPageCount, setCarouselPageCount] = useState(0);

useEffect(() => {
Airship.analytics.trackScreen(SCREEN_NAME);
Expand Down Expand Up @@ -250,6 +254,38 @@ export default function EmbeddedViewsScreen(_props: EmbeddedViewsScreenProps) {

{SIZE_CONFIGS.map(renderSizeCard)}

<View style={styles.evCard}>
<View style={styles.evCardHeader}>
<Text style={styles.evCardTitle}>Carousel</Text>
</View>
<Text style={styles.evCardDescription}>
<Text style={styles.evCodeText}>
ID: "{CAROUSEL_EMBEDDED_ID}" · Page {carouselPageCount > 0 ? carouselPage + 1 : 0} of {carouselPageCount}
</Text>
</Text>
<View style={styles.evCarouselContainer}>
<AirshipEmbeddedCarousel
embeddedId={CAROUSEL_EMBEDDED_ID}
style={styles.evFlexGrow}
currentPage={carouselPage}
onPageChanged={setCarouselPage}
onPageCountChanged={setCarouselPageCount}
/>
</View>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<Button
title="Previous"
disabled={carouselPage <= 0}
onPress={() => setCarouselPage(carouselPage - 1)}
/>
<Button
title="Next"
disabled={carouselPage >= carouselPageCount - 1}
onPress={() => setCarouselPage(carouselPage + 1)}
/>
</View>
</View>

<View style={styles.evCard}>
<View style={styles.evCardHeader}>
<Text style={styles.evCardTitle}>Instance Selection</Text>
Expand Down
Loading
Loading