Permalink
Browse files

Refactor: Replace book arrays by a more context aware model

Re #265
  • Loading branch information...
pichfl committed Oct 27, 2018
1 parent e237080 commit 6219f74500d8f8528d9aacc86239568186f06f62
@@ -7,3 +7,4 @@ disabled_rules:
- type_name
- switch_case_alignment
- identifier_name
- trailing_comma
@@ -6,9 +6,9 @@
// Copyright © 2018 Tortuga Power. All rights reserved.
//
import UIKit
import MediaPlayer
import SwiftReorder
import UIKit

// swiftlint:disable file_length
@@ -123,73 +123,35 @@ class BaseListViewController: UIViewController {
}
}

func queueBooksForPlayback(_ startItem: LibraryItem, forceAutoplay: Bool = false) -> [Book] {
var books = [Book]()
let shouldAutoplayLibrary = UserDefaults.standard.bool(forKey: Constants.UserDefaults.autoplayEnabled.rawValue)
let shouldAutoplay = shouldAutoplayLibrary || forceAutoplay

if let book = startItem as? Book {
books.append(book)
}

if let playlist = startItem as? Playlist {
books.append(contentsOf: playlist.getRemainingBooks())
}

guard
shouldAutoplay,
let remainingItems = self.items.split(whereSeparator: { $0 == startItem }).last
else {
return books
}

for item in remainingItems {
if let playlist = item as? Playlist {
books.append(contentsOf: playlist.getRemainingBooks())
} else if let book = item as? Book, !book.isCompleted {
books.append(book)
}
}

return books
}

func setupPlayer(books: [Book] = []) {
// Stop setup if no books were found
if books.isEmpty {
return
}

func setupPlayer(book: Book) {
// Make sure player is for a different book
guard
let firstBook = books.first,
let currentBook = PlayerManager.shared.currentBook,
currentBook == firstBook
currentBook == book
else {
// Handle loading new player
self.loadPlayer(books: books)
self.loadPlayer(book: book)

return
}

self.showPlayerView(book: currentBook)
}

func loadPlayer(books: [Book]) {
guard let book = books.first else { return }

func loadPlayer(book: Book) {
guard DataManager.exists(book) else {
self.showAlert("File missing!", message: "This book’s file was removed from your device. Import the file again to play the book")

return
}

// Replace player with new one
PlayerManager.shared.load(books) { (loaded) in
PlayerManager.shared.load(book) { loaded in
guard loaded else {
self.showAlert("File error!", message: "This book's file couldn't be loaded. Make sure you're not using files with DRM protection (like .aax files)")
return
}

self.showPlayerView(book: book)

PlayerManager.shared.playPause()
@@ -208,6 +170,7 @@ class BaseListViewController: UIViewController {
}

// MARK: - Callback events
@objc func reloadData() {
self.tableView.beginUpdates()
self.tableView.reloadSections(IndexSet(integer: Section.library.rawValue), with: .none)
@@ -228,7 +191,7 @@ class BaseListViewController: UIViewController {
guard
let userInfo = notification.userInfo,
let filename = userInfo["filename"] as? String else {
return
return
}

DispatchQueue.main.async {
@@ -241,8 +204,8 @@ class BaseListViewController: UIViewController {
guard
let userInfo = notification.userInfo,
let operation = userInfo["operation"] as? ImportOperation
else {
return
else {
return
}

self.loadingTitleLabel.text = "Processing \(operation.files.count) file(s)"
@@ -265,8 +228,8 @@ class BaseListViewController: UIViewController {
let currentBook = PlayerManager.shared.currentBook,
let index = self.library.itemIndex(with: currentBook.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: .library)) as? BookCellView
else {
return
else {
return
}

bookCell.playbackState = .playing
@@ -277,8 +240,8 @@ class BaseListViewController: UIViewController {
let book = PlayerManager.shared.currentBook,
let index = self.library.itemIndex(with: book.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: .library)) as? BookCellView
else {
return
else {
return
}

bookCell.playbackState = .paused
@@ -290,8 +253,8 @@ class BaseListViewController: UIViewController {
let book = userInfo["book"] as? Book,
let index = self.library.itemIndex(with: book.fileURL),
let bookCell = self.tableView.cellForRow(at: IndexPath(row: index, section: .library)) as? BookCellView
else {
return
else {
return
}

bookCell.playbackState = .stopped
@@ -301,7 +264,7 @@ class BaseListViewController: UIViewController {
guard let userInfo = notification.userInfo,
let fileURL = userInfo["fileURL"] as? URL,
let progress = userInfo["progress"] as? Double else {
return
return
}

guard let index = (self.items.index { (item) -> Bool in
@@ -362,6 +325,7 @@ class BaseListViewController: UIViewController {
}

// MARK: - TableView DataSource
extension BaseListViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == Section.library.rawValue
@@ -387,11 +351,9 @@ extension BaseListViewController: UITableViewDataSource {
cell.type = item is Playlist ? .playlist : .book

cell.onArtworkTap = { [weak self] in
guard let books = self?.queueBooksForPlayback(item) else {
return
}
guard let book = item.getBookToPlay() else { return }

self?.setupPlayer(books: books)
self?.setupPlayer(book: book)
}

if let book = item as? Book {
@@ -411,6 +373,7 @@ extension BaseListViewController: UITableViewDataSource {
}

// MARK: - TableView Delegate
extension BaseListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
return true
@@ -442,6 +405,7 @@ extension BaseListViewController: UITableViewDelegate {
}

// MARK: - Reorder Delegate
extension BaseListViewController: TableViewReorderDelegate {
@objc func tableView(_ tableView: UITableView, reorderRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}

@@ -475,10 +439,13 @@ extension BaseListViewController: TableViewReorderDelegate {
})
}

@objc func tableViewDidFinishReordering(_ tableView: UITableView, from initialSourceIndexPath: IndexPath, to finalDestinationIndexPath: IndexPath, dropped overIndexPath: IndexPath?) {}
@objc func tableViewDidFinishReordering(_ tableView: UITableView, from initialSourceIndexPath: IndexPath, to finalDestinationIndexPath: IndexPath, dropped overIndexPath: IndexPath?) {
//
}
}

// MARK: DocumentPicker Delegate
extension BaseListViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
for url in urls {
Oops, something went wrong.

0 comments on commit 6219f74

Please sign in to comment.