Skip to content

Conversation

TheCodeTraveler
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler commented Jun 10, 2025

Description of Change

API Change

This PR updates ICameraView.CaptureImage() to return the Stream captured by the camera:

// Previous (Existing) API
ValueTask CaptureImage(CancellationToken token);

to

// Updated API
Task<Stream> CaptureImage(CancellationToken token);

Developer Experience Improvement

This improves the developer experience, allowing devs to retrieve the image directly from the method, whereas the current/previous experience required them to subscribe to CameraView.MediaCaptured:

// Previous (Existing) Developer Experience
public partial class CameraPage : Content
{
  public CameraPage()
  {
      InitializeComponent();

      cameraView.MediaCaptured += HandleMediaCaptured;
      cameraView.MediaCaptureFailed += HandleMediaCaptureFailed
  }

  async void CaptureButtonTapped(object? sender, EventArgs e)
  {
      await cameraView.CaptureImage(CancellationToken.None);
  }

  void HandleMediaCaptured(object? sender, MediaCapturedEventArgs e)
  {  
      capturedImage.Source = ImageSource.FromStream(() => e.Meda);
  }
  
  async void HandleMediaCapturedFailed(object? sender, MediaCaptureFailedEventArgs e)
  {  
      await Toast.Make("Capture Failed").Show();
  }
}
// Updated Developer Experience
public partial class CameraPage : Content
{
  public CameraPage()
  {
      InitializeComponent();
  }

  async void CaptureButtonTapped(object? sender, EventArgs e)
  {
      try
      {
            var imageStream = await cameraView.CaptureImage(CancellationToken.None);
            capturedImage.Source = ImageSource.FromStream(() => imageStream);
      }
      catch(CameraException e)
      {
            await Toast.Make("Capture Failed").Show();
      }
  }
}

PR Checklist

@Copilot Copilot AI review requested due to automatic review settings June 10, 2025 19:11
@TheCodeTraveler TheCodeTraveler added pending documentation This feature requires documentation breaking change This label is used for PRs that include a breaking change needs discussion Discuss it on the next Monthly standup labels Jun 10, 2025
Copilot

This comment was marked as outdated.

@TheCodeTraveler TheCodeTraveler requested a review from Copilot June 10, 2025 19:14
Copilot

This comment was marked as outdated.

@TheCodeTraveler TheCodeTraveler requested a review from Copilot June 10, 2025 19:40
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors ICameraView.CaptureImage() to return a Task<Stream> instead of ValueTask, implements a semaphore-protected event‐based capture flow, and adds disposal logic to CameraView.

  • Introduces IDisposable on CameraView for cleanup of SemaphoreSlim
  • Changes CaptureImage to async Task<Stream>, uses TaskCompletionSource<Stream> with MediaCaptured/MediaCaptureFailed events
  • Updates ICameraView interface signature and adjusts related bindable property defaults

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs Added IDisposable, semaphore management, new CaptureImage logic
src/CommunityToolkit.Maui.Camera/Interfaces/ICameraView.shared.cs Updated CaptureImage from ValueTask to Task<Stream>
Comments suppressed due to low confidence (2)

src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs:221

  • The new CaptureImage behavior (success, failure, cancellation, and concurrent calls) isn’t covered by existing tests—consider adding unit or integration tests to validate event unsubscription, exception paths, and semaphore behavior.
public async Task<Stream> CaptureImage(CancellationToken token)

src/CommunityToolkit.Maui.Camera/Interfaces/ICameraView.shared.cs:58

  • Update any public API docs, samples, and IntelliSense comments to reflect that CaptureImage now returns a Task<Stream> instead of ValueTask and no longer relies on the MediaCaptured event.
Task<Stream> CaptureImage(CancellationToken token);

@TheCodeTraveler TheCodeTraveler added approved This Proposal has been approved and is ready to be added to the Toolkit and removed pending documentation This feature requires documentation labels Jun 11, 2025
@TheCodeTraveler TheCodeTraveler enabled auto-merge (squash) June 14, 2025 19:37
@TheCodeTraveler TheCodeTraveler merged commit ecc51ed into main Jun 14, 2025
10 checks passed
@TheCodeTraveler TheCodeTraveler deleted the Return-Camera-Image-from-`CameraView.CaptureImage()` branch June 14, 2025 20:12
@github-actions github-actions bot locked and limited conversation to collaborators Jun 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved This Proposal has been approved and is ready to be added to the Toolkit breaking change This label is used for PRs that include a breaking change needs discussion Discuss it on the next Monthly standup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants