From 906206f93b6ee3a711c408ecda408d47fbb471e0 Mon Sep 17 00:00:00 2001 From: Xavier Molloy <44061143+xavimolloy@users.noreply.github.com> Date: Tue, 31 Oct 2023 10:32:53 +0100 Subject: [PATCH] [ANDROAPP-5682] add DataMatrix support for QR Code generator, fix BottomSheetShell title and content visibility (#125) --- .../designsystem/component/internal/qr/QrCodeGenerator.kt | 7 ++++--- .../dhis/mobile/ui/designsystem/component/BottomSheet.kt | 7 +++++-- .../mobile/ui/designsystem/component/ButtonCarousel.kt | 4 ++-- .../dhis/mobile/ui/designsystem/component/QrCodeBlock.kt | 3 ++- .../designsystem/component/internal/qr/QrCodeGenerator.kt | 6 +++--- .../designsystem/component/internal/qr/QrCodeGenerator.kt | 7 ++++--- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/designsystem/src/androidMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt b/designsystem/src/androidMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt index 0cc967ec9..8ddcd3e4a 100644 --- a/designsystem/src/androidMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt +++ b/designsystem/src/androidMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt @@ -9,15 +9,16 @@ import com.google.zxing.EncodeHintType import com.google.zxing.MultiFormatWriter import com.google.zxing.WriterException import com.google.zxing.common.BitMatrix +import com.google.zxing.datamatrix.DataMatrixWriter internal actual class QrCodeGenerator { - actual fun generate(data: String): ImageBitmap? { + actual fun generate(data: String, isDataMatrix: Boolean): ImageBitmap? { return try { - val writer = MultiFormatWriter() + val writer = if (!isDataMatrix) MultiFormatWriter() else DataMatrixWriter() val bitMatrix = writer.encode( data, - BarcodeFormat.QR_CODE, + if (isDataMatrix) BarcodeFormat.DATA_MATRIX else BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE, mapOf(Pair(EncodeHintType.MARGIN, 1)), diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/BottomSheet.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/BottomSheet.kt index 263635927..6cfd8fdf0 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/BottomSheet.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/BottomSheet.kt @@ -38,6 +38,8 @@ import org.hisp.dhis.mobile.ui.designsystem.theme.Shape import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing.Spacing0 import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing.Spacing24 +import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing.Spacing56 +import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing.Spacing8 import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor import org.hisp.dhis.mobile.ui.designsystem.theme.TextColor import org.hisp.dhis.mobile.ui.designsystem.theme.Color as ThemeColor @@ -193,7 +195,7 @@ fun BottomSheetShell( content?.let { it.invoke() Divider( - modifier = Modifier.fillMaxWidth().padding(top = Spacing.Spacing8), + modifier = Modifier.fillMaxWidth().padding(top = Spacing8), color = TextColor.OnDisabledSurface, ) } @@ -210,10 +212,11 @@ fun BottomSheetShell( Modifier.fillMaxWidth() .then(shadowModifier) .background(SurfaceColor.SurfaceBright) - .padding(Spacing24), + .padding(start = Spacing24, top = Spacing24, end = Spacing24, bottom = Spacing56), contentAlignment = Alignment.BottomCenter, ) { buttonBlock?.invoke() + Spacer(Modifier.size(Spacing8)) } } } diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/ButtonCarousel.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/ButtonCarousel.kt index 68d3f41fc..c25839821 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/ButtonCarousel.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/ButtonCarousel.kt @@ -8,7 +8,7 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width @@ -79,7 +79,7 @@ fun ButtonCarousel( ) { Row( modifier - .fillMaxSize() + .fillMaxWidth() .horizontalScroll(rememberScrollState()) .padding(horizontal = Spacing24), horizontalArrangement = Arrangement.Center, diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/QrCodeBlock.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/QrCodeBlock.kt index d2f9db057..9fe9c6b86 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/QrCodeBlock.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/QrCodeBlock.kt @@ -31,6 +31,7 @@ import org.hisp.dhis.mobile.ui.designsystem.theme.TextColor fun QrCodeBlock( data: String, modifier: Modifier = Modifier, + isDataMatrix: Boolean = false, qrCodeSize: Dp = 240.dp, ) { if (data.isNotBlank()) { @@ -40,7 +41,7 @@ fun QrCodeBlock( .testTag("QR_CODE_BLOCK_CONTAINER"), horizontalAlignment = Alignment.CenterHorizontally, ) { - val qrCode by rememberQrCodeGenerator(data) + val qrCode by rememberQrCodeGenerator(data, isDataMatrix) Box(Modifier.size(qrCodeSize)) { qrCode?.let { bitmap -> diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt index b730ae684..f2dcfda8e 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt @@ -9,19 +9,19 @@ import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.graphics.ImageBitmap @Composable -internal fun rememberQrCodeGenerator(value: String): State { +internal fun rememberQrCodeGenerator(value: String, isDataMatrix: Boolean): State { val qrCodeGenerator = LocalQrCodeGenerator.current val result = remember(value) { mutableStateOf(null) } LaunchedEffect(value) { - result.value = qrCodeGenerator.generate(value) + result.value = qrCodeGenerator.generate(value, isDataMatrix) } return result } internal expect class QrCodeGenerator() { - fun generate(data: String): ImageBitmap? + fun generate(data: String, isDataMatrix: Boolean): ImageBitmap? } internal const val QR_CODE_SIZE = 512 // px diff --git a/designsystem/src/desktopMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt b/designsystem/src/desktopMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt index ea6e88e27..70c802e63 100644 --- a/designsystem/src/desktopMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt +++ b/designsystem/src/desktopMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt @@ -7,6 +7,7 @@ import com.google.zxing.EncodeHintType import com.google.zxing.MultiFormatWriter import com.google.zxing.WriterException import com.google.zxing.common.BitMatrix +import com.google.zxing.datamatrix.DataMatrixWriter import java.awt.image.BufferedImage internal actual class QrCodeGenerator { @@ -14,12 +15,12 @@ internal actual class QrCodeGenerator { private val colorBlack = 0xFF000000.toInt() private val colorWhite = 0xFFFFFFFF.toInt() - actual fun generate(data: String): ImageBitmap? { + actual fun generate(data: String, isDataMatrix: Boolean): ImageBitmap? { return try { - val writer = MultiFormatWriter() + val writer = if (!isDataMatrix) MultiFormatWriter() else DataMatrixWriter() val bitMatrix = writer.encode( data, - BarcodeFormat.QR_CODE, + if (isDataMatrix) BarcodeFormat.DATA_MATRIX else BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE, mapOf(Pair(EncodeHintType.MARGIN, 1)),