Skip to content

Commit 87aee55

Browse files
committed
Improve permission flow on mirror camera with hooks
1 parent 8b8d9c0 commit 87aee55

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

app/src/main/java/com/kylecorry/trail_sense/tools/mirror/ui/ToolMirrorCameraFragment.kt

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import android.view.View
77
import android.view.ViewGroup
88
import androidx.camera.view.PreviewView
99
import com.kylecorry.andromeda.fragments.BoundFragment
10+
import com.kylecorry.andromeda.permissions.Permissions
1011
import com.kylecorry.andromeda.torch.ScreenTorch
1112
import com.kylecorry.trail_sense.databinding.FragmentToolMirrorCameraBinding
1213
import com.kylecorry.trail_sense.shared.permissions.alertNoCameraPermission
1314
import com.kylecorry.trail_sense.shared.permissions.requestCamera
1415

1516
class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>() {
1617
private val flashlight by lazy { ScreenTorch(requireActivity().window) }
17-
private var isCameraEnabled = true
18+
private var isCameraEnabled by state(false)
19+
private var wasPermissionRequested by state(false)
1820

1921
override fun generateBinding(
2022
layoutInflater: LayoutInflater, container: ViewGroup?
@@ -31,7 +33,7 @@ class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>(
3133

3234
override fun onResume() {
3335
super.onResume()
34-
startCamera()
36+
isCameraEnabled = Permissions.isCameraEnabled(requireContext())
3537
flashlight.on()
3638
}
3739

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

4446
private fun startCamera() {
45-
if (!isCameraEnabled){
46-
return
47-
}
48-
requestCamera {
49-
if (it) {
50-
binding.camera.start(
51-
readFrames = false,
52-
preferBackCamera = false,
53-
shouldStabilizePreview = false
54-
)
55-
} else {
56-
isCameraEnabled = false
57-
alertNoCameraPermission()
58-
}
59-
}
47+
binding.camera.start(
48+
readFrames = false,
49+
preferBackCamera = false,
50+
shouldStabilizePreview = false
51+
)
6052
}
6153

6254
private fun stopCamera() {
6355
binding.camera.stop()
6456
}
57+
58+
override fun onUpdate() {
59+
super.onUpdate()
60+
effect("camera_permission", wasPermissionRequested, lifecycleHookTrigger.onResume()) {
61+
if (!wasPermissionRequested) {
62+
wasPermissionRequested = true
63+
requestCamera {
64+
isCameraEnabled = it
65+
if (!it) {
66+
alertNoCameraPermission()
67+
}
68+
}
69+
}
70+
}
71+
72+
effect("camera", isCameraEnabled, lifecycleHookTrigger.onResume()) {
73+
if (isCameraEnabled) {
74+
startCamera()
75+
} else {
76+
stopCamera()
77+
}
78+
}
79+
}
6580
}

0 commit comments

Comments
 (0)