Skip to content

Commit

Permalink
Swift 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardopereira committed May 7, 2019
1 parent 2b4d655 commit 312cd7f
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Sodes/SodesAudio/ByteRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
43 changes: 24 additions & 19 deletions Sodes/SodesAudio/PlaybackController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,16 @@ 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 {
print(error)
}
} 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
Expand Down Expand Up @@ -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,
Expand All @@ -547,7 +543,7 @@ fileprivate extension PlaybackController {
MPNowPlayingInfoCenter.default().nowPlayingInfo = info
}

fileprivate func updateArtwork() {
func updateArtwork() {
if let image = currentSource?.artworkImage {
currentArtwork = image
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -613,21 +608,24 @@ 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):
status = .preparing(playWhenReady:shouldResume, startTime: startTime)
case .idle, .error(_), .buffering:
break
}
} else {
}
else {
switch status {
case .playing:
play()
Expand All @@ -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
Expand Down Expand Up @@ -678,7 +679,7 @@ fileprivate extension PlaybackController {
SodesLog(previousRoute)
}

fileprivate func playerDidChangeTimeControlStatus() {
func playerDidChangeTimeControlStatus() {
if #available(iOS 10.0, *) {
switch player.timeControlStatus {
case .paused:
Expand All @@ -697,6 +698,8 @@ fileprivate extension PlaybackController {
case .paused, .playing, .buffering:
status = .buffering
}
default:
fatalError("Missing \(player.timeControlStatus) implementation")
}
} else {
// Discussion:
Expand All @@ -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 {
Expand All @@ -746,6 +749,8 @@ fileprivate extension PlaybackController {
case .unknown:
SodesLog("Item status unknown")
status = .error(nil)
default:
fatalError("Missing \(item.status) implementation")
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sodes/SodesAudioTests/ByteRangeSerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)

}

Expand Down
2 changes: 1 addition & 1 deletion Sodes/SodesAudioTests/ByteRangeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ByteRangeTests: XCTestCase {
XCTAssertEqual(range.lowerBound, 0)
XCTAssertEqual(range.upperBound, 10)
XCTAssertEqual(range.lastValidIndex, 9)
XCTAssertEqual(range.subdataRange, Range<Int>((0..<10)))
XCTAssertEqual(range.subdataRange, 0..<10)
}

// MARK: Leading
Expand Down
8 changes: 4 additions & 4 deletions Sodes/SodesAudioTests/DataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sodes/SodesExample/EpisodeDuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions Sodes/SodesFoundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import Foundation

public extension FileManager {

public func cachesDirectory() -> URL? {
func cachesDirectory() -> URL? {
let directories = urls(
for: .cachesDirectory,
in: .userDomainMask
)
return directories.first
}

public func documentsDirectory() -> URL? {
func documentsDirectory() -> URL? {
let directories = urls(
for: .documentDirectory,
in: .userDomainMask
)
return directories.first
}

public func createDirectoryAt(_ url: URL) -> Bool {
func createDirectoryAt(_ url: URL) -> Bool {
do {
try createDirectory(
at: url,
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion Sodes/SodesFoundation/Throttle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sodes/SodesFoundationTests/DelegatedHTTPOperationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

}

0 comments on commit 312cd7f

Please sign in to comment.