|
| 1 | +using DIPS.Mobile.UI.API.Camera; |
| 2 | +using DIPS.Mobile.UI.API.Camera.ImageCapturing; |
| 3 | +using DIPS.Mobile.UI.API.Camera.ImageCapturing.Settings; |
| 4 | + |
| 5 | +namespace Components.ComponentsSamples.ImageCapturing; |
| 6 | + |
| 7 | +public partial class MultiImageCaptureModalPage |
| 8 | +{ |
| 9 | + private readonly bool m_requiresConfirmation; |
| 10 | + private readonly List<CapturedImage> m_capturedImages = []; |
| 11 | + private readonly ImageCapture m_imageCapture = new(); |
| 12 | + private readonly TaskCompletionSource<List<CapturedImage>> m_completion = |
| 13 | + new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 14 | + |
| 15 | + public MultiImageCaptureModalPage(bool requiresConfirmation) |
| 16 | + { |
| 17 | + InitializeComponent(); |
| 18 | + m_requiresConfirmation = requiresConfirmation; |
| 19 | + } |
| 20 | + |
| 21 | + public Task<List<CapturedImage>> CompletionTask => m_completion.Task; |
| 22 | + |
| 23 | + protected override async void OnAppearing() |
| 24 | + { |
| 25 | + base.OnAppearing(); |
| 26 | + |
| 27 | + var cameraOptions = new CameraOptions |
| 28 | + { |
| 29 | + CancelButtonCommand = new Command(OnCancelled) |
| 30 | + }; |
| 31 | + |
| 32 | + var multiImageCaptureOptions = new MultiImageCaptureOptions |
| 33 | + { |
| 34 | + RequiresConfirmationOnEachImage = m_requiresConfirmation, |
| 35 | + FinishedButtonCommand = new Command(OnFinished) |
| 36 | + }; |
| 37 | + |
| 38 | + await m_imageCapture.StartMultiImageCapture(CameraPreview, OnImageCaptured, OnCameraFailed, |
| 39 | + cameraOptions, multiImageCaptureOptions); |
| 40 | + } |
| 41 | + |
| 42 | + protected override void OnDisappearing() |
| 43 | + { |
| 44 | + base.OnDisappearing(); |
| 45 | + |
| 46 | + // Covers swipe-to-dismiss and Android back button. No-op if a handler already completed the TCS. |
| 47 | + m_completion.TrySetResult([]); |
| 48 | + } |
| 49 | + |
| 50 | + private void OnImageCaptured(CapturedImage capturedImage) |
| 51 | + { |
| 52 | + m_capturedImages.Add(capturedImage); |
| 53 | + } |
| 54 | + |
| 55 | + private async void OnFinished() |
| 56 | + { |
| 57 | + m_completion.TrySetResult(m_capturedImages); |
| 58 | + await Shell.Current.Navigation.PopModalAsync(); |
| 59 | + } |
| 60 | + |
| 61 | + private async void OnCancelled() |
| 62 | + { |
| 63 | + m_completion.TrySetResult([]); |
| 64 | + await Shell.Current.Navigation.PopModalAsync(); |
| 65 | + } |
| 66 | + |
| 67 | + private async void OnCameraFailed(CameraException e) |
| 68 | + { |
| 69 | + await DisplayAlertAsync("Something failed!", e.Message, "Ok"); |
| 70 | + m_completion.TrySetResult([]); |
| 71 | + await Shell.Current.Navigation.PopModalAsync(); |
| 72 | + } |
| 73 | +} |
0 commit comments