fix state update in stop(); pass camera direction in switchCamera(); fix the tor…
…ch state for toggleTorch()
Showing
1 changed file
with
17 additions
and
9 deletions
| @@ -241,40 +241,48 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { | @@ -241,40 +241,48 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { | ||
| 241 | /// | 241 | /// |
| 242 | /// After calling this method, the camera can be restarted using [start]. | 242 | /// After calling this method, the camera can be restarted using [start]. |
| 243 | Future<void> stop() async { | 243 | Future<void> stop() async { |
| 244 | + _disposeListeners(); | ||
| 245 | + | ||
| 246 | + _throwIfNotInitialized(); | ||
| 247 | + | ||
| 244 | await MobileScannerPlatform.instance.stop(); | 248 | await MobileScannerPlatform.instance.stop(); |
| 245 | 249 | ||
| 246 | // After the camera stopped, set the torch state to off, | 250 | // After the camera stopped, set the torch state to off, |
| 247 | // as the torch state callback is never called when the camera is stopped. | 251 | // as the torch state callback is never called when the camera is stopped. |
| 248 | - torchState.value = TorchState.off; | 252 | + value = value.copyWith(torchState: TorchState.off); |
| 249 | } | 253 | } |
| 250 | 254 | ||
| 251 | /// Switch between the front and back camera. | 255 | /// Switch between the front and back camera. |
| 252 | Future<void> switchCamera() async { | 256 | Future<void> switchCamera() async { |
| 253 | - await MobileScannerPlatform.instance.stop(); | 257 | + _throwIfNotInitialized(); |
| 254 | 258 | ||
| 255 | - final CameraFacing cameraDirection; | 259 | + await stop(); |
| 256 | 260 | ||
| 257 | - // TODO: update the camera facing direction state | 261 | + final CameraFacing cameraDirection = value.cameraDirection; |
| 258 | 262 | ||
| 259 | - await start(cameraDirection: cameraDirection); | 263 | + await start( |
| 264 | + cameraDirection: cameraDirection == CameraFacing.front ? CameraFacing.back : CameraFacing.front, | ||
| 265 | + ); | ||
| 260 | } | 266 | } |
| 261 | 267 | ||
| 262 | /// Switches the flashlight on or off. | 268 | /// Switches the flashlight on or off. |
| 263 | /// | 269 | /// |
| 264 | /// Does nothing if the device has no torch. | 270 | /// Does nothing if the device has no torch. |
| 265 | Future<void> toggleTorch() async { | 271 | Future<void> toggleTorch() async { |
| 266 | - final bool hasTorch; | 272 | + _throwIfNotInitialized(); |
| 273 | + | ||
| 274 | + final TorchState torchState = value.torchState; | ||
| 267 | 275 | ||
| 268 | - if (!hasTorch) { | 276 | + if (torchState == TorchState.unavailable) { |
| 269 | return; | 277 | return; |
| 270 | } | 278 | } |
| 271 | 279 | ||
| 272 | - final TorchState newState = torchState.value == TorchState.off ? TorchState.on : TorchState.off; | 280 | + final TorchState newState = torchState == TorchState.off ? TorchState.on : TorchState.off; |
| 273 | 281 | ||
| 274 | // Update the torch state to the new state. | 282 | // Update the torch state to the new state. |
| 275 | // When the platform has updated the torch state, | 283 | // When the platform has updated the torch state, |
| 276 | // it will send an update through the torch state event stream. | 284 | // it will send an update through the torch state event stream. |
| 277 | - await MobileScannerPlatform.instance.setTorchState(); | 285 | + await MobileScannerPlatform.instance.setTorchState(newState); |
| 278 | } | 286 | } |
| 279 | 287 | ||
| 280 | @override | 288 | @override |
-
Please register or login to post a comment