这是indexloc提供的服务,不要输入任何密码
Skip to content

[camera_avfoundation] Implementation swift migration - part 10 #9664

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RobertOdrowaz
Copy link
Contributor

Migrates camera implementation as part of flutter/flutter#119109

This PR migrates the 7th chunk of FLTCam class to Swift:

  • captureToFile
  • getTemporaryFilePath

We can also finally switch to Swift interface for dispatch queue specific because all usages are now in Swift.

Pre-Review Checklist

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

  1. 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

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 migrates captureToFile and getTemporaryFilePath to Swift and updates the dispatch queue handling to use modern Swift APIs. I've provided suggestions to improve the robustness of the new Swift code by avoiding potential runtime issues related to force-indexing and force-casting.

Comment on lines +386 to +387
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[
0]

Choose a reason for hiding this comment

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

medium

Force-indexing an array with [0] can lead to a runtime crash if the array is empty. While it's unlikely for FileManager.default.urls(for: .documentDirectory, ...) to return an empty array, adopting a safer pattern using .first and handling the optional result will make the code more robust and idiomatic.

  private func getTemporaryFilePath(withExtension ext: String, subfolder: String, prefix: String)
    throws
    -> String
  {
    guard
      let documentDirectory = FileManager.default.urls(
        for: .documentDirectory, in: .userDomainMask).first
    else {
      throw NSError(
        domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError,
        userInfo: [NSLocalizedDescriptionKey: "Documents directory not found."])
    }
    let fileDirectory = documentDirectory.appendingPathComponent("camera").appendingPathComponent(
      subfolder)
    let fileName = prefix + UUID().uuidString
    let file = fileDirectory.appendingPathComponent(fileName).appendingPathExtension(ext).path

    let fileManager = FileManager.default
    if !fileManager.fileExists(atPath: fileDirectory.path) {
      try fileManager.createDirectory(
        at: fileDirectory,
        withIntermediateDirectories: true,
        attributes: nil)
    }

    return file
  }

@RobertOdrowaz RobertOdrowaz force-pushed the feature/camera-implementation-swift-migration-part10 branch from e47c2b5 to f472203 Compare July 23, 2025 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants