diff --git a/Source/EPUBCore/FRHighlight.swift b/Source/EPUBCore/FRHighlight.swift index 65cafe20c..5cd010bec 100644 --- a/Source/EPUBCore/FRHighlight.swift +++ b/Source/EPUBCore/FRHighlight.swift @@ -8,19 +8,19 @@ import UIKit -enum HighlightStyle: Int { +public enum HighlightStyle: Int { case Yellow case Green case Blue case Pink case Underline - init () { self = .Yellow } + public init () { self = .Yellow } /** Return HighlightStyle for CSS class. */ - static func styleForClass(className: String) -> HighlightStyle { + public static func styleForClass(className: String) -> HighlightStyle { switch className { case "highlight-yellow": return .Yellow @@ -40,7 +40,7 @@ enum HighlightStyle: Int { /** Return CSS class for HighlightStyle. */ - static func classForStyle(style: Int) -> String { + public static func classForStyle(style: Int) -> String { switch style { case HighlightStyle.Yellow.rawValue: return "highlight-yellow" @@ -60,7 +60,7 @@ enum HighlightStyle: Int { /** Return CSS class for HighlightStyle. */ - static func colorForStyle(style: Int, nightMode: Bool = false) -> UIColor { + public static func colorForStyle(style: Int, nightMode: Bool = false) -> UIColor { switch style { case HighlightStyle.Yellow.rawValue: return UIColor(red: 255/255, green: 235/255, blue: 107/255, alpha: nightMode ? 0.9 : 1) @@ -78,7 +78,7 @@ enum HighlightStyle: Int { } } -class FRHighlight: NSObject { +public class FRHighlight: NSObject { var id: String! var content: String! var contentPre: String! @@ -91,7 +91,7 @@ class FRHighlight: NSObject { /** Match a highlight on string. */ - static func matchHighlight(text: String!, andId id: String) -> FRHighlight? { + public static func matchHighlight(text: String!, andId id: String) -> FRHighlight? { let pattern = "((.|\\s)*?)" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(text, options: [], range: NSRange(location: 0, length: text.utf16.count)) @@ -135,7 +135,7 @@ class FRHighlight: NSObject { return mapped.first } - static func removeById(highlightId: String) -> String? { + public static func removeById(highlightId: String) -> String? { let currentPage = FolioReader.sharedInstance.readerCenter.currentPage if let removedId = currentPage.webView.js("removeHighlightById('\(highlightId)')") { diff --git a/Source/FolioReaderFontsMenu.swift b/Source/FolioReaderFontsMenu.swift index e28697de2..35eabaaf7 100644 --- a/Source/FolioReaderFontsMenu.swift +++ b/Source/FolioReaderFontsMenu.swift @@ -8,7 +8,7 @@ import UIKit -class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate { +class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRecognizerDelegate { var menuView: UIView! diff --git a/Source/FolioReaderKit.swift b/Source/FolioReaderKit.swift index 792047053..57642e9bf 100755 --- a/Source/FolioReaderKit.swift +++ b/Source/FolioReaderKit.swift @@ -453,7 +453,7 @@ internal extension UIImage { } } -extension UIViewController: UIGestureRecognizerDelegate { +internal extension UIViewController { func setCloseButton() { self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(readerImageNamed: "icon-close"), style: UIBarButtonItemStyle.Plain, target: self, action:#selector(UIViewController.dismiss)) diff --git a/Source/FolioReaderPlayerMenu.swift b/Source/FolioReaderPlayerMenu.swift index e5a5b2ee0..d6a44c6b0 100644 --- a/Source/FolioReaderPlayerMenu.swift +++ b/Source/FolioReaderPlayerMenu.swift @@ -8,7 +8,7 @@ import UIKit -class FolioReaderPlayerMenu: UIViewController, SMSegmentViewDelegate { +class FolioReaderPlayerMenu: UIViewController, SMSegmentViewDelegate, UIGestureRecognizerDelegate { var menuView: UIView! var playPauseBtn: UIButton! diff --git a/Source/Models/Highlight+Properties.swift b/Source/Models/Highlight+Properties.swift new file mode 100644 index 000000000..4c11b72aa --- /dev/null +++ b/Source/Models/Highlight+Properties.swift @@ -0,0 +1,23 @@ +// +// Highlight+Properties.swift +// FolioReaderKit +// +// Created by Heberti Almeida on 06/07/16. +// Copyright (c) 2015 Folio Reader. All rights reserved. +// + +import Foundation +import CoreData + +public extension Highlight { + + @NSManaged public var bookId: String + @NSManaged public var content: String + @NSManaged public var contentPost: String + @NSManaged public var contentPre: String + @NSManaged public var date: NSDate + @NSManaged public var highlightId: String + @NSManaged public var page: NSNumber + @NSManaged public var type: NSNumber + +} \ No newline at end of file diff --git a/Source/Models/Highlight.swift b/Source/Models/Highlight.swift index 44a951106..a76a3ab41 100644 --- a/Source/Models/Highlight.swift +++ b/Source/Models/Highlight.swift @@ -9,26 +9,53 @@ import Foundation import CoreData -@objc(Highlight) -class Highlight: NSManagedObject { - - @NSManaged var bookId: String - @NSManaged var content: String - @NSManaged var contentPost: String - @NSManaged var contentPre: String - @NSManaged var date: NSDate - @NSManaged var highlightId: String - @NSManaged var page: NSNumber - @NSManaged var type: NSNumber - -} - public typealias Completion = (error: NSError?) -> () let coreDataManager = CoreDataManager() -extension Highlight { +@objc(Highlight) +public class Highlight: NSManagedObject { + + public func persist(completion: Completion?) { + var highlight: Highlight? + + do { + let fetchRequest = NSFetchRequest(entityName: "Highlight") + fetchRequest.predicate = NSPredicate(format:"highlightId = %@", highlightId) + highlight = try coreDataManager.managedObjectContext.executeFetchRequest(fetchRequest).last as? Highlight + } catch let error as NSError { + print(error) + highlight = nil + } + + if highlight != nil { + highlight!.content = content + highlight!.contentPre = contentPre + highlight!.contentPost = contentPost + highlight!.date = date + highlight!.type = type + } else { + highlight = NSEntityDescription.insertNewObjectForEntityForName("Highlight", inManagedObjectContext: coreDataManager.managedObjectContext) as? Highlight + + highlight!.bookId = bookId + highlight!.content = content + highlight!.contentPre = contentPre + highlight!.contentPost = contentPost + highlight!.date = date + highlight!.highlightId = highlightId + highlight!.page = page + highlight!.type = type + } + + // Save + do { + try coreDataManager.managedObjectContext.save() + completion?(error: nil) + } catch let error as NSError { + completion?(error: error) + } + } - static func persistHighlight(object: FRHighlight, completion: Completion?) { + public static func persistHighlight(object: FRHighlight, completion: Completion?) { var highlight: Highlight? do { @@ -63,17 +90,13 @@ extension Highlight { // Save do { try coreDataManager.managedObjectContext.save() - if (completion != nil) { - completion!(error: nil) - } + completion?(error: nil) } catch let error as NSError { - if (completion != nil) { - completion!(error: error) - } + completion?(error: error) } } - static func removeById(highlightId: String) { + public static func removeById(highlightId: String) { var highlight: Highlight? do { @@ -88,7 +111,7 @@ extension Highlight { } } - static func updateById(highlightId: String, type: HighlightStyle) { + public static func updateById(highlightId: String, type: HighlightStyle) { var highlight: Highlight? do { @@ -103,7 +126,7 @@ extension Highlight { } } - static func allByBookId(bookId: String, andPage page: NSNumber? = nil) -> [Highlight] { + public static func allByBookId(bookId: String, andPage page: NSNumber? = nil) -> [Highlight] { var highlights: [Highlight]? let predicate = (page != nil) ? NSPredicate(format: "bookId = %@ && page = %@", bookId, page!) : NSPredicate(format: "bookId = %@", bookId)