Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from brightec/fix/#5
Browse files Browse the repository at this point in the history
Fix/#5
  • Loading branch information
NickHolcombe authored Mar 21, 2019
2 parents 18e251c + d7df1df commit 63bbd62
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions kbarcode/src/main/java/uk/co/brightec/kbarcode/BarcodeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.annotation.IntDef
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import timber.log.Timber
import uk.co.brightec.kbarcode.camera.OnCameraErrorListener
Expand Down Expand Up @@ -51,19 +52,18 @@ class BarcodeView @JvmOverloads constructor(
}

private val surfaceView = SurfaceView(context)
@VisibleForTesting
internal var startRequested: Boolean = false
@ScaleType
private var previewScaleType: Int = CENTER_INSIDE
set(value) {
field = value
requestLayout()
}
private val surfaceLiveData = SurfaceLiveData()
@VisibleForTesting
internal val surfaceObserver = Observer<Boolean> { surfaceAvailable ->
if (surfaceAvailable && startRequested) {
startRequested = false
private val surfaceAvailable = MutableLiveData<Boolean>()
private val surfaceAvailableObserver = object : Observer<Boolean> {
override fun onChanged(available: Boolean) {
if (!available) return

surfaceAvailable.removeObserver(this)
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED
) {
Expand Down Expand Up @@ -100,17 +100,27 @@ class BarcodeView @JvmOverloads constructor(
}

val surfaceHolder = surfaceView.holder
surfaceHolder.addCallback(surfaceLiveData)
surfaceHolder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
surfaceAvailable.value = true
}

override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}

override fun surfaceDestroyed(holder: SurfaceHolder) {
surfaceAvailable.value = false
release()
}
})
barcodeScanner.addSurface(surfaceHolder.surface)
@Suppress("LeakingThis") // Intentional invocation
addView(surfaceView)
}

override fun start() {
startRequested = true
// We remove and add here to ensure the observer gets called even if the value is the same
surfaceLiveData.removeObserver(surfaceObserver)
surfaceLiveData.observeForever(surfaceObserver)
surfaceAvailable.removeObserver(surfaceAvailableObserver)
surfaceAvailable.observeForever(surfaceAvailableObserver)
}

override fun resume() {
Expand All @@ -123,7 +133,7 @@ class BarcodeView @JvmOverloads constructor(

override fun release() {
barcodeScanner.release()
surfaceLiveData.removeObserver(surfaceObserver)
surfaceAvailable.removeObserver(surfaceAvailableObserver)
}

override fun setCameraFacing(facing: Int) {
Expand Down Expand Up @@ -271,19 +281,6 @@ class BarcodeView @JvmOverloads constructor(
}
}

private class SurfaceLiveData : LiveData<Boolean>(), SurfaceHolder.Callback {

override fun surfaceCreated(holder: SurfaceHolder) {
value = true
}

override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}

override fun surfaceDestroyed(holder: SurfaceHolder) {
value = false
}
}

companion object {

/**
Expand Down

0 comments on commit 63bbd62

Please sign in to comment.