Skip to content

Commit

Permalink
Improve permission flow on mirror camera with hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed May 18, 2024
1 parent 8b8d9c0 commit 87aee55
Showing 1 changed file with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ 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
import com.kylecorry.trail_sense.shared.permissions.requestCamera

class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>() {
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?
Expand All @@ -31,7 +33,7 @@ class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>(

override fun onResume() {
super.onResume()
startCamera()
isCameraEnabled = Permissions.isCameraEnabled(requireContext())
flashlight.on()
}

Expand All @@ -42,24 +44,37 @@ class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>(
}

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()
}
}
}
}

0 comments on commit 87aee55

Please sign in to comment.