Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions examples/demo/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ behavior.

### Send In-App Message
Trigger pre-configured in-app message layouts by setting SDK triggers:
- **Top Banner**
- **Bottom Banner**
- **Center Modal**
- **Full Screen**
- **Top**
- **Bottom**
- **Center**
- **HTML**
- **Full**
- **Full Bleed**
- **Carousel**
- **Carousel Bleed**

> These require matching in-app messages configured in your OneSignal dashboard.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,43 @@ enum class InAppMessageType(
val triggerValue: String,
) {
TOP_BANNER(
title = "TOP BANNER",
title = "TOP",
triggerKey = "iam_type",
triggerValue = "top_banner",
),
BOTTOM_BANNER(
title = "BOTTOM BANNER",
title = "BOTTOM",
triggerKey = "iam_type",
triggerValue = "bottom_banner",
),
CENTER_MODAL(
title = "CENTER MODAL",
title = "CENTER",
triggerKey = "iam_type",
triggerValue = "center_modal",
),
HTML(
title = "HTML",
triggerKey = "iam_type",
triggerValue = "html",
),
FULL_SCREEN(
title = "FULL SCREEN",
title = "FULL",
triggerKey = "iam_type",
triggerValue = "full_screen",
),
FULL_BLEED(
title = "FULL BLEED",
triggerKey = "iam_type",
triggerValue = "full_bleed",
),
CAROUSEL(
title = "CAROUSEL",
triggerKey = "iam_type",
triggerValue = "carousel",
),
CAROUSEL_BLEED(
title = "CAROUSEL BLEED",
triggerKey = "iam_type",
triggerValue = "carousel_bleed",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.onesignal.example.ui.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand All @@ -15,11 +16,17 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.onesignal.example.ui.theme.DemoLayout
Expand All @@ -33,6 +40,9 @@ private val ButtonTextStyle
fontWeight = FontWeight.SemiBold,
)

private const val MIN_TEXT_SCALE = 0.7f
private const val TEXT_SCALE_STEP = 0.95f

private fun Modifier.applyTestTag(tag: String?): Modifier =
if (tag != null) this.testTag(tag) else this

Expand Down Expand Up @@ -69,6 +79,50 @@ fun PrimaryButton(
}
}

@Composable
fun GridPrimaryButton(
text: String,
onClick: () -> Unit,
modifier: Modifier = Modifier,
testTag: String? = null,
) {
Button(
onClick = onClick,
modifier = modifier.height(DemoLayout.buttonHeight).applyTestTag(testTag),
colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.primary),
shape = ButtonShape,
elevation = ButtonDefaults.buttonElevation(
defaultElevation = 0.dp,
pressedElevation = 0.dp,
),
contentPadding = PaddingValues(horizontal = DemoLayout.gap),
) {
ScaleDownText(text = text, style = ButtonTextStyle, color = Color.White)
}
}

@Composable
private fun ScaleDownText(text: String, style: TextStyle, color: Color) {
var scaledStyle by remember(text, style) { mutableStateOf(style) }
var readyToDraw by remember(text, style) { mutableStateOf(false) }
Text(
text = text,
style = scaledStyle,
color = color,
maxLines = 1,
softWrap = false,
modifier = Modifier.drawWithContent { if (readyToDraw) drawContent() },
onTextLayout = { result ->
val canShrink = scaledStyle.fontSize.value * TEXT_SCALE_STEP >= style.fontSize.value * MIN_TEXT_SCALE
if (result.didOverflowWidth && canShrink) {
scaledStyle = scaledStyle.copy(fontSize = scaledStyle.fontSize * TEXT_SCALE_STEP)
} else {
readyToDraw = true
}
},
)
}

@Composable
fun OutlineButton(
text: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.onesignal.example.ui.main

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand All @@ -30,6 +32,7 @@ import com.onesignal.example.ui.components.CustomNotificationDialog
import com.onesignal.example.ui.components.DemoSection
import com.onesignal.example.ui.components.DestructiveButton
import com.onesignal.example.ui.components.DropdownRow
import com.onesignal.example.ui.components.GridPrimaryButton
import com.onesignal.example.ui.components.LocalSnackbarController
import com.onesignal.example.ui.components.CompositeLoginDialog
import com.onesignal.example.ui.components.LoginDialog
Expand Down Expand Up @@ -472,13 +475,22 @@ fun SendInAppMessageSection(
sectionKey = "send_iam",
onInfoClick = onInfoClick,
) {
InAppMessageType.entries.forEach { type ->
val typeTag = type.name.lowercase()
PrimaryButton(
text = type.title,
onClick = { onSendMessage(type) },
testTag = "send_iam_${typeTag}_button",
)
Column(
modifier = Modifier.padding(horizontal = DemoLayout.pagePadding, vertical = 4.dp),
verticalArrangement = Arrangement.spacedBy(DemoLayout.gap),
) {
InAppMessageType.entries.chunked(2).forEach { rowTypes ->
Row(horizontalArrangement = Arrangement.spacedBy(DemoLayout.gap)) {
rowTypes.forEach { type ->
GridPrimaryButton(
text = type.title,
onClick = { onSendMessage(type) },
modifier = Modifier.weight(1f),
testTag = "send_iam_${type.triggerValue}_button",
)
}
}
}
}
}
}
Expand Down
Loading