Skip to content

Commit

Permalink
Add 'End of chapter' option for sleep timer
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Dec 17, 2018
1 parent a8cfa4e commit f87e394
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion BookPlayer/Extensions/Notification+BookPlayer.swift
Expand Up @@ -14,7 +14,7 @@ extension Notification.Name {
public static let importOperation = Notification.Name("com.tortugapower.audiobookplayer.operation.new")
public static let requestReview = Notification.Name("com.tortugapower.audiobookplayer.requestreview")
public static let updatePercentage = Notification.Name("com.tortugapower.audiobookplayer.book.percentage")
public static let updateChapter = Notification.Name("com.tortugapower.audiobookplayer.book.chapter")
public static let chapterChange = Notification.Name("com.tortugapower.audiobookplayer.book.chapter")
public static let bookReady = Notification.Name("com.tortugapower.audiobookplayer.book.ready")
public static let bookPlayed = Notification.Name("com.tortugapower.audiobookplayer.book.play")
public static let bookPaused = Notification.Name("com.tortugapower.audiobookplayer.book.pause")
Expand Down
32 changes: 21 additions & 11 deletions BookPlayer/Models/Book+CoreDataClass.swift
Expand Up @@ -20,17 +20,7 @@ public class Book: LibraryItem {
return self.title + "." + self.ext
}

var currentChapter: Chapter? {
guard let chapters = self.chapters?.array as? [Chapter], !chapters.isEmpty else {
return nil
}

for chapter in chapters where chapter.start <= self.currentTime && chapter.end > self.currentTime {
return chapter
}

return nil
}
var currentChapter: Chapter?

var displayTitle: String {
return self.title
Expand Down Expand Up @@ -72,6 +62,8 @@ public class Book: LibraryItem {
self.addToChapters(chapter)
}
}

self.currentChapter = self.chapters?.array.first as? Chapter
}

convenience init(from bookUrl: FileItem, context: NSManagedObjectContext) {
Expand Down Expand Up @@ -113,6 +105,24 @@ public class Book: LibraryItem {
}
}

public override func awakeFromFetch() {
super.awakeFromFetch()

self.updateCurrentChapter()
}

func updateCurrentChapter() {
guard let chapters = self.chapters?.array as? [Chapter], !chapters.isEmpty else {
return
}

guard let currentChapter = (chapters.first { (chapter) -> Bool in
chapter.start <= self.currentTime && chapter.end > self.currentTime
}) else { return }

self.currentChapter = currentChapter
}

override func getBookToPlay() -> Book? {
return self
}
Expand Down
24 changes: 10 additions & 14 deletions BookPlayer/Player/PlayerManager.swift
Expand Up @@ -122,6 +122,12 @@ class PlayerManager: NSObject {
NotificationCenter.default.post(name: .bookEnd, object: nil)
}

if let currentChapter = book.currentChapter,
book.currentTime > currentChapter.end || book.currentTime < currentChapter.start {
book.updateCurrentChapter()
NotificationCenter.default.post(name: .chapterChange, object: nil, userInfo: nil)
}

let userInfo = [
"time": currentTime,
"fileURL": book.fileURL
Expand Down Expand Up @@ -159,13 +165,9 @@ class PlayerManager: NSObject {
}

set {
guard let player = self.audioPlayer else {
return
}
guard let player = self.audioPlayer else { return }

player.currentTime = newValue

self.currentBook?.currentTime = newValue
}
}

Expand Down Expand Up @@ -236,9 +238,7 @@ class PlayerManager: NSObject {
// MARK: - Seek Controls

func jumpTo(_ time: Double, fromEnd: Bool = false) {
guard let player = self.audioPlayer else {
return
}
guard let player = self.audioPlayer else { return }

player.currentTime = min(max(fromEnd ? player.duration - time : time, 0), player.duration)

Expand All @@ -250,9 +250,7 @@ class PlayerManager: NSObject {
}

func jumpBy(_ direction: Double) {
guard let player = self.audioPlayer else {
return
}
guard let player = self.audioPlayer else { return }

player.currentTime += direction

Expand Down Expand Up @@ -373,9 +371,7 @@ class PlayerManager: NSObject {

// Toggle play/pause of book
func playPause(autoplayed _: Bool = false) {
guard let audioplayer = self.audioPlayer else {
return
}
guard let audioplayer = self.audioPlayer else { return }

// Pause player if it's playing
if audioplayer.isPlaying {
Expand Down
19 changes: 16 additions & 3 deletions BookPlayer/Player/SleepTimer.swift
Expand Up @@ -61,6 +61,13 @@ final class SleepTimer {
}))
}

self.alert.addAction(UIAlertAction(title: "End of current chapter", style: .default) { _ in
self.cancel()
self.alert.message = "Sleeping when the chapter ends"
NotificationCenter.default.addObserver(self, selector: #selector(self.end), name: .chapterChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.end), name: .bookChange, object: nil)
})

self.alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
}

Expand All @@ -80,6 +87,8 @@ final class SleepTimer {
self.alert.message = defaultMessage

self.timer?.invalidate()
NotificationCenter.default.removeObserver(self, name: .bookEnd, object: nil)
NotificationCenter.default.removeObserver(self, name: .chapterChange, object: nil)
}

private func cancel() {
Expand All @@ -96,12 +105,16 @@ final class SleepTimer {
self.alert.message = "Sleeping in \(self.durationFormatter.string(from: self.timeLeft)!)"

if self.timeLeft <= 0 {
self.timer?.invalidate()

self.onEnd?(false)
self.end()
}
}

@objc private func end() {
self.reset()

self.onEnd?(false)
}

// MARK: Public methods

func actionSheet(onStart: @escaping SleepTimerStart, onProgress: @escaping SleepTimerProgress, onEnd: @escaping SleepTimerEnd) -> UIAlertController {
Expand Down

0 comments on commit f87e394

Please sign in to comment.