Skip to content

Commit

Permalink
Cleaning up the QrEncoder interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jklein24 committed Feb 21, 2023
1 parent 1bfd5ab commit cd8f64f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun QrCodeView(
modifier: Modifier = Modifier,
colors: QrCodeColors = QrCodeColors.default(),
dotShape: DotShape = DotShape.Square,
encoder: QrEncoder = QrEncoder(),
encoder: QrEncoder = ZxingQrEncoder(),
overlayContent: (@Composable () -> Unit)? = null,
) {
Box(modifier = modifier, contentAlignment = Alignment.Center) {
Expand Down Expand Up @@ -70,9 +70,9 @@ fun QrCodeView(
modifier: Modifier = Modifier,
colors: QrCodeColors = QrCodeColors.default(),
dotShape: DotShape = DotShape.Square,
encoder: QrEncoder = QrEncoder()
encoder: QrEncoder = ZxingQrEncoder()
) {
val encodedData = remember(data, encoder) { encoder(data) }
val encodedData = remember(data, encoder) { encoder.encode(data) }

Canvas(modifier = modifier.background(colors.background)) {
encodedData?.let { matrix ->
Expand Down
14 changes: 11 additions & 3 deletions composeqrcode/src/main/java/com/lightspark/composeqr/QrEncoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import com.google.zxing.qrcode.encoder.ByteMatrix
import com.google.zxing.qrcode.encoder.Encoder

/**
* Encodes a string into a QR code ByteMatrix. This is just a small wrapper around the ZXing library with sane defaults.
* Encodes a string into a QR code ByteMatrix. The default implementation, `ZxingQrEncoder` just
* wraps the ZXing library with sane defaults.
*/
class QrEncoder {
operator fun invoke(qrData: String): ByteMatrix? {
interface QrEncoder {
fun encode(qrData: String): ByteMatrix?
}

/**
* This is just a small wrapper around the ZXing library with sane defaults.
*/
class ZxingQrEncoder : QrEncoder {
override fun encode(qrData: String): ByteMatrix? {
return Encoder.encode(
qrData,
ErrorCorrectionLevel.H,
Expand Down

0 comments on commit cd8f64f

Please sign in to comment.