Skip to content

Commit 98d4af8

Browse files
committed
升级到swift4.2
升级到swift4.2
1 parent 7b6b9f4 commit 98d4af8

29 files changed

+426
-351
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ android/.idea/
55
.DS_Store
66
.idea/
77
*.iml
8+
ios/RCTAuroraIMUI.xcodeproj/project.xcworkspace/xcuserdata/VisonMac.xcuserdatad/UserInterfaceState.xcuserstate
9+
ios/RCTAuroraIMUI.xcodeproj/xcuserdata/VisonMac.xcuserdatad/xcschemes/xcschememanagement.plist

ios/RCTAuroraIMUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@
881881
623EA9981EE55D0800EC43AE = {
882882
CreatedOnToolsVersion = 8.3.2;
883883
DevelopmentTeam = RL64Y54S9V;
884+
LastSwiftMigration = 1020;
884885
ProvisioningStyle = Automatic;
885886
};
886887
623EA9A01EE55D0800EC43AE = {
@@ -895,6 +896,7 @@
895896
developmentRegion = English;
896897
hasScannedForEncodings = 0;
897898
knownRegions = (
899+
English,
898900
en,
899901
);
900902
mainGroup = 626021ED1ED96BA20038AB20;
@@ -1113,7 +1115,7 @@
11131115
PRODUCT_BUNDLE_IDENTIFIER = HXHG.RCTAuroraIMUI;
11141116
PRODUCT_NAME = "$(TARGET_NAME)";
11151117
SKIP_INSTALL = YES;
1116-
SWIFT_VERSION = 3.0;
1118+
SWIFT_VERSION = 4.2;
11171119
};
11181120
name = Debug;
11191121
};
@@ -1139,7 +1141,7 @@
11391141
PRODUCT_BUNDLE_IDENTIFIER = HXHG.RCTAuroraIMUI;
11401142
PRODUCT_NAME = "$(TARGET_NAME)";
11411143
SKIP_INSTALL = YES;
1142-
SWIFT_VERSION = 3.0;
1144+
SWIFT_VERSION = 4.2;
11431145
};
11441146
name = Release;
11451147
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

ios/RCTAuroraIMUI/IMUICommon/Controllers/IMUIAudioPlayerHelper.swift

Lines changed: 122 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,114 +10,151 @@ import UIKit
1010
import AVFoundation
1111

1212
public protocol IMUIAudioPlayerDelegate:NSObjectProtocol {
13-
func didAudioPlayerBeginPlay(_ AudioPlayer:AVAudioPlayer)
14-
func didAudioPlayerStopPlay(_ AudioPlayer:AVAudioPlayer)
15-
func didAudioPlayerPausePlay(_ AudioPlayer:AVAudioPlayer)
13+
func didAudioPlayerBeginPlay(_ AudioPlayer:AVAudioPlayer)
14+
func didAudioPlayerStopPlay(_ AudioPlayer:AVAudioPlayer)
15+
func didAudioPlayerPausePlay(_ AudioPlayer:AVAudioPlayer)
1616
}
1717

