Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set color to border #14

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ internal class QROverlayView @JvmOverloads constructor(
private val binding = QuickieOverlayViewBinding.inflate(LayoutInflater.from(context), this)
private val grayColor = ContextCompat.getColor(context, R.color.quickie_gray)
private val accentColor = getAccentColor()
private var borderColor = grayColor
private var successBorderColor = accentColor
private val backgroundColor = ColorUtils.setAlphaComponent(Color.BLACK, BACKGROUND_ALPHA.roundToInt())
private val alphaPaint = Paint().apply { alpha = BACKGROUND_ALPHA.roundToInt() }
private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG)
Expand Down Expand Up @@ -76,7 +78,7 @@ internal class QROverlayView @JvmOverloads constructor(
}

override fun onDraw(canvas: Canvas) {
strokePaint.color = if (isHighlighted) accentColor else grayColor
strokePaint.color = if (isHighlighted) successBorderColor else borderColor
maskCanvas!!.drawColor(backgroundColor)
maskCanvas!!.drawRoundRect(outerFrame, outerRadius, outerRadius, strokePaint)
maskCanvas!!.drawRoundRect(innerFrame, innerRadius, innerRadius, transparentPaint)
Expand Down Expand Up @@ -188,6 +190,14 @@ internal class QROverlayView @JvmOverloads constructor(

private fun Float.toPx() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, resources.displayMetrics)

fun setBorderColor(borderColor: Int) {
this.borderColor = ContextCompat.getColor(context, borderColor)
}

fun setSuccessBorderColor(successBorderColor: Int) {
this.successBorderColor = ContextCompat.getColor(context, successBorderColor)
}

companion object {
private const val BACKGROUND_ALPHA = 0.77 * 255
private const val BUTTON_BACKGROUND_ALPHA = 0.6 * 255
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ internal class QRScannerActivity : AppCompatActivity() {
binding.overlayView.setCustomTextAndIcon(it.stringRes, it.drawableRes)
hapticFeedback = it.hapticFeedback
showTorchToggle = it.showTorchToggle
binding.overlayView.setBorderColor(it.borderColor)
binding.overlayView.setSuccessBorderColor(it.successBorderColor)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ internal class ParcelableScannerConfig(
val drawableRes: Int,
val hapticFeedback: Boolean,
val showTorchToggle: Boolean,
val borderColor: Int,
val successBorderColor: Int
) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.g00fy2.quickie.config

import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes

Expand All @@ -11,7 +12,9 @@ public class ScannerConfig internal constructor(
internal val stringRes: Int,
internal val drawableRes: Int,
internal val hapticFeedback: Boolean,
internal val showTorchToggle: Boolean
internal val showTorchToggle: Boolean,
internal val borderColor: Int,
internal val successBorderColor: Int
) {

public class Builder {
Expand All @@ -20,6 +23,8 @@ public class ScannerConfig internal constructor(
private var overlayDrawableRes: Int = 0
private var hapticSuccessFeedback: Boolean = true
private var showTorchToggle: Boolean = false
private var borderColor: Int = 0
private var successBorderColor: Int = 0

/**
* Set a list of interested barcode formats. List must not be empty.
Expand Down Expand Up @@ -48,6 +53,19 @@ public class ScannerConfig internal constructor(
*/
public fun setShowTorchToggle(enable: Boolean): Builder = apply { showTorchToggle = enable }

/**
* Set border color
*/
public fun setBorderColor(@ColorRes borderColor: Int): Builder = apply { this.borderColor = borderColor }

/**
* Set border color in success mode
*/
public fun setBorderSuccessColor(@ColorRes successBorderColor: Int): Builder =
apply {
this.successBorderColor = successBorderColor
}

/**
* Build the BarcodeConfig required by the ScanBarcode ActivityResultContract.
*/
Expand All @@ -57,7 +75,9 @@ public class ScannerConfig internal constructor(
overlayStringRes,
overlayDrawableRes,
hapticSuccessFeedback,
showTorchToggle
showTorchToggle,
borderColor,
successBorderColor
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ internal fun ScannerConfig.toParcelableConfig() =
drawableRes = drawableRes,
hapticFeedback = hapticFeedback,
showTorchToggle = showTorchToggle,
borderColor = borderColor,
successBorderColor = successBorderColor
)