Skip to content

Commit

Permalink
Support for visionOS platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Hengyu committed Dec 5, 2023
1 parent 07cb1e4 commit 6e6c0d7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "FeedbackSwift",
defaultLocalization: "en",
platforms: [.iOS(.v14), .macCatalyst(.v14)],
platforms: [.iOS(.v14), .macCatalyst(.v14), .visionOS(.v1)],
products: [
.library(name: "FeedbackSwift", targets: ["FeedbackSwift"])
],
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

## Requirements

- iOS 14.0+, macCatalyst 14.0+
- iOS 14.0+, macCatalyst 14.0+, visionOS 1.0+

## Installation

Expand Down
18 changes: 11 additions & 7 deletions Sources/FeedbackSwift/FeedbackViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ open class FeedbackViewController: UITableViewController {

tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 44.0
#if os(iOS)
tableView.keyboardDismissMode = .onDrag
#endif
tableView.cellLayoutMarginsFollowReadableWidth = true

cellFactories.forEach(tableView.register(with:))
Expand Down Expand Up @@ -278,13 +280,15 @@ extension FeedbackViewController: UIImagePickerControllerDelegate, UINavigationC
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
) {
switch getMediaFromImagePickerInfo(info) {
case let media?:
feedbackEditingService.update(attachmentMedia: media)
wireframe.dismiss(completion: .none)
case _:
wireframe.dismiss(completion: .none)
wireframe.showUnknownErrorAlert()
Task {
switch await getMediaFromImagePickerInfo(info) {
case let media?:
feedbackEditingService.update(attachmentMedia: media)
wireframe.dismiss(completion: .none)
case _:
wireframe.dismiss(completion: .none)
wireframe.showUnknownErrorAlert()
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/FeedbackSwift/FeedbackWireframe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ extension FeedbackWireframe: FeedbackWireframeProtocol {
}
)
}
#if os(iOS)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
alertController.addAction(
UIAlertAction(
Expand All @@ -109,6 +110,7 @@ extension FeedbackWireframe: FeedbackWireframeProtocol {
}
})
}
#endif

if let deleteAction {
alertController.addAction(
Expand Down Expand Up @@ -229,7 +231,7 @@ private extension FeedbackWireframe {
}

private func openSettings() {
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(visionOS)
guard let url = URL(string: UIApplication.openSettingsURLString) else { return }
UIApplication.shared.open(url)
#endif
Expand Down
33 changes: 18 additions & 15 deletions Sources/FeedbackSwift/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,39 @@ func localized(_ key: String) -> String {
return key
}

func getMediaFromImagePickerInfo(_ info: [UIImagePickerController.InfoKey: Any]) -> Media? {
let imageType: String
let movieType: String
if #available(iOS 14.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *) {
imageType = UTType.image.identifier
movieType = UTType.movie.identifier
} else {
imageType = kUTTypeImage as String
movieType = kUTTypeMovie as String
}
func getMediaFromImagePickerInfo(_ info: [UIImagePickerController.InfoKey: Any]) async -> Media? {
let imageType = UTType.image.identifier
let movieType = UTType.movie.identifier

switch info[.mediaType] as? String {
case imageType?:
guard let image = info[.originalImage] as? UIImage else { return nil }
return .image(image)
case movieType?:
guard let url = info[.mediaURL] as? URL else { return nil }
return getMediaFromURL(url)
let image = try? await getMediaFromURL(url)
return image
default: return nil
}
}

func getMediaFromURL(_ url: URL) -> Media? {
func getMediaFromURL(_ url: URL) async throws -> Media {
let asset = AVURLAsset(url: url)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 1, timescale: 1)
guard let cgImage = try? generator.copyCGImage(at: time, actualTime: nil)
else { return nil }
return .video(UIImage(cgImage: cgImage), url)
#if os(visionOS)
let image = try await generator.image(at: time)
return Media.video(.init(cgImage: image.image), url)
#else
if #available(iOS 16.0, macCatalyst 16.0, *) {
let image = try await generator.image(at: time)
return Media.video(.init(cgImage: image.image), url)
} else {
let image = try generator.copyCGImage(at: time, actualTime: nil)
return .video(UIImage(cgImage: image), url)
}
#endif
}

func push<Item>(_ item: Item?) -> (((Item) -> Void) -> Void)? {
Expand Down

0 comments on commit 6e6c0d7

Please sign in to comment.