diff --git a/Sodes/SodesAudio/ByteRange.swift b/Sodes/SodesAudio/ByteRange.swift index 1667ed8..dddbc5d 100755 --- a/Sodes/SodesAudio/ByteRange.swift +++ b/Sodes/SodesAudio/ByteRange.swift @@ -113,7 +113,7 @@ func combine(_ ranges: [ByteRange]) -> [ByteRange] { combinedRanges.append(uncheckedRange) } else { for range in intersectingRanges { - if let index = combinedRanges.index(of: range) { + if let index = combinedRanges.firstIndex(of: range) { combinedRanges.remove(at: index) } } diff --git a/Sodes/SodesAudio/PlaybackController.swift b/Sodes/SodesAudio/PlaybackController.swift index a525d93..841da88 100755 --- a/Sodes/SodesAudio/PlaybackController.swift +++ b/Sodes/SodesAudio/PlaybackController.swift @@ -267,11 +267,7 @@ public class PlaybackController: NSObject { if #available(iOS 11.0, *) { do { - try audioSession.setCategory( - convertFromAVAudioSessionCategory(AVAudioSession.Category.playback), - mode: convertFromAVAudioSessionMode(AVAudioSession.Mode.default), - routeSharingPolicy: .longForm - ) + try audioSession.setCategory(.playback, mode: .default, policy: .longForm) try audioSession.setActive(true) } catch { @@ -279,8 +275,8 @@ public class PlaybackController: NSObject { } } else { // Fallback on earlier versions - _ = try! audioSession.setCategory(convertFromAVAudioSessionCategory(AVAudioSession.Category.playback)) - _ = try! audioSession.setMode(AVAudioSession.Mode.spokenAudio) + _ = try! audioSession.setCategory(.playback) + _ = try! audioSession.setMode(.spokenAudio) } resourceLoaderDelegate.delegate = self @@ -532,12 +528,12 @@ extension PlaybackController { fileprivate extension PlaybackController { - fileprivate func post(_ notification: PlaybackControllerNotification, userInfo: [String: Any]? = nil) { + func post(_ notification: PlaybackControllerNotification, userInfo: [String: Any]? = nil) { let note = Notification(name: notification.name, object: self, userInfo: userInfo) NotificationCenter.default.post(note) } - fileprivate func updateNowPlayingInfo() { + func updateNowPlayingInfo() { let info = currentSource?.nowPlayingInfo( image: currentArtwork, duration: duration, @@ -547,7 +543,7 @@ fileprivate extension PlaybackController { MPNowPlayingInfoCenter.default().nowPlayingInfo = info } - fileprivate func updateArtwork() { + func updateArtwork() { if let image = currentSource?.artworkImage { currentArtwork = image } @@ -558,7 +554,7 @@ fileprivate extension PlaybackController { } } - fileprivate func didSetPlayerItem(oldValue: AVPlayerItem?) { + func didSetPlayerItem(oldValue: AVPlayerItem?) { let keyPaths = ["status", "duration", "loadedTimeRanges"] oldValue?.remove(observer: self, for: keyPaths, context: &PlaybackControllerContext) currentPlayerItem?.add(observer: self, for: keyPaths, context: &PlaybackControllerContext) @@ -587,14 +583,13 @@ fileprivate extension PlaybackController { } } - fileprivate func handleAudioSessionInterruptionNotification(note: Notification) { + func handleAudioSessionInterruptionNotification(note: Notification) { SodesLog(note) guard let typeNumber = note.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber else {return} guard let type = AVAudioSession.InterruptionType(rawValue: typeNumber.uintValue) else {return} switch type { - case .began: if case .paused(let manually) = status, manually == true { break @@ -613,13 +608,15 @@ fileprivate extension PlaybackController { case .playing: if shouldResume { play() - } else { + } + else { pause(manually: false) } case .paused(let manually): if manually { // Do not resume! The user manually paused. - } else { + } + else { play() } case .preparing(_, let startTime): @@ -627,7 +624,8 @@ fileprivate extension PlaybackController { case .idle, .error(_), .buffering: break } - } else { + } + else { switch status { case .playing: play() @@ -641,10 +639,13 @@ fileprivate extension PlaybackController { break } } + + default: + fatalError("Missing \(type) implementation") } } - fileprivate func handleAudioSessionRouteChangeNotification(notification: Notification) { + func handleAudioSessionRouteChangeNotification(notification: Notification) { SodesLog(notification) var headphonesConnected = false @@ -678,7 +679,7 @@ fileprivate extension PlaybackController { SodesLog(previousRoute) } - fileprivate func playerDidChangeTimeControlStatus() { + func playerDidChangeTimeControlStatus() { if #available(iOS 10.0, *) { switch player.timeControlStatus { case .paused: @@ -697,6 +698,8 @@ fileprivate extension PlaybackController { case .paused, .playing, .buffering: status = .buffering } + default: + fatalError("Missing \(player.timeControlStatus) implementation") } } else { // Discussion: @@ -719,7 +722,7 @@ fileprivate extension PlaybackController { updateNowPlayingInfo() } - fileprivate func playerItemDidChangeStatus(_ item: AVPlayerItem) { + func playerItemDidChangeStatus(_ item: AVPlayerItem) { switch item.status { case .readyToPlay: if case .preparing(let shouldPlay, let startTime) = status { @@ -746,6 +749,8 @@ fileprivate extension PlaybackController { case .unknown: SodesLog("Item status unknown") status = .error(nil) + default: + fatalError("Missing \(item.status) implementation") } } diff --git a/Sodes/SodesAudioTests/ByteRangeSerializationTests.swift b/Sodes/SodesAudioTests/ByteRangeSerializationTests.swift index e2edbe5..7399e09 100755 --- a/Sodes/SodesAudioTests/ByteRangeSerializationTests.swift +++ b/Sodes/SodesAudioTests/ByteRangeSerializationTests.swift @@ -59,7 +59,7 @@ class ByteRangeSerializationTests: XCTestCase { XCTAssertEqual(contentLength, output!.1.contentLength) XCTAssertEqual(inputRanges, output!.0) XCTAssertEqual("e", output!.1.etag) - XCTAssertEqualWithAccuracy(inputDate.timeIntervalSince(output!.1.lastModified!), 0.0, accuracy: 1.0) + XCTAssertEqual(inputDate.timeIntervalSince(output!.1.lastModified!), 0.0, accuracy: 1.0) } func testItSavesByteRangesTwiceAndReadsThemBack() { @@ -79,14 +79,14 @@ class ByteRangeSerializationTests: XCTestCase { XCTAssertEqual(contentLength, output1!.1.contentLength) XCTAssertEqual(inputRanges1, output1!.0) XCTAssertEqual("e", output1!.1.etag) - XCTAssertEqualWithAccuracy(inputDate.timeIntervalSince(output1!.1.lastModified!), 0.0, accuracy: 1.0) + XCTAssertEqual(inputDate.timeIntervalSince(output1!.1.lastModified!), 0.0, accuracy: 1.0) XCTAssertTrue(FileManager.default.save(byteRanges: inputRanges2, cacheInfo: inputInfo, to: fileUrl)) let output2 = FileManager.default.readRanges(at: fileUrl) XCTAssertEqual(contentLength, output2!.1.contentLength) XCTAssertEqual(inputRanges2, output2!.0) XCTAssertEqual("e", output2!.1.etag) - XCTAssertEqualWithAccuracy(inputDate.timeIntervalSince(output2!.1.lastModified!), 0.0, accuracy: 1.0) + XCTAssertEqual(inputDate.timeIntervalSince(output2!.1.lastModified!), 0.0, accuracy: 1.0) } diff --git a/Sodes/SodesAudioTests/ByteRangeTests.swift b/Sodes/SodesAudioTests/ByteRangeTests.swift index a80c661..8931c2e 100755 --- a/Sodes/SodesAudioTests/ByteRangeTests.swift +++ b/Sodes/SodesAudioTests/ByteRangeTests.swift @@ -18,7 +18,7 @@ class ByteRangeTests: XCTestCase { XCTAssertEqual(range.lowerBound, 0) XCTAssertEqual(range.upperBound, 10) XCTAssertEqual(range.lastValidIndex, 9) - XCTAssertEqual(range.subdataRange, Range((0..<10))) + XCTAssertEqual(range.subdataRange, 0..<10) } // MARK: Leading diff --git a/Sodes/SodesAudioTests/DataTests.swift b/Sodes/SodesAudioTests/DataTests.swift index 5194e68..97b8528 100644 --- a/Sodes/SodesAudioTests/DataTests.swift +++ b/Sodes/SodesAudioTests/DataTests.swift @@ -12,28 +12,28 @@ import XCTest class DataTests: XCTestCase { func test_itReturnsSubdataForValidResponse_Variant1() { - let data = Data(bytes: [0,1,2,3,4,5,6,7,8,9]) + let data = Data([0,1,2,3,4,5,6,7,8,9]) let range: ByteRange = (0..<10) let subdata = data.byteRangeResponseSubdata(in: range) XCTAssertEqual(subdata, data) } func test_itReturnsSubdataForValidResponse_Variant2() { - let data = Data(bytes: [0,1,2,3,4,5,6,7,8,9]) + let data = Data([0,1,2,3,4,5,6,7,8,9]) let range: ByteRange = (100..<110) let subdata = data.byteRangeResponseSubdata(in: range) XCTAssertEqual(subdata, data) } func test_itReturnsNilForAnInvalidResponse_Variant1() { - let data = Data(bytes: [0,1,2,3,4,5,6,7,8,9]) + let data = Data([0,1,2,3,4,5,6,7,8,9]) let range: ByteRange = (0..<100) let subdata = data.byteRangeResponseSubdata(in: range) XCTAssertNil(subdata) } func test_itReturnsNilForAnInvalidResponse_Variant2() { - let data = Data(bytes: [0,1,2,3,4,5,6,7,8,9]) + let data = Data([0,1,2,3,4,5,6,7,8,9]) let range: ByteRange = (100..<200) let subdata = data.byteRangeResponseSubdata(in: range) XCTAssertNil(subdata) diff --git a/Sodes/SodesExample/EpisodeDuration.swift b/Sodes/SodesExample/EpisodeDuration.swift index 0d69c6d..9a636c1 100644 --- a/Sodes/SodesExample/EpisodeDuration.swift +++ b/Sodes/SodesExample/EpisodeDuration.swift @@ -60,9 +60,9 @@ public struct EpisodeDurationParsing { } public static func string(from duration: TimeInterval) -> String { - guard duration > 0 else {return "00:00"} - guard duration <= (23*3600 + 59*60 + 59) else {return "23:59:59"} + let limit: TimeInterval = 23*3600 + 59*60 + 59 + guard duration <= limit else {return "23:59:59"} let hours = floor(duration / 3600) let minutesAndSeconds = duration.truncatingRemainder(dividingBy: 3600) diff --git a/Sodes/SodesFoundation/FileManager.swift b/Sodes/SodesFoundation/FileManager.swift index b9eecfa..0b29d37 100644 --- a/Sodes/SodesFoundation/FileManager.swift +++ b/Sodes/SodesFoundation/FileManager.swift @@ -10,7 +10,7 @@ import Foundation public extension FileManager { - public func cachesDirectory() -> URL? { + func cachesDirectory() -> URL? { let directories = urls( for: .cachesDirectory, in: .userDomainMask @@ -18,7 +18,7 @@ public extension FileManager { return directories.first } - public func documentsDirectory() -> URL? { + func documentsDirectory() -> URL? { let directories = urls( for: .documentDirectory, in: .userDomainMask @@ -26,7 +26,7 @@ public extension FileManager { return directories.first } - public func createDirectoryAt(_ url: URL) -> Bool { + func createDirectoryAt(_ url: URL) -> Bool { do { try createDirectory( at: url, @@ -40,12 +40,12 @@ public extension FileManager { return true } - public func createSubdirectory(_ name: String, atUrl url: URL) -> Bool { + func createSubdirectory(_ name: String, atUrl url: URL) -> Bool { let subdirectoryUrl = url.appendingPathComponent(name, isDirectory: true) return self.createDirectoryAt(subdirectoryUrl) } - public func removeDirectory(_ directory: URL) -> Bool { + func removeDirectory(_ directory: URL) -> Bool { do { try removeItem(atPath: directory.absoluteString) } @@ -55,7 +55,7 @@ public extension FileManager { return true } - public func removeFile(at url: URL) -> Bool { + func removeFile(at url: URL) -> Bool { do { try removeItem(atPath: url.absoluteString) } diff --git a/Sodes/SodesFoundation/Throttle.swift b/Sodes/SodesFoundation/Throttle.swift index 6995236..f8752c4 100644 --- a/Sodes/SodesFoundation/Throttle.swift +++ b/Sodes/SodesFoundation/Throttle.swift @@ -31,7 +31,7 @@ public class Throttle { timer = { let timer = Timer( timeInterval: minimumInterval, - target: timerTarget, + target: timerTarget!, selector: #selector(TimerTarget.timerFired), userInfo: nil, repeats: true diff --git a/Sodes/SodesFoundationTests/DelegatedHTTPOperationTests.swift b/Sodes/SodesFoundationTests/DelegatedHTTPOperationTests.swift index 6f7a559..e417db9 100644 --- a/Sodes/SodesFoundationTests/DelegatedHTTPOperationTests.swift +++ b/Sodes/SodesFoundationTests/DelegatedHTTPOperationTests.swift @@ -36,7 +36,7 @@ class DelegatedHTTPOperationTests: XCTestCase { XCTAssertEqual(self.receivedBytes, received) break case .error(let r, let e): - SodesLog("response: \(r), error: \(e)") + SodesLog("response: \(String(describing: r)), error: \(String(describing: e))") XCTFail() break } @@ -57,7 +57,7 @@ extension DelegatedHTTPOperationTests: HTTPOperationDataDelegate { func delegatedHTTPOperation(_ operation: DelegatedHTTPOperation, didReceiveData data: Data) { XCTAssertEqual(OperationQueue.current, delegateQueue) - receivedBytes += data.count + receivedBytes = receivedBytes + Int64(data.count) } }