|
| 1 | +using DIPS.Mobile.UI.API.Camera.ImageCapturing.Views; |
| 2 | +using DIPS.Mobile.UI.API.Camera.ImageCapturing.Views.BottomToolbar.StreamingState; |
| 3 | +using DIPS.Mobile.UI.Components.Alerting.Dialog; |
| 4 | +using DIPS.Mobile.UI.Resources.LocalizedStrings.LocalizedStrings; |
| 5 | + |
| 6 | +namespace DIPS.Mobile.UI.API.Camera.ImageCapturing; |
| 7 | + |
| 8 | +public partial class ImageCapture |
| 9 | +{ |
| 10 | + private readonly List<CapturedImage> m_capturedImages = []; |
| 11 | + private CapturedImagesGalleryButton? m_capturedImagesGalleryButton; |
| 12 | + private CapturedImagesGalleryOverlay? m_galleryOverlay; |
| 13 | + |
| 14 | + private bool HasReachedImageLimit => |
| 15 | + m_cameraSession is MultiCaptureSession multiCaptureSession |
| 16 | + && m_capturedImages.Count >= multiCaptureSession.MultiImageCaptureOptions.MaxImageCount; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Only show optimistic UI when no per-image confirmation is needed. With per-image confirmation, the image is not |
| 20 | + /// committed until the user accepts it, and should not update UI optimistically. |
| 21 | + /// </summary> |
| 22 | + private bool ShouldUpdateImagePreviewImmediately => |
| 23 | + m_cameraSession is MultiCaptureSession { MultiImageCaptureOptions.RequiresConfirmationOnEachImage: false }; |
| 24 | + |
| 25 | + private void OptimisticallyUpdateGalleryButton() |
| 26 | + { |
| 27 | + if (!ShouldUpdateImagePreviewImmediately) |
| 28 | + return; |
| 29 | + |
| 30 | + var pendingImageCount = m_capturedImages.Count + 1; |
| 31 | + m_capturedImagesGalleryButton?.ShowPendingCapture(pendingImageCount); |
| 32 | + } |
| 33 | + |
| 34 | + private CapturedImagesGalleryButton? CreateCapturedImagesGalleryButtonForMultiCapture() |
| 35 | + { |
| 36 | + if (m_cameraSession is not MultiCaptureSession) |
| 37 | + { |
| 38 | + m_capturedImagesGalleryButton = null; |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + m_capturedImagesGalleryButton = new CapturedImagesGalleryButton(OpenCapturedImagesGallery); |
| 43 | + |
| 44 | + m_capturedImagesGalleryButton.ShowMostRecentImageAndCount(m_capturedImages); |
| 45 | + |
| 46 | + return m_capturedImagesGalleryButton; |
| 47 | + } |
| 48 | + |
| 49 | + private void AddImageAndUpdateImagePreview(CapturedImage capturedImage) |
| 50 | + { |
| 51 | + m_capturedImages.Add(capturedImage); |
| 52 | + |
| 53 | + m_capturedImagesGalleryButton?.ShowMostRecentImageAndCount(m_capturedImages); |
| 54 | + |
| 55 | + if (HasReachedImageLimit) |
| 56 | + { |
| 57 | + ShowMaxImagesReachedMessage(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void ShowMaxImagesReachedMessage() |
| 62 | + { |
| 63 | + if (m_cameraSession is not MultiCaptureSession multiCaptureSession) |
| 64 | + throw new InvalidOperationException($"{nameof(ShowMaxImagesReachedMessage)} is only valid during a multi capture session, but the session was {m_cameraSession?.GetType().Name ?? "null"}."); |
| 65 | + |
| 66 | + var maxImageCount = multiCaptureSession.MultiImageCaptureOptions.MaxImageCount; |
| 67 | + _ = DialogService.ShowMessage(configurator => configurator |
| 68 | + .SetTitle(DUILocalizedStrings.MaxImagesReachedTitle) |
| 69 | + .SetDescription(string.Format(DUILocalizedStrings.MaxImagesReachedMessage, maxImageCount)) |
| 70 | + .SetActionTitle(DUILocalizedStrings.Ok)); |
| 71 | + } |
| 72 | + |
| 73 | + private void OpenCapturedImagesGallery() |
| 74 | + { |
| 75 | + if (m_capturedImages.Count == 0) |
| 76 | + throw new InvalidOperationException("Captured-images gallery opened with no images. The preview is hidden when empty, so this should be unreachable."); |
| 77 | + |
| 78 | + if (m_cameraPreview is null) |
| 79 | + throw new InvalidOperationException("Captured-images gallery opened after the camera preview was torn down."); |
| 80 | + |
| 81 | + // Guard against repeat taps |
| 82 | + if (m_galleryOverlay is not null) |
| 83 | + return; |
| 84 | + |
| 85 | + m_cameraPreview.HidePreview(); |
| 86 | + |
| 87 | + var startingIndex = m_capturedImages.Count - 1; |
| 88 | + |
| 89 | + m_galleryOverlay = new CapturedImagesGalleryOverlay( |
| 90 | + m_capturedImages, |
| 91 | + startingIndex, |
| 92 | + onRemoveImageAtAction: RemoveImageAndUpdateGalleryButton, |
| 93 | + onCloseAction: CloseCapturedImagesGallery); |
| 94 | + |
| 95 | + m_cameraPreview.AddViewToRoot(m_galleryOverlay); |
| 96 | + } |
| 97 | + |
| 98 | + private async void CloseCapturedImagesGallery() |
| 99 | + { |
| 100 | + try |
| 101 | + { |
| 102 | + ArgumentNullException.ThrowIfNull(m_cameraPreview); |
| 103 | + ArgumentNullException.ThrowIfNull(m_galleryOverlay); |
| 104 | + |
| 105 | + var overlay = m_galleryOverlay; |
| 106 | + m_galleryOverlay = null; |
| 107 | + |
| 108 | + m_cameraPreview.ShowPreview(); |
| 109 | + |
| 110 | + await overlay.FadeOutAsync(); |
| 111 | + |
| 112 | + // The user can navigate away from the page during the fade, which nulls the preview. |
| 113 | + if (m_cameraPreview is null) |
| 114 | + return; |
| 115 | + |
| 116 | + m_cameraPreview.RemoveViewFromRoot(overlay); |
| 117 | + |
| 118 | + m_capturedImagesGalleryButton?.ShowMostRecentImageAndCount(m_capturedImages); |
| 119 | + m_bottomToolbarView?.SetShutterButtonEnabled(true); |
| 120 | + } |
| 121 | + catch (Exception e) |
| 122 | + { |
| 123 | + Log(e.Message); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void TearDownGalleryOverlay() |
| 128 | + { |
| 129 | + m_cameraPreview?.RemoveViewFromRoot(m_galleryOverlay); |
| 130 | + m_galleryOverlay = null; |
| 131 | + } |
| 132 | + |
| 133 | + private void RemoveImageAndUpdateGalleryButton(int index) |
| 134 | + { |
| 135 | + if (index < 0 || index >= m_capturedImages.Count) |
| 136 | + throw new ArgumentOutOfRangeException(nameof(index), index, $"The gallery asked to remove an image at an index outside the captured set (count {m_capturedImages.Count})."); |
| 137 | + |
| 138 | + var removedImage = m_capturedImages[index]; |
| 139 | + m_capturedImages.RemoveAt(index); |
| 140 | + |
| 141 | + if (m_cameraSession is MultiCaptureSession multiCaptureSession) |
| 142 | + { |
| 143 | + multiCaptureSession.MultiImageCaptureOptions.OnImageRemoved?.Invoke(removedImage); |
| 144 | + } |
| 145 | + |
| 146 | + m_capturedImagesGalleryButton?.ShowMostRecentImageAndCount(m_capturedImages); |
| 147 | + } |
| 148 | +} |
0 commit comments