NCViewerPDFSearch.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // NCViewerPDFSearch.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/03/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import PDFKit
  25. @objc protocol NCViewerPDFSearchDelegate : class {
  26. func searchPdfSelection(_ pdfSelection: PDFSelection)
  27. }
  28. class NCViewerPDFSearch: UITableViewController, UISearchBarDelegate, PDFDocumentDelegate {
  29. var searchBar = UISearchBar()
  30. var pdfDocument: PDFDocument?
  31. var searchResultArray = [PDFSelection]()
  32. weak var delegate: NCViewerPDFSearchDelegate?
  33. //MARK: - LifeCycle
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. searchBar.delegate = self
  37. searchBar.sizeToFit()
  38. searchBar.searchBarStyle = .minimal
  39. navigationItem.titleView = searchBar
  40. tableView.dataSource = self
  41. tableView.delegate = self
  42. tableView.register(UINib.init(nibName: "NCViewerPDFSearchCell", bundle: nil), forCellReuseIdentifier: "Cell")
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. super.viewWillAppear(animated)
  46. searchBar.becomeFirstResponder()
  47. }
  48. //MARK: - UITableView DataSource / Delegate
  49. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  50. return 82
  51. }
  52. override func numberOfSections(in tableView: UITableView) -> Int {
  53. return 1
  54. }
  55. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  56. return self.searchResultArray.count
  57. }
  58. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  59. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NCViewerPDFSearchCell
  60. let pdfSelection = searchResultArray[indexPath.row] as PDFSelection
  61. //if let pdfOutline = pdfDocument?.outlineItem(for: pdfSelection) {
  62. // cell.outlineLabel.text = pdfOutline.label
  63. //}
  64. let pdfPage = pdfSelection.pages.first
  65. cell.pageNumberLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + ": " + (pdfPage?.label ?? "")
  66. let extendSelection = pdfSelection.copy() as! PDFSelection
  67. extendSelection.extend(atStart: 10)
  68. extendSelection.extend(atEnd: 90)
  69. extendSelection.extendForLineBoundaries()
  70. let nsRange = NSString(string: extendSelection.string!).range(of: pdfSelection.string!, options: String.CompareOptions.caseInsensitive)
  71. if nsRange.location != NSNotFound {
  72. let attributedSubString = NSAttributedString.init(string: NSString(string: extendSelection.string!).substring(with: nsRange), attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17)])
  73. let attributedString = NSMutableAttributedString.init(string: extendSelection.string!)
  74. attributedString.replaceCharacters(in: nsRange, with: attributedSubString)
  75. cell.searchResultTextLabel.attributedText = attributedString
  76. }
  77. return cell
  78. }
  79. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  80. let pdfSelection = searchResultArray[indexPath.row]
  81. delegate?.searchPdfSelection(pdfSelection)
  82. dismiss(animated: true)
  83. }
  84. //MARK: - PDFSelection Delegate
  85. func didMatchString(_ instance: PDFSelection) {
  86. searchResultArray.append(instance)
  87. tableView.reloadData()
  88. }
  89. //MARK: - UIScrollView Delegate
  90. override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  91. searchBar.resignFirstResponder()
  92. }
  93. //MARK: - UISearchBarDelegate
  94. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  95. searchBar.resignFirstResponder()
  96. }
  97. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  98. pdfDocument?.cancelFindString()
  99. navigationItem.setRightBarButton(nil, animated: false)
  100. dismiss(animated: true)
  101. }
  102. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  103. if searchText.count < 2 { return }
  104. searchResultArray.removeAll()
  105. tableView.reloadData()
  106. pdfDocument?.cancelFindString()
  107. pdfDocument?.delegate = self
  108. pdfDocument?.beginFindString(searchText, withOptions: .caseInsensitive)
  109. }
  110. func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
  111. let cancelBarButtonItem = UIBarButtonItem.init(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, target: self, action: #selector(cancelBarButtonItemClicked))
  112. navigationItem.setRightBarButton(cancelBarButtonItem, animated: true)
  113. return true
  114. }
  115. @objc func cancelBarButtonItemClicked() {
  116. searchBarCancelButtonClicked(searchBar)
  117. }
  118. }
  119. class NCViewerPDFSearchCell: UITableViewCell {
  120. @IBOutlet weak var outlineLabel: UILabel!
  121. @IBOutlet weak var pageNumberLabel: UILabel!
  122. @IBOutlet weak var searchResultTextLabel: UILabel!
  123. override func awakeFromNib() {
  124. super.awakeFromNib()
  125. }
  126. }