Skip to content

Commit

Permalink
Merge pull request #148 from GianniCarlo/fix/display-library
Browse files Browse the repository at this point in the history
Multiple Library fixes
  • Loading branch information
GianniCarlo authored Jun 11, 2018
2 parents 6153545 + a8d89b5 commit e127a6a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
34 changes: 24 additions & 10 deletions BookPlayer/Library/BaseListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,26 @@ class BaseListViewController: UIViewController {

NotificationCenter.default.addObserver(self, selector: #selector(self.dismissMiniPlayer), name: Notification.Name.AudiobookPlayer.playerPresented, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.presentMiniPlayer), name: Notification.Name.AudiobookPlayer.playerDismissed, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.onBookPlay), name: Notification.Name.AudiobookPlayer.bookPlayed, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.onBookPause), name: Notification.Name.AudiobookPlayer.bookPaused, object: nil)
}

guard let identifier = UserDefaults.standard.string(forKey: UserDefaultsConstants.lastPlayedBook),
let lastPlayedBook = DataManager.getBook(from: identifier) else {
return
@objc func onBookPlay() {
guard let index = self.library.itemIndex(with: PlayerManager.shared.currentBook.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? BookCellView else {
return
}

// Preload player
PlayerManager.shared.load([lastPlayedBook]) { (loaded) in
guard loaded else {
return
}
bookCell.artworkButton.setImage(#imageLiteral(resourceName: "playerIconPlay"), for: .normal)
}

self.showPlayerView(book: lastPlayedBook)
@objc func onBookPause() {
guard let index = self.library.itemIndex(with: PlayerManager.shared.currentBook.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? BookCellView else {
return
}

bookCell.artworkButton.setImage(#imageLiteral(resourceName: "playerIconPause"), for: .normal)
}

@objc func presentMiniPlayer() {
Expand Down Expand Up @@ -181,7 +187,6 @@ extension BaseListViewController: UITableViewDataSource {

let item = self.items[indexPath.row]

cell.progress = item.percentCompleted / 100.0
cell.artwork = item.artwork
cell.title = item.title
cell.isPlaylist = item is Playlist
Expand All @@ -190,11 +195,20 @@ extension BaseListViewController: UITableViewDataSource {

if let book = item as? Book {
cell.subtitle = book.author

cell.progressView.isHidden = book.currentTime == 0
cell.progress = item.percentCompleted / 100.0

cell.onArtworkTap = { [weak self] in
self?.setupPlayer(books: [book])
}
} else if let playlist = item as? Playlist {
cell.subtitle = playlist.info()

let totalPercentage = playlist.totalPercentage()
cell.progressView.isHidden = totalPercentage == 0
cell.progress = totalPercentage

cell.onArtworkTap = { [weak self] in
self?.setupPlayer(books: playlist.getRemainingBooks())
}
Expand Down
16 changes: 15 additions & 1 deletion BookPlayer/Library/LibraryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ class LibraryViewController: BaseListViewController, UIGestureRecognizerDelegate
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData), name: Notification.Name.AudiobookPlayer.bookDeleted, object: nil)

self.loadLibrary()

guard let identifier = UserDefaults.standard.string(forKey: UserDefaultsConstants.lastPlayedBook),
let lastPlayedBook = DataManager.getBook(from: identifier) else {
return
}

// Preload player
PlayerManager.shared.load([lastPlayedBook]) { (loaded) in
guard loaded else {
return
}

self.showPlayerView(book: lastPlayedBook)
}
}

// No longer need to deregister observers for iOS 9+!
Expand Down Expand Up @@ -316,7 +330,7 @@ extension LibraryViewController {
return cell
}

bookCell.titleColor = UIColor(red:0.37, green:0.64, blue:0.85, alpha:1.0)
bookCell.titleColor = UIColor(red: 0.37, green: 0.64, blue: 0.85, alpha: 1.0)
bookCell.artworkButton.setImage(#imageLiteral(resourceName: "playerIconPlay"), for: .normal)

return bookCell
Expand Down
20 changes: 19 additions & 1 deletion BookPlayer/Library/PlaylistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ class PlaylistViewController: BaseListViewController {
@IBAction func addAction() {
self.presentImportFilesAlert()
}

@objc override func onBookPlay() {
guard let index = self.playlist.itemIndex(with: PlayerManager.shared.currentBook.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? BookCellView else {
return
}

bookCell.artworkButton.setImage(#imageLiteral(resourceName: "playerIconPlay"), for: .normal)
}

@objc override func onBookPause() {
guard let index = self.playlist.itemIndex(with: PlayerManager.shared.currentBook.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? BookCellView else {
return
}

bookCell.artworkButton.setImage(#imageLiteral(resourceName: "playerIconPause"), for: .normal)
}
}

extension PlaylistViewController {
Expand All @@ -52,7 +70,7 @@ extension PlaylistViewController {
return cell
}

bookCell.titleColor = UIColor(red:0.37, green:0.64, blue:0.85, alpha:1.0)
bookCell.titleColor = UIColor(red: 0.37, green: 0.64, blue: 0.85, alpha: 1.0)
bookCell.artworkButton.setImage(#imageLiteral(resourceName: "playerIconPlay"), for: .normal)

return bookCell
Expand Down
8 changes: 1 addition & 7 deletions BookPlayer/Library/Views/BookCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BookCellView: UITableViewCell {
self.titleLabel.text = newValue
}
}

var titleColor: UIColor! {
get {
return self.titleLabel.textColor
Expand Down Expand Up @@ -72,16 +72,10 @@ class BookCellView: UITableViewCell {
self.accessoryType = .disclosureIndicator

self.progressTrailing.constant = 0

// @TODO: Remove and calculate accumulated playlist progress
self.progressView.isHidden = true
} else {
self.accessoryType = .none

self.progressTrailing.constant = 16.0

// @TODO: Remove and calculate accumulated playlist progress
self.progressView.isHidden = false
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions BookPlayer/Models/Playlist+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public class Playlist: LibraryItem {
return book.artwork
}

func totalPercentage() -> Double {
guard let books = self.books?.array as? [Book] else {
return 0.0
}

var totalDuration = 0.0
var totalProgress = 0.0

for book in books {
totalDuration += book.duration
totalProgress += book.currentTime
}

return totalProgress / totalDuration
}

func getRemainingBooks() -> [Book] {
guard
let books = self.books?.array as? [Book], let firstUnfinishedBook = books.first(where: { (book) -> Bool in
Expand Down

0 comments on commit e127a6a

Please sign in to comment.