Skip to content

Commit 94a4bba

Browse files
authored
Merge pull request #538 from code-payments/chore/camera-gesture-refine
chore: allow pinch to zoom and drag to zoom to work together
2 parents 8224145 + 9b92cc4 commit 94a4bba

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

app/src/main/java/com/getcode/view/main/camera/CodeScanner.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import com.getcode.util.Biometrics
4545
import com.getcode.utils.trace
4646
import com.kik.kikx.kikcodes.implementation.KikCodeAnalyzer
4747
import com.kik.kikx.kikcodes.implementation.KikCodeScannerImpl
48+
import com.kik.kikx.kikcodes.implementation.rememberKikCodeAnalyzer
4849
import com.kik.kikx.models.ScannableKikCode
4950
import kotlinx.coroutines.Dispatchers
5051
import kotlinx.coroutines.flow.distinctUntilChanged
@@ -222,14 +223,17 @@ private fun setupInteractionControls(
222223
cameraGesturesEnabled: Boolean,
223224
onTap: (Offset) -> Unit,
224225
) {
225-
var isPinchZooming = false
226+
var shouldIgnoreScroll = false
227+
val handler = Handler(Looper.getMainLooper())
228+
var resetIgnore: Runnable? = null
226229

227230
// Pinch-to-zoom gesture detector
228231
val scaleGestureDetector = ScaleGestureDetector(
229232
previewView.context,
230233
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
231234
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
232-
isPinchZooming = true
235+
shouldIgnoreScroll = true
236+
resetIgnore?.let { handler.removeCallbacks(it) }
233237
return true
234238
}
235239

@@ -250,19 +254,18 @@ private fun setupInteractionControls(
250254
}
251255

252256
override fun onScaleEnd(detector: ScaleGestureDetector) {
253-
isPinchZooming = false
257+
resetIgnore = Runnable { shouldIgnoreScroll = false }
258+
previewView.postDelayed(resetIgnore, 500)
254259
}
255260
})
256261

257262
// Gesture detector for tap and drag-to-zoom
258263
val gestureDetector = GestureDetector(
259264
previewView.context,
260265
object : GestureDetector.OnGestureListener {
261-
private var initialZoomLevel = 0f
262266
private var accumulatedDelta = 0f
263267

264268
override fun onDown(e: MotionEvent): Boolean {
265-
initialZoomLevel = cameraInfo.zoomState.value?.zoomRatio ?: 1f
266269
accumulatedDelta = 0f
267270
return true
268271
}
@@ -285,11 +288,11 @@ private fun setupInteractionControls(
285288
distanceX: Float,
286289
distanceY: Float
287290
): Boolean {
288-
if (!isPinchZooming) {
291+
if (!shouldIgnoreScroll) {
289292
accumulatedDelta += distanceY
290293

291294
val deltaZoom = accumulatedDelta / 1000f
292-
295+
val initialZoomLevel = cameraInfo.zoomState.value?.zoomRatio ?: 1f
293296
val maxZoom = cameraInfo.zoomState.value?.maxZoomRatio ?: 1f
294297
val minZoom = cameraInfo.zoomState.value?.minZoomRatio ?: 1f
295298

0 commit comments

Comments
 (0)