<commit_before>//
//  FolioReaderKit.swift
//  FolioReaderKit
//
//  Created by <NAME> on 08/04/15.
//  Copyright (c) 2015 Folio Reader. All rights reserved.
//

import Foundation
import UIKit

// MARK: - Internal constants for devices

internal let isPad = UIDevice.currentDevice().userInterfaceIdiom == .Pad
internal let isPhone = UIDevice.currentDevice().userInterfaceIdiom == .Phone

// MARK: - Internal constants

internal let kApplicationDocumentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
internal let kCurrentFontFamily = "com.folioreader.kCurrentFontFamily"
internal let kCurrentFontSize = "com.folioreader.kCurrentFontSize"
internal let kCurrentAudioRate = "com.folioreader.kCurrentAudioRate"
internal let kCurrentHighlightStyle = "com.folioreader.kCurrentHighlightStyle"
internal var kCurrentMediaOverlayStyle = "com.folioreader.kMediaOverlayStyle"
internal let kNightMode = "com.folioreader.kNightMode"
internal let kCurrentTOCMenu = "com.folioreader.kCurrentTOCMenu"
internal let kMigratedToRealm = "com.folioreader.kMigratedToRealm"
internal let kHighlightRange = 30
internal var kBookId: String!

/**
 Defines the media overlay and TTS selection
 
 - Default:   The background is colored
 - Underline: The underlined is colored
 - TextColor: The text is colored
 */
enum MediaOverlayStyle: Int {
    case Default
    case Underline
    case TextColor
    
    init () {
        self = .Default
    }
    
    func className() -> String {
        return "mediaOverlayStyle\(self.rawValue)"
    }
}

/**
*  Main Library class with some useful constants and methods
*/
public class FolioReader : NSObject {
    static let sharedInstance = FolioReader()
    static let defaults = NSUserDefaults.standardUserDefaults()
    weak var readerCenter: FolioReaderCenter!
    weak var readerContainer: FolioReaderBaseContainer!
    weak var readerAudioPlayer: FolioReaderAudioPlayer!
    var isReaderOpen = false
    var isReaderReady = false
    
    private override init() {
        let isMigrated = FolioReader.defaults.boolForKey(kMigratedToRealm)
        if !isMigrated {
            Highlight.migrateUserDataToRealm()
        }
    }
    
    var nightMode: Bool {
        get { return FolioReader.defaults.boolForKey(kNightMode) }
        set (value) {
            FolioReader.defaults.setBool(value, forKey: kNightMode)
        }
    }
    var currentFontName: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentFontFamily) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentFontFamily)
        }
    }
    
    var currentFontSize: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentFontSize) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentFontSize)
        }
    }
    
    var currentAudioRate: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentAudioRate) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentAudioRate)
        }
    }

    var currentHighlightStyle: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentHighlightStyle) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentHighlightStyle)
        }
    }
    
    var currentMediaOverlayStyle: MediaOverlayStyle {
        get { return MediaOverlayStyle(rawValue: FolioReader.defaults.valueForKey(kCurrentMediaOverlayStyle) as! Int)! }
        set (value) {
            FolioReader.defaults.setValue(value.rawValue, forKey: kCurrentMediaOverlayStyle)
        }
    }
    
    // MARK: - Get Cover Image
    
    /**
     Read Cover Image and Return an IUImage
     */
    
    public class func getCoverImage(epubPath: String) -> UIImage? {
        return FREpubParser().parseCoverImage(epubPath)
    }

    // MARK: - Present Folio Reader
    
    /**
     Present a Folio Reader for a Parent View Controller.
    */
    public class func presentReader(parentViewController parentViewController: UIViewController, withEpubPath epubPath: String, andConfig config: FolioReaderConfig, shouldRemoveEpub: Bool = true, animated: Bool = true) {
        let reader = FolioReaderContainer(config: config, epubPath: epubPath, removeEpub: shouldRemoveEpub)
        FolioReader.sharedInstance.readerContainer = reader
        parentViewController.presentViewController(reader, animated: animated, completion: nil)
    }
    
    public class func pushReader(parentViewController parentViewController: UIViewController, withEpubPath epubPath: String, andConfig config: FolioReaderConfig, shouldRemoveEpub: Bool = true, animated: Bool = true) {
        let reader = FolioReaderContainer(config: config, epubPath: epubPath, removeEpub: shouldRemoveEpub)
        FolioReader.sharedInstance.readerContainer = reader
        parentViewController.navigationController?.setNavigationBarHidden(true, animated: false)
        parentViewController.navigationController?.pushViewController(reader, animated: true)
    }
    
    public class func presentReader(customReader: FolioReaderBaseContainer, parentViewController: UIViewController, animated: Bool = true) {
        FolioReader.sharedInstance.readerContainer = customReader
        parentViewController.presentViewController(customReader, animated: animated, completion: nil)
    }
    
    public class func pushReader(customReader: FolioReaderBaseContainer, parentViewController: UIViewController, animated: Bool = true) {
        FolioReader.sharedInstance.readerContainer = customReader
        parentViewController.navigationController?.setNavigationBarHidden(true, animated: false)
        parentViewController.navigationController?.pushViewController(customReader, animated: true)
    }
    
    
    <commit_msg>Adds optional completion function to present/push default and custom readers<commit_after>//
//  FolioReaderKit.swift
//  FolioReaderKit
//
//  Created by <NAME> on 08/04/15.
//  Copyright (c) 2015 Folio Reader. All rights reserved.
//

import Foundation
import UIKit

// MARK: - Internal constants for devices

internal let isPad = UIDevice.currentDevice().userInterfaceIdiom == .Pad
internal let isPhone = UIDevice.currentDevice().userInterfaceIdiom == .Phone

// MARK: - Internal constants

internal let kApplicationDocumentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
internal let kCurrentFontFamily = "com.folioreader.kCurrentFontFamily"
internal let kCurrentFontSize = "com.folioreader.kCurrentFontSize"
internal let kCurrentAudioRate = "com.folioreader.kCurrentAudioRate"
internal let kCurrentHighlightStyle = "com.folioreader.kCurrentHighlightStyle"
internal var kCurrentMediaOverlayStyle = "com.folioreader.kMediaOverlayStyle"
internal let kNightMode = "com.folioreader.kNightMode"
internal let kCurrentTOCMenu = "com.folioreader.kCurrentTOCMenu"
internal let kMigratedToRealm = "com.folioreader.kMigratedToRealm"
internal let kHighlightRange = 30
internal var kBookId: String!

/**
 Defines the media overlay and TTS selection
 
 - Default:   The background is colored
 - Underline: The underlined is colored
 - TextColor: The text is colored
 */
enum MediaOverlayStyle: Int {
    case Default
    case Underline
    case TextColor
    
    init () {
        self = .Default
    }
    
    func className() -> String {
        return "mediaOverlayStyle\(self.rawValue)"
    }
}

/**
*  Main Library class with some useful constants and methods
*/
public class FolioReader : NSObject {
    static let sharedInstance = FolioReader()
    static let defaults = NSUserDefaults.standardUserDefaults()
    weak var readerCenter: FolioReaderCenter!
    weak var readerContainer: FolioReaderBaseContainer!
    weak var readerAudioPlayer: FolioReaderAudioPlayer!
    var isReaderOpen = false
    var isReaderReady = false
    
    private override init() {
        let isMigrated = FolioReader.defaults.boolForKey(kMigratedToRealm)
        if !isMigrated {
            Highlight.migrateUserDataToRealm()
        }
    }
    
    var nightMode: Bool {
        get { return FolioReader.defaults.boolForKey(kNightMode) }
        set (value) {
            FolioReader.defaults.setBool(value, forKey: kNightMode)
        }
    }
    var currentFontName: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentFontFamily) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentFontFamily)
        }
    }
    
    var currentFontSize: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentFontSize) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentFontSize)
        }
    }
    
    var currentAudioRate: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentAudioRate) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentAudioRate)
        }
    }

    var currentHighlightStyle: Int {
        get { return FolioReader.defaults.valueForKey(kCurrentHighlightStyle) as! Int }
        set (value) {
            FolioReader.defaults.setValue(value, forKey: kCurrentHighlightStyle)
        }
    }
    
    var currentMediaOverlayStyle: MediaOverlayStyle {
        get { return MediaOverlayStyle(rawValue: FolioReader.defaults.valueForKey(kCurrentMediaOverlayStyle) as! Int)! }
        set (value) {
            FolioReader.defaults.setValue(value.rawValue, forKey: kCurrentMediaOverlayStyle)
        }
    }
    
    // MARK: - Get Cover Image
    
    /**
     Read Cover Image and Return an IUImage
     */
    
    public class func getCoverImage(epubPath: String) -> UIImage? {
        return FREpubParser().parseCoverImage(epubPath)
    }

    // MARK: - Present Folio Reader
    
    /**
     Present a Folio Reader for a Parent View Controller.
    */
    public class func presentReader(parentViewController parentViewController: UIViewController, withEpubPath epubPath: String, andConfig config: FolioReaderConfig, shouldRemoveEpub: Bool = true, animated: Bool = true, completion: (() -> Void)? = nil) {
        let reader = FolioReaderContainer(config: config, epubPath: epubPath, removeEpub: shouldRemoveEpub)
        FolioReader.sharedInstance.readerContainer = reader
        parentViewController.presentViewController(reader, animated: animated, completion: completion)
    }
    
    /**
     Push a Folio Reader into the `parentViewController` navigation controller.
     */
    public class func pushReader(parentViewController parentViewController: UIViewController, withEpubPath epubPath: String, andConfig config: FolioReaderConfig, shouldRemoveEpub: Bool = true, animated: Bool = true) {
        let reader = FolioReaderContainer(config: config, epubPath: epubPath, removeEpub: shouldRemoveEpub)
        FolioReader.sharedInstance.readerContainer = reader
        parentViewController.navigationController?.setNavigationBarHidden(true, animated: false)
        parentViewController.navigationController?.pushViewController(reader, animated: true)
    }
    
    /**
     Present a custom Folio Reader for a Parent View Controller.
     */
    public class func presentReader(customReader: FolioReaderBaseContainer, parentViewController: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
        FolioReader.sharedInstance.readerContainer = customReader
        parentViewController.presentViewController(customReader, animated: animated, completion: completion)
    }
    
    /**
     Push a custom Folio Reader into the `parentViewController` navigation controller.
     */
    public class func pushReader(customReader: FolioReaderBaseContainer, parentViewController: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
        FolioReader.sharedInstance.readerContainer = customReader
        parentViewController.navigationController?.setNavigationBarHidden(true, animated: false)
        parentViewController.navigationController?.pushViewController(customReader, animated: true)
        completion?()
    }
    
    
    
{'fraction_non_alphanumeric': 0.07847976307996052, 'fraction_numerical': 0.0021388614675880223, 'mean_word_length': 3.4942698706099815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6785286', 'n_tokens_mistral': 3367, 'n_tokens_neox': 3286, 'n_words': 812}