This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ViewerController.swift
717 lines (577 loc) · 27.9 KB
/
ViewerController.swift
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import UIKit
import CoreData
public protocol ViewerControllerDataSource: class {
func numberOfItemsInViewerController(_ viewerController: ViewerController) -> Int
func viewerController(_ viewerController: ViewerController, viewableAt indexPath: IndexPath) -> Viewable
}
public protocol ViewerControllerDelegate: class {
func viewerController(_ viewerController: ViewerController, didChangeFocusTo indexPath: IndexPath)
func viewerControllerDidDismiss(_ viewerController: ViewerController)
func viewerController(_ viewerController: ViewerController, didFailDisplayingViewableAt indexPath: IndexPath, error: NSError)
func viewerController(_ viewerController: ViewerController, didLongPressViewableAt indexPath: IndexPath)
}
/// The ViewerController takes care of displaying the user's photos and videos in full-screen. You can swipe right or left to navigate between them.
public class ViewerController: UIViewController {
static let domain = "com.3lvis.Viewer"
fileprivate static let HeaderHeight = CGFloat(64)
fileprivate static let FooterHeight = CGFloat(50)
fileprivate static let DraggingMargin = CGFloat(60)
fileprivate var isSlideshow: Bool
public init(initialIndexPath: IndexPath, collectionView: UICollectionView, isSlideshow: Bool = false) {
self.initialIndexPath = initialIndexPath
self.currentIndexPath = initialIndexPath
self.collectionView = collectionView
self.proposedCurrentIndexPath = initialIndexPath
self.isSlideshow = isSlideshow
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = .clear
self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.modalPresentationStyle = .overCurrentContext
#if os(iOS)
self.modalPresentationCapturesStatusBarAppearance = true
#endif
}
fileprivate var proposedCurrentIndexPath: IndexPath
public required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public weak var delegate: ViewerControllerDelegate?
public weak var dataSource: ViewerControllerDataSource?
/**
Flag that tells the viewer controller to autoplay videos on focus
*/
public var autoplayVideos: Bool = false
/**
Viewable background color
*/
public var viewableBackgroundColor: UIColor = .black
/**
Cache for the reused ViewableControllers
*/
fileprivate let viewableControllerCache = NSCache<NSString, ViewableController>()
/**
Temporary variable used to present the initial controller on viewDidAppear
*/
fileprivate var initialIndexPath: IndexPath
/**
The UICollectionView to be used when dismissing and presenting elements
*/
fileprivate unowned var collectionView: UICollectionView
/**
CGPoint used for diffing the panning on an image
*/
fileprivate var originalDraggedCenter = CGPoint.zero
/**
Used for doing a different animation when dismissing in the middle of a dragging gesture
*/
fileprivate var isDragging = false
/**
Keeps track of where the status bar should be hidden or not
*/
fileprivate var shouldHideStatusBar = false
/**
Keeps track of where the status bar should be light or not
*/
public var shouldUseLightStatusBar = true
/**
Critical button visibility state tracker, it's used to force the buttons to keep being hidden when they are toggled
*/
fileprivate var buttonsAreVisible = false
/**
Tracks the index for the current viewer item controller
*/
fileprivate(set) public var currentIndexPath: IndexPath
/**
A helper to prevent the paginated scroll view to be set up twice when is presented
*/
fileprivate(set) public var isPresented = false
fileprivate lazy var overlayView: UIView = {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = self.viewableBackgroundColor
view.alpha = 0
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return view
}()
fileprivate lazy var pageController: UIPageViewController = {
let controller = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
controller.dataSource = self
controller.delegate = self
return controller
}()
public var headerView: UIView?
public var footerView: UIView?
private lazy var defaultHeaderView: DefaultHeaderView = {
let defaultHeaderView = DefaultHeaderView()
defaultHeaderView.delegate = self
defaultHeaderView.translatesAutoresizingMaskIntoConstraints = false
defaultHeaderView.alpha = 0
return defaultHeaderView
}()
lazy var scrollView: PaginatedScrollView = {
let view = PaginatedScrollView(frame: self.view.frame, parentController: self, initialPage: self.initialIndexPath.totalRow(self.collectionView))
view.viewDataSource = self
view.viewDelegate = self
view.backgroundColor = .clear
return view
}()
lazy var slideshowView: SlideshowView = {
let view = SlideshowView(frame: self.view.frame, parentController: self, initialPage: self.initialIndexPath.totalRow(self.collectionView))
view.dataSource = self
view.delegate = self
view.backgroundColor = .clear
return view
}()
// MARK: View Lifecycle
public override func viewDidLoad() {
super.viewDidLoad()
#if os(iOS)
self.view.addSubview(self.scrollView)
#else
let menuTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.menu(gesture:)))
menuTapRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
self.view.addGestureRecognizer(menuTapRecognizer)
if self.isSlideshow {
self.view.addSubview(self.slideshowView)
} else {
self.addChild(self.pageController)
self.pageController.view.frame = UIScreen.main.bounds
self.view.addSubview(self.pageController.view)
self.pageController.didMove(toParent: self)
let playPauseTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.playPause(gesture:)))
playPauseTapRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.playPause.rawValue)]
self.view.addGestureRecognizer(playPauseTapRecognizer)
let selectTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.select(gesture:)))
selectTapRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.select.rawValue)]
self.view.addGestureRecognizer(selectTapRecognizer)
let rightSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(rightSwipe(gesture:)))
rightSwipeRecognizer.direction = .right
self.view.addGestureRecognizer(rightSwipeRecognizer)
let leftSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(leftSwipe(gesture:)))
leftSwipeRecognizer.direction = .left
self.view.addGestureRecognizer(leftSwipeRecognizer)
}
#endif
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
self.view.addGestureRecognizer(recognizer)
}
#if os(tvOS)
@objc func menu(gesture: UITapGestureRecognizer) {
guard gesture.state == .ended else { return }
self.dismiss(nil)
}
@objc func playPause(gesture: UITapGestureRecognizer) {
guard gesture.state == .ended else { return }
self.playIfVideo()
}
@objc func select(gesture: UITapGestureRecognizer) {
guard gesture.state == .ended else { return }
self.playIfVideo()
}
func playIfVideo() {
let viewableController = self.findOrCreateViewableController(self.currentIndexPath)
let isVideo = viewableController.viewable?.type == .video
if isVideo {
viewableController.play()
}
}
@objc func rightSwipe(gesture: UISwipeGestureRecognizer) {
guard gesture.state == .ended else { return }
self.scrollView.goRight()
}
@objc func leftSwipe(gesture: UISwipeGestureRecognizer) {
guard gesture.state == .ended else { return }
self.scrollView.goLeft()
}
public override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
let result = super.shouldUpdateFocus(in: context)
if context.focusHeading == .up {
return false
}
return result
}
#endif
@objc func longPress(gesture: UILongPressGestureRecognizer) {
guard gesture.state == .began else { return }
self.delegate?.viewerController(self, didLongPressViewableAt: self.currentIndexPath)
}
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if self.isPresented {
if self.isSlideshow {
self.slideshowView.configure()
} else {
self.scrollView.configure()
}
if !self.collectionView.indexPathsForVisibleItems.contains(self.currentIndexPath) && self.collectionView.numberOfSections > self.currentIndexPath.section && self.collectionView.numberOfItems(inSection: self.currentIndexPath.section) > self.currentIndexPath.item {
self.collectionView.scrollToItem(at: self.currentIndexPath, at: .bottom, animated: true)
}
}
}
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.present(with: self.initialIndexPath, completion: nil)
}
public func reload(at indexPath: IndexPath) {
let viewableController = self.findOrCreateViewableController(indexPath)
viewableController.display()
}
}
extension ViewerController {
#if os(iOS)
public override var prefersStatusBarHidden: Bool {
let orientation = UIApplication.shared.statusBarOrientation
if orientation.isLandscape {
return true
}
return self.shouldHideStatusBar
}
public override var preferredStatusBarStyle: UIStatusBarStyle {
if self.shouldUseLightStatusBar {
return .lightContent
} else {
return self.presentingViewController?.preferredStatusBarStyle ?? .default
}
}
#endif
private func presentedViewCopy() -> UIImageView {
let presentedView = UIImageView()
presentedView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
presentedView.contentMode = .scaleAspectFill
presentedView.clipsToBounds = true
return presentedView
}
fileprivate func findOrCreateViewableController(_ indexPath: IndexPath) -> ViewableController {
let viewable = self.dataSource!.viewerController(self, viewableAt: indexPath)
var viewableController: ViewableController
if let cachedController = self.viewableControllerCache.object(forKey: indexPath.description as NSString) {
viewableController = cachedController
} else {
viewableController = ViewableController()
viewableController.delegate = self
viewableController.dataSource = self
let gesture = UIPanGestureRecognizer(target: self, action: #selector(ViewerController.panAction(_:)))
gesture.delegate = self
viewableController.imageView.addGestureRecognizer(gesture)
self.viewableControllerCache.setObject(viewableController, forKey: indexPath.description as NSString)
}
viewableController.update(with: viewable, at: indexPath)
viewableController.viewableBackgroundColor = self.viewableBackgroundColor
return viewableController
}
fileprivate func toggleButtons(_ shouldShow: Bool) {
UIView.animate(withDuration: 0.3, animations: {
#if os(iOS)
self.setNeedsStatusBarAppearanceUpdate()
#endif
self.headerView?.alpha = shouldShow ? 1 : 0
self.footerView?.alpha = shouldShow ? 1 : 0
})
}
private func fadeButtons(_ alpha: CGFloat) {
self.headerView?.alpha = alpha
self.footerView?.alpha = alpha
}
fileprivate func present(with indexPath: IndexPath, completion: (() -> Void)?) {
guard let selectedCell = self.collectionView.cellForItem(at: indexPath) else { return }
let viewable = self.dataSource!.viewerController(self, viewableAt: indexPath)
let image = viewable.placeholder
selectedCell.alpha = 0
let presentedView = self.presentedViewCopy()
presentedView.frame = self.view.convert(selectedCell.frame, from: self.collectionView)
presentedView.image = image
self.view.addSubview(self.overlayView)
self.view.addSubview(presentedView)
if self.headerView == nil {
self.headerView = defaultHeaderView
}
if let headerView = self.headerView {
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.alpha = 0
self.view.addSubview(headerView)
NSLayoutConstraint.activate([
headerView.topAnchor.constraint(equalTo: view.compatibleTopAnchor),
headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
headerView.heightAnchor.constraint(equalToConstant: ViewerController.HeaderHeight)
])
}
if let footerView = self.footerView {
footerView.translatesAutoresizingMaskIntoConstraints = false
footerView.alpha = 0
self.view.addSubview(footerView)
NSLayoutConstraint.activate([
footerView.bottomAnchor.constraint(equalTo: view.compatibleBottomAnchor),
footerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
footerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
footerView.heightAnchor.constraint(equalToConstant: ViewerController.FooterHeight)
])
}
let centeredImageFrame = image.centeredFrame()
UIView.animate(withDuration: 0.25, animations: {
self.presentingViewController?.tabBarController?.tabBar.alpha = 0
self.overlayView.alpha = 1.0
#if os(iOS)
self.setNeedsStatusBarAppearanceUpdate()
#endif
presentedView.frame = centeredImageFrame
}, completion: { _ in
self.toggleButtons(true)
self.buttonsAreVisible = true
self.currentIndexPath = indexPath
presentedView.removeFromSuperview()
self.overlayView.removeFromSuperview()
self.view.backgroundColor = .black
self.isPresented = true
let controller = self.findOrCreateViewableController(indexPath)
controller.display()
self.delegate?.viewerController(self, didChangeFocusTo: indexPath)
#if os(iOS)
completion?()
#else
if self.isSlideshow {
self.slideshowView.start()
UIApplication.shared.isIdleTimerDisabled = true
} else {
self.pageController.setViewControllers([controller], direction: .forward, animated: false, completion: { _ in
completion?()
})
}
#endif
})
}
public func dismiss(_ completion: (() -> Void)?) {
let controller = self.findOrCreateViewableController(self.currentIndexPath)
self.dismiss(controller, completion: completion)
}
private func dismiss(_ viewableController: ViewableController, completion: (() -> Void)?) {
if self.isSlideshow {
self.slideshowView.stop()
UIApplication.shared.isIdleTimerDisabled = false
}
guard let indexPath = viewableController.indexPath else { return }
guard let selectedCellFrame = self.collectionView.layoutAttributesForItem(at: indexPath)?.frame else { return }
let viewable = self.dataSource!.viewerController(self, viewableAt: indexPath)
let image = viewable.placeholder
viewableController.imageView.alpha = 0
viewableController.view.backgroundColor = .clear
viewableController.willDismiss()
self.view.alpha = 0
self.fadeButtons(0)
self.buttonsAreVisible = false
self.updateHiddenCellsUsingVisibleIndexPath(self.currentIndexPath)
self.shouldHideStatusBar = false
#if os(iOS)
self.setNeedsStatusBarAppearanceUpdate()
#endif
self.overlayView.alpha = self.isDragging ? viewableController.view.backgroundColor!.cgColor.alpha : 1.0
self.overlayView.frame = UIScreen.main.bounds
let presentedView = self.presentedViewCopy()
presentedView.frame = image.centeredFrame()
presentedView.image = image
if self.isDragging {
presentedView.center = viewableController.imageView.center
}
let window = self.applicationWindow()
window.addSubview(self.overlayView)
window.addSubview(presentedView)
self.shouldUseLightStatusBar = false
UIView.animate(withDuration: 0.30, animations: {
self.presentingViewController?.tabBarController?.tabBar.alpha = 1
self.overlayView.alpha = 0.0
#if os(iOS)
self.setNeedsStatusBarAppearanceUpdate()
#endif
presentedView.frame = self.view.convert(selectedCellFrame, from: self.collectionView)
}, completion: { _ in
if let existingCell = self.collectionView.cellForItem(at: indexPath) {
existingCell.alpha = 1
}
self.headerView?.removeFromSuperview()
self.footerView?.removeFromSuperview()
presentedView.removeFromSuperview()
self.overlayView.removeFromSuperview()
self.dismiss(animated: false, completion: nil)
// A small delay is required to avoid racing conditions between the dismissing animation and the
// state change after the animation is completed.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.isPresented = false
self.delegate?.viewerControllerDidDismiss(self)
completion?()
}
})
}
@objc func panAction(_ gesture: UIPanGestureRecognizer) {
let controller = self.findOrCreateViewableController(self.currentIndexPath)
guard !controller.hasZoomed else { return }
let viewHeight = controller.imageView.frame.size.height
let viewHalfHeight = viewHeight / 2
var translatedPoint = gesture.translation(in: controller.imageView)
if gesture.state == .began {
self.shouldHideStatusBar = false
#if os(iOS)
self.setNeedsStatusBarAppearanceUpdate()
#endif
self.view.backgroundColor = .clear
self.originalDraggedCenter = controller.imageView.center
self.isDragging = true
self.updateHiddenCellsUsingVisibleIndexPath(self.currentIndexPath)
controller.willDismiss()
}
translatedPoint = CGPoint(x: self.originalDraggedCenter.x, y: self.originalDraggedCenter.y + translatedPoint.y)
let alphaDiff = ((translatedPoint.y - viewHalfHeight) / viewHalfHeight) * 2.5
let isDraggedUp = translatedPoint.y < viewHalfHeight
let alpha = isDraggedUp ? 1 + alphaDiff : 1 - alphaDiff
controller.dimControls(alpha)
controller.imageView.center = translatedPoint
controller.view.backgroundColor = self.viewableBackgroundColor.withAlphaComponent(alpha)
if self.buttonsAreVisible {
self.fadeButtons(alpha)
}
if gesture.state == .ended {
let centerAboveDraggingArea = controller.imageView.center.y < viewHalfHeight - ViewerController.DraggingMargin
let centerBellowDraggingArea = controller.imageView.center.y > viewHalfHeight + ViewerController.DraggingMargin
if centerAboveDraggingArea || centerBellowDraggingArea {
self.dismiss(controller, completion: nil)
} else {
self.isDragging = false
UIView.animate(withDuration: 0.20, animations: {
controller.imageView.center = self.originalDraggedCenter
controller.view.backgroundColor = self.viewableBackgroundColor
controller.dimControls(1.0)
if self.buttonsAreVisible {
self.fadeButtons(1)
}
self.shouldHideStatusBar = !self.buttonsAreVisible
#if os(iOS)
self.setNeedsStatusBarAppearanceUpdate()
#endif
}, completion: { _ in
controller.display()
self.view.backgroundColor = self.viewableBackgroundColor
})
}
}
}
fileprivate func centerElementIfNotVisible(_ indexPath: IndexPath, animated: Bool) {
if !self.collectionView.indexPathsForVisibleItems.contains(indexPath) {
self.collectionView.scrollToItem(at: indexPath, at: .top, animated: animated)
}
}
private func updateHiddenCellsUsingVisibleIndexPath(_ visibleIndexPath: IndexPath) {
for indexPath in self.collectionView.indexPathsForVisibleItems {
if let cell = self.collectionView.cellForItem(at: indexPath) {
cell.alpha = indexPath == visibleIndexPath ? 0 : 1
}
}
}
fileprivate func evaluateCellVisibility(collectionView: UICollectionView, currentIndexPath: IndexPath, upcomingIndexPath: IndexPath) {
if !collectionView.indexPathsForVisibleItems.contains(upcomingIndexPath) {
var position: UICollectionView.ScrollPosition?
if currentIndexPath.compareDirection(upcomingIndexPath) == .forward {
position = .bottom
} else if currentIndexPath.compareDirection(upcomingIndexPath) == .backward {
position = .top
}
if let position = position {
collectionView.scrollToItem(at: upcomingIndexPath, at: position, animated: true)
}
}
}
}
extension ViewerController: ViewableControllerDelegate {
func viewableControllerDidTapItem(_: ViewableController) {
self.shouldHideStatusBar = !self.shouldHideStatusBar
self.buttonsAreVisible = !self.buttonsAreVisible
self.toggleButtons(self.buttonsAreVisible)
}
func viewableController(_: ViewableController, didFailDisplayingVieweableWith error: NSError) {
self.delegate?.viewerController(self, didFailDisplayingViewableAt: self.currentIndexPath, error: error)
}
}
extension ViewerController: ViewableControllerDataSource {
func viewableControllerOverlayIsVisible(_: ViewableController) -> Bool {
return self.buttonsAreVisible
}
func viewableControllerIsFocused(_ viewableController: ViewableController) -> Bool {
let focusedViewableController = self.findOrCreateViewableController(self.currentIndexPath)
return viewableController == focusedViewableController
}
func viewableControllerShouldAutoplayVideo(_: ViewableController) -> Bool {
return self.autoplayVideos
}
}
extension ViewerController: UIGestureRecognizerDelegate {
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer is UIPanGestureRecognizer {
let panGestureRecognizer = gestureRecognizer as! UIPanGestureRecognizer
let velocity = panGestureRecognizer.velocity(in: panGestureRecognizer.view!)
let allowOnlyVerticalScrolls = abs(velocity.y) > abs(velocity.x)
return allowOnlyVerticalScrolls
}
return true
}
}
extension ViewerController: ViewableControllerContainerDataSource {
func numberOfPagesInViewableControllerContainer(_ viewableControllerContainer: ViewableControllerContainer) -> Int {
return self.dataSource?.numberOfItemsInViewerController(self) ?? 0
}
func viewableControllerContainer(_ viewableControllerContainer: ViewableControllerContainer, controllerAtIndex index: Int) -> UIViewController {
let indexPath = IndexPath.indexPathForIndex(self.collectionView, index: index)!
return self.findOrCreateViewableController(indexPath)
}
}
extension ViewerController: ViewableControllerContainerDelegate {
func viewableControllerContainer(_ viewableControllerContainer: ViewableControllerContainer, didMoveToIndex index: Int) {
let indexPath = IndexPath.indexPathForIndex(self.collectionView, index: index)!
self.evaluateCellVisibility(collectionView: self.collectionView, currentIndexPath: self.currentIndexPath, upcomingIndexPath: indexPath)
self.currentIndexPath = indexPath
self.delegate?.viewerController(self, didChangeFocusTo: indexPath)
let viewableController = self.findOrCreateViewableController(indexPath)
viewableController.display()
}
func viewableControllerContainer(_ viewableControllerContainer: ViewableControllerContainer, didMoveFromIndex index: Int) {
let indexPath = IndexPath.indexPathForIndex(self.collectionView, index: index)!
let viewableController = self.findOrCreateViewableController(indexPath)
viewableController.willDismiss()
}
}
extension ViewerController: UIPageViewControllerDelegate {
public func pageViewController(_: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
guard let controllers = pendingViewControllers as? [ViewableController] else { fatalError() }
for controller in controllers {
self.delegate?.viewerController(self, didChangeFocusTo: controller.indexPath!)
self.proposedCurrentIndexPath = controller.indexPath!
}
}
public func pageViewController(_: UIPageViewController, didFinishAnimating _: Bool, previousViewControllers _: [UIViewController], transitionCompleted completed: Bool) {
if completed {
self.delegate?.viewerController(self, didChangeFocusTo: self.proposedCurrentIndexPath)
self.currentIndexPath = self.proposedCurrentIndexPath
self.delegate?.viewerController(self, didChangeFocusTo: self.currentIndexPath)
self.centerElementIfNotVisible(self.currentIndexPath, animated: false)
}
}
}
extension ViewerController: UIPageViewControllerDataSource {
public func pageViewController(_: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let viewerItemController = viewController as? ViewableController, let newIndexPath = viewerItemController.indexPath?.previous(self.collectionView) {
let controller = self.findOrCreateViewableController(newIndexPath)
controller.display()
return controller
}
return nil
}
public func pageViewController(_: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let viewerItemController = viewController as? ViewableController, let newIndexPath = viewerItemController.indexPath?.next(self.collectionView) {
let controller = self.findOrCreateViewableController(newIndexPath)
controller.display()
return controller
}
return nil
}
}
extension ViewerController: DefaultHeaderViewDelegate {
func headerView(_ headerView: DefaultHeaderView, didPressClearButton button: UIButton) {
dismiss(nil)
}
}