2 // FolioReaderChapterList.swift
5 // Created by Heberti Almeida on 15/04/15.
6 // Copyright (c) 2015 Folio Reader. All rights reserved.
11 /// Table Of Contents delegate
12 @objc protocol FolioReaderChapterListDelegate: class {
14 Notifies when the user selected some item on menu.
16 func chapterList(_ chapterList: FolioReaderChapterList, didSelectRowAtIndexPath indexPath: IndexPath, withTocReference reference: FRTocReference)
19 Notifies when chapter list did totally dismissed.
21 func chapterList(didDismissedChapterList chapterList: FolioReaderChapterList)
24 class FolioReaderChapterList: UITableViewController {
26 weak var delegate: FolioReaderChapterListDelegate?
27 fileprivate var tocItems = [FRTocReference]()
28 fileprivate var book: FRBook
29 fileprivate var readerConfig: FolioReaderConfig
30 fileprivate var folioReader: FolioReader
32 init(folioReader: FolioReader, readerConfig: FolioReaderConfig, book: FRBook, delegate: FolioReaderChapterListDelegate?) {
33 self.readerConfig = readerConfig
34 self.folioReader = folioReader
35 self.delegate = delegate
38 super.init(style: UITableViewStyle.plain)
41 required init?(coder aDecoder: NSCoder) {
42 fatalError("init with coder not supported")
45 override func viewDidLoad() {
48 // Register cell classes
49 self.tableView.register(FolioReaderChapterListCell.self, forCellReuseIdentifier: kReuseCellIdentifier)
50 self.tableView.separatorInset = UIEdgeInsets.zero
51 self.tableView.backgroundColor = self.folioReader.isNight(self.readerConfig.nightModeMenuBackground, self.readerConfig.menuBackgroundColor)
52 self.tableView.separatorColor = self.folioReader.isNight(self.readerConfig.nightModeSeparatorColor, self.readerConfig.menuSeparatorColor)
54 self.tableView.rowHeight = UITableViewAutomaticDimension
55 self.tableView.estimatedRowHeight = 50
58 self.tocItems = self.book.flatTableOfContents
61 // MARK: - Table view data source
63 override func numberOfSections(in tableView: UITableView) -> Int {
67 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
71 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
72 let cell = tableView.dequeueReusableCell(withIdentifier: kReuseCellIdentifier, for: indexPath) as! FolioReaderChapterListCell
74 cell.setup(withConfiguration: self.readerConfig)
75 let tocReference = tocItems[(indexPath as NSIndexPath).row]
76 let isSection = tocReference.children.count > 0
78 cell.indexLabel?.text = tocReference.title.trimmingCharacters(in: .whitespacesAndNewlines)
80 // Add audio duration for Media Ovelay
81 if let resource = tocReference.resource {
82 if let mediaOverlay = resource.mediaOverlay {
83 let duration = self.book.duration(for: "#"+mediaOverlay)
85 if let durationFormatted = (duration != nil ? duration : "")?.clockTimeToMinutesString() {
86 let text = cell.indexLabel?.text ?? ""
87 cell.indexLabel?.text = text + (duration != nil ? (" - " + durationFormatted) : "")
92 // Mark current reading chapter
94 let currentPageNumber = self.folioReader.readerCenter?.currentPageNumber,
95 let reference = self.book.spine.spineReferences[safe: currentPageNumber - 1],
96 (tocReference.resource != nil) {
97 let resource = reference.resource
98 cell.indexLabel?.textColor = (tocReference.resource == resource ? self.readerConfig.tintColor : self.readerConfig.menuTextColor)
101 cell.layoutMargins = UIEdgeInsets.zero
102 cell.preservesSuperviewLayoutMargins = false
103 cell.contentView.backgroundColor = isSection ? UIColor(white: 0.7, alpha: 0.1) : UIColor.clear
104 cell.backgroundColor = UIColor.clear
108 // MARK: - Table view delegate
110 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
111 let tocReference = tocItems[(indexPath as NSIndexPath).row]
112 delegate?.chapterList(self, didSelectRowAtIndexPath: indexPath, withTocReference: tocReference)
114 tableView.deselectRow(at: indexPath, animated: true)
116 self.delegate?.chapterList(didDismissedChapterList: self)