diff --git a/Source/EPUBCore/FRBook.swift b/Source/EPUBCore/FRBook.swift index c45aad9d7..14da88004 100755 --- a/Source/EPUBCore/FRBook.swift +++ b/Source/EPUBCore/FRBook.swift @@ -23,70 +23,65 @@ open class FRBook: NSObject { public var resources = FRResources() public var tableOfContents: [FRTocReference]! public var flatTableOfContents: [FRTocReference]! - - public func hasAudio() -> Bool { - return smils.smils.count > 0 ? true : false + + var hasAudio: Bool { + return smils.smils.count > 0 } - public func title() -> String? { + var title: String? { return metadata.titles.first } - public func authorName() -> String? { + var authorName: String? { return metadata.creators.first?.name } // MARK: - Media Overlay Metadata // http://www.idpf.org/epub/301/spec/epub-mediaoverlays.html#sec-package-metadata - func duration() -> String? { - return metadata.findMetaByProperty("media:duration"); - } - - // @NOTE: should "#" be automatically prefixed with the ID? - func durationFor(_ ID: String) -> String? { - return metadata.findMetaByProperty("media:duration", refinedBy: ID) + var duration: String? { + return metadata.findMetaByProperty("media:duration") } - - func activeClass() -> String { + var activeClass: String { guard let className = metadata.findMetaByProperty("media:active-class") else { return "epub-media-overlay-active" } return className } - func playbackActiveClass() -> String { + var playbackActiveClass: String { guard let className = metadata.findMetaByProperty("media:playback-active-class") else { return "epub-media-overlay-playing" } return className } - // MARK: - Media Overlay (SMIL) retrieval /** Get Smil File from a resource (if it has a media-overlay) */ - func smilFileForResource(_ resource: FRResource!) -> FRSmilFile! { - if( resource == nil || resource.mediaOverlay == nil ){ - return nil - } + func smilFileForResource(_ resource: FRResource?) -> FRSmilFile? { + guard let resource = resource, let mediaOverlay = resource.mediaOverlay else { return nil } // lookup the smile resource to get info about the file - let smilResource = resources.findById(resource.mediaOverlay) + guard let smilResource = resources.findById(mediaOverlay) else { return nil } // use the resource to get the file - return smils.findByHref( smilResource!.href ) + return smils.findByHref(smilResource.href) } - func smilFileForHref(_ href: String) -> FRSmilFile! { + func smilFile(forHref href: String) -> FRSmilFile? { return smilFileForResource(resources.findByHref(href)) } - func smilFileForId(_ ID: String) -> FRSmilFile! { + func smilFile(forId ID: String) -> FRSmilFile? { return smilFileForResource(resources.findById(ID)) } + // @NOTE: should "#" be automatically prefixed with the ID? + func duration(for ID: String) -> String? { + return metadata.findMetaByProperty("media:duration", refinedBy: ID) + } } diff --git a/Source/EPUBCore/FREpubParser.swift b/Source/EPUBCore/FREpubParser.swift index 440685416..429817095 100755 --- a/Source/EPUBCore/FREpubParser.swift +++ b/Source/EPUBCore/FREpubParser.swift @@ -39,14 +39,14 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { } func parseTitle(_ epubPath: String) throws -> String? { - guard let book = try readEpub(epubPath: epubPath, removeEpub: false), let title = book.title() else { + guard let book = try readEpub(epubPath: epubPath, removeEpub: false), let title = book.title else { throw FolioReaderError(kind: .TitleNotAvailable) } return title } func parseAuthorName(_ epubPath: String) throws -> String? { - guard let book = try readEpub(epubPath: epubPath, removeEpub: false), let authorName = book.authorName() else { + guard let book = try readEpub(epubPath: epubPath, removeEpub: false), let authorName = book.authorName else { throw FolioReaderError(kind: .AuthorNameNotAvailable) } return authorName diff --git a/Source/FolioReaderAudioPlayer.swift b/Source/FolioReaderAudioPlayer.swift index c1b510840..7429dfd51 100644 --- a/Source/FolioReaderAudioPlayer.swift +++ b/Source/FolioReaderAudioPlayer.swift @@ -155,7 +155,7 @@ open class FolioReaderAudioPlayer: NSObject { } @objc func play() { - if (self.book.hasAudio() == true) { + if book.hasAudio { guard let currentPage = self.folioReader.readerCenter?.currentPage else { return } currentPage.webView?.js("playAudio()") } else { @@ -178,7 +178,7 @@ open class FolioReaderAudioPlayer: NSObject { self.stop() - let smilFile = self.book.smilFileForHref(href) + let smilFile = book.smilFile(forHref: href) // if no smil file for this href and the same href is being requested, we've hit the end. stop playing if smilFile == nil && currentHref != nil && href == currentHref { @@ -308,7 +308,7 @@ open class FolioReaderAudioPlayer: NSObject { */ fileprivate func nextAudioFragment() -> FRSmilElement? { - guard let smilFile = self.book.smilFileForHref(currentHref) else { + guard let smilFile = book.smilFile(forHref: currentHref) else { return nil } @@ -362,7 +362,7 @@ open class FolioReaderAudioPlayer: NSObject { return } - let playbackActiveClass = self.book.playbackActiveClass() + let playbackActiveClass = book.playbackActiveClass guard let sentence = currentPage.webView?.js("getSentenceWithIndex('\(playbackActiveClass)')") else { if (readerCenter.isLastPage() == true) { self.stop() @@ -441,7 +441,7 @@ open class FolioReaderAudioPlayer: NSObject { } // Get book title - if let title = self.book.title() { + if let title = self.book.title { songInfo[MPMediaItemPropertyAlbumTitle] = title as AnyObject? } diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift index 0691d13eb..bfe013cff 100755 --- a/Source/FolioReaderCenter.swift +++ b/Source/FolioReaderCenter.swift @@ -274,7 +274,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl rightBarIcons.append(UIBarButtonItem(image: shareIcon, style: .plain, target: self, action:#selector(shareChapter(_:)))) } - if (self.book.hasAudio() == true || self.readerConfig.enableTTS == true) { + if self.book.hasAudio || self.readerConfig.enableTTS { rightBarIcons.append(UIBarButtonItem(image: audioIcon, style: .plain, target: self, action:#selector(presentPlayerMenu(_:)))) } @@ -285,7 +285,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl navigationItem.rightBarButtonItems = rightBarIcons if(self.readerConfig.displayTitle){ - navigationItem.title = book.title() + navigationItem.title = book.title } } @@ -1051,7 +1051,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl var shareItems = [AnyObject]() // Get book title - if let title = self.book.title() { + if let title = self.book.title { bookTitle = title subject += " “\(title)”" } @@ -1109,7 +1109,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl var shareItems = [AnyObject]() // Get book title - if let title = self.book.title() { + if let title = self.book.title { bookTitle = title subject += " “\(title)”" } diff --git a/Source/FolioReaderChapterList.swift b/Source/FolioReaderChapterList.swift index f177003e3..bdf27ca1d 100755 --- a/Source/FolioReaderChapterList.swift +++ b/Source/FolioReaderChapterList.swift @@ -80,7 +80,7 @@ class FolioReaderChapterList: UITableViewController { // Add audio duration for Media Ovelay if let resource = tocReference.resource { if let mediaOverlay = resource.mediaOverlay { - let duration = self.book.durationFor("#"+mediaOverlay) + let duration = self.book.duration(for: "#"+mediaOverlay) if let durationFormatted = (duration != nil ? duration : "")?.clockTimeToMinutesString() { let text = cell.indexLabel?.text ?? "" diff --git a/Source/FolioReaderContainer.swift b/Source/FolioReaderContainer.swift index 3ab6eddbc..63dac525e 100755 --- a/Source/FolioReaderContainer.swift +++ b/Source/FolioReaderContainer.swift @@ -171,7 +171,7 @@ open class FolioReaderContainer: UIViewController { DispatchQueue.main.async { // Add audio player if needed - if (self.book.hasAudio() == true || self.readerConfig.enableTTS == true) { + if self.book.hasAudio || self.readerConfig.enableTTS { self.addAudioPlayer() } self.centerViewController?.reloadData() diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index af857d01e..5c42d21f7 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -196,7 +196,7 @@ open class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRe refreshPageMode() - if (self.readerConfig.enableTTS == true && self.book.hasAudio() == false) { + if self.readerConfig.enableTTS && !self.book.hasAudio { webView.js("wrappingSentencesWithinPTags()") if let audioPlayer = self.folioReader.readerAudioPlayer, (audioPlayer.isPlaying() == true) { @@ -485,7 +485,7 @@ open class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRe return } - let playbackActiveClass = self.book.playbackActiveClass() + let playbackActiveClass = self.book.playbackActiveClass currentPage.webView?.js("audioMarkID('\(playbackActiveClass)','\(identifier)')") } diff --git a/Source/FolioReaderQuoteShare.swift b/Source/FolioReaderQuoteShare.swift index b195e9ca6..f93842a84 100644 --- a/Source/FolioReaderQuoteShare.swift +++ b/Source/FolioReaderQuoteShare.swift @@ -87,7 +87,7 @@ class FolioReaderQuoteShare: UIViewController { var bookTitle = "" var authorName = "" - if let title = self.book.title() { + if let title = self.book.title { bookTitle = title } @@ -262,7 +262,7 @@ class FolioReaderQuoteShare: UIViewController { var shareItems = [AnyObject]() // Get book title - if let title = self.book.title() { + if let title = self.book.title { bookTitle = title subject += " “\(title)”" } diff --git a/Source/FolioReaderWebView.swift b/Source/FolioReaderWebView.swift index 0935ea322..704c4abca 100644 --- a/Source/FolioReaderWebView.swift +++ b/Source/FolioReaderWebView.swift @@ -48,7 +48,7 @@ open class FolioReaderWebView: UIWebView { // MARK: - UIMenuController open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { - guard (self.readerConfig.useReaderMenuController == true) else { + guard readerConfig.useReaderMenuController else { return super.canPerformAction(action, withSender: sender) } @@ -59,9 +59,9 @@ open class FolioReaderWebView: UIWebView { } else { if action == #selector(highlight(_:)) || (action == #selector(define(_:)) && isOneWord) - || (action == #selector(play(_:)) && (self.book.hasAudio() == true || self.readerConfig.enableTTS == true)) - || (action == #selector(share(_:)) && self.readerConfig.allowSharing == true) - || (action == #selector(copy(_:)) && self.readerConfig.allowSharing == true) { + || (action == #selector(play(_:)) && (book.hasAudio || readerConfig.enableTTS)) + || (action == #selector(share(_:)) && readerConfig.allowSharing) + || (action == #selector(copy(_:)) && readerConfig.allowSharing) { return true } return false @@ -275,7 +275,7 @@ open class FolioReaderWebView: UIWebView { // default menu menuItems = [highlightItem, defineItem, shareItem] - if (self.book.hasAudio() == true || self.readerConfig.enableTTS == true) { + if self.book.hasAudio || self.readerConfig.enableTTS { menuItems.insert(playAudioItem, at: 0) }