-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4751ab2
commit 1ffc730
Showing
5 changed files
with
154 additions
and
29 deletions.
There are no files selected for viewing
33 changes: 4 additions & 29 deletions
33
common/src/commonMain/kotlin/org/hisp/dhis/common/screens/SwitchScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,16 @@ | ||
package org.hisp.dhis.common.screens | ||
|
||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.size | ||
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.Modifier | ||
import org.hisp.dhis.mobile.ui.designsystem.component.ColumnComponentContainer | ||
import org.hisp.dhis.mobile.ui.designsystem.component.RowComponentContainer | ||
import org.hisp.dhis.mobile.ui.designsystem.component.SubTitle | ||
import org.hisp.dhis.mobile.ui.designsystem.component.Switch | ||
import org.hisp.dhis.mobile.ui.designsystem.component.Title | ||
import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing | ||
import org.hisp.dhis.mobile.ui.designsystem.component.QrCodeBlock | ||
|
||
@Composable | ||
fun SwitchScreen() { | ||
ColumnComponentContainer { | ||
Title("Switches") | ||
SubTitle("Toggled enabled and disabled switch") | ||
var switchOne by remember { mutableStateOf(true) } | ||
var switchTwo by remember { mutableStateOf(true) } | ||
var switchThree by remember { mutableStateOf(false) } | ||
var switchFour by remember { mutableStateOf(false) } | ||
|
||
RowComponentContainer { | ||
Switch(isChecked = switchOne, onCheckedChange = { switchOne = !it }) | ||
Switch(isChecked = switchTwo, onCheckedChange = { switchTwo = !it }, enabled = false) | ||
} | ||
|
||
Spacer(Modifier.size(Spacing.Spacing6)) | ||
SubTitle("Untoggled enabled and disabled switch") | ||
|
||
RowComponentContainer { | ||
Switch(isChecked = switchThree, onCheckedChange = { switchThree = !it }) | ||
Switch(isChecked = switchFour, onCheckedChange = { switchFour = !it }, enabled = false) | ||
} | ||
QrCodeBlock( | ||
data = "QR code value", | ||
) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...Main/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.qr | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Color | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import com.google.zxing.BarcodeFormat | ||
import com.google.zxing.EncodeHintType | ||
import com.google.zxing.qrcode.QRCodeWriter | ||
|
||
actual class QrCodeGenerator { | ||
|
||
actual fun generate(data: String): ImageBitmap { | ||
val writer = QRCodeWriter() | ||
val bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, 512, 512, mapOf(Pair(EncodeHintType.MARGIN, 1))) | ||
val width = bitMatrix.width | ||
val height = bitMatrix.height | ||
|
||
val pixels = IntArray(width * height) | ||
for (y in 0 until height) { | ||
for (x in 0 until width) { | ||
pixels[y * width + x] = if (bitMatrix[x, y]) Color.BLACK else Color.WHITE | ||
} | ||
} | ||
|
||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | ||
bitmap.setPixels(pixels, 0, width, 0, 0, width, height) | ||
|
||
return bitmap.asImageBitmap() | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/QrCodeBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.unit.dp | ||
import org.hisp.dhis.mobile.ui.designsystem.component.internal.qr.rememberQrCodeGenerator | ||
import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing | ||
import org.hisp.dhis.mobile.ui.designsystem.theme.TextColor | ||
|
||
@Composable | ||
fun QrCodeBlock( | ||
data: String, | ||
modifier: Modifier = Modifier, | ||
) { | ||
Column( | ||
modifier = modifier.padding(vertical = Spacing.Spacing16), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
val qrCode by rememberQrCodeGenerator(data) | ||
// TODO: Extract out a size to a constants file? | ||
Box(Modifier.size(240.dp)) { | ||
qrCode?.let { bitmap -> | ||
Image( | ||
bitmap = bitmap, | ||
contentDescription = null, | ||
modifier = Modifier.matchParentSize(), | ||
contentScale = ContentScale.FillBounds, | ||
) | ||
} | ||
} | ||
Text( | ||
text = data, | ||
style = MaterialTheme.typography.titleMedium, | ||
color = TextColor.OnSurface, | ||
) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...Main/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.qr | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
|
||
@Composable | ||
fun rememberQrCodeGenerator(value: String): State<ImageBitmap?> { | ||
val qrCodeGenerator = LocalQrCodeGenerator.current | ||
val result = remember(value) { mutableStateOf<ImageBitmap?>(null) } | ||
|
||
LaunchedEffect(value) { | ||
result.value = qrCodeGenerator.generate(value) | ||
} | ||
|
||
return result | ||
} | ||
|
||
expect class QrCodeGenerator() { | ||
fun generate(data: String): ImageBitmap | ||
} | ||
|
||
val LocalQrCodeGenerator = staticCompositionLocalOf { QrCodeGenerator() } |
45 changes: 45 additions & 0 deletions
45
...Main/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/qr/QrCodeGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.qr | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.toComposeImageBitmap | ||
import com.google.zxing.BarcodeFormat | ||
import com.google.zxing.EncodeHintType | ||
import com.google.zxing.qrcode.QRCodeWriter | ||
import java.awt.image.BufferedImage | ||
|
||
actual class QrCodeGenerator { | ||
|
||
private val colorBlack = 0xFF000000.toInt() | ||
private val colorWhite = 0xFFFFFFFF.toInt() | ||
|
||
actual fun generate(data: String): ImageBitmap { | ||
val writer = QRCodeWriter() | ||
val bitMatrix = writer.encode( | ||
data, | ||
BarcodeFormat.QR_CODE, | ||
512, | ||
512, | ||
mapOf( | ||
Pair( | ||
EncodeHintType.MARGIN, | ||
1, | ||
), | ||
), | ||
) | ||
val width = bitMatrix.width | ||
val height = bitMatrix.height | ||
|
||
val image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) | ||
|
||
val pixels = IntArray(width * height) | ||
for (y in 0 until height) { | ||
for (x in 0 until width) { | ||
pixels[y * width + x] = if (bitMatrix[x, y]) colorBlack else colorWhite | ||
} | ||
} | ||
|
||
image.setRGB(0, 0, width, height, pixels, 0, width) | ||
|
||
return image.toComposeImageBitmap() | ||
} | ||
} |