1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
//
// NCViewerPDF.swift
// Nextcloud
//
// Created by Marino Faggiana on 06/02/2020.
// Copyright © 2020 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import PDFKit
import EasyTipView
class NCViewerPDF: UIViewController, NCViewerPDFSearchDelegate {
var metadata = tableMetadata()
var imageIcon: UIImage?
private var filePath = ""
private var pdfView = PDFView()
private var pdfThumbnailScrollView = UIScrollView()
private var pdfThumbnailView = PDFThumbnailView()
private var pdfDocument: PDFDocument?
private let pageView = UIView()
private let pageViewLabel = UILabel()
private var tipView: EasyTipView?
private let thumbnailViewHeight: CGFloat = 70
private let thumbnailViewWidth: CGFloat = 80
private let thumbnailPadding: CGFloat = 2
private let animateDuration: TimeInterval = 0.3
private var defaultBackgroundColor: UIColor = .clear
private var pdfThumbnailScrollViewTopAnchor: NSLayoutConstraint?
private var pdfThumbnailScrollViewTrailingAnchor: NSLayoutConstraint?
private var pdfThumbnailScrollViewWidthAnchor: NSLayoutConstraint?
private var pageViewWidthAnchor: NSLayoutConstraint?
// MARK: - View Life Cycle
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
let pageCount = CGFloat(pdfDocument?.pageCount ?? 0)
if #available(iOS 13.0, *) {
defaultBackgroundColor = pdfView.backgroundColor
} else {
defaultBackgroundColor = .lightGray
}
view.backgroundColor = defaultBackgroundColor
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
navigationItem.title = metadata.fileNameView
// PDF VIEW
if UIDevice.current.userInterfaceIdiom == .phone {
pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
} else {
pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width-thumbnailViewWidth, height: view.frame.height))
}
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
pdfView.document = pdfDocument
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
view.addSubview(pdfView)
// PDF THUMBNAIL
pdfThumbnailScrollView.translatesAutoresizingMaskIntoConstraints = false
pdfThumbnailScrollView.backgroundColor = defaultBackgroundColor
pdfThumbnailScrollView.showsVerticalScrollIndicator = false
view.addSubview(pdfThumbnailScrollView)
pdfThumbnailScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
pdfThumbnailScrollViewTopAnchor?.isActive = true
pdfThumbnailScrollViewTrailingAnchor = pdfThumbnailScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
pdfThumbnailScrollViewTrailingAnchor?.isActive = true
pdfThumbnailScrollViewWidthAnchor = pdfThumbnailScrollView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
pdfThumbnailScrollViewWidthAnchor?.isActive = true
pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
pdfThumbnailView.pdfView = pdfView
pdfThumbnailView.layoutMode = .vertical
pdfThumbnailView.thumbnailSize = CGSize(width: thumbnailViewHeight, height: thumbnailViewHeight)
pdfThumbnailView.backgroundColor = .clear
if UIDevice.current.userInterfaceIdiom == .phone {
self.pdfThumbnailScrollView.isHidden = true
} else {
self.pdfThumbnailScrollView.isHidden = false
}
pdfThumbnailScrollView.addSubview(pdfThumbnailView)
NSLayoutConstraint.activate([
pdfThumbnailView.topAnchor.constraint(equalTo: pdfThumbnailScrollView.topAnchor),
pdfThumbnailView.bottomAnchor.constraint(equalTo: pdfThumbnailScrollView.bottomAnchor),
pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.leadingAnchor),
pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.trailingAnchor, constant: (UIApplication.shared.keyWindow?.safeAreaInsets.left ?? 0)),
pdfThumbnailView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
])
let contentViewCenterY = pdfThumbnailView.centerYAnchor.constraint(equalTo: pdfThumbnailScrollView.centerYAnchor)
contentViewCenterY.priority = .defaultLow
let contentViewHeight = pdfThumbnailView.heightAnchor.constraint(equalToConstant: CGFloat(pageCount * thumbnailViewHeight) + CGFloat(pageCount * thumbnailPadding) + 30)
contentViewHeight.priority = .defaultLow
NSLayoutConstraint.activate([
contentViewCenterY,
contentViewHeight
])
// COUNTER PDF PAGE VIEW
pageView.translatesAutoresizingMaskIntoConstraints = false
pageView.layer.cornerRadius = 10
pageView.backgroundColor = NCBrandColor.shared.systemBackground.withAlphaComponent(
UIAccessibility.isReduceTransparencyEnabled ? 1 : 0.5
)
view.addSubview(pageView)
NSLayoutConstraint.activate([
pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
pageView.heightAnchor.constraint(equalToConstant: 30),
pageView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 10)
])
pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
pageViewWidthAnchor?.isActive = true
pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
pageViewLabel.textAlignment = .center
pageViewLabel.textColor = NCBrandColor.shared.label
pageView.addSubview(pageViewLabel)
NSLayoutConstraint.activate([
pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor),
pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor),
pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor),
pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor)
])
// GESTURE
let tapPdfView = UITapGestureRecognizer(target: self, action: #selector(tapPdfView))
tapPdfView.numberOfTapsRequired = 1
pdfView.addGestureRecognizer(tapPdfView)
// recognize single / double tap
for gesture in pdfView.gestureRecognizers! {
tapPdfView.require(toFail: gesture)
}
let swipePdfView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
swipePdfView.direction = .right
swipePdfView.delegate = self
pdfView.addGestureRecognizer(swipePdfView)
let edgePdfView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
edgePdfView.edges = .right
edgePdfView.delegate = self
pdfView.addGestureRecognizer(edgePdfView)
let swipePdfThumbnailScrollView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
swipePdfThumbnailScrollView.direction = .right
pdfThumbnailScrollView.addGestureRecognizer(swipePdfThumbnailScrollView)
NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(goToPage), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
// Tip
if UIDevice.current.userInterfaceIdiom == .phone && !NCManageDatabase.shared.tipExists(NCGlobal.shared.tipNCViewerPDFThumbnail){
var preferences = EasyTipView.Preferences()
preferences.drawing.foregroundColor = .white
preferences.drawing.backgroundColor = NCBrandColor.shared.nextcloud
preferences.drawing.textAlignment = .left
preferences.drawing.arrowPosition = .right
preferences.drawing.cornerRadius = 10
preferences.positioning.bubbleInsets.right = UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0
preferences.animating.dismissTransform = CGAffineTransform(translationX: 0, y: 100)
preferences.animating.showInitialTransform = CGAffineTransform(translationX: 0, y: -100)
preferences.animating.showInitialAlpha = 0
preferences.animating.showDuration = 1.5
preferences.animating.dismissDuration = 1.5
tipView = EasyTipView(text: NSLocalizedString("_tip_pdf_thumbnails_", comment: ""), preferences: preferences, delegate: self)
}
setConstraints()
handlePageChange()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tipView?.show(forView: self.pdfThumbnailScrollView, withinSuperview: self.view)
}
@objc func viewUnload() {
navigationController?.popViewController(animated: true)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { context in
if UIDevice.current.userInterfaceIdiom == .phone {
// Close
self.tipView?.dismiss()
self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
self.pdfThumbnailScrollView.isHidden = true
}
}, completion: { context in
self.setConstraints()
})
}
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
NotificationCenter.default.removeObserver(self, name: Notification.Name.PDFViewPageChanged, object: nil)
}
// MARK: - NotificationCenter
@objc func uploadStartFile(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let serverUrl = userInfo["serverUrl"] as? String,
serverUrl == self.metadata.serverUrl,
let fileName = userInfo["fileName"] as? String,
fileName == self.metadata.fileName
else { return }
NCActivityIndicator.shared.start()
}
@objc func uploadedFile(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let serverUrl = userInfo["serverUrl"] as? String,
serverUrl == self.metadata.serverUrl,
let fileName = userInfo["fileName"] as? String,
fileName == self.metadata.fileName,
let errorCode = userInfo["errorCode"] as? Int
else {
return
}
NCActivityIndicator.shared.stop()
if errorCode == 0 {
pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
pdfView.document = pdfDocument
pdfView.layoutDocumentView()
}
}
@objc func favoriteFile(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let ocId = userInfo["ocId"] as? String,
ocId == self.metadata.ocId,
let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
else { return }
self.metadata = metadata
}
@objc func moveFile(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let ocId = userInfo["ocId"] as? String,
ocId == self.metadata.ocId,
let ocIdNew = userInfo["ocIdNew"] as? String,
let metadataNew = NCManageDatabase.shared.getMetadataFromOcId(ocIdNew)
else { return }
self.metadata = metadataNew
}
@objc func deleteFile(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let ocId = userInfo["ocId"] as? String,
ocId == self.metadata.ocId
else { return }
viewUnload()
}
@objc func renameFile(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let ocId = userInfo["ocId"] as? String,
ocId == self.metadata.ocId,
let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
else { return }
self.metadata = metadata
navigationItem.title = metadata.fileNameView
}
@objc func searchText() {
let viewerPDFSearch = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
viewerPDFSearch.delegate = self
viewerPDFSearch.pdfDocument = pdfDocument
let navigaionController = UINavigationController(rootViewController: viewerPDFSearch)
self.present(navigaionController, animated: true)
}
@objc func goToPage() {
guard let pdfDocument = pdfView.document else { return }
let alertMessage = NSString(format: NSLocalizedString("_this_document_has_%@_pages_", comment: "") as NSString, "\(pdfDocument.pageCount)") as String
let alertController = UIAlertController(title: NSLocalizedString("_go_to_page_", comment: ""), message: alertMessage, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: { textField in
textField.placeholder = NSLocalizedString("_page_", comment: "")
textField.keyboardType = .decimalPad
})
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { [unowned self] _ in
if let pageLabel = alertController.textFields?.first?.text {
self.selectPage(with: pageLabel)
}
}))
self.present(alertController, animated: true)
}
// MARK: - Action
@objc func openMenuMore() {
if imageIcon == nil { imageIcon = UIImage(named: "file_pdf") }
NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false, imageIcon: imageIcon)
}
// MARK: - Gesture Recognizer
@objc func tapPdfView(_ recognizer: UITapGestureRecognizer) {
pdfThumbnailScrollViewTopAnchor?.isActive = false
if navigationController?.isNavigationBarHidden ?? false {
navigationController?.setNavigationBarHidden(false, animated: true)
pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
} else {
navigationController?.setNavigationBarHidden(true, animated: true)
pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.topAnchor)
}
pdfThumbnailScrollViewTopAnchor?.isActive = true
handlePageChange()
}
@objc func gestureOpenPdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
guard let pdfDocument = pdfView.document, !pdfDocument.isLocked else { return }
if UIDevice.current.userInterfaceIdiom == .phone && self.pdfThumbnailScrollView.isHidden {
if let tipView = self.tipView {
tipView.dismiss()
NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerPDFThumbnail)
self.tipView = nil
}
self.pdfThumbnailScrollView.isHidden = false
self.pdfThumbnailScrollViewWidthAnchor?.constant = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
UIView.animate(withDuration: animateDuration, animations: {
self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
self.view.layoutIfNeeded()
})
}
}
@objc func gestureClosePdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
if recognizer.state == .recognized && UIDevice.current.userInterfaceIdiom == .phone && !self.pdfThumbnailScrollView.isHidden {
UIView.animate(withDuration: animateDuration) {
self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
self.view.layoutIfNeeded()
} completion: { _ in
self.pdfThumbnailScrollView.isHidden = true
}
}
}
// MARK: -
func setConstraints() {
let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
UIView.animate(withDuration: animateDuration, animations: {
if UIDevice.current.userInterfaceIdiom == .phone {
// Close
self.pdfThumbnailScrollView.isHidden = true
self.pdfThumbnailScrollViewTrailingAnchor?.constant = widthThumbnail
self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
} else {
// Open
self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
}
self.view.layoutIfNeeded()
self.pdfView.autoScales = true
})
}
@objc func handlePageChange() {
guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
guard let totalPages = pdfView.document?.pageCount else { return }
let visibleRect = CGRect(x: pdfThumbnailScrollView.contentOffset.x, y: pdfThumbnailScrollView.contentOffset.y, width: pdfThumbnailScrollView.bounds.size.width, height: pdfThumbnailScrollView.bounds.size.height)
let centerPoint = CGPoint(x: visibleRect.size.width/2, y: visibleRect.size.height/2)
let currentPageY = CGFloat(curPage) * thumbnailViewHeight + CGFloat(curPage) * thumbnailPadding
var gotoY = currentPageY - centerPoint.y
let startY = visibleRect.origin.y < 0 ? 0 : (visibleRect.origin.y + thumbnailViewHeight)
let endY = visibleRect.origin.y + visibleRect.height
if currentPageY < startY {
if gotoY < 0 { gotoY = 0 }
pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
} else if currentPageY > endY {
if gotoY > pdfThumbnailView.frame.height - visibleRect.height {
gotoY = pdfThumbnailView.frame.height - visibleRect.height
}
pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
} else {
print("visible")
}
pageView.alpha = 1
pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
UIView.animate(withDuration: 1.0, delay: 2.5, animations: {
self.pageView.alpha = 0
})
}
func searchPdfSelection(_ pdfSelection: PDFSelection) {
removeAllAnnotations()
pdfSelection.pages.forEach { page in
let highlight = PDFAnnotation(bounds: pdfSelection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = NCBrandColor.shared.annotationColor
page.addAnnotation(highlight)
}
if let page = pdfSelection.pages.first {
pdfView.go(to: page)
}
handlePageChange()
}
private func selectPage(with label: String) {
guard let pdf = pdfView.document else { return }
if let pageNr = Int(label) {
if pageNr > 0 && pageNr <= pdf.pageCount {
if let page = pdf.page(at: pageNr - 1) {
self.pdfView.go(to: page)
}
} else {
let alertController = UIAlertController(title: NSLocalizedString("_invalid_page_", comment: ""),
message: NSLocalizedString("_the_entered_page_number_doesn't_exist_", comment: ""),
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
}
func removeAllAnnotations() {
guard let document = pdfDocument else { return }
for i in 0..<document.pageCount {
if let page = document.page(at: i) {
let annotations = page.annotations
for annotation in annotations {
page.removeAnnotation(annotation)
}
}
}
}
}
extension NCViewerPDF: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
extension NCViewerPDF: EasyTipViewDelegate {
func easyTipViewDidTap(_ tipView: EasyTipView) {
NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerPDFThumbnail)
}
func easyTipViewDidDismiss(_ tipView: EasyTipView) { }
}
|