1818
public class IMUIAudioPlayerHelper: NSObject {
19-
20-
static let sharedInstance = IMUIAudioPlayerHelper()
21-
22-
23-
var player:AVAudioPlayer!
24-
weak var delegate:IMUIAudioPlayerDelegate?
25-
26-
// play tick callback
27-
public typealias ProgressCallback = (_ currentTime: TimeInterval, _ duration: TimeInterval) -> ()
28-
public typealias FinishCallback = () -> ()
29-
30-
var playProgressCallback: ProgressCallback?
31-
var playFinishCallback: FinishCallback?
32-
var updater: CADisplayLink! = nil
33-
34-
override init() {
35-
super.init()
36-
NotificationCenter.default.addObserver(self, selector: #selector(sensorStateChange(notification:)), name: NSNotification.Name(rawValue: "UIDeviceProximityStateDidChangeNotification"), object: nil)
3719

38-
}
39-
40-
func sensorStateChange(notification:Notification) {
41-
if UIDevice.current.proximityState {
42-
do {
43-
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
20+
@objc public static let sharedInstance = IMUIAudioPlayerHelper()
21+
22+
23+
var player:AVAudioPlayer!
24+
weak var delegate:IMUIAudioPlayerDelegate?
25+
26+
// play tick callback
27+
public typealias ProgressCallback = (_ identify: String,
28+
_ averagePower: Float,
29+
_ currentTime: TimeInterval,
30+
_ duration: TimeInterval) -> ()
31+
public typealias FinishCallback = (String) -> ()
32+
public typealias StopCallback = (String) -> ()
33+
34+
var playProgressCallback: ProgressCallback?
35+
var playFinishCallback: FinishCallback?
36+
var playStopCallback: StopCallback?
37+
38+
var updater: CADisplayLink! = nil
39+
var identify = ""
40+
41+
override init() {
42+
super.init()
43+
NotificationCenter.default.addObserver(self, selector: #selector(sensorStateChange), name: UIDevice.proximityStateDidChangeNotification, object: nil)
44+
}
45+
46+
@objc func sensorStateChange() {
47+
do {
48+
if UIDevice.current.proximityState {
49+
if #available(iOS 10.0, *) {
50+
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
51+
} else {
52+
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playAndRecord)
53+
}
54+
} else {
4455

45-
}catch let error as NSError {
46-
print("set category fail \(error)")
56+
if #available(iOS 10.0, *) {
57+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
58+
} else {
59+
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
60+
}
4761
}
48-
}else{
49-
do {
50-
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
51-
52-
}catch let error as NSError {
53-
print("set category fail \(error)")
62+
} catch let error as NSError {
63+
print("set category fail \(error)")
64+
}
65+
66+
}
67+
68+
deinit {
69+
NotificationCenter.default.removeObserver(self)
70+
}
71+
72+
open func playAudioWithData(_ identify: String,
73+
_ voiceData: Data,
74+
_ progressCallback: @escaping ProgressCallback,
75+
_ finishCallBack: @escaping FinishCallback,
76+
_ stopCallBack: @escaping StopCallback) {
77+
self.stopAudio()
78+
do {
79+
self.identify = identify
80+
self.playProgressCallback = progressCallback
81+
self.playFinishCallback = finishCallBack
82+
self.playStopCallback = stopCallBack
83+
84+
if #available(iOS 10.0, *) {
85+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
86+
} else {
87+
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
5488
}
89+
90+
updater = CADisplayLink(target: self, selector: #selector(self.trackAudio))
91+
updater.add(to: RunLoop.current, forMode: .common)
92+
updater.frameInterval = 1
93+
} catch let error as NSError {
94+
print("set category fail \(error)")
95+
}
96+
97+
if self.player != nil {
98+
self.player.stop()
99+
self.player = nil
55100
}
101+
102+
do {
103+
let voicePlayer:AVAudioPlayer = try AVAudioPlayer(data: voiceData)
104+
voicePlayer.delegate = self
105+
voicePlayer.play()
106+
voicePlayer.isMeteringEnabled = true;
107+
self.player = voicePlayer
108+
} catch let error as NSError {
109+
print("alloc AVAudioPlayer with voice data fail with error \(error)")
110+
}
111+
112+
UIDevice.current.isProximityMonitoringEnabled = true
56113
}
57114

58-
open func playAudioWithData(_ voiceData:Data, progressCallback: @escaping ProgressCallback, finishCallBack: @escaping FinishCallback) {
59-
do {
60-
self.playProgressCallback = progressCallback
61-
self.playFinishCallback = finishCallBack
62-
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
63-
updater = CADisplayLink(target: self, selector: #selector(self.trackAudio))
64-
updater.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
65-
updater.frameInterval = 1
66-
} catch let error as NSError {
67-
print("set category fail \(error)")
115+
open func renewProgressCallback(_ identfy: String, progressCallback: @escaping ProgressCallback) {
116+
if identfy == self.identify {
117+
self.playProgressCallback = progressCallback
118+
}
68119
}
69120

70-
if self.player != nil {
71-
self.player.stop()
72-
self.player = nil
121+
@objc func trackAudio() {
122+
player.updateMeters()
123+
self.playProgressCallback?(self.identify, player.averagePower(forChannel: 0), player.currentTime, player.duration)
73124
}
74-
do {
75-
let voicePlayer:AVAudioPlayer = try AVAudioPlayer(data: voiceData)
76-
voicePlayer.delegate = self
77-
voicePlayer.play()
78-
self.player = voicePlayer
79-
} catch let error as NSError {
80-
print("alloc AVAudioPlayer with voice data fail with error \(error)")
125+
126+
func pausePlayingAudio() {
127+
self.player?.pause()
128+
updater.invalidate()
129+
81130
}
82131

83-
UIDevice.current.isProximityMonitoringEnabled = true
84-
}
85-
86-
func trackAudio() {
87-
self.playProgressCallback?(player.currentTime, player.duration)
88-
}
89-
90-
func pausePlayingAudio() {
91-
self.player?.pause()
92-
updater.invalidate()
132+
func resumePlayingAudio() {
133+
self.player?.play()
134+
updater.add(to: RunLoop.current, forMode: .common)
135+
}
93136

94-
}
95-
96-
func resumePlayingAudio() {
97-
self.player?.play()
98-
updater.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
99-
}
100-
101-
func stopAudio() {
102-
if self.player != nil{
103-
if self.player.isPlaying {
104-
self.player.stop()
137+
@objc open func stopAudio() {
138+
self.playProgressCallback = nil
139+
updater?.invalidate()
140+
if (self.player != nil) && self.player.isPlaying {
141+
self.player?.stop()
105142
}
106-
updater.invalidate()
143+
144+
// DispatchQueue.main.async {
145+
self.playStopCallback?(self.identify)
146+
// }
147+
107148
UIDevice.current.isProximityMonitoringEnabled = false
108149
self.delegate?.didAudioPlayerStopPlay(self.player)
109150
}
110-
}
111-
deinit {
112-
NotificationCenter.default.removeObserver(self)
113-
}
114151
}
115152

116153

117154
// MARK: - AVAudioPlayerDelegate
118155
extension IMUIAudioPlayerHelper: AVAudioPlayerDelegate {
119-
public func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
120-
self.stopAudio()
121-
self.playFinishCallback?()
122-
}
156+
public func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
157+
self.stopAudio()
158+
self.playFinishCallback?(self.identify)
159+
}
123160
}

ios/RCTAuroraIMUI/IMUICommon/Extension/StringExtension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public extension String {
1717

1818
func sizeWithConstrainedWidth(with width: CGFloat, font: UIFont) -> CGSize {
1919
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
20-
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
20+
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
2121
var tmpWidth = boundingBox.width
2222
if tmpWidth<30{
2323
tmpWidth = 30

ios/RCTAuroraIMUI/IMUICommon/Extension/UIColorExtension.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@ import Foundation
1010
import UIKit
1111

1212
public extension UIColor {
13-
convenience init(red: Int, green: Int, blue: Int) {
14-
assert(red >= 0 && red <= 255, "Invalid red component")
15-
assert(green >= 0 && green <= 255, "Invalid green component")
16-
assert(blue >= 0 && blue <= 255, "Invalid blue component")
17-
18-
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
19-
}
20-
21-
convenience init(netHex: Int) {
22-
self.init(red: (netHex >> 16) & 0xff, green: (netHex >> 8) & 0xff, blue: netHex & 0xff)
23-
24-
}
25-
26-
class func hexStringToUIColor (hex:String) -> UIColor {
27-
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
28-
29-
if (cString.hasPrefix("#")) {
30-
cString.remove(at: cString.startIndex)
13+
@objc convenience init(red: Int, green: Int, blue: Int) {
14+
assert(red >= 0 && red <= 255, "Invalid red component")
15+
assert(green >= 0 && green <= 255, "Invalid green component")
16+
assert(blue >= 0 && blue <= 255, "Invalid blue component")
17+
18+
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
3119
}
3220

33-
if ((cString.characters.count) != 6) {
34-
return UIColor.gray
21+
@objc convenience init(netHex: Int) {
22+
self.init(red: (netHex >> 16) & 0xff, green: (netHex >> 8) & 0xff, blue: netHex & 0xff)
23+
3524
}
3625

37-
var rgbValue:UInt32 = 0
38-
Scanner(string: cString).scanHexInt32(&rgbValue)
39-
40-
return UIColor(
41-
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
42-
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
43-
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
44-
alpha: CGFloat(1.0)
45-
)
46-
}
26+
@objc class func hexStringToUIColor (hex:String) -> UIColor {
27+
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
28+
29+
if (cString.hasPrefix("#")) {
30+
cString.remove(at: cString.startIndex)
31+
}
32+
33+
if ((cString.count) != 6) {
34+
return UIColor.gray
35+
}
36+
37+
var rgbValue:UInt32 = 0
38+
Scanner(string: cString).scanHexInt32(&rgbValue)
39+
40+
return UIColor(
41+
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
42+
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
43+
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
44+
alpha: CGFloat(1.0)
45+
)
46+
}
4747
}

ios/RCTAuroraIMUI/IMUICommon/Extension/UIImageExtension.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import Foundation
1010
import UIKit
1111

1212
public extension UIImage {
13-
14-
var jpegRepresentationData: Data! {
15-
return UIImageJPEGRepresentation(self, 1.0) // QUALITY min = 0 / max = 1
16-
}
17-
18-
var pngRepresentationData: Data! {
19-
return UIImagePNGRepresentation(self)
20-
}
13+
14+
var jpegRepresentationData: Data! {
15+
return self.jpegData(compressionQuality: 1.0) // QUALITY min = 0 / max = 1
16+
}
17+
18+
var pngRepresentationData: Data! {
19+
return self.pngData()
20+
}
2121
}

0 commit comments

Comments
 (0)