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

Commit

Permalink
Add Note function (#335)
Browse files Browse the repository at this point in the history
* Made a new screen for taking notes and modified some other codes

* removed unnecessary chagnes

* commit missing file
  • Loading branch information
ngo275 authored and hebertialmeida committed May 24, 2018
1 parent 43d5c57 commit 51021b2
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 18 deletions.
4 changes: 4 additions & 0 deletions FolioReaderKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
1A9590131D397BE900D56699 /* ScrollScrubber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A958F901D397BE900D56699 /* ScrollScrubber.swift */; };
1A9590151D397C1300D56699 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1A9590141D397C1300D56699 /* Images.xcassets */; };
1A9590171D397CAF00D56699 /* FolioReaderKit.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 1A9590161D397CAF00D56699 /* FolioReaderKit.podspec */; };
256AE60B20A1D4DD00EAD746 /* FolioReaderAddHighlightNote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 256AE60A20A1D4DD00EAD746 /* FolioReaderAddHighlightNote.swift */; };
3A27F08C1FD9BE5800D84A57 /* FolioReaderUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A27F08A1FD9BE5800D84A57 /* FolioReaderUserDefaults.swift */; };
3A27F08E1FD9BE5800D84A57 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A27F08B1FD9BE5800D84A57 /* Extensions.swift */; };
3A27F09D1FD9C24A00D84A57 /* FRBook.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A958F051D397BE900D56699 /* FRBook.swift */; };
Expand Down Expand Up @@ -173,6 +174,7 @@
1A958F901D397BE900D56699 /* ScrollScrubber.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScrollScrubber.swift; sourceTree = "<group>"; };
1A9590141D397C1300D56699 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
1A9590161D397CAF00D56699 /* FolioReaderKit.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = FolioReaderKit.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
256AE60A20A1D4DD00EAD746 /* FolioReaderAddHighlightNote.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FolioReaderAddHighlightNote.swift; sourceTree = "<group>"; };
3A27F08A1FD9BE5800D84A57 /* FolioReaderUserDefaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FolioReaderUserDefaults.swift; sourceTree = "<group>"; };
3A27F08B1FD9BE5800D84A57 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
3AD5EEB81D9433C100E42810 /* FolioReaderWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FolioReaderWebView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -241,6 +243,7 @@
1A958F0F1D397BE900D56699 /* FolioReaderAudioPlayer.swift */,
1A958F101D397BE900D56699 /* FolioReaderCenter.swift */,
1A958F111D397BE900D56699 /* FolioReaderChapterList.swift */,
256AE60A20A1D4DD00EAD746 /* FolioReaderAddHighlightNote.swift */,
1A958F121D397BE900D56699 /* FolioReaderChapterListCell.swift */,
1A958F131D397BE900D56699 /* FolioReaderConfig.swift */,
1A958F141D397BE900D56699 /* FolioReaderContainer.swift */,
Expand Down Expand Up @@ -579,6 +582,7 @@
1A65DDD71DA7450A0033C277 /* FolioReaderQuoteShare.swift in Sources */,
3A27F08C1FD9BE5800D84A57 /* FolioReaderUserDefaults.swift in Sources */,
1A958FA21D397BE900D56699 /* FolioReaderHighlightList.swift in Sources */,
256AE60B20A1D4DD00EAD746 /* FolioReaderAddHighlightNote.swift in Sources */,
1A958FA71D397BE900D56699 /* FolioReaderPlayerMenu.swift in Sources */,
1A958F921D397BE900D56699 /* FREpubParser.swift in Sources */,
1A958F9C1D397BE900D56699 /* FolioReaderCenter.swift in Sources */,
Expand Down
209 changes: 209 additions & 0 deletions Source/FolioReaderAddHighlightNote.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//
// FolioReaderAddHighlightNote.swift
// FolioReaderKit
//
// Created by ShuichiNagao on 2018/05/06.
//

import UIKit
import RealmSwift

class FolioReaderAddHighlightNote: UIViewController {

var textView: UITextView!
var highlightLabel: UILabel!
var scrollView: UIScrollView!
var containerView = UIView()
var highlight: Highlight!
var highlightSaved = false
var isEditHighlight = false
var resizedTextView = false

private var folioReader: FolioReader
private var readerConfig: FolioReaderConfig

init(withHighlight highlight: Highlight, folioReader: FolioReader, readerConfig: FolioReaderConfig) {
self.folioReader = folioReader
self.highlight = highlight
self.readerConfig = readerConfig

super.init(nibName: nil, bundle: Bundle.frameworkBundle())
}

required init?(coder aDecoder: NSCoder) {
fatalError("storyboards are incompatible with truth and beauty")
}

// MARK: - life cycle methods

override func viewDidLoad() {
super.viewDidLoad()

setCloseButton(withConfiguration: readerConfig)
prepareScrollView()
configureTextView()
configureLabel()
configureNavBar()
configureKeyboardObserver()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

textView.becomeFirstResponder()
setNeedsStatusBarAppearanceUpdate()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

scrollView.frame = view.bounds
containerView.frame = view.bounds
scrollView.contentSize = view.bounds.size
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if !highlightSaved && !isEditHighlight {
guard let currentPage = folioReader.readerCenter?.currentPage else { return }
currentPage.webView?.js("removeThisHighlight()")
}
}

// MARK: - private methods

private func prepareScrollView(){
scrollView = UIScrollView()
scrollView.delegate = self as! UIScrollViewDelegate
scrollView.contentSize = CGSize.init(width: view.frame.width, height: view.frame.height )
scrollView.bounces = false

containerView = UIView()
containerView.backgroundColor = .white
scrollView.addSubview(containerView)
view.addSubview(scrollView)

let leftConstraint = NSLayoutConstraint(item: scrollView!, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0)
let rightConstraint = NSLayoutConstraint(item: scrollView!, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 0)
let topConstraint = NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
let botConstraint = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)

view.addConstraints([leftConstraint, rightConstraint, topConstraint, botConstraint])
}

private func configureTextView(){
textView = UITextView()
textView.delegate = self
textView.translatesAutoresizingMaskIntoConstraints = false
textView.textColor = .black
textView.font = UIFont.boldSystemFont(ofSize: 15)
containerView.addSubview(textView)

if isEditHighlight {
textView.text = highlight.noteForHighlight
}

let leftConstraint = NSLayoutConstraint(item: textView!, attribute: .left, relatedBy: .equal, toItem: containerView, attribute: .left, multiplier: 1.0, constant: 20)
let rightConstraint = NSLayoutConstraint(item: textView!, attribute: .right, relatedBy: .equal, toItem: containerView, attribute: .right, multiplier: 1.0, constant: -20)
let topConstraint = NSLayoutConstraint(item: textView, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 100)
let heiConstraint = NSLayoutConstraint(item: textView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: view.frame.height - 100)

containerView.addConstraints([leftConstraint, rightConstraint, topConstraint, heiConstraint])
}

private func configureLabel() {
highlightLabel = UILabel()
highlightLabel.translatesAutoresizingMaskIntoConstraints = false
highlightLabel.numberOfLines = 3
highlightLabel.font = UIFont.systemFont(ofSize: 15)
highlightLabel.text = highlight.content.stripHtml().truncate(250, trailing: "...").stripLineBreaks()

containerView.addSubview(self.highlightLabel!)

let leftConstraint = NSLayoutConstraint(item: highlightLabel!, attribute: .left, relatedBy: .equal, toItem: containerView, attribute: .left, multiplier: 1.0, constant: 20)
let rightConstraint = NSLayoutConstraint(item: highlightLabel!, attribute: .right, relatedBy: .equal, toItem: containerView, attribute: .right, multiplier: 1.0, constant: -20)
let topConstraint = NSLayoutConstraint(item: highlightLabel, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 20)
let heiConstraint = NSLayoutConstraint(item: highlightLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 70)

containerView.addConstraints([leftConstraint, rightConstraint, topConstraint, heiConstraint])
}

private func configureNavBar() {
let navBackground = folioReader.isNight(readerConfig.nightModeMenuBackground, UIColor.white)
let tintColor = readerConfig.tintColor
let navText = folioReader.isNight(UIColor.white, UIColor.black)
let font = UIFont(name: "Avenir-Light", size: 17)!
setTranslucentNavigation(false, color: navBackground, tintColor: tintColor, titleColor: navText, andFont: font)

let titleAttrs = [NSAttributedStringKey.foregroundColor: readerConfig.tintColor]
let saveButton = UIBarButtonItem(title: readerConfig.localizedSave, style: .plain, target: self, action: #selector(saveNote(_:)))
saveButton.setTitleTextAttributes(titleAttrs, for: UIControlState())
navigationItem.rightBarButtonItem = saveButton
}

private func configureKeyboardObserver() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc private func keyboardWillShow(notification: NSNotification){
//give room at the bottom of the scroll view, so it doesn't cover up anything the user needs to tap
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)

var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = keyboardFrame.size.height
self.scrollView.contentInset = contentInset
}

@objc private func keyboardWillHide(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
self.scrollView.contentInset = contentInset
}

@objc private func saveNote(_ sender: UIBarButtonItem) {
if !textView.text.isEmpty {
if isEditHighlight {
let realm = try! Realm(configuration: readerConfig.realmConfiguration)
realm.beginWrite()
highlight.noteForHighlight = textView.text
highlightSaved = true
try! realm.commitWrite()
} else {
highlight.noteForHighlight = textView.text
highlight.persist(withConfiguration: readerConfig)
highlightSaved = true
}
}

dismiss()
}
}

// MARK: - UITextViewDelegate
extension FolioReaderAddHighlightNote: UITextViewDelegate {

func textViewDidChange(_ textView: UITextView) {
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height + 15)
textView.frame = newFrame;
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.frame.size.height = textView.frame.height + 30

if resizedTextView {
scrollView.scrollRectToVisible(textView.frame, animated: true)
}
else{
resizedTextView = true
}

return true
}
}
12 changes: 12 additions & 0 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,18 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
}
present(nav, animated: true, completion: nil)
}

/**
Present add highlight note
*/
func presentAddHighlightNote(_ highlight: Highlight, edit: Bool) {
let addHighlightView = FolioReaderAddHighlightNote(withHighlight: highlight, folioReader: folioReader, readerConfig: readerConfig)
addHighlightView.isEditHighlight = edit
let nav = UINavigationController(rootViewController: addHighlightView)
nav.modalPresentationStyle = .formSheet

present(nav, animated: true, completion: nil)
}
}

// MARK: FolioPageDelegate
Expand Down
4 changes: 3 additions & 1 deletion Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ open class FolioReaderConfig: NSObject {
// MARK: Realm

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

// MARK: Localized strings

Expand Down Expand Up @@ -207,6 +207,8 @@ open class FolioReaderConfig: NSObject {
open var localizedTakePhoto = NSLocalizedString("Take Photo", comment: "")
open var localizedShareImageQuote = NSLocalizedString("Share image quote", comment: "")
open var localizedShareTextQuote = NSLocalizedString("Share text quote", comment: "")
open var localizedSave = NSLocalizedString("Save", comment: "")
open var localizedHighlightNote = NSLocalizedString("Note", comment: "")

public convenience init(withIdentifier identifier: String) {
self.init()
Expand Down
52 changes: 44 additions & 8 deletions Source/FolioReaderHighlightList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FolioReaderHighlightList: UITableViewController {
let cell = tableView.dequeueReusableCell(withIdentifier: kReuseCellIdentifier, for: indexPath)
cell.backgroundColor = UIColor.clear

let highlight = highlights[(indexPath as NSIndexPath).row]
let highlight = highlights[indexPath.row]

// Format date
let dateFormatter = DateFormatter()
Expand Down Expand Up @@ -114,14 +114,37 @@ class FolioReaderHighlightList: UITableViewController {
highlightLabel.attributedText = text
highlightLabel.sizeToFit()
highlightLabel.frame = CGRect(x: 20, y: 46, width: view.frame.width-40, height: highlightLabel.frame.height)

// Note text if it exists
if let note = highlight.noteForHighlight {
var noteLabel: UILabel!
if cell.contentView.viewWithTag(789) == nil {
noteLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width-40, height: 0))
noteLabel.tag = 789
noteLabel.font = UIFont.systemFont(ofSize: 14)
noteLabel.autoresizingMask = UIViewAutoresizing.flexibleWidth
noteLabel.numberOfLines = 3
noteLabel.textColor = UIColor.gray
cell.contentView.addSubview(noteLabel)
} else {
noteLabel = cell.contentView.viewWithTag(789) as! UILabel
}

noteLabel.text = note
noteLabel.sizeToFit()
noteLabel.frame = CGRect(x: 20, y: 46 + highlightLabel.frame.height + 10, width: view.frame.width-40, height: noteLabel.frame.height)
} else {
cell.contentView.viewWithTag(789)?.removeFromSuperview()
}

cell.layoutMargins = UIEdgeInsets.zero
cell.preservesSuperviewLayoutMargins = false

return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let highlight = highlights[(indexPath as NSIndexPath).row]
let highlight = highlights[indexPath.row]

let cleanString = highlight.content.stripHtml().truncate(250, trailing: "...").stripLineBreaks()
let text = NSMutableAttributedString(string: cleanString)
Expand All @@ -135,35 +158,48 @@ class FolioReaderHighlightList: UITableViewController {
options: [NSStringDrawingOptions.usesLineFragmentOrigin, NSStringDrawingOptions.usesFontLeading],
context: nil)

return s.size.height + 66
var totalHeight = s.size.height + 66

if let note = highlight.noteForHighlight {
let noteLabel = UILabel()
noteLabel.frame = CGRect(x: 20, y: 46 , width: view.frame.width-40, height: CGFloat.greatestFiniteMagnitude)
noteLabel.text = note
noteLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
noteLabel.numberOfLines = 0
noteLabel.font = UIFont.systemFont(ofSize: 14)

noteLabel.sizeToFit()
totalHeight += noteLabel.frame.height
}

return totalHeight
}

// MARK: - Table view delegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let highlight = highlights[safe: (indexPath as NSIndexPath).row] else {
return
}
guard let highlight = highlights[safe: indexPath.row] else { return }

self.folioReader.readerCenter?.changePageWith(page: highlight.page, andFragment: highlight.highlightId)
self.dismiss()
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let highlight = highlights[(indexPath as NSIndexPath).row]
guard let highlight = highlights[safe: indexPath.row] else { return }

if (highlight.page == self.folioReader.readerCenter?.currentPageNumber),
let page = self.folioReader.readerCenter?.currentPage {
Highlight.removeFromHTMLById(withinPage: page, highlightId: highlight.highlightId) // Remove from HTML
}

highlight.remove(withConfiguration: self.readerConfig) // Remove from Database
highlights.remove(at: (indexPath as NSIndexPath).row)
highlights.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}


// MARK: - Handle rotation transition

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
Expand Down
Loading

0 comments on commit 51021b2

Please sign in to comment.