fix(ios): Add safety checks and remove some of the ! in types declaration (#4182)

This commit is contained in:
Olivier Bouillet 2024-09-22 18:41:25 +02:00 committed by GitHub
parent 17dc2c064f
commit ae82c83eef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 27 deletions

View File

@ -81,22 +81,25 @@ enum RCTVideoUtils {
return 0 return 0
} }
static func urlFilePath(filepath: NSString!, searchPath: FileManager.SearchPathDirectory) -> NSURL! { static func urlFilePath(filepath: NSString?, searchPath: FileManager.SearchPathDirectory) -> NSURL! {
if filepath.contains("file://") { guard let _filepath = filepath else { return nil }
return NSURL(string: filepath as String)
if _filepath.contains("file://") {
return NSURL(string: _filepath as String)
} }
// if no file found, check if the file exists in the Document directory // if no file found, check if the file exists in the Document directory
let paths: [String]! = NSSearchPathForDirectoriesInDomains(searchPath, .userDomainMask, true) let paths: [String] = NSSearchPathForDirectoriesInDomains(searchPath, .userDomainMask, true)
var relativeFilePath: String! = filepath.lastPathComponent var relativeFilePath: String = _filepath.lastPathComponent
// the file may be multiple levels below the documents directory // the file may be multiple levels below the documents directory
let directoryString: String! = searchPath == .cachesDirectory ? "Library/Caches/" : "Documents" let directoryString: String = searchPath == .cachesDirectory ? "Library/Caches/" : "Documents"
let fileComponents: [String]! = filepath.components(separatedBy: directoryString) let fileComponents: [String] = _filepath.components(separatedBy: directoryString)
if fileComponents.count > 1 { if fileComponents.count > 1 {
relativeFilePath = fileComponents[1] relativeFilePath = fileComponents[1]
} }
let path: String! = (paths.first! as NSString).appendingPathComponent(relativeFilePath) guard let _pathFirst = paths.first else { return nil }
let path: String = (_pathFirst as NSString).appendingPathComponent(relativeFilePath)
if FileManager.default.fileExists(atPath: path) { if FileManager.default.fileExists(atPath: path) {
return NSURL.fileURL(withPath: path) as NSURL return NSURL.fileURL(withPath: path) as NSURL
} }
@ -135,7 +138,7 @@ enum RCTVideoUtils {
return [] return []
} }
let audioTracks: NSMutableArray! = NSMutableArray() let audioTracks = NSMutableArray()
let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: asset, for: .audible) let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: asset, for: .audible)
@ -146,14 +149,14 @@ enum RCTVideoUtils {
if (values?.count ?? 0) > 0, let value = values?[0] { if (values?.count ?? 0) > 0, let value = values?[0] {
title = value as! String title = value as! String
} }
let language: String! = currentOption?.extendedLanguageTag ?? "" let language: String = currentOption?.extendedLanguageTag ?? ""
let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!) let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!)
let audioTrack = [ let audioTrack = [
"index": NSNumber(value: i), "index": NSNumber(value: i),
"title": title, "title": title,
"language": language ?? "", "language": language,
"selected": currentOption?.displayName == selectedOption?.displayName, "selected": currentOption?.displayName == selectedOption?.displayName,
] as [String: Any] ] as [String: Any]
audioTracks.add(audioTrack) audioTracks.add(audioTrack)
@ -178,7 +181,7 @@ enum RCTVideoUtils {
if (values?.count ?? 0) > 0, let value = values?[0] { if (values?.count ?? 0) > 0, let value = values?[0] {
title = value as! String title = value as! String
} }
let language: String! = currentOption?.extendedLanguageTag ?? "" let language: String = currentOption?.extendedLanguageTag ?? ""
let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!) let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!)
let textTrack = TextTrack([ let textTrack = TextTrack([
"index": NSNumber(value: i), "index": NSNumber(value: i),
@ -363,10 +366,11 @@ enum RCTVideoUtils {
static func prepareAsset(source: VideoSource) -> (asset: AVURLAsset?, assetOptions: NSMutableDictionary?)? { static func prepareAsset(source: VideoSource) -> (asset: AVURLAsset?, assetOptions: NSMutableDictionary?)? {
guard let sourceUri = source.uri, sourceUri != "" else { return nil } guard let sourceUri = source.uri, sourceUri != "" else { return nil }
var asset: AVURLAsset! var asset: AVURLAsset!
let bundlePath = Bundle.main.path(forResource: source.uri, ofType: source.type) ?? "" let bundlePath = Bundle.main.path(forResource: sourceUri, ofType: source.type) ?? ""
let url = source.isNetwork || source.isAsset guard let url = source.isNetwork || source.isAsset
? URL(string: source.uri ?? "") ? URL(string: sourceUri)
: URL(fileURLWithPath: bundlePath) : URL(fileURLWithPath: bundlePath) else { return nil }
let assetOptions: NSMutableDictionary! = NSMutableDictionary() let assetOptions: NSMutableDictionary! = NSMutableDictionary()
if source.isNetwork { if source.isNetwork {
@ -375,9 +379,9 @@ enum RCTVideoUtils {
} }
let cookies: [AnyObject]! = HTTPCookieStorage.shared.cookies let cookies: [AnyObject]! = HTTPCookieStorage.shared.cookies
assetOptions.setObject(cookies as Any, forKey: AVURLAssetHTTPCookiesKey as NSCopying) assetOptions.setObject(cookies as Any, forKey: AVURLAssetHTTPCookiesKey as NSCopying)
asset = AVURLAsset(url: url!, options: assetOptions as? [String: Any]) asset = AVURLAsset(url: url, options: assetOptions as? [String: Any])
} else { } else {
asset = AVURLAsset(url: url!) asset = AVURLAsset(url: url)
} }
return (asset, assetOptions) return (asset, assetOptions)
} }

View File

@ -48,12 +48,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
private var _preventsDisplaySleepDuringVideoPlayback = true private var _preventsDisplaySleepDuringVideoPlayback = true
private var _preferredForwardBufferDuration: Float = 0.0 private var _preferredForwardBufferDuration: Float = 0.0
private var _playWhenInactive = false private var _playWhenInactive = false
private var _ignoreSilentSwitch: String! = "inherit" // inherit, ignore, obey private var _ignoreSilentSwitch: String = "inherit" // inherit, ignore, obey
private var _mixWithOthers: String! = "inherit" // inherit, mix, duck private var _mixWithOthers: String = "inherit" // inherit, mix, duck
private var _resizeMode: String! = "cover" private var _resizeMode: String = "cover"
private var _fullscreen = false private var _fullscreen = false
private var _fullscreenAutorotate = true private var _fullscreenAutorotate = true
private var _fullscreenOrientation: String! = "all" private var _fullscreenOrientation: String = "all"
private var _fullscreenPlayerPresented = false private var _fullscreenPlayerPresented = false
private var _fullscreenUncontrolPlayerPresented = false // to call events switching full screen mode from player controls private var _fullscreenUncontrolPlayerPresented = false // to call events switching full screen mode from player controls
private var _filterName: String! private var _filterName: String!
@ -741,14 +741,14 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc @objc
func setIgnoreSilentSwitch(_ ignoreSilentSwitch: String?) { func setIgnoreSilentSwitch(_ ignoreSilentSwitch: String?) {
_ignoreSilentSwitch = ignoreSilentSwitch _ignoreSilentSwitch = ignoreSilentSwitch ?? "inherit"
RCTPlayerOperations.configureAudio(ignoreSilentSwitch: _ignoreSilentSwitch, mixWithOthers: _mixWithOthers, audioOutput: _audioOutput) RCTPlayerOperations.configureAudio(ignoreSilentSwitch: _ignoreSilentSwitch, mixWithOthers: _mixWithOthers, audioOutput: _audioOutput)
applyModifiers() applyModifiers()
} }
@objc @objc
func setMixWithOthers(_ mixWithOthers: String?) { func setMixWithOthers(_ mixWithOthers: String?) {
_mixWithOthers = mixWithOthers _mixWithOthers = mixWithOthers ?? "inherit"
applyModifiers() applyModifiers()
} }
@ -1050,9 +1050,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc @objc
func setFullscreenOrientation(_ orientation: String?) { func setFullscreenOrientation(_ orientation: String?) {
_fullscreenOrientation = orientation _fullscreenOrientation = orientation ?? "all"
if _fullscreenPlayerPresented { if _fullscreenPlayerPresented {
_playerViewController?.preferredOrientation = orientation _playerViewController?.preferredOrientation = _fullscreenOrientation
} }
} }
@ -1224,7 +1224,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
} }
@objc @objc
func setAdTagUrl(_ adTagUrl: String!) { func setAdTagUrl(_ adTagUrl: String?) {
_adTagUrl = adTagUrl _adTagUrl = adTagUrl
} }