react-native-video/ios/Video/RCTVideoManager.swift
Nick Fujita 68b9db4d11
iOS Swift Conversion (#2527)
Converts iOS implementation from Objective-c to Swift

# During the conversion process some updates to the code structure were also made
- Modularize codebase from single file to smaller focused files
- Untangled large nested IF statements
- Added more null checks, since Swift is more strict with null pointers
- Added property to allow for decoding of local video sources with self contained key for offline playback
- Updates example apps to test react-native 0.63.4 and uses auto native dependency imports for android and ios
2022-05-19 22:29:25 +09:00

64 lines
2.4 KiB
Swift

import AVFoundation
import React
@objc(RCTVideoManager)
class RCTVideoManager: RCTViewManager {
override func view() -> UIView {
return RCTVideo(eventDispatcher: bridge.eventDispatcher())
}
func methodQueue() -> DispatchQueue {
return bridge.uiManager.methodQueue
}
@objc(save:reactTag:resolver:rejecter:)
func save(options: NSDictionary, reactTag: NSNumber, resolve: @escaping RCTPromiseResolveBlock,reject: @escaping RCTPromiseRejectBlock) -> Void {
bridge.uiManager.prependUIBlock({_ , viewRegistry in
let view = viewRegistry?[reactTag]
if !(view is RCTVideo) {
RCTLogError("Invalid view returned from registry, expecting RCTVideo, got: %@", String(describing: view))
} else if let view = view as? RCTVideo {
view.save(options: options, resolve: resolve, reject: reject)
}
})
}
@objc(setLicenseResult:reactTag:)
func setLicenseResult(license: NSString, reactTag: NSNumber) -> Void {
bridge.uiManager.prependUIBlock({_ , viewRegistry in
let view = viewRegistry?[reactTag]
if !(view is RCTVideo) {
RCTLogError("Invalid view returned from registry, expecting RCTVideo, got: %@", String(describing: view))
} else if let view = view as? RCTVideo {
view.setLicenseResult(license as String)
}
})
}
@objc(setLicenseResultError:reactTag:)
func setLicenseResultError(error: NSString, reactTag: NSNumber) -> Void {
bridge.uiManager.prependUIBlock({_ , viewRegistry in
let view = viewRegistry?[reactTag]
if !(view is RCTVideo) {
RCTLogError("Invalid view returned from registry, expecting RCTVideo, got: %@", String(describing: view))
} else if let view = view as? RCTVideo {
view.setLicenseResultError(error as String)
}
})
}
override func constantsToExport() -> [AnyHashable : Any]? {
return [
"ScaleNone": AVLayerVideoGravity.resizeAspect,
"ScaleToFill": AVLayerVideoGravity.resize,
"ScaleAspectFit": AVLayerVideoGravity.resizeAspect,
"ScaleAspectFill": AVLayerVideoGravity.resizeAspectFill
]
}
override class func requiresMainQueueSetup() -> Bool {
return true
}
}