2 // FolioReaderHighlightList.swift
5 // Created by Heberti Almeida on 01/09/15.
6 // Copyright (c) 2015 Folio Reader. All rights reserved.
11 class FolioReaderHighlightList: UITableViewController {
13 fileprivate var highlights = [Highlight]()
14 fileprivate var readerConfig: FolioReaderConfig
15 fileprivate var folioReader: FolioReader
17 init(folioReader: FolioReader, readerConfig: FolioReaderConfig) {
18 self.readerConfig = readerConfig
19 self.folioReader = folioReader
21 super.init(style: UITableViewStyle.plain)
24 required init?(coder aDecoder: NSCoder) {
25 fatalError("init with coder not supported")
28 override func viewDidLoad() {
31 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: kReuseCellIdentifier)
32 self.tableView.separatorInset = UIEdgeInsets.zero
33 self.tableView.backgroundColor = self.folioReader.isNight(self.readerConfig.nightModeMenuBackground, self.readerConfig.menuBackgroundColor)
34 self.tableView.separatorColor = self.folioReader.isNight(self.readerConfig.nightModeSeparatorColor, self.readerConfig.menuSeparatorColor)
36 guard let bookId = (self.folioReader.readerContainer?.book.name as NSString?)?.deletingPathExtension else {
41 self.highlights = Highlight.allByBookId(withConfiguration: self.readerConfig, bookId: bookId)
44 // MARK: - Table view data source
46 override func numberOfSections(in tableView: UITableView) -> Int {
50 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
51 return highlights.count
54 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
55 let cell = tableView.dequeueReusableCell(withIdentifier: kReuseCellIdentifier, for: indexPath)
56 cell.backgroundColor = UIColor.clear
58 let highlight = highlights[(indexPath as NSIndexPath).row]
61 let dateFormatter = DateFormatter()
62 dateFormatter.dateFormat = self.readerConfig.localizedHighlightsDateFormat
63 let dateString = dateFormatter.string(from: highlight.date)
66 var dateLabel: UILabel!
67 if cell.contentView.viewWithTag(456) == nil {
68 dateLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width-40, height: 16))
70 dateLabel.autoresizingMask = UIViewAutoresizing.flexibleWidth
71 dateLabel.font = UIFont(name: "Avenir-Medium", size: 12)
72 cell.contentView.addSubview(dateLabel)
74 dateLabel = cell.contentView.viewWithTag(456) as! UILabel
77 dateLabel.text = dateString.uppercased()
78 dateLabel.textColor = self.folioReader.isNight(UIColor(white: 5, alpha: 0.3), UIColor.lightGray)
79 dateLabel.frame = CGRect(x: 20, y: 20, width: view.frame.width-40, height: dateLabel.frame.height)
82 let cleanString = highlight.content.stripHtml().truncate(250, trailing: "...").stripLineBreaks()
83 let text = NSMutableAttributedString(string: cleanString)
84 let range = NSRange(location: 0, length: text.length)
85 let paragraph = NSMutableParagraphStyle()
86 paragraph.lineSpacing = 3
87 let textColor = self.folioReader.isNight(self.readerConfig.menuTextColor, UIColor.black)
89 text.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: range)
90 text.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "Avenir-Light", size: 16)!, range: range)
91 text.addAttribute(NSAttributedStringKey.foregroundColor, value: textColor, range: range)
93 if (highlight.type == HighlightStyle.underline.rawValue) {
94 text.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.clear, range: range)
95 text.addAttribute(NSAttributedStringKey.underlineColor, value: HighlightStyle.colorForStyle(highlight.type, nightMode: self.folioReader.nightMode), range: range)
96 text.addAttribute(NSAttributedStringKey.underlineStyle, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), range: range)
98 text.addAttribute(NSAttributedStringKey.backgroundColor, value: HighlightStyle.colorForStyle(highlight.type, nightMode: self.folioReader.nightMode), range: range)
102 var highlightLabel: UILabel!
103 if cell.contentView.viewWithTag(123) == nil {
104 highlightLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width-40, height: 0))
105 highlightLabel.tag = 123
106 highlightLabel.autoresizingMask = UIViewAutoresizing.flexibleWidth
107 highlightLabel.numberOfLines = 0
108 highlightLabel.textColor = UIColor.black
109 cell.contentView.addSubview(highlightLabel)
111 highlightLabel = cell.contentView.viewWithTag(123) as! UILabel
114 highlightLabel.attributedText = text
115 highlightLabel.sizeToFit()
116 highlightLabel.frame = CGRect(x: 20, y: 46, width: view.frame.width-40, height: highlightLabel.frame.height)
118 cell.layoutMargins = UIEdgeInsets.zero
119 cell.preservesSuperviewLayoutMargins = false
123 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
124 let highlight = highlights[(indexPath as NSIndexPath).row]
126 let cleanString = highlight.content.stripHtml().truncate(250, trailing: "...").stripLineBreaks()
127 let text = NSMutableAttributedString(string: cleanString)
128 let range = NSRange(location: 0, length: text.length)
129 let paragraph = NSMutableParagraphStyle()
130 paragraph.lineSpacing = 3
131 text.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: range)
132 text.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "Avenir-Light", size: 16)!, range: range)
134 let s = text.boundingRect(with: CGSize(width: view.frame.width-40, height: CGFloat.greatestFiniteMagnitude),
135 options: [NSStringDrawingOptions.usesLineFragmentOrigin, NSStringDrawingOptions.usesFontLeading],
138 return s.size.height + 66
141 // MARK: - Table view delegate
143 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
144 guard let highlight = highlights[safe: (indexPath as NSIndexPath).row] else {
148 self.folioReader.readerCenter?.changePageWith(page: highlight.page, andFragment: highlight.highlightId)
152 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
153 if editingStyle == .delete {
154 let highlight = highlights[(indexPath as NSIndexPath).row]
156 if (highlight.page == self.folioReader.readerCenter?.currentPageNumber),
157 let page = self.folioReader.readerCenter?.currentPage {
158 Highlight.removeFromHTMLById(withinPage: page, highlightId: highlight.highlightId) // Remove from HTML
161 highlight.remove(withConfiguration: self.readerConfig) // Remove from Database
162 highlights.remove(at: (indexPath as NSIndexPath).row)
163 tableView.deleteRows(at: [indexPath], with: .fade)
167 // MARK: - Handle rotation transition
169 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
170 tableView.reloadData()