From 87aee5519b2b8363d11a888273baa018c572d4e0 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Sat, 18 May 2024 10:09:16 -0400 Subject: [PATCH] Improve permission flow on mirror camera with hooks --- .../mirror/ui/ToolMirrorCameraFragment.kt | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/mirror/ui/ToolMirrorCameraFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/mirror/ui/ToolMirrorCameraFragment.kt index 78a56e191..5e227c654 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/mirror/ui/ToolMirrorCameraFragment.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/mirror/ui/ToolMirrorCameraFragment.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import androidx.camera.view.PreviewView import com.kylecorry.andromeda.fragments.BoundFragment +import com.kylecorry.andromeda.permissions.Permissions import com.kylecorry.andromeda.torch.ScreenTorch import com.kylecorry.trail_sense.databinding.FragmentToolMirrorCameraBinding import com.kylecorry.trail_sense.shared.permissions.alertNoCameraPermission @@ -14,7 +15,8 @@ import com.kylecorry.trail_sense.shared.permissions.requestCamera class ToolMirrorCameraFragment : BoundFragment() { private val flashlight by lazy { ScreenTorch(requireActivity().window) } - private var isCameraEnabled = true + private var isCameraEnabled by state(false) + private var wasPermissionRequested by state(false) override fun generateBinding( layoutInflater: LayoutInflater, container: ViewGroup? @@ -31,7 +33,7 @@ class ToolMirrorCameraFragment : BoundFragment( override fun onResume() { super.onResume() - startCamera() + isCameraEnabled = Permissions.isCameraEnabled(requireContext()) flashlight.on() } @@ -42,24 +44,37 @@ class ToolMirrorCameraFragment : BoundFragment( } private fun startCamera() { - if (!isCameraEnabled){ - return - } - requestCamera { - if (it) { - binding.camera.start( - readFrames = false, - preferBackCamera = false, - shouldStabilizePreview = false - ) - } else { - isCameraEnabled = false - alertNoCameraPermission() - } - } + binding.camera.start( + readFrames = false, + preferBackCamera = false, + shouldStabilizePreview = false + ) } private fun stopCamera() { binding.camera.stop() } + + override fun onUpdate() { + super.onUpdate() + effect("camera_permission", wasPermissionRequested, lifecycleHookTrigger.onResume()) { + if (!wasPermissionRequested) { + wasPermissionRequested = true + requestCamera { + isCameraEnabled = it + if (!it) { + alertNoCameraPermission() + } + } + } + } + + effect("camera", isCameraEnabled, lifecycleHookTrigger.onResume()) { + if (isCameraEnabled) { + startCamera() + } else { + stopCamera() + } + } + } } \ No newline at end of file