From 5f3028ad8f297567689e8257cd9412d94a64d457 Mon Sep 17 00:00:00 2001 From: Heberti Almeida Date: Wed, 28 Sep 2016 00:47:58 -0300 Subject: [PATCH 1/3] Initial working version converted to Swift 3 --- Example/Example.xcodeproj/project.pbxproj | 8 +- Example/Example/AppDelegate.swift | 6 +- Example/Example/ViewController.swift | 14 +- .../FolioReaderTests/FolioReaderTests.swift | 2 +- .../SharingProviderTests.swift | 4 +- Example/Podfile | 2 +- Example/Podfile.lock | 25 +- FolioReaderKit.podspec | 7 +- Source/EPUBCore/FRBook.swift | 26 +- Source/EPUBCore/FREpubParser.swift | 82 ++--- Source/EPUBCore/FRMediaType.swift | 8 +- Source/EPUBCore/FRMetadata.swift | 12 +- Source/EPUBCore/FRResource.swift | 6 +- Source/EPUBCore/FRResources.swift | 18 +- Source/EPUBCore/FRSmilElement.swift | 18 +- Source/EPUBCore/FRSmils.swift | 26 +- Source/EPUBCore/FRSpine.swift | 4 +- Source/FolioReaderAudioPlayer.swift | 104 +++--- Source/FolioReaderCenter.swift | 348 +++++++++--------- Source/FolioReaderChapterList.swift | 32 +- Source/FolioReaderChapterListCell.swift | 6 +- Source/FolioReaderConfig.swift | 110 +++--- Source/FolioReaderContainer.swift | 41 ++- Source/FolioReaderFontsMenu.swift | 146 ++++---- Source/FolioReaderHighlightList.swift | 66 ++-- Source/FolioReaderKit.swift | 250 +++++++------ Source/FolioReaderPage.swift | 159 ++++---- Source/FolioReaderPageIndicator.swift | 42 +-- Source/FolioReaderPlayerMenu.swift | 156 ++++---- Source/FolioReaderQuoteShare.swift | 162 ++++---- Source/FolioReaderSharingProvider.swift | 8 +- Source/FolioReaderWebView.swift | 100 ++--- Source/Models/CoreDataManager.swift | 18 +- Source/Models/Highlight+Helper.swift | 104 +++--- Source/Models/Highlight+Migration.swift | 64 ++-- Source/Models/Highlight.swift | 28 +- Source/PageViewController.swift | 48 +-- Source/QuoteImage.swift | 10 +- Source/ScrollScrubber.swift | 86 +++-- Vendor/FontBlaster/FontBlaster.swift | 228 ------------ Vendor/HAControls/HADiscreteSlider.swift | 200 +++++----- Vendor/SMSegmentView/SMSegment.swift | 52 +-- Vendor/SMSegmentView/SMSegmentView.swift | 70 ++-- 43 files changed, 1360 insertions(+), 1546 deletions(-) delete mode 100644 Vendor/FontBlaster/FontBlaster.swift diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index b10eb193b..af77dd3f7 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -760,7 +760,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.folioreader.Example; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -776,7 +776,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.folioreader.Example; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -793,7 +793,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.folioreader.FolioReaderTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Example.app/Example"; }; name = Debug; @@ -811,7 +811,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.folioreader.FolioReaderTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Example.app/Example"; }; name = Release; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 5c7a14cc6..9353ee387 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -14,16 +14,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { FolioReader.applicationWillResignActive() } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { FolioReader.applicationWillTerminate() } diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index cd9639ed2..093e199af 100755 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -25,11 +25,11 @@ class ViewController: UIViewController { setCover(bookTwo, index: 1) } - @IBAction func didOpen(sender: AnyObject) { + @IBAction func didOpen(_ sender: AnyObject) { openEpub(sender.tag); } - func openEpub(sampleNum: Int) { + func openEpub(_ sampleNum: Int) { let config = FolioReaderConfig() config.shouldHideNavigationOnTap = sampleNum == 1 ? true : false config.scrollDirection = sampleNum == 1 ? .horizontal : .vertical @@ -45,23 +45,23 @@ class ViewController: UIViewController { // config.menuBackgroundColor = UIColor.lightGrayColor() // Custom sharing quote background - let customImageQuote = QuoteImage(withImage: UIImage(named: "demo-bg")!, alpha: 0.6, backgroundColor: UIColor.blackColor()) + let customImageQuote = QuoteImage(withImage: UIImage(named: "demo-bg")!, alpha: 0.6, backgroundColor: UIColor.black) let customQuote = QuoteImage(withColor: UIColor(red:0.30, green:0.26, blue:0.20, alpha:1.0), alpha: 1.0, textColor: UIColor(red:0.86, green:0.73, blue:0.70, alpha:1.0)) config.quoteCustomBackgrounds = [customImageQuote, customQuote] // Epub file let epubName = epubSampleFiles[sampleNum-1]; - let bookPath = NSBundle.mainBundle().pathForResource(epubName, ofType: "epub") + let bookPath = Bundle.main.path(forResource: epubName, ofType: "epub") FolioReader.presentReader(parentViewController: self, withEpubPath: bookPath!, andConfig: config, shouldRemoveEpub: false) } - func setCover(button: UIButton, index: Int) { + func setCover(_ button: UIButton, index: Int) { let epubName = epubSampleFiles[index]; - let bookPath = NSBundle.mainBundle().pathForResource(epubName, ofType: "epub") + let bookPath = Bundle.main.path(forResource: epubName, ofType: "epub") if let image = FolioReader.getCoverImage(bookPath!) { - button.setBackgroundImage(image, forState: .Normal) + button.setBackgroundImage(image, for: .normal) } } } diff --git a/Example/FolioReaderTests/FolioReaderTests.swift b/Example/FolioReaderTests/FolioReaderTests.swift index dec6db055..edd0b5a52 100644 --- a/Example/FolioReaderTests/FolioReaderTests.swift +++ b/Example/FolioReaderTests/FolioReaderTests.swift @@ -17,7 +17,7 @@ class FolioReaderTests: QuickSpec { var subject: FREpubParser! beforeEach { - let path = NSBundle(forClass: self.dynamicType).pathForResource("The Silver Chair", ofType: "epub")! + let path = Bundle(for: type(of: self)).path(forResource: "The Silver Chair", ofType: "epub")! subject = FREpubParser() subject.readEpub(epubPath: path) } diff --git a/Example/FolioReaderTests/SharingProviderTests.swift b/Example/FolioReaderTests/SharingProviderTests.swift index c4441647b..02717d0f7 100644 --- a/Example/FolioReaderTests/SharingProviderTests.swift +++ b/Example/FolioReaderTests/SharingProviderTests.swift @@ -30,7 +30,7 @@ class SharingProviderTests: QuickSpec { } it("returns text for a mail activity") { - let itemForActivityType = subject.activityViewController(activityViewController, itemForActivityType: UIActivityTypeMail) as? String + let itemForActivityType = subject.activityViewController(activityViewController, itemForActivityType: UIActivityType.mail) as? String expect(itemForActivityType).to(equal(subject.text)) } } @@ -41,7 +41,7 @@ class SharingProviderTests: QuickSpec { } it("returns HTML for a mail activity") { - let itemForActivityType = subject.activityViewController(activityViewController, itemForActivityType: UIActivityTypeMail) as? String + let itemForActivityType = subject.activityViewController(activityViewController, itemForActivityType: UIActivityType.mail) as? String expect(itemForActivityType).to(equal(subject.html)) } } diff --git a/Example/Podfile b/Example/Podfile index 4ab1cb243..d70e49eb0 100755 --- a/Example/Podfile +++ b/Example/Podfile @@ -30,7 +30,7 @@ post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| # Force add version - config.build_settings['SWIFT_VERSION'] = '2.3' + config.build_settings['SWIFT_VERSION'] = '3.0' # ensure testability is set https://github.com/CocoaPods/CocoaPods/issues/4505 if config.name == 'Debug' diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 4c4ce0640..99eb85202 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,14 +1,16 @@ PODS: - - AEXML (3.0.0) + - AEXML (4.0.0) - FolioReaderKit (0.9.2): - - AEXML (= 3.0) - - JSQWebViewController (~> 3.0) - - MenuItemKit (= 1.1.3) + - AEXML (= 4.0) + - FontBlaster (= 3.0.0) + - JSQWebViewController (~> 5.0) + - MenuItemKit (= 2.0) - RealmSwift (~> 1.0) - SSZipArchive (= 1.5) - ZFDragableModalTransition (~> 0.6) - - JSQWebViewController (3.0.0) - - MenuItemKit (1.1.3) + - FontBlaster (3.0.0) + - JSQWebViewController (5.0.0) + - MenuItemKit (2.0) - Nimble (4.1.0) - Quick (0.9.2) - Realm (1.1.0): @@ -29,10 +31,11 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - AEXML: 716fb0a8decba4a3517324a71fee3685b30233d2 - FolioReaderKit: 36323726496c38908430d79c432e7e4710e2bc01 - JSQWebViewController: eaa6bd68d9e1426ae25ade99c9bbde4c6cdd4120 - MenuItemKit: 11c448d9172936ee79ebb78f36dffd396020a9e5 + AEXML: 6fc6433aa35bdc15dd8eb8d54eb7aa4520a010eb + FolioReaderKit: a4a0b5d0aab63d847fe80ca02162ac5b8588200f + FontBlaster: 0f4a3e2f965968e2dfc3f7f5cec7214c3f3b2f07 + JSQWebViewController: 0461aa42612b221571556df61d9c09f080cb2eef + MenuItemKit: 9af69953dc983803ee85230e205fadbac0f26213 Nimble: 97a0a4cae5124c117115634b2d055d8c97d0af19 Quick: 18d057bc66451eedd5d1c8dc99ba2a5db6e60226 Realm: ceecf1a4540c4ce9efe196fe73fa9855bce05bd8 @@ -40,6 +43,6 @@ SPEC CHECKSUMS: SSZipArchive: 29daace2bccb90a47de2837744da397728ff9207 ZFDragableModalTransition: 0d294eaaba6edfcb9839595de765f9ca06a4b524 -PODFILE CHECKSUM: 55962a6106e53da04861e4602fc98856987b97b3 +PODFILE CHECKSUM: 87487883f550f6b3c934f4b560943a658a5b1f84 COCOAPODS: 1.0.1 diff --git a/FolioReaderKit.podspec b/FolioReaderKit.podspec index 6e63ed4c3..eddcd4bc2 100644 --- a/FolioReaderKit.podspec +++ b/FolioReaderKit.podspec @@ -32,10 +32,11 @@ Pod::Spec.new do |s| s.libraries = "z" s.frameworks = 'CoreData' s.dependency 'SSZipArchive', '1.5' - s.dependency 'MenuItemKit', '1.1.3' + s.dependency 'MenuItemKit', '2.0' s.dependency 'ZFDragableModalTransition', '~> 0.6' - s.dependency 'AEXML', '3.0' - s.dependency 'JSQWebViewController', '~> 3.0' + s.dependency 'AEXML', '4.0' + s.dependency 'FontBlaster', '3.0.0' + s.dependency 'JSQWebViewController', '~> 5.0' s.dependency 'RealmSwift', '~> 1.0' # s.dependency 'SMSegmentView' end diff --git a/Source/EPUBCore/FRBook.swift b/Source/EPUBCore/FRBook.swift index d9a997d61..3f2b3b94d 100755 --- a/Source/EPUBCore/FRBook.swift +++ b/Source/EPUBCore/FRBook.swift @@ -9,7 +9,7 @@ import UIKit -public class FRBook: NSObject { +open class FRBook: NSObject { var resources = FRResources() var metadata = FRMetadata() var spine = FRSpine() @@ -38,19 +38,23 @@ public class FRBook: NSObject { } // @NOTE: should "#" be automatically prefixed with the ID? - func durationFor(ID: String) -> String? { + func durationFor(_ ID: String) -> String? { return metadata.findMetaByProperty("media:duration", refinedBy: ID) } - func activeClass() -> String! { - let className = metadata.findMetaByProperty("media:active-class"); - return className ?? "epub-media-overlay-active"; + func activeClass() -> String { + guard let className = metadata.findMetaByProperty("media:active-class") else { + return "epub-media-overlay-active" + } + return className } - func playbackActiveClass() -> String! { - let className = metadata.findMetaByProperty("media:playback-active-class"); - return className ?? "epub-media-overlay-playing"; + func playbackActiveClass() -> String { + guard let className = metadata.findMetaByProperty("media:playback-active-class") else { + return "epub-media-overlay-playing" + } + return className } @@ -59,7 +63,7 @@ public class FRBook: NSObject { /** Get Smil File from a resource (if it has a media-overlay) */ - func smilFileForResource(resource: FRResource!) -> FRSmilFile! { + func smilFileForResource(_ resource: FRResource!) -> FRSmilFile! { if( resource == nil || resource.mediaOverlay == nil ){ return nil } @@ -71,11 +75,11 @@ public class FRBook: NSObject { return smils.findByHref( smilResource!.href ) } - func smilFileForHref(href: String) -> FRSmilFile! { + func smilFileForHref(_ href: String) -> FRSmilFile! { return smilFileForResource(resources.findByHref(href)) } - func smilFileForId(ID: String) -> FRSmilFile! { + func smilFileForId(_ ID: String) -> FRSmilFile! { return smilFileForResource(resources.findById(ID)) } diff --git a/Source/EPUBCore/FREpubParser.swift b/Source/EPUBCore/FREpubParser.swift index 612e94d3e..32225aa39 100755 --- a/Source/EPUBCore/FREpubParser.swift +++ b/Source/EPUBCore/FREpubParser.swift @@ -19,13 +19,13 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { var bookBasePath: String! var resourcesBasePath: String! var shouldRemoveEpub = true - private var epubPathToRemove: String? + fileprivate var epubPathToRemove: String? /** Parse the Cover Image from an epub file. Returns an UIImage. */ - func parseCoverImage(epubPath: String) -> UIImage? { + func parseCoverImage(_ epubPath: String) -> UIImage? { guard let book = readEpub(epubPath: epubPath, removeEpub: false), let coverImage = book.coverImage else { return nil } @@ -41,29 +41,29 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { shouldRemoveEpub = removeEpub var isDir: ObjCBool = false - let fileManager = NSFileManager.defaultManager() + let fileManager = FileManager.default let bookName = (withEpubPath as NSString).lastPathComponent - bookBasePath = (kApplicationDocumentsDirectory as NSString).stringByAppendingPathComponent(bookName) + bookBasePath = (kApplicationDocumentsDirectory as NSString).appendingPathComponent(bookName) - guard fileManager.fileExistsAtPath(withEpubPath) else { + guard fileManager.fileExists(atPath: withEpubPath) else { print("Epub file does not exist.") return nil } // Unzip if necessary var needsUnzip = false - if fileManager.fileExistsAtPath(bookBasePath, isDirectory:&isDir) { - if !isDir { needsUnzip = true } + if fileManager.fileExists(atPath: bookBasePath, isDirectory:&isDir) { + if !isDir.boolValue { needsUnzip = true } } else { needsUnzip = true } if needsUnzip { - SSZipArchive.unzipFileAtPath(withEpubPath, toDestination: bookBasePath, delegate: self) + SSZipArchive.unzipFile(atPath: withEpubPath, toDestination: bookBasePath, delegate: self) } // Skip from backup this folder - addSkipBackupAttributeToItemAtURL(NSURL(fileURLWithPath: bookBasePath, isDirectory: true)) + addSkipBackupAttributeToItemAtURL(URL(fileURLWithPath: bookBasePath, isDirectory: true)) kBookId = bookName readContainer() @@ -74,17 +74,17 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { /** Read and parse container.xml file. */ - private func readContainer() { + fileprivate func readContainer() { let containerPath = "META-INF/container.xml" do { - let containerData = try NSData(contentsOfFile: (bookBasePath as NSString).stringByAppendingPathComponent(containerPath), options: .DataReadingMappedAlways) - let xmlDoc = try AEXMLDocument(xmlData: containerData) + let containerData = try Data(contentsOf: URL(fileURLWithPath: (bookBasePath as NSString).appendingPathComponent(containerPath)), options: .alwaysMapped) + let xmlDoc = try AEXMLDocument(xml: containerData) let opfResource = FRResource() opfResource.href = xmlDoc.root["rootfiles"]["rootfile"].attributes["full-path"] opfResource.mediaType = FRMediaType.determineMediaType(xmlDoc.root["rootfiles"]["rootfile"].attributes["full-path"]!) book.opfResource = opfResource - resourcesBasePath = (bookBasePath as NSString).stringByAppendingPathComponent((book.opfResource.href as NSString).stringByDeletingLastPathComponent) + resourcesBasePath = (bookBasePath as NSString).appendingPathComponent((book.opfResource.href as NSString).deletingLastPathComponent) } catch { print("Cannot read container.xml") } @@ -93,13 +93,13 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { /** Read and parse .opf file. */ - private func readOpf() { - let opfPath = (bookBasePath as NSString).stringByAppendingPathComponent(book.opfResource.href) + fileprivate func readOpf() { + let opfPath = (bookBasePath as NSString).appendingPathComponent(book.opfResource.href) var identifier: String? do { - let opfData = try NSData(contentsOfFile: opfPath, options: .DataReadingMappedAlways) - let xmlDoc = try AEXMLDocument(xmlData: opfData) + let opfData = try Data(contentsOf: URL(fileURLWithPath: opfPath), options: .alwaysMapped) + let xmlDoc = try AEXMLDocument(xml: opfData) // Base OPF info if let package = xmlDoc.children.first { @@ -116,7 +116,7 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { resource.id = item.attributes["id"] resource.properties = item.attributes["properties"] resource.href = item.attributes["href"] - resource.fullHref = (resourcesBasePath as NSString).stringByAppendingPathComponent(item.attributes["href"]!).stringByRemovingPercentEncoding + resource.fullHref = (resourcesBasePath as NSString).appendingPathComponent(resource.href).removingPercentEncoding resource.mediaType = FRMediaType.mediaTypeByName(item.attributes["media-type"]!, fileName: resource.href) resource.mediaOverlay = item.attributes["media-overlay"] @@ -179,16 +179,16 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { /** Reads and parses a .smil file */ - private func readSmilFile(resource: FRResource) { + fileprivate func readSmilFile(_ resource: FRResource) { do { - let smilData = try NSData(contentsOfFile: resource.fullHref, options: .DataReadingMappedAlways) + let smilData = try Data(contentsOf: URL(fileURLWithPath: resource.fullHref), options: .alwaysMapped) var smilFile = FRSmilFile(resource: resource) - let xmlDoc = try AEXMLDocument(xmlData: smilData) + let xmlDoc = try AEXMLDocument(xml: smilData) let children = xmlDoc.root["body"].children if children.count > 0 { - smilFile.data.appendContentsOf(readSmilFileElements(children)) + smilFile.data.append(contentsOf: readSmilFileElements(children)) } book.smils.add(smilFile) @@ -197,7 +197,7 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { } } - private func readSmilFileElements(children:[AEXMLElement]) -> [FRSmilElement] { + fileprivate func readSmilFileElements(_ children:[AEXMLElement]) -> [FRSmilElement] { var data = [FRSmilElement]() // convert each smil element to a FRSmil object @@ -207,7 +207,7 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { // if this element has children, convert them to objects too if item.children.count > 0 { - smil.children.appendContentsOf(readSmilFileElements(item.children)) + smil.children.append(contentsOf: readSmilFileElements(item.children)) } data.append(smil) @@ -219,22 +219,22 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { /** Read and parse the Table of Contents. */ - private func findTableOfContents() -> [FRTocReference] { + fileprivate func findTableOfContents() -> [FRTocReference] { var tableOfContent = [FRTocReference]() var tocItems: [AEXMLElement]? guard let tocResource = book.tocResource else { return tableOfContent } - let tocPath = (resourcesBasePath as NSString).stringByAppendingPathComponent(tocResource.href) + let tocPath = (resourcesBasePath as NSString).appendingPathComponent(tocResource.href) do { if tocResource.mediaType == FRMediaType.NCX { - let ncxData = try NSData(contentsOfFile: tocPath, options: .DataReadingMappedAlways) - let xmlDoc = try AEXMLDocument(xmlData: ncxData) + let ncxData = try Data(contentsOf: URL(fileURLWithPath: tocPath), options: .alwaysMapped) + let xmlDoc = try AEXMLDocument(xml: ncxData) if let itemsList = xmlDoc.root["navMap"]["navPoint"].all { tocItems = itemsList } } else { - let tocData = try NSData(contentsOfFile: tocPath, options: .DataReadingMappedAlways) - let xmlDoc = try AEXMLDocument(xmlData: tocData) + let tocData = try Data(contentsOf: URL(fileURLWithPath: tocPath), options: .alwaysMapped) + let xmlDoc = try AEXMLDocument(xml: tocData) if let nav = xmlDoc.root["body"]["nav"].first, let itemsList = nav["ol"]["li"].all { tocItems = itemsList @@ -261,7 +261,7 @@ class FREpubParser: NSObject, SSZipArchiveDelegate { - parameter element: A `AEXMLElement`, usually the `` - returns: If found the `