Skip to content

Commit

Permalink
Add optional close button to scanner view
Browse files Browse the repository at this point in the history
  • Loading branch information
G00fY2 committed Jan 8, 2023
1 parent 4208fcc commit 3a39f40
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ internal class QROverlayView @JvmOverloads constructor(
}
}

fun setCloseVisibilityAndOnClick(visible: Boolean, action: () -> Unit = {}) {
binding.closeImageView.visibility = if (visible) View.VISIBLE else View.GONE
binding.closeImageView.setOnClickListener { action() }
if (visible) binding.closeImageView.setTintAndStateAwareBackground()
}

fun setTorchVisibilityAndOnClick(visible: Boolean, action: (Boolean) -> Unit = {}) {
binding.torchImageView.visibility = if (visible) View.VISIBLE else View.GONE
binding.torchImageView.setOnClickListener { action(!it.isSelected) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class QRScannerActivity : AppCompatActivity() {
private var barcodeFormats = intArrayOf(Barcode.FORMAT_QR_CODE)
private var hapticFeedback = true
private var showTorchToggle = false
private var showCloseButton = false
private var useFrontCamera = false
internal var errorDialog: Dialog? = null
set(value) {
Expand Down Expand Up @@ -124,6 +125,7 @@ internal class QRScannerActivity : AppCompatActivity() {
try {
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalysis)
binding.overlayView.visibility = View.VISIBLE
binding.overlayView.setCloseVisibilityAndOnClick(showCloseButton) { finish() }
if (showTorchToggle && camera.cameraInfo.hasFlashUnit()) {
binding.overlayView.setTorchVisibilityAndOnClick(true) { camera.cameraControl.enableTorch(it) }
camera.cameraInfo.torchState.observe(this) { binding.overlayView.setTorchState(it == TorchState.ON) }
Expand Down Expand Up @@ -184,6 +186,7 @@ internal class QRScannerActivity : AppCompatActivity() {
hapticFeedback = it.hapticFeedback
showTorchToggle = it.showTorchToggle
useFrontCamera = it.useFrontCamera
showCloseButton = it.showCloseButton
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ internal class ParcelableScannerConfig(
val showTorchToggle: Boolean,
val horizontalFrameRatio: Float,
val useFrontCamera: Boolean,
val showCloseButton: Boolean,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.annotation.StringRes
/**
* Builder for ScannerConfig used in ScanBarcode ActivityResultContract.
*/
@Suppress("LongParameterList")
public class ScannerConfig internal constructor(
internal val formats: IntArray,
internal val stringRes: Int,
Expand All @@ -14,6 +15,7 @@ public class ScannerConfig internal constructor(
internal val showTorchToggle: Boolean,
internal val horizontalFrameRatio: Float,
internal val useFrontCamera: Boolean,
internal val showCloseButton: Boolean,
) {

public class Builder {
Expand All @@ -24,6 +26,7 @@ public class ScannerConfig internal constructor(
private var showTorchToggle: Boolean = false
private var horizontalFrameRatio: Float = 1f
private var useFrontCamera: Boolean = false
private var showCloseButton: Boolean = false

/**
* Set a list of interested barcode formats. List must not be empty.
Expand Down Expand Up @@ -63,6 +66,11 @@ public class ScannerConfig internal constructor(
*/
public fun setUseFrontCamera(enable: Boolean): Builder = apply { useFrontCamera = enable }

/**
* Show or hide (default) close button.
*/
public fun setShowCloseButton(enable: Boolean): Builder = apply { showCloseButton = enable }

/**
* Build the BarcodeConfig required by the ScanBarcode ActivityResultContract.
*/
Expand All @@ -75,6 +83,7 @@ public class ScannerConfig internal constructor(
showTorchToggle,
horizontalFrameRatio,
useFrontCamera,
showCloseButton,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ internal fun ScannerConfig.toParcelableConfig() =
showTorchToggle = showTorchToggle,
horizontalFrameRatio = horizontalFrameRatio,
useFrontCamera = useFrontCamera,
showCloseButton = showCloseButton,
)
26 changes: 26 additions & 0 deletions quickie/src/main/res/drawable/quickie_ic_close.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 Thomas Wirth
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file has been modified by Thomas Wirth by replacing the fillColor.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24.0dp"
android:height="24.0dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M18.3,5.71a0.996,0.996 0,0 0,-1.41 0L12,10.59 7.11,5.7A0.996,0.996 0,1 0,5.7 7.11L10.59,12 5.7,16.89a0.996,0.996 0,1 0,1.41 1.41L12,13.41l4.89,4.89a0.996,0.996 0,1 0,1.41 -1.41L13.41,12l4.89,-4.89c0.38,-0.38 0.38,-1.02 0,-1.4z"
android:fillColor="@color/quickie_white"/>
</vector>
15 changes: 14 additions & 1 deletion quickie/src/main/res/layout/quickie_overlay_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@
app:drawableTopCompat="@drawable/quickie_ic_qrcode"
/>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/close_image_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="top|start"
android:layout_margin="12dp"
android:background="@drawable/quickie_bg_round"
android:padding="12dp"
android:visibility="gone"
app:srcCompat="@drawable/quickie_ic_close"
tools:visibility="visible"
/>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/torch_image_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="top|end"
android:layout_margin="12dp"
android:background="@drawable/quickie_bg_torch"
android:background="@drawable/quickie_bg_round"
android:padding="12dp"
android:visibility="gone"
app:srcCompat="@drawable/quickie_ic_torch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MainActivity : AppCompatActivity() {
setOverlayDrawableRes(R.drawable.ic_scan_barcode) // drawable resource used for the scanner overlay
setHapticSuccessFeedback(false) // enable (default) or disable haptic feedback when a barcode was detected
setShowTorchToggle(true) // show or hide (default) torch/flashlight toggle button
setShowCloseButton(true) // show or hide (default) close button
setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
setUseFrontCamera(false) // use the front camera
}
Expand Down

0 comments on commit 3a39f40

Please sign in to comment.