-
Notifications
You must be signed in to change notification settings - Fork 430
Refactor CameraView.CaptureImage
, CameraView.StartCameraPreview
and CameraView.StopCameraPreview
#2655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 the CameraView methods to remove the non-awaitable Handler.Invoke pattern and instead use properly awaitable asynchronous methods from CameraManager. Additionally, it removes the IAsynchronousHandler interface and updates namespaces and command mappings to align with the new design.
- Refactored CameraView public methods (CaptureImage, StartCameraPreview, StopCameraPreview) to directly call asynchronous CameraManager methods.
- Removed the asynchronous handler pattern and updated command mappings in CameraViewHandler.
- Updated namespace usage by removing references to CommunityToolkit.Maui.Core.Primitives.
Reviewed Changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs | Refactored async method implementations and updated error messages. |
src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.windows.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.macios.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.android.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/Primitives/MediaCapturedEventArgs.shared.cs | Updated namespace from Views to Core. |
src/CommunityToolkit.Maui.Camera/Primitives/MediaCaptureFailedEventArgs.shared.cs | Updated namespace from Views to Core. |
src/CommunityToolkit.Maui.Camera/Primitives/CameraViewDefaults.shared.cs | Updated namespace and removed unused using directive. |
src/CommunityToolkit.Maui.Camera/Primitives/CameraPosition.shared.cs | Updated namespace from Primitives to Core. |
src/CommunityToolkit.Maui.Camera/Primitives/CameraFlashMode.shared.cs | Updated namespace from Primitives to Core. |
src/CommunityToolkit.Maui.Camera/Interfaces/ICameraView.shared.cs | Removed IAsynchronousHandler inheritance and updated method signatures. |
src/CommunityToolkit.Maui.Camera/Interfaces/IAsynchronouseHandler.cs | Removed the asynchronous handler interface. |
src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs | Updated command mappings and refactored async command implementations. |
src/CommunityToolkit.Maui.Camera/Extensions/CameraViewExtensions.macios.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/Extensions/CameraViewExtensions.android.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/CameraManager.windows.cs | Removed unused namespace reference and updated code accordingly. |
src/CommunityToolkit.Maui.Camera/CameraManager.shared.cs | Updated namespace from Core.Primitives to Core. |
src/CommunityToolkit.Maui.Camera/CameraManager.net.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/CameraManager.macios.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/CameraManager.android.cs | Removed unused namespace reference. |
src/CommunityToolkit.Maui.Camera/CameraInfo.shared.cs | Removed unused namespace reference. |
Co-authored-by: Copilot <[email protected]>
…d-StopCameraPreview' of https://github.com/CommunityToolkit/Maui into Refactor-CameraView.CaptureImage,-StartCameraPreview-and-StopCameraPreview
There was a problem hiding this 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 core CameraView methods to support proper asynchronous operations and exception handling by removing the async void patterns, updating method signatures, and adjusting internal namespaces.
- Refactors asynchronous CameraView methods (CaptureImage, StartCameraPreview, and StopCameraPreview)
- Removes obsolete IAsynchronousHandler interface and updates namespace references
- Updates command mappings in CameraViewHandler to use proper asynchronous calls
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
src/CommunityToolkit.Maui.Camera/Providers/* | Removed redundant using directives and updated namespaces |
src/CommunityToolkit.Maui.Camera/Primitives/* | Changed namespaces from Views to Core |
src/CommunityToolkit.Maui.Camera/Interfaces/* | Updated method signatures and access modifiers |
src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs | Updated command mappings and refactored async methods |
src/CommunityToolkit.Maui.Camera/CameraManager.* | Removed obsolete using directives and updated namespaces |
samples/CommunityToolkit.Maui.Sample/Pages/Views/CameraView/CameraViewPage.xaml.cs | Updated namespace usage |
Comments suppressed due to low confidence (1)
src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs:146
- The MapStopCameraPreview function uses await inside its body but is not marked as async, which will lead to a compile error. Consider adding the async modifier to the function signature.
static void MapStopCameraPreview(CameraViewHandler handler, ICameraView view, object? arg3)
Description of Change
This PR refactors the following public methods in
CameraView
:CameraView.CaptureImage(CancellationToken)
,CameraView.StartCameraPreview(CancellationToken)
CameraView.StopCameraPreview()
Previously, these methods deferred to the Handler to invoke the underlying platform-specfic implemetnation via
Handler.Invoke()
. The problem with this approach is that thatHandler.Invoke
is notawait
'able, yet was invoking asynchronous methods usingasync void
.Using
async void
lead to issues where developers are unable to catch valid Exceptions thrown by the Operating System, as noted in #2520.With this PR, developers will be able to wrap these methods, like
await CaptureImage(token)
, in a try/catch block to gracefully handle any exception thrown.Linked Issues
PR Checklist
approved
(bug) orChampioned
(feature/proposal)main
at time of PRAdditional information
In #2520 we discussed promoting the underlying
CameraManager
class to bepublic
. However, after further investigation, because eachCameraView
requires a unique instance ofCameraManager
, it is best to keepCameraManager
internal.This PR also demotes
CameraView.OnMediaCaptured
andCameraView.OnMediaCapturedFailed
frompublic
tointernal
. These methods fireevent MediaCaptured
andevent MediaCaptureFailed
, respectively, and we should not allow library users to manually fire these events.