Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Add possibility to specify custom Realm Configuration #204

Merged
merged 1 commit into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ViewController: UIViewController {
// config.menuTextColor = UIColor.brownColor()
// config.menuBackgroundColor = UIColor.lightGrayColor()
// config.hidePageIndicator = true
// config.realmConfiguration = Realm.Configuration(fileURL: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("highlights.realm"))

// Custom sharing quote background
let customImageQuote = QuoteImage(withImage: UIImage(named: "demo-bg")!, alpha: 0.6, backgroundColor: UIColor.black)
Expand Down
9 changes: 8 additions & 1 deletion Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit

import RealmSwift

// MARK: - FolioReaderScrollDirection

/**
Expand Down Expand Up @@ -162,7 +164,12 @@ open class FolioReaderConfig: NSObject {

/// Enable or disable default Quote Image backgrounds
open var quotePreserveDefaultBackgrounds = true


// MARK: Realm

/// Realm configuration for storing highlights
open var realmConfiguration = Realm.Configuration()

// MARK: Localized strings

/// Localizes Highlight title
Expand Down
12 changes: 6 additions & 6 deletions Source/Models/Highlight+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extension Highlight {
*/
public func persist(_ completion: Completion? = nil) {
do {
let realm = try! Realm()
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
realm.beginWrite()
realm.add(self, update: true)
try realm.commitWrite()
Expand All @@ -110,7 +110,7 @@ extension Highlight {
*/
public func remove() {
do {
let realm = try! Realm()
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
realm.beginWrite()
realm.delete(self)
try realm.commitWrite()
Expand All @@ -128,7 +128,7 @@ extension Highlight {
var highlight: Highlight?
let predicate = NSPredicate(format:"highlightId = %@", highlightId)

let realm = try! Realm()
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
highlight = realm.objects(Highlight.self).filter(predicate).toArray(Highlight.self).first
highlight?.remove()
}
Expand All @@ -143,7 +143,7 @@ extension Highlight {
var highlight: Highlight?
let predicate = NSPredicate(format:"highlightId = %@", highlightId)
do {
let realm = try! Realm()
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
highlight = realm.objects(Highlight.self).filter(predicate).toArray(Highlight.self).first
realm.beginWrite()

Expand All @@ -167,7 +167,7 @@ extension 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)
let realm = try! Realm()
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
highlights = realm.objects(Highlight.self).filter(predicate).toArray(Highlight.self)
return highlights!
}
Expand All @@ -179,7 +179,7 @@ extension Highlight {
*/
public static func all() -> [Highlight] {
var highlights: [Highlight]?
let realm = try! Realm()
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
highlights = realm.objects(Highlight.self).toArray(Highlight.self)
return highlights!
}
Expand Down