-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[camera_avfoundation] Implementation swift migration - part 13 #9930
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?
[camera_avfoundation] Implementation swift migration - part 13 #9930
Conversation
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
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.
Code Review
This pull request successfully migrates the updateOrientation
and setCaptureSessionPreset
methods from Objective-C to Swift, and subsequently removes the now-obsolete FLTCam
class. This is a great step forward in the Swift migration. The implementation looks solid, and I have a couple of minor suggestions to enhance code safety and adopt more idiomatic Swift patterns.
if (try? captureDevice.lockForConfiguration()) != nil { | ||
// Set the best device format found and finish the device configuration. | ||
captureDevice.activeFormat = bestFormat | ||
captureDevice.unlockForConfiguration() | ||
break | ||
} |
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.
While (try? ... ) != nil
works for checking if a throwing function succeeded, using a do-try-catch
block is more idiomatic in Swift. It makes the intent of handling a potential error clearer. If the lock fails, the catch
block can be left empty to preserve the current fallthrough logic.
do {
try captureDevice.lockForConfiguration()
// Set the best device format found and finish the device configuration.
captureDevice.activeFormat = bestFormat
captureDevice.unlockForConfiguration()
break
} catch {
// Fallthrough if lock fails.
}
withWidth: Double(previewSize!.width), | ||
height: Double(previewSize!.height) |
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.
Using force unwrapping with !
can lead to a crash if previewSize
is nil
. Although it's likely initialized by this point, it's safer to use optional chaining and the nil-coalescing operator (??
) to provide a default value. This improves the robustness of the code by preventing a potential crash.
withWidth: Double(previewSize?.width ?? 0),
height: Double(previewSize?.height ?? 0)
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.
why was it non-optional previously?
We typically try to avoid !
, unless we are sure it won't be nil
, in which case you should add a comment explaining why.
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.
This is exciting!
## 0.9.21+3 | ||
|
||
* Migrates `updateOrientation` and `setCaptureSessionPreset` methods to Swift. | ||
* Removes `FLTCam` class. |
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.
Yay!
@@ -126,7 +143,7 @@ final class DefaultCamera: FLTCam, Camera { | |||
let captureVideoOutput = FLTDefaultCaptureVideoDataOutput( | |||
captureVideoOutput: AVCaptureVideoDataOutput()) | |||
captureVideoOutput.videoSettings = [ | |||
kCVPixelBufferPixelFormatTypeKey as String: videoFormat | |||
kCVPixelBufferPixelFormatTypeKey as String: videoFormat as Any |
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.
why is as Any needed? (videoSettings's type is already set)
case .max: | ||
if let bestFormat = highestResolutionFormat(forCaptureDevice: captureDevice) { | ||
videoCaptureSession.sessionPreset = .inputPriority | ||
if (try? captureDevice.lockForConfiguration()) != nil { |
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.
it's unclear to me what lockForConfiguration
returns. Can you break into 2 statements?
break | ||
} | ||
} | ||
fallthrough |
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.
why fallthrough?
withWidth: Double(previewSize!.width), | ||
height: Double(previewSize!.height) |
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.
why was it non-optional previously?
We typically try to avoid !
, unless we are sure it won't be nil
, in which case you should add a comment explaining why.
private func updateOrientation() { | ||
guard !isRecording else { return } | ||
|
||
let orientation: UIDeviceOrientation = |
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.
can be infered?
} | ||
} | ||
|
||
private func getVideoOrientation(forDeviceOrientation deviceOrientation: UIDeviceOrientation) |
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.
nit: get
Migrates camera implementation as part of flutter/flutter#119109
This PR migrates the last chunk of
FLTCam
class to Swift:updateOrientation
setCaptureSessionPreset
FLTCam
classPre-Review Checklist
[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assist
bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3