Skip to content

Commit

Permalink
GS1 QR code is show as QR Code if value does not have GS1 prefix value.
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdyrod committed Jul 2, 2024
1 parent 9d81916 commit 2299924
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ class QRImageControllerImpl(
) : QRImageController {

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun getWriterFromRendering(value: String, renderingType: UiRenderType) = when {
value.startsWith(GS1Elements.GS1_d2_IDENTIFIER.element) -> Pair(
DataMatrixWriter(),
BarcodeFormat.DATA_MATRIX,
)
renderingType == UiRenderType.QR_CODE -> Pair(QRCodeWriter(), BarcodeFormat.QR_CODE)
renderingType == UiRenderType.BAR_CODE -> Pair(Code128Writer(), BarcodeFormat.CODE_128)
else -> throw IllegalArgumentException()
}
fun getWriterFromRendering(value: String, renderingType: UiRenderType) =
when (renderingType) {
UiRenderType.GS1_DATAMATRIX ->
if (value.startsWith(GS1Elements.GS1_d2_IDENTIFIER.element)) {
Pair(DataMatrixWriter(), BarcodeFormat.DATA_MATRIX)
} else {
Pair(QRCodeWriter(), BarcodeFormat.QR_CODE)
}
UiRenderType.QR_CODE -> Pair(QRCodeWriter(), BarcodeFormat.QR_CODE)
UiRenderType.BAR_CODE -> Pair(Code128Writer(), BarcodeFormat.CODE_128)
else -> throw IllegalArgumentException()
}

private fun formattedContent(value: String) =
value.removePrefix(GS1Elements.GS1_d2_IDENTIFIER.element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ class QRImageControllerTest {
@Test
fun shouldReturnDataMatrixWriter() {
val testValue = "]d2\u001D01084700069915412110081996195256\u001D10DXB2005\u001D17220228"
controller.getWriterFromRendering(testValue, UiRenderType.QR_CODE).let { (writer, format) ->
controller.getWriterFromRendering(testValue, UiRenderType.GS1_DATAMATRIX).let { (writer, format) ->
assertTrue(writer is DataMatrixWriter)
assertTrue(format == BarcodeFormat.DATA_MATRIX)
}
}

@Test
fun shouldReturnQRWriterWhenValueDoesNotHaveGS1Prefix() {
val testValue = "01084700069915412110081996195256\u001D10DXB2005\u001D17220228"
controller.getWriterFromRendering(testValue, UiRenderType.GS1_DATAMATRIX).let { (writer, format) ->
assertTrue(writer is QRCodeWriter)
assertTrue(format == BarcodeFormat.QR_CODE)
}
}

@Test
fun shouldReturnQRWriter() {
val testValue = "qrValue"
Expand Down

0 comments on commit 2299924

Please sign in to comment.