Skip to content

Commit

Permalink
[ANDROAPP-5682] add DataMatrix support for QR Code generator, fix Bot…
Browse files Browse the repository at this point in the history
…tomSheetShell title and content visibility (#125)
  • Loading branch information
xavimolloy authored Oct 31, 2023
1 parent 0d44146 commit 906206f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}
Expand All @@ -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))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,7 +79,7 @@ fun ButtonCarousel(
) {
Row(
modifier
.fillMaxSize()
.fillMaxWidth()
.horizontalScroll(rememberScrollState())
.padding(horizontal = Spacing24),
horizontalArrangement = Arrangement.Center,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.ImageBitmap

@Composable
internal fun rememberQrCodeGenerator(value: String): State<ImageBitmap?> {
internal fun rememberQrCodeGenerator(value: String, isDataMatrix: Boolean): State<ImageBitmap?> {
val qrCodeGenerator = LocalQrCodeGenerator.current
val result = remember(value) { mutableStateOf<ImageBitmap?>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ 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 {

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)),
Expand Down

0 comments on commit 906206f

Please sign in to comment.