Navaron Bracke

move barcode scanner options builder into method for android

@@ -151,28 +151,16 @@ class MobileScannerHandler( @@ -151,28 +151,16 @@ class MobileScannerHandler(
151 null 151 null
152 } 152 }
153 153
154 - var barcodeScannerOptions: BarcodeScannerOptions? = null  
155 - if (formats != null) {  
156 - val formatsList: MutableList<Int> = mutableListOf()  
157 - for (formatValue in formats) {  
158 - formatsList.add(BarcodeFormats.fromRawValue(formatValue).intValue)  
159 - }  
160 - barcodeScannerOptions = if (formatsList.size == 1) {  
161 - BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first())  
162 - .build()  
163 - } else {  
164 - BarcodeScannerOptions.Builder().setBarcodeFormats(  
165 - formatsList.first(),  
166 - *formatsList.subList(1, formatsList.size).toIntArray()  
167 - ).build()  
168 - }  
169 - } 154 + val barcodeScannerOptions: BarcodeScannerOptions? = buildBarcodeScannerOptions(formats)
170 155
171 val position = 156 val position =
172 if (facing == 0) CameraSelector.DEFAULT_FRONT_CAMERA else CameraSelector.DEFAULT_BACK_CAMERA 157 if (facing == 0) CameraSelector.DEFAULT_FRONT_CAMERA else CameraSelector.DEFAULT_BACK_CAMERA
173 158
174 - val detectionSpeed: DetectionSpeed = if (speed == 0) DetectionSpeed.NO_DUPLICATES  
175 - else if (speed ==1) DetectionSpeed.NORMAL else DetectionSpeed.UNRESTRICTED 159 + val detectionSpeed: DetectionSpeed = when (speed) {
  160 + 0 -> DetectionSpeed.NO_DUPLICATES
  161 + 1 -> DetectionSpeed.NORMAL
  162 + else -> DetectionSpeed.UNRESTRICTED
  163 + }
176 164
177 mobileScanner!!.start( 165 mobileScanner!!.start(
178 barcodeScannerOptions, 166 barcodeScannerOptions,
@@ -284,4 +272,26 @@ class MobileScannerHandler( @@ -284,4 +272,26 @@ class MobileScannerHandler(
284 272
285 result.success(null) 273 result.success(null)
286 } 274 }
  275 +
  276 + private fun buildBarcodeScannerOptions(formats: List<Int>?): BarcodeScannerOptions? {
  277 + if (formats == null) {
  278 + return null
  279 + }
  280 +
  281 + val formatsList: MutableList<Int> = mutableListOf()
  282 +
  283 + for (formatValue in formats) {
  284 + formatsList.add(BarcodeFormats.fromRawValue(formatValue).intValue)
  285 + }
  286 +
  287 + if (formatsList.size == 1) {
  288 + return BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first())
  289 + .build()
  290 + }
  291 +
  292 + return BarcodeScannerOptions.Builder().setBarcodeFormats(
  293 + formatsList.first(),
  294 + *formatsList.subList(1, formatsList.size).toIntArray()
  295 + ).build()
  296 + }
287 } 297 }