@@ -7,14 +7,16 @@ import android.view.View
7
7
import android.view.ViewGroup
8
8
import androidx.camera.view.PreviewView
9
9
import com.kylecorry.andromeda.fragments.BoundFragment
10
+ import com.kylecorry.andromeda.permissions.Permissions
10
11
import com.kylecorry.andromeda.torch.ScreenTorch
11
12
import com.kylecorry.trail_sense.databinding.FragmentToolMirrorCameraBinding
12
13
import com.kylecorry.trail_sense.shared.permissions.alertNoCameraPermission
13
14
import com.kylecorry.trail_sense.shared.permissions.requestCamera
14
15
15
16
class ToolMirrorCameraFragment : BoundFragment <FragmentToolMirrorCameraBinding >() {
16
17
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 )
18
20
19
21
override fun generateBinding (
20
22
layoutInflater : LayoutInflater , container : ViewGroup ?
@@ -31,7 +33,7 @@ class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>(
31
33
32
34
override fun onResume () {
33
35
super .onResume()
34
- startCamera( )
36
+ isCameraEnabled = Permissions .isCameraEnabled(requireContext() )
35
37
flashlight.on()
36
38
}
37
39
@@ -42,24 +44,37 @@ class ToolMirrorCameraFragment : BoundFragment<FragmentToolMirrorCameraBinding>(
42
44
}
43
45
44
46
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
+ )
60
52
}
61
53
62
54
private fun stopCamera () {
63
55
binding.camera.stop()
64
56
}
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
+ }
65
80
}
0 commit comments