diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj
index 8a3f85d48..2329441ae 100644
--- a/Example/Example.xcodeproj/project.pbxproj
+++ b/Example/Example.xcodeproj/project.pbxproj
@@ -158,12 +158,15 @@
isa = PBXNativeTarget;
buildConfigurationList = 1A42C29B1C0E3883000F2137 /* Build configuration list for PBXNativeTarget "Example" */;
buildPhases = (
+ 6BB4B87019CBF22A26D57251 /* 📦 Check Pods Manifest.lock */,
BFF78AC0EC7FAA2ADF7765C4 /* [CP] Check Pods Manifest.lock */,
1A42C2851C0E3882000F2137 /* Sources */,
1A42C2861C0E3882000F2137 /* Frameworks */,
1A42C2871C0E3882000F2137 /* Resources */,
B6A166DC30D64CDC32736E22 /* [CP] Embed Pods Frameworks */,
8F716768EB59E9B6D88C48D7 /* [CP] Copy Pods Resources */,
+ 52EDD0255393518C859DA01C /* 📦 Embed Pods Frameworks */,
+ EF9B4ADBBC85CBF81E88F153 /* 📦 Copy Pods Resources */,
);
buildRules = (
);
@@ -178,12 +181,15 @@
isa = PBXNativeTarget;
buildConfigurationList = CA10C1381C572A4B0049165D /* Build configuration list for PBXNativeTarget "FolioReaderTests" */;
buildPhases = (
+ 0642B6A6EF0631B5697FC382 /* 📦 Check Pods Manifest.lock */,
1D8126CC73301F2413345444 /* [CP] Check Pods Manifest.lock */,
CA10C12D1C572A4B0049165D /* Sources */,
CA10C12E1C572A4B0049165D /* Frameworks */,
CA10C12F1C572A4B0049165D /* Resources */,
C1392FF51E4FD15EBB5AA8DD /* [CP] Embed Pods Frameworks */,
652A7706A17894A2B0BA1503 /* [CP] Copy Pods Resources */,
+ E82F486563C1547E0775A59F /* 📦 Embed Pods Frameworks */,
+ 064D38CE83FACC358B7C0EC8 /* 📦 Copy Pods Resources */,
);
buildRules = (
);
@@ -263,6 +269,36 @@
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
+ 0642B6A6EF0631B5697FC382 /* 📦 Check Pods Manifest.lock */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "📦 Check Pods Manifest.lock";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
+ showEnvVarsInLog = 0;
+ };
+ 064D38CE83FACC358B7C0EC8 /* 📦 Copy Pods Resources */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "📦 Copy Pods Resources";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-FolioReaderTests/Pods-FolioReaderTests-resources.sh\"\n";
+ showEnvVarsInLog = 0;
+ };
1D8126CC73301F2413345444 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
@@ -278,6 +314,21 @@
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
+ 52EDD0255393518C859DA01C /* 📦 Embed Pods Frameworks */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "📦 Embed Pods Frameworks";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Example/Pods-Example-frameworks.sh\"\n";
+ showEnvVarsInLog = 0;
+ };
652A7706A17894A2B0BA1503 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
@@ -293,6 +344,21 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-FolioReaderTests/Pods-FolioReaderTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
+ 6BB4B87019CBF22A26D57251 /* 📦 Check Pods Manifest.lock */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "📦 Check Pods Manifest.lock";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
+ showEnvVarsInLog = 0;
+ };
8F716768EB59E9B6D88C48D7 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
@@ -353,6 +419,36 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-FolioReaderTests/Pods-FolioReaderTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
+ E82F486563C1547E0775A59F /* 📦 Embed Pods Frameworks */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "📦 Embed Pods Frameworks";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-FolioReaderTests/Pods-FolioReaderTests-frameworks.sh\"\n";
+ showEnvVarsInLog = 0;
+ };
+ EF9B4ADBBC85CBF81E88F153 /* 📦 Copy Pods Resources */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "📦 Copy Pods Resources";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Example/Pods-Example-resources.sh\"\n";
+ showEnvVarsInLog = 0;
+ };
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
diff --git a/Example/Podfile.lock b/Example/Podfile.lock
index 9dcfbf6c7..c686aa99e 100644
--- a/Example/Podfile.lock
+++ b/Example/Podfile.lock
@@ -4,14 +4,20 @@ PODS:
- AEXML
- FontBlaster
- JSQWebViewController
+ - RealmSwift
- SSZipArchive
- UIMenuItem-CXAImageSupport
- ZFDragableModalTransition
- - FontBlaster (2.1.2)
+ - FontBlaster (2.1.4)
- JSQWebViewController (3.0.0)
- - Nimble (4.0.1)
+ - Nimble (4.1.0)
- Quick (0.9.2)
- - SSZipArchive (1.1)
+ - Realm (1.0.1):
+ - Realm/Headers (= 1.0.1)
+ - Realm/Headers (1.0.1)
+ - RealmSwift (1.0.1):
+ - Realm (= 1.0.1)
+ - SSZipArchive (1.3)
- UIMenuItem-CXAImageSupport (0.0.1)
- ZFDragableModalTransition (0.6)
@@ -26,15 +32,17 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
AEXML: 716fb0a8decba4a3517324a71fee3685b30233d2
- FolioReaderKit: 38622e8f80681ed3ab888f0563d85929caa429a4
- FontBlaster: 95ebf29a025dbeda4530300099c6f533e805682c
+ FolioReaderKit: e5e80a3b1d03f4c5b47e36bdb4117b3de73ad9af
+ FontBlaster: b780f709b8f705638d0c80a1ecdfb23b3077d0a6
JSQWebViewController: eaa6bd68d9e1426ae25ade99c9bbde4c6cdd4120
- Nimble: 0f3c8b8b084cda391209c3c5efbb48bedeeb920a
+ Nimble: 97a0a4cae5124c117115634b2d055d8c97d0af19
Quick: 18d057bc66451eedd5d1c8dc99ba2a5db6e60226
- SSZipArchive: de3d215ab73f87db627f81f2ce24f1731e3886c3
+ Realm: 2e15e39d35b08abb529e212ed5d8af107b5236c8
+ RealmSwift: d73ea2af08c3676dbdd5e4b669651a0f492162a4
+ SSZipArchive: 5525196ee195b473dcc4a0d4d2cba98bfa3eb1c1
UIMenuItem-CXAImageSupport: 2945e2af4487414caad801ed8ff6ac8db274e986
ZFDragableModalTransition: 0d294eaaba6edfcb9839595de765f9ca06a4b524
PODFILE CHECKSUM: 7bafcb0eb37bf78bf161a6ebcb80be176777e3a8
-COCOAPODS: 1.0.1
+COCOAPODS: 1.0.0
diff --git a/FolioReaderKit.podspec b/FolioReaderKit.podspec
index e2733d99d..79e6a9b78 100644
--- a/FolioReaderKit.podspec
+++ b/FolioReaderKit.podspec
@@ -23,7 +23,7 @@ Pod::Spec.new do |s|
]
s.resources = [
'Source/**/*.{js,css,xcdatamodeld}',
- 'Source/Resources/Images/*.png',
+ 'Source/Resources/Images/*.png',
'Source/Resources/Fonts/**/*.{otf,ttf}'
]
s.preserve_paths = 'Source/**/*.xcdatamodeld'
@@ -37,5 +37,6 @@ Pod::Spec.new do |s|
s.dependency 'AEXML'
s.dependency 'FontBlaster'
s.dependency 'JSQWebViewController'
+ s.dependency 'RealmSwift'
# s.dependency 'SMSegmentView'
end
diff --git a/Source/EPUBCore/FRHighlight.swift b/Source/EPUBCore/FRHighlight.swift
index 5cd010bec..acb432981 100644
--- a/Source/EPUBCore/FRHighlight.swift
+++ b/Source/EPUBCore/FRHighlight.swift
@@ -84,14 +84,16 @@ public class FRHighlight: NSObject {
var contentPre: String!
var contentPost: String!
var date: NSDate!
- var page: NSNumber!
+ var page: Int!
var bookId: String!
var type: HighlightStyle!
+ var startOffset: Int = -1
+ var endOffset: Int! = -1
/**
Match a highlight on string.
*/
- public static func matchHighlight(text: String!, andId id: String) -> FRHighlight? {
+ public static func matchHighlight(text: String!, andId id: String, startOffset: String, endOffset: 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))
@@ -129,6 +131,8 @@ public class FRHighlight: NSObject {
highlight.contentPost = contentPost
highlight.page = currentPageNumber
highlight.bookId = (kBookId as NSString).stringByDeletingPathExtension
+ highlight.startOffset = Int(startOffset) ?? -1
+ highlight.endOffset = Int(endOffset) ?? -1
return highlight
}
diff --git a/Source/FolioReaderHighlightList.swift b/Source/FolioReaderHighlightList.swift
index 0ef4d6046..76b2f0cb5 100644
--- a/Source/FolioReaderHighlightList.swift
+++ b/Source/FolioReaderHighlightList.swift
@@ -92,12 +92,12 @@ class FolioReaderHighlightList: UITableViewController {
text.addAttribute(NSFontAttributeName, value: UIFont(name: "Avenir-Light", size: 16)!, range: range)
text.addAttribute(NSForegroundColorAttributeName, value: textColor, range: range)
- if highlight.type.integerValue == HighlightStyle.Underline.rawValue {
+ if highlight.type == HighlightStyle.Underline.rawValue {
text.addAttribute(NSBackgroundColorAttributeName, value: UIColor.clearColor(), range: range)
- text.addAttribute(NSUnderlineColorAttributeName, value: HighlightStyle.colorForStyle(highlight.type.integerValue, nightMode: FolioReader.sharedInstance.nightMode), range: range)
+ text.addAttribute(NSUnderlineColorAttributeName, value: HighlightStyle.colorForStyle(highlight.type, nightMode: FolioReader.sharedInstance.nightMode), range: range)
text.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue), range: range)
} else {
- text.addAttribute(NSBackgroundColorAttributeName, value: HighlightStyle.colorForStyle(highlight.type.integerValue, nightMode: FolioReader.sharedInstance.nightMode), range: range)
+ text.addAttribute(NSBackgroundColorAttributeName, value: HighlightStyle.colorForStyle(highlight.type, nightMode: FolioReader.sharedInstance.nightMode), range: range)
}
// Text
@@ -143,7 +143,7 @@ class FolioReaderHighlightList: UITableViewController {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let highlight = highlights[indexPath.row]
- FolioReader.sharedInstance.readerCenter.changePageWith(page: highlight.page.integerValue, andFragment: highlight.highlightId)
+ FolioReader.sharedInstance.readerCenter.changePageWith(page: highlight.page, andFragment: highlight.highlightId)
dismissViewControllerAnimated(true, completion: nil)
}
diff --git a/Source/FolioReaderKit.swift b/Source/FolioReaderKit.swift
index 0b1c8b398..2e22c9c1a 100755
--- a/Source/FolioReaderKit.swift
+++ b/Source/FolioReaderKit.swift
@@ -54,8 +54,6 @@ enum MediaOverlayStyle: Int {
* Main Library class with some useful constants and methods
*/
public class FolioReader : NSObject {
- private override init() {}
-
static let sharedInstance = FolioReader()
static let defaults = NSUserDefaults.standardUserDefaults()
weak var readerCenter: FolioReaderCenter!
@@ -65,6 +63,12 @@ public class FolioReader : NSObject {
var isReaderOpen = false
var isReaderReady = false
+ private override init() {
+ let isMigrated = NSUserDefaults.standardUserDefaults().boolForKey("isMigrated")
+ if !isMigrated {
+ Highlight.migrateUserDataToRealm()
+ }
+ }
var nightMode: Bool {
get { return FolioReader.defaults.boolForKey(kNightMode) }
diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift
index 574e4b3f2..c29289dc6 100755
--- a/Source/FolioReaderPage.swift
+++ b/Source/FolioReaderPage.swift
@@ -122,7 +122,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
if highlights.count > 0 {
for item in highlights {
- let style = HighlightStyle.classForStyle(item.type.integerValue)
+ let style = HighlightStyle.classForStyle(item.type)
let tag = "\(item.content)"
let locator = item.contentPre + item.content + item.contentPost
let range: NSRange = html.rangeOfString(locator, options: .LiteralSearch)
@@ -551,6 +551,8 @@ extension UIWebView {
let json = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: []) as! NSArray
let dic = json.firstObject as! [String: String]
let rect = CGRectFromString(dic["rect"]!)
+ let startOffset = dic["startOffset"]!
+ let endOffset = dic["endOffset"]!
// Force remove text selection
userInteractionEnabled = false
@@ -561,8 +563,8 @@ extension UIWebView {
// Persist
let html = js("getHTML()")
- if let highlight = FRHighlight.matchHighlight(html, andId: dic["id"]!) {
- Highlight.persistHighlight(highlight, completion: nil)
+ if let highlight = FRHighlight.matchHighlight(html, andId: dic["id"]!, startOffset: startOffset, endOffset: endOffset) {
+ Highlight.persistHighlight(highlight)
}
} catch {
print("Could not receive JSON")
diff --git a/Source/Models/FolioReader.xcdatamodeld/FolioReader.xcdatamodel/contents b/Source/Models/FolioReader.xcdatamodeld/FolioReader.xcdatamodel/contents
index 044594f6a..c4c64b3b1 100644
--- a/Source/Models/FolioReader.xcdatamodeld/FolioReader.xcdatamodel/contents
+++ b/Source/Models/FolioReader.xcdatamodeld/FolioReader.xcdatamodel/contents
@@ -1,6 +1,6 @@
-
-
+
+
diff --git a/Source/Models/Highlight+Properties.swift b/Source/Models/Highlight+Properties.swift
index 4c11b72aa..767ff9026 100644
--- a/Source/Models/Highlight+Properties.swift
+++ b/Source/Models/Highlight+Properties.swift
@@ -7,17 +7,7 @@
//
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
+extension Highlight {
}
\ No newline at end of file
diff --git a/Source/Models/Highlight.swift b/Source/Models/Highlight.swift
index a76a3ab41..d67cb5301 100644
--- a/Source/Models/Highlight.swift
+++ b/Source/Models/Highlight.swift
@@ -7,105 +7,64 @@
//
import Foundation
+import RealmSwift
import CoreData
public typealias Completion = (error: NSError?) -> ()
-let coreDataManager = CoreDataManager()
-@objc(Highlight)
-public class Highlight: NSManagedObject {
+public class Highlight: Object {
- 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)
- }
+ dynamic var bookId:String!
+ dynamic var content:String!
+ dynamic var contentPost:String!
+ dynamic var contentPre:String!
+ dynamic var date:NSDate!
+ dynamic var highlightId:String!
+ dynamic var page:Int = 0
+ dynamic var type:Int = 0
+ dynamic var startOffset:Int = -1
+ dynamic var endOffset:Int = -1
+
+ override public class func primaryKey()-> String{
+ return "highlightId"
}
- public static func persistHighlight(object: FRHighlight, completion: Completion?) {
- var highlight: Highlight?
-
+ public static func persistHighlight(object: FRHighlight, completion: Completion? = nil) {
do {
- let fetchRequest = NSFetchRequest(entityName: "Highlight")
- fetchRequest.predicate = NSPredicate(format:"highlightId = %@", object.id)
- highlight = try coreDataManager.managedObjectContext.executeFetchRequest(fetchRequest).last as? Highlight
- } catch let error as NSError {
- print(error)
- highlight = nil
- }
-
- if highlight != nil {
- highlight!.content = object.content
- highlight!.contentPre = object.contentPre
- highlight!.contentPost = object.contentPost
- highlight!.date = object.date
- highlight!.type = object.type.hashValue
- } else {
- highlight = NSEntityDescription.insertNewObjectForEntityForName("Highlight", inManagedObjectContext: coreDataManager.managedObjectContext) as? Highlight
- coreDataManager.saveContext()
-
- highlight!.bookId = object.bookId
- highlight!.content = object.content
- highlight!.contentPre = object.contentPre
- highlight!.contentPost = object.contentPost
- highlight!.date = NSDate()
- highlight!.highlightId = object.id
- highlight!.page = object.page
- highlight!.type = object.type.hashValue
- }
-
- // Save
- do {
- try coreDataManager.managedObjectContext.save()
+ let realm = try! Realm()
+
+ let newHighlight = Highlight()
+ newHighlight.bookId = object.bookId
+ newHighlight.content = object.content
+ newHighlight.contentPre = object.contentPre
+ newHighlight.contentPost = object.contentPost
+ newHighlight.date = NSDate()
+ newHighlight.highlightId = object.id
+ newHighlight.page = object.page
+ newHighlight.type = object.type.hashValue
+ newHighlight.startOffset = object.startOffset
+ newHighlight.endOffset = object.endOffset
+
+ realm.beginWrite()
+ realm.add(newHighlight, update: true)
+ try! realm.commitWrite()
completion?(error: nil)
} catch let error as NSError {
+ print("Error on persist highlight: \(error)")
completion?(error: error)
}
}
public static func removeById(highlightId: String) {
var highlight: Highlight?
+ let predicate = NSPredicate(format:"highlightId = %@", highlightId)
do {
- let fetchRequest = NSFetchRequest(entityName: "Highlight")
- fetchRequest.predicate = NSPredicate(format:"highlightId = %@", highlightId)
-
- highlight = try coreDataManager.managedObjectContext.executeFetchRequest(fetchRequest).last as? Highlight
- coreDataManager.managedObjectContext.deleteObject(highlight!)
- coreDataManager.saveContext()
+ let realm = try! Realm()
+ highlight = realm.objects(Highlight).filter(predicate).toArray(Highlight).first
+ realm.beginWrite()
+ realm.delete(highlight!)
+ try! realm.commitWrite()
} catch let error as NSError {
print("Error on remove highlight: \(error)")
}
@@ -113,33 +72,66 @@ public class Highlight: NSManagedObject {
public static func updateById(highlightId: String, type: HighlightStyle) {
var highlight: Highlight?
-
+ let predicate = NSPredicate(format:"highlightId = %@", highlightId)
do {
- let fetchRequest = NSFetchRequest(entityName: "Highlight")
- fetchRequest.predicate = NSPredicate(format:"highlightId = %@", highlightId)
+ let realm = try! Realm()
+ highlight = realm.objects(Highlight).filter(predicate).toArray(Highlight).first
+ realm.beginWrite()
- highlight = try coreDataManager.managedObjectContext.executeFetchRequest(fetchRequest).last as? Highlight
highlight?.type = type.hashValue
- coreDataManager.saveContext()
+
+ try! realm.commitWrite()
} catch let error as NSError {
- print("Error on update highlight: \(error)")
+ print("Error on updateById : \(error)")
}
+
}
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)
+ let realm = try! Realm()
+ highlights = realm.objects(Highlight).filter(predicate).toArray(Highlight) ?? [Highlight]()
+ return highlights!
+ }
+
+ public static func migrateUserDataToRealm(){
+ var highlights: [NSManagedObject]?
+ let coreDataManager = CoreDataManager()
do {
let fetchRequest = NSFetchRequest(entityName: "Highlight")
let sorter: NSSortDescriptor = NSSortDescriptor(key: "date" , ascending: false)
- fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = [sorter]
- highlights = try coreDataManager.managedObjectContext.executeFetchRequest(fetchRequest) as? [Highlight]
- return highlights!
- } catch {
- return [Highlight]()
+ highlights = try coreDataManager.managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
+ let realm = try! Realm()
+
+ realm.beginWrite()
+ realm.deleteAll()
+ for oldHighlight in highlights! {
+ var newHighlight = Highlight()
+ newHighlight.bookId = oldHighlight.valueForKey("bookId") as! String
+ newHighlight.content = oldHighlight.valueForKey("content") as! String
+ newHighlight.contentPost = oldHighlight.valueForKey("contentPost") as! String
+ newHighlight.contentPre = oldHighlight.valueForKey("contentPre") as! String
+ newHighlight.date = oldHighlight.valueForKey("date") as! NSDate
+ newHighlight.highlightId = oldHighlight.valueForKey("highlightId") as! String
+ newHighlight.page = oldHighlight.valueForKey("page") as! Int
+ newHighlight.type = oldHighlight.valueForKey("type") as! Int
+
+ realm.add(newHighlight, update: true)
+ }
+ try! realm.commitWrite()
+ NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isMigrated")
+ } catch let error as NSError {
+ print("Error on migrateuserDataToRealm : \(error)")
}
}
+}
+
+extension Results {
+ func toArray(ofType: T.Type) -> [T] {
+ return flatMap { $0 as? T }
+ }
}
\ No newline at end of file
diff --git a/Source/Resources/Bridge.js b/Source/Resources/Bridge.js
index 7305882e0..0862a6d20 100755
--- a/Source/Resources/Bridge.js
+++ b/Source/Resources/Bridge.js
@@ -83,6 +83,8 @@ function setFontSize(cls) {
*/
function highlightString(style) {
var range = window.getSelection().getRangeAt(0);
+ var startOffset = range.startOffset;
+ var endOffset = range.endOffset;
var selectionContents = range.extractContents();
var elm = document.createElement("highlight");
var id = guid();
@@ -96,7 +98,7 @@ function highlightString(style) {
thisHighlight = elm;
var params = [];
- params.push({id: id, rect: getRectForSelectedText(elm)});
+ params.push({id: id, rect: getRectForSelectedText(elm), startOffset: startOffset.toString(), endOffset: endOffset.toString()});
return JSON.stringify(params);
}