parent
bfcbe4446e
commit
9d2300bd0a
3 changed files with 17 additions and 4 deletions
|
@ -8,6 +8,7 @@ enum class PassBarCodeFormat {
|
|||
CODE_39,
|
||||
CODE_128,
|
||||
DATA_MATRIX,
|
||||
EAN_8,
|
||||
EAN_13,
|
||||
ITF,
|
||||
PDF_417,
|
||||
|
@ -23,6 +24,7 @@ enum class PassBarCodeFormat {
|
|||
CODE_39 -> BarcodeFormat.CODE_39
|
||||
CODE_128 -> BarcodeFormat.CODE_128
|
||||
DATA_MATRIX -> BarcodeFormat.DATA_MATRIX
|
||||
EAN_8 -> BarcodeFormat.EAN_8
|
||||
EAN_13 -> BarcodeFormat.EAN_13
|
||||
ITF -> BarcodeFormat.ITF
|
||||
PDF_417 -> BarcodeFormat.PDF_417
|
||||
|
|
|
@ -9,8 +9,7 @@ import kotlinx.android.synthetic.main.barcode_edit.view.*
|
|||
import org.ligi.kaxt.doAfterEdit
|
||||
import org.ligi.passandroid.model.pass.BarCode
|
||||
import org.ligi.passandroid.model.pass.PassBarCodeFormat
|
||||
import org.ligi.passandroid.model.pass.PassBarCodeFormat.EAN_13
|
||||
import org.ligi.passandroid.model.pass.PassBarCodeFormat.QR_CODE
|
||||
import org.ligi.passandroid.model.pass.PassBarCodeFormat.*
|
||||
import org.ligi.passandroid.ui.BarcodeUIController
|
||||
import org.ligi.passandroid.ui.PassViewHelper
|
||||
import java.util.*
|
||||
|
@ -56,6 +55,7 @@ class BarcodeEditController(val rootView: View, internal val context: AppCompatA
|
|||
|
||||
rootView.randomButton.setOnClickListener({
|
||||
rootView.messageInput.setText(when (barcodeFormat) {
|
||||
EAN_8 -> getRandomEAN8()
|
||||
EAN_13 -> getRandomEAN13()
|
||||
PassBarCodeFormat.ITF -> getRandomITF()
|
||||
else -> UUID.randomUUID().toString().toUpperCase()
|
||||
|
|
|
@ -1,14 +1,25 @@
|
|||
package org.ligi.passandroid.ui.edit
|
||||
|
||||
import com.google.zxing.oned.EAN13Writer
|
||||
import com.google.zxing.oned.EAN8Writer
|
||||
import com.google.zxing.oned.UPCEANWriter
|
||||
|
||||
fun getRandomEAN13(): String {
|
||||
val randomString = (0..11).map { (Math.random() * 9).toInt() }.joinToString(separator = "")
|
||||
return (0..9).map { randomString + it }.first(::isValidEAN13)
|
||||
}
|
||||
|
||||
fun isValidEAN13(payload: String) = try {
|
||||
EAN13Writer().encode(payload)
|
||||
fun isValidEAN13(payload: String) = isValidEAN(payload, EAN13Writer())
|
||||
|
||||
fun getRandomEAN8(): String {
|
||||
val randomString = (0..6).map { (Math.random() * 9).toInt() }.joinToString(separator = "")
|
||||
return (0..9).map { randomString + it }.first(::isValidEAN8)
|
||||
}
|
||||
|
||||
fun isValidEAN8(payload: String) = isValidEAN(payload, EAN8Writer())
|
||||
|
||||
fun isValidEAN(payload: String, writer: UPCEANWriter) = try {
|
||||
writer.encode(payload)
|
||||
true
|
||||
} catch(e: IllegalArgumentException) {
|
||||
false
|
||||
|
|
Loading…
Reference in a new issue