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 3b24b20 commit a1e80ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 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
18 changes: 11 additions & 7 deletions Sources/FeedbackSwift/FeedbackViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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 @@ -284,13 +286,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
5 changes: 4 additions & 1 deletion Sources/FeedbackSwift/FeedbackWireframe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import UIKit
import MobileCoreServices
import MessageUI
import UniformTypeIdentifiers

protocol FeedbackWireframeProtocol {
func showTopicsView(with service: FeedbackEditingServiceProtocol)
Expand Down Expand Up @@ -94,6 +95,7 @@ extension FeedbackWireframe: FeedbackWireframeProtocol {
}
)
}
#if os(iOS)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
alertController.addAction(
UIAlertAction(
Expand All @@ -108,6 +110,7 @@ extension FeedbackWireframe: FeedbackWireframeProtocol {
}
})
}
#endif

if let deleteAction {
alertController.addAction(
Expand Down Expand Up @@ -188,7 +191,7 @@ private extension FeedbackWireframe {
func showImagePicker(sourceType: UIImagePickerController.SourceType) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = sourceType
imagePicker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
imagePicker.mediaTypes = [UTType.image.identifier, UTType.movie.identifier]
imagePicker.allowsEditing = false
imagePicker.delegate = imagePickerDelegate
imagePicker.modalPresentationStyle = .formSheet
Expand Down
22 changes: 16 additions & 6 deletions Sources/FeedbackSwift/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func localized(_ key: String) -> String {
return key
}

func getMediaFromImagePickerInfo(_ info: [UIImagePickerController.InfoKey: Any]) -> Media? {
func getMediaFromImagePickerInfo(_ info: [UIImagePickerController.InfoKey: Any]) async -> Media? {
let imageType: String
let movieType: String
if #available(iOS 14.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *) {
Expand All @@ -43,19 +43,29 @@ func getMediaFromImagePickerInfo(_ info: [UIImagePickerController.InfoKey: Any])
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 a1e80ab

Please sign in to comment.