-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathCalendarView.swift
1459 lines (1225 loc) · 52.5 KB
/
CalendarView.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Created by Bryan Keller on 1/15/20.
// Copyright © 2020 Airbnb Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import UIKit
// MARK: - CalendarView
/// A declarative, performant calendar `UIView` that supports use cases ranging from simple date pickers all the way up to
/// fully-featured calendar apps. Its declarative API makes updating the calendar straightforward, while also providing many
/// customization points to support a diverse set of designs and use cases.
///
/// `CalendarView` does not handle any business logic related to day range selection or deselection. Instead, it provides a
/// single callback for day selection, allowing you to customize selection behavior in any way that you’d like.
///
/// Your business logic can respond to the day selection callback, regenerate `CalendarView` content based on changes to the
/// backing-models for your feature, then set the content on `CalendarView`. This will trigger `CalendarView` to re-render,
/// reflecting all new changes from the content you provide.
///
/// `CalendarView`’s content contains all information about how to render the calendar (you can think of `CalendarView` as a
/// pure function of its content). The most important things provided by the content are:
/// * The date range to display
/// * e.g. September, 2019 - April, 2020
/// * A months-layout (vertical or horizontal)
/// * An optional `CalendarItem` to display for each day in the date range if you don't want to use the default day view
/// * e.g. a view with a label representing a single day
public final class CalendarView: UIView {
// MARK: Lifecycle
/// Initializes a new `CalendarView` instance with the provided initial content.
///
/// - Parameters:
/// - initialContent: The content to use when initially rendering `CalendarView`.
public init(initialContent: CalendarViewContent) {
content = initialContent
super.init(frame: .zero)
commonInit()
}
required init?(coder: NSCoder) {
let startDate = Date() // now
let endDate = Date(timeIntervalSinceNow: 31_536_000) // one year from now
content = CalendarViewContent(visibleDateRange: startDate...endDate, monthsLayout: .vertical)
super.init(coder: coder)
commonInit()
}
// MARK: Public
/// A closure (that is retained) that is invoked whenever a day is selected. It is the responsibility of your feature code to decide what to
/// do with each day. For example, you might store the most recent day in a selected day property, then read that property in your
/// `dayItemProvider` closure to add specific "selected" styling to a particular day view.
public var daySelectionHandler: ((DayComponents) -> Void)?
/// A closure (that is retained) that is invoked inside `scrollViewDidScroll(_:)`
public var didScroll: ((_ visibleDayRange: DayComponentsRange, _ isUserDragging: Bool) -> Void)?
/// A closure (that is retained) that is invoked inside `scrollViewDidEndDragging(_: willDecelerate:)`.
public var didEndDragging: ((_ visibleDayRange: DayComponentsRange, _ willDecelerate: Bool) -> Void)?
/// A closure (that is retained) that is invoked inside `scrollViewDidEndDecelerating(_:)`.
public var didEndDecelerating: ((_ visibleDayRange: DayComponentsRange) -> Void)?
/// A closure (that is retained) that is invoked during a multiple-selection-drag-gesture. Multiple selection is initiated with a long press,
/// followed by a drag / pan. As the gesture crosses over more days in the calendar, this handler will be invoked with each new day. It
/// is the responsibility of your feature code to decide what to do with this stream of days. For example, you might convert them to
/// `Date` instances and use them as input to the `dayRangeItemProvider`.
public var multiDaySelectionDragHandler: ((DayComponents, UIGestureRecognizer.State) -> Void)? {
didSet {
configureMultiDaySelectionPanGestureRecognizer()
}
}
/// Whether or not the calendar's scroll view is currently over-scrolling, i.e, whether the rubber-banding or bouncing effect is in
/// progress.
public var isOverScrolling: Bool {
let scrollAxis = scrollMetricsMutator.scrollAxis
let offset = scrollView.offset(for: scrollAxis)
return offset < scrollView.minimumOffset(for: scrollAxis) ||
offset > scrollView.maximumOffset(for: scrollAxis)
}
/// The range of months that are partially of fully visible.
public var visibleMonthRange: MonthComponentsRange? {
visibleItemsDetails?.visibleMonthRange
}
/// The range of days that are partially or fully visible.
public var visibleDayRange: DayComponentsRange? {
visibleItemsDetails?.visibleDayRange
}
/// `CalendarView` only supports positive values for `layoutMargins`. Negative values will be changed to `0`.
public override var layoutMargins: UIEdgeInsets {
get { super.layoutMargins }
set {
super.layoutMargins = UIEdgeInsets(
top: max(newValue.top, 0),
left: max(newValue.left, 0),
bottom: max(newValue.bottom, 0),
right: max(newValue.right, 0))
}
}
/// `CalendarView` only supports positive values for `directionalLayoutMargins`. Negative values will be changed to
/// `0`.
public override var directionalLayoutMargins: NSDirectionalEdgeInsets {
get { super.directionalLayoutMargins }
set {
super.directionalLayoutMargins = NSDirectionalEdgeInsets(
top: max(newValue.top, 0),
leading: max(newValue.leading, 0),
bottom: max(newValue.bottom, 0),
trailing: max(newValue.trailing, 0))
}
}
public override func didMoveToWindow() {
super.didMoveToWindow()
if window == nil {
scrollToItemContext = nil
}
}
public override func layoutMarginsDidChange() {
super.layoutMarginsDidChange()
setNeedsLayout()
}
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
// This can be called with a different trait collection instance, even if nothing in the trait
// collection has changed (noticed from SwiftUI). We guard against this to prevent and
// unnecessary layout pass.
guard traitCollection.layoutDirection != previousTraitCollection?.layoutDirection else {
return
}
setNeedsLayout()
}
public override func layoutSubviews() {
super.layoutSubviews()
// Setting the scroll view's frame in `layoutSubviews` causes over-scrolling to not work. We
// work around this by only setting the frame if it's changed.
if scrollView.frame != bounds {
scrollView.frame = bounds
}
if traitCollection.layoutDirection == .rightToLeft {
scrollView.transform = .init(scaleX: -1, y: 1)
} else {
scrollView.transform = .identity
}
if bounds != previousBounds || layoutMargins != previousLayoutMargins {
maintainScrollPositionAfterBoundsOrMarginsChange()
previousBounds = bounds
previousLayoutMargins = layoutMargins
}
guard isReadyForLayout else { return }
// Layout with an extended bounds if Voice Over is running, reducing the likelihood of a
// Voice Over user experiencing "No heading found" when navigating by heading. We also check to
// make sure an accessibility element has already been focused, otherwise the first
// accessibility element will be off-screen when a user first focuses into the calendar view.
let extendLayoutRegion = UIAccessibility.isVoiceOverRunning && initialItemViewWasFocused
_layoutSubviews(extendLayoutRegion: extendLayoutRegion)
}
/// Sets the content of the `CalendarView`, causing it to re-render, with no animation.
///
/// - Parameters:
/// - content: The content to use when rendering `CalendarView`.
public func setContent(_ content: CalendarViewContent) {
setContent(content, animated: false)
}
/// Sets the content of the `CalendarView`, causing it to re-render, with an optional animation.
///
/// If you call this function with `animated` set to `true` in your own animation closure, that animation will be used to perform
/// the content update. If you call this function with `animated` set to `true` outside of an animation closure, a default animation
/// will be used. Calling this function with `animated` set to `false` will result in a non-animated content update, even if you call
/// it from an animation closure.
///
/// - Parameters:
/// - content: The content to use when rendering `CalendarView`.
/// - animated: Whether or not the content update should be animated.
public func setContent(_ content: CalendarViewContent, animated: Bool) {
let oldContent = self.content
let isInAnimationClosure = UIView.areAnimationsEnabled && UIView.inheritedAnimationDuration > 0
// Do a preparation layout pass with an extended bounds, if we're animating. This ensures that
// views don't pop in if they're animating in from outside the actual bounds.
if animated {
UIView.performWithoutAnimation {
_layoutSubviews(extendLayoutRegion: isInAnimationClosure)
}
}
_visibleItemsProvider = nil
// We only need to clear the `scrollToItemContext` if the monthsLayout changed or the visible
// day range changed.
if content.monthsLayout != oldContent.monthsLayout || content.dayRange != oldContent.dayRange {
scrollToItemContext = nil
}
let isAnchorLayoutItemValid: Bool
switch anchorLayoutItem?.itemType {
case .monthHeader(let month):
isAnchorLayoutItemValid = content.monthRange.contains(month)
case .dayOfWeekInMonth(_, let month):
isAnchorLayoutItemValid = content.monthRange.contains(month)
case .day(let day):
isAnchorLayoutItemValid = content.dayRange.contains(day)
case .none:
isAnchorLayoutItemValid = false
}
if isAnchorLayoutItemValid {
// If we have a valid `anchorLayoutItem`, change it to be the topmost item. Normally, the
// `anchorLayoutItem` is the centermost item, but when our content changes, it can make the
// transition look better if our layout reference point is at the top of the screen.
anchorLayoutItem = visibleItemsDetails?.firstLayoutItem ?? anchorLayoutItem
} else {
// If the `anchorLayoutItem` is no longer valid (due to it no longer being in the visible day
// range), set it to nil. This will force us to find a new `anchorLayoutItem`.
anchorLayoutItem = nil
}
if content.monthsLayout.isPaginationEnabled {
scrollView.decelerationRate = .fast
} else {
scrollView.decelerationRate = .normal
}
if
oldContent.monthsLayout != content.monthsLayout ||
oldContent.monthDayInsets != content.monthDayInsets ||
oldContent.dayAspectRatio != content.dayAspectRatio ||
oldContent.dayOfWeekAspectRatio != content.dayOfWeekAspectRatio ||
oldContent.horizontalDayMargin != content.horizontalDayMargin ||
oldContent.verticalDayMargin != content.verticalDayMargin
{
invalidateIntrinsicContentSize()
}
self.content = content
setNeedsLayout()
// If we're animating, force layout with the inherited animation closure or with our own default
// animation. Forcing layout ensures that frame adjustments happen with an animation.
if animated {
let animations = {
self.isAnimatedUpdatePass = true
self.layoutIfNeeded()
self.isAnimatedUpdatePass = false
}
if isInAnimationClosure {
animations()
} else {
UIView.animate(withDuration: 0.3, animations: animations)
}
}
}
/// Returns the accessibility element associated with the specified visible date. If the date is not currently visible, then there will be no
/// associated accessibility element and this function will return `nil`.
///
/// Use this function to programmatically change the currently-focused date via
/// `UIAccessibility.post(notification:argument:)`, passing the returned accessibility element as the parameter for
/// `argument`.
///
/// - Parameters:
/// - date: The date for which to obtain an accessibility element. If the date is not currently visible, then it will not have an
/// associated accessibility element.
/// - Returns: An accessibility element associated with the specified `date`, or `nil` if one cannot be found.
public func accessibilityElementForVisibleDate(_ date: Date) -> Any? {
let day = calendar.day(containing: date)
guard let visibleDayRange, visibleDayRange.contains(day) else { return nil }
for (visibleItem, visibleView) in visibleViewsForVisibleItems {
guard case .layoutItemType(.day(day)) = visibleItem.itemType else { continue }
return visibleView
}
return nil
}
/// Scrolls the calendar to the specified month with the specified position.
///
/// If the calendar has a non-zero frame, this function will scroll to the specified month immediately. Otherwise the scroll-to-month
/// action will be queued and executed once the calendar has a non-zero frame. If this function is invoked multiple times before the
/// calendar has a non-zero frame, only the most recent scroll-to-month action will be executed.
///
/// - Parameters:
/// - dateInTargetMonth: A date in the target month to which to scroll into view.
/// - scrollPosition: The final position of the `CalendarView`'s scrollable region after the scroll completes.
/// - animated: Whether the scroll should be animated (from the current position), or whether the scroll should update the
/// visible region immediately with no animation.
public func scroll(
toMonthContaining dateInTargetMonth: Date,
scrollPosition: CalendarViewScrollPosition,
animated: Bool)
{
let month = calendar.month(containing: dateInTargetMonth)
guard content.monthRange.contains(month) else {
assertionFailure("""
Attempted to scroll to month \(month), which is out of bounds of the total date range
\(content.monthRange).
""")
return
}
// Cancel in-flight scroll
scrollView.setContentOffset(scrollView.contentOffset, animated: false)
scrollToItemContext = ScrollToItemContext(
targetItem: .month(month),
scrollPosition: scrollPosition,
animated: animated)
if animated {
startScrollingTowardTargetItem()
} else {
setNeedsLayout()
layoutIfNeeded()
}
}
/// Scrolls the calendar to the specified day with the specified position.
///
/// If the calendar has a non-zero frame, this function will scroll to the specified day immediately. Otherwise the scroll-to-day action
/// will be queued and executed once the calendar has a non-zero frame. If this function is invoked multiple times before the calendar
/// has a non-zero frame, only the most recent scroll-to-day action will be executed.
///
/// - Parameters:
/// - dateInTargetDay: A date in the target day to which to scroll into view.
/// - scrollPosition: The final position of the `CalendarView`'s scrollable region after the scroll completes.
/// - animated: Whether the scroll should be animated (from the current position), or whether the scroll should update the
/// visible region immediately with no animation.
public func scroll(
toDayContaining dateInTargetDay: Date,
scrollPosition: CalendarViewScrollPosition,
animated: Bool)
{
let day = calendar.day(containing: dateInTargetDay)
guard content.dayRange.contains(day) else {
assertionFailure("""
Attempted to scroll to day \(day), which is out of bounds of the total date range
\(content.dayRange).
""")
return
}
// Cancel in-flight scroll
scrollView.setContentOffset(scrollView.contentOffset, animated: false)
scrollToItemContext = ScrollToItemContext(
targetItem: .day(day),
scrollPosition: scrollPosition,
animated: animated)
if animated {
startScrollingTowardTargetItem()
} else {
setNeedsLayout()
layoutIfNeeded()
}
}
// MARK: Internal
lazy var doubleLayoutPassSizingLabel = DoubleLayoutPassSizingLabel(provider: self)
// MARK: Fileprivate
fileprivate var content: CalendarViewContent
fileprivate var previousPageIndex: Int?
fileprivate lazy var scrollView: CalendarScrollView = {
let scrollView = CalendarScrollView()
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = scrollViewDelegate
return scrollView
}()
fileprivate lazy var multiDaySelectionLongPressGestureRecognizer: UILongPressGestureRecognizer = {
let gestureRecognizer = UILongPressGestureRecognizer(
target: self,
action: #selector(multiDaySelectionGestureRecognized(_:)))
gestureRecognizer.allowableMovement = .greatestFiniteMagnitude
gestureRecognizer.delegate = gestureRecognizerDelegate
return gestureRecognizer
}()
fileprivate lazy var multiDaySelectionPanGestureRecognizer: UIPanGestureRecognizer = {
let gestureRecognizer = UIPanGestureRecognizer(
target: self,
action: #selector(multiDaySelectionGestureRecognized(_:)))
gestureRecognizer.maximumNumberOfTouches = 1
gestureRecognizer.maximumNumberOfTouches = 1
gestureRecognizer.delegate = gestureRecognizerDelegate
return gestureRecognizer
}()
fileprivate var scrollToItemContext: ScrollToItemContext? {
willSet {
scrollToItemDisplayLink?.invalidate()
}
}
fileprivate var calendar: Calendar {
content.calendar
}
// This hack is needed to prevent the scroll view from over-scrolling far past the content. This
// occurs in 2 scenarios:
// - On macOS if you scroll quickly toward a boundary
// - On iOS if you scroll quickly toward a boundary and targetContentOffset is mutated
//
// https://openradar.appspot.com/radar?id=4966130615582720 demonstrates this issue on macOS.
fileprivate func preventLargeOverScrollIfNeeded() {
guard isRunningOnMac || content.monthsLayout.isPaginationEnabled else { return }
let scrollAxis = scrollMetricsMutator.scrollAxis
let offset = scrollView.offset(for: scrollAxis)
let boundsSize: CGFloat
switch scrollAxis {
case .vertical: boundsSize = scrollView.bounds.height * 0.7
case .horizontal: boundsSize = scrollView.bounds.width * 0.7
}
let newOffset: CGPoint?
if offset < scrollView.minimumOffset(for: scrollAxis) - boundsSize {
switch scrollAxis {
case .vertical:
newOffset = CGPoint(
x: scrollView.contentOffset.x,
y: scrollView.minimumOffset(for: scrollAxis))
case .horizontal:
newOffset = CGPoint(
x: scrollView.minimumOffset(for: scrollAxis),
y: scrollView.contentOffset.y)
}
} else if offset > scrollView.maximumOffset(for: scrollAxis) + boundsSize {
switch scrollAxis {
case .vertical:
newOffset = CGPoint(
x: scrollView.contentOffset.x,
y: scrollView.maximumOffset(for: scrollAxis))
case .horizontal:
newOffset = CGPoint(
x: scrollView.maximumOffset(for: scrollAxis),
y: scrollView.contentOffset.y)
}
} else {
newOffset = nil
}
if let newOffset {
scrollView.performWithoutNotifyingDelegate {
// Passing `false` for `animated` is necessary to stop the in-flight deceleration animation
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {
self.scrollView.setContentOffset(newOffset, animated: false)
})
}
}
}
// MARK: Private
private let reuseManager = ItemViewReuseManager()
private let subviewInsertionIndexTracker = SubviewInsertionIndexTracker()
private var _scrollMetricsMutator: ScrollMetricsMutator?
private var anchorLayoutItem: LayoutItem?
private var _visibleItemsProvider: VisibleItemsProvider?
private var visibleItemsDetails: VisibleItemsDetails?
private var visibleViewsForVisibleItems = [VisibleItem: ItemView]()
private var isAnimatedUpdatePass = false
private var previousBounds = CGRect.zero
private var previousLayoutMargins = UIEdgeInsets.zero
private weak var scrollToItemDisplayLink: CADisplayLink?
private var scrollToItemAnimationStartTime: CFTimeInterval?
private weak var autoScrollDisplayLink: CADisplayLink?
private var autoScrollOffset: CGFloat?
private var lastMultiDaySelectionDay: Day?
private lazy var scrollViewDelegate = ScrollViewDelegate(calendarView: self)
private lazy var gestureRecognizerDelegate = GestureRecognizerDelegate(calendarView: self)
// Necessary to work around a `UIScrollView` behavior difference on Mac. See `scrollViewDidScroll`
// and `preventLargeOverScrollIfNeeded` for more context.
private lazy var isRunningOnMac: Bool = {
if #available(iOS 13.0, *) {
if ProcessInfo.processInfo.isMacCatalystApp {
return true
}
}
return false
}()
private var initialItemViewWasFocused = false {
didSet {
guard initialItemViewWasFocused != oldValue else { return }
setNeedsLayout()
layoutIfNeeded()
}
}
private var isReadyForLayout: Bool {
// There's no reason to attempt layout unless we have a non-zero `bounds.size`. We'll have a
// non-zero size once the `frame` is set to something non-zero, either manually or via the
// Auto Layout engine.
bounds.size != .zero
}
private var scale: CGFloat {
let scale = traitCollection.displayScale
// The documentation mentions that 0 is a possible value, so we guard against this.
// It's unclear whether values between 0 and 1 are possible, otherwise `max(scale, 1)` would
// suffice.
return scale > 0 ? scale : 1
}
private var scrollMetricsMutator: ScrollMetricsMutator {
let scrollAxis: ScrollAxis
switch content.monthsLayout {
case .vertical: scrollAxis = .vertical
case .horizontal: scrollAxis = .horizontal
}
let scrollMetricsMutator: ScrollMetricsMutator
if let previousScrollMetricsMutator = _scrollMetricsMutator {
if scrollAxis != previousScrollMetricsMutator.scrollAxis {
scrollMetricsMutator = ScrollMetricsMutator(
scrollMetricsProvider: scrollView,
scrollAxis: scrollAxis)
} else {
scrollMetricsMutator = previousScrollMetricsMutator
}
} else {
scrollMetricsMutator = ScrollMetricsMutator(
scrollMetricsProvider: scrollView,
scrollAxis: scrollAxis)
}
_scrollMetricsMutator = scrollMetricsMutator
return scrollMetricsMutator
}
private var visibleItemsProvider: VisibleItemsProvider {
if
let existingVisibleItemsProvider = _visibleItemsProvider,
existingVisibleItemsProvider.size == bounds.size,
existingVisibleItemsProvider.layoutMargins == directionalLayoutMargins,
existingVisibleItemsProvider.scale == scale,
existingVisibleItemsProvider.backgroundColor == backgroundColor
{
return existingVisibleItemsProvider
} else {
let visibleItemsProvider = VisibleItemsProvider(
calendar: calendar,
content: content,
size: bounds.size,
layoutMargins: directionalLayoutMargins,
scale: scale,
backgroundColor: backgroundColor)
_visibleItemsProvider = visibleItemsProvider
return visibleItemsProvider
}
}
private var maximumPerAnimationTickOffset: CGFloat {
switch content.monthsLayout {
case .vertical: return bounds.height
case .horizontal: return bounds.width
}
}
private var firstLayoutMarginValue: CGFloat {
switch content.monthsLayout {
case .vertical: return directionalLayoutMargins.top
case .horizontal: return directionalLayoutMargins.leading
}
}
private var lastLayoutMarginValue: CGFloat {
switch content.monthsLayout {
case .vertical: return directionalLayoutMargins.bottom
case .horizontal: return directionalLayoutMargins.trailing
}
}
private func commonInit() {
if #available(iOS 13.0, *) {
backgroundColor = .systemBackground
} else {
backgroundColor = .white
}
// Must be the first subview so that `UINavigationController` can monitor its scroll position
// and make navigation bars opaque on scroll.
insertSubview(scrollView, at: 0)
installDoubleLayoutPassSizingLabel()
setContent(content)
NotificationCenter.default.addObserver(
self,
selector: #selector(accessibilityElementFocused(_:)),
name: UIAccessibility.elementFocusedNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(setNeedsLayout),
name: UIAccessibility.voiceOverStatusDidChangeNotification,
object: nil)
}
private func anchorLayoutItem(
for scrollToItemContext: ScrollToItemContext,
visibleItemsProvider: VisibleItemsProvider)
-> LayoutItem
{
let offset: CGPoint
switch scrollMetricsMutator.scrollAxis {
case .vertical:
offset = CGPoint(
x: scrollView.contentOffset.x + directionalLayoutMargins.leading,
y: scrollView.contentOffset.y)
case .horizontal:
offset = CGPoint(
x: scrollView.contentOffset.x,
y: scrollView.contentOffset.y + directionalLayoutMargins.top)
}
switch scrollToItemContext.targetItem {
case .month(let month):
return visibleItemsProvider.anchorMonthHeaderItem(
for: month,
offset: offset,
scrollPosition: scrollToItemContext.scrollPosition)
case .day(let day):
return visibleItemsProvider.anchorDayItem(
for: day,
offset: offset,
scrollPosition: scrollToItemContext.scrollPosition)
}
}
private func positionRelativeToVisibleBounds(
for targetItem: ScrollToItemContext.TargetItem)
-> ScrollToItemContext.PositionRelativeToVisibleBounds?
{
guard let visibleItemsDetails else { return nil }
switch targetItem {
case .month(let month):
let monthHeaderItemType = LayoutItem.ItemType.monthHeader(month)
if let monthFrame = visibleItemsDetails.framesForVisibleMonths[month] {
return .partiallyOrFullyVisible(frame: monthFrame)
} else if monthHeaderItemType < visibleItemsDetails.centermostLayoutItem.itemType {
return .before
} else if monthHeaderItemType > visibleItemsDetails.centermostLayoutItem.itemType {
return .after
} else {
preconditionFailure("Could not find a corresponding frame for \(month).")
}
case .day(let day):
let dayLayoutItemType = LayoutItem.ItemType.day(day)
if let dayFrame = visibleItemsDetails.framesForVisibleDays[day] {
return .partiallyOrFullyVisible(frame: dayFrame)
} else if dayLayoutItemType < visibleItemsDetails.centermostLayoutItem.itemType {
return .before
} else if dayLayoutItemType > visibleItemsDetails.centermostLayoutItem.itemType {
return .after
} else {
preconditionFailure("Could not find a corresponding frame for \(day).")
}
}
}
// This exists so that we can force a layout ourselves in preparation for an animated update.
private func _layoutSubviews(extendLayoutRegion: Bool) {
scrollView.performWithoutNotifyingDelegate {
scrollMetricsMutator.setUpInitialMetricsIfNeeded()
scrollMetricsMutator.updateContentSizePerpendicularToScrollAxis(viewportSize: bounds.size)
}
let anchorLayoutItem: LayoutItem
if let scrollToItemContext, !scrollToItemContext.animated {
anchorLayoutItem = self.anchorLayoutItem(
for: scrollToItemContext,
visibleItemsProvider: visibleItemsProvider)
// Clear the `scrollToItemContext` once we use it. This could happen over the course of
// several layout pass attempts since `isReadyForLayout` might be false initially.
self.scrollToItemContext = nil
} else if let previousAnchorLayoutItem = self.anchorLayoutItem {
anchorLayoutItem = previousAnchorLayoutItem
} else {
let initialScrollToItemContext = ScrollToItemContext(
targetItem: .month(content.monthRange.lowerBound),
scrollPosition: .firstFullyVisiblePosition,
animated: false)
anchorLayoutItem = self.anchorLayoutItem(
for: initialScrollToItemContext,
visibleItemsProvider: visibleItemsProvider)
}
let currentVisibleItemsDetails = visibleItemsProvider.detailsForVisibleItems(
surroundingPreviouslyVisibleLayoutItem: anchorLayoutItem,
offset: scrollView.contentOffset,
extendLayoutRegion: extendLayoutRegion)
self.anchorLayoutItem = currentVisibleItemsDetails.centermostLayoutItem
updateVisibleViews(withVisibleItems: currentVisibleItemsDetails.visibleItems)
visibleItemsDetails = currentVisibleItemsDetails
let minimumScrollOffset = visibleItemsDetails?.contentStartBoundary.map {
($0 - firstLayoutMarginValue).alignedToPixel(forScreenWithScale: scale)
}
let maximumScrollOffset = visibleItemsDetails?.contentEndBoundary.map {
($0 + lastLayoutMarginValue).alignedToPixel(forScreenWithScale: scale)
}
scrollView.performWithoutNotifyingDelegate {
scrollMetricsMutator.updateScrollBoundaries(
minimumScrollOffset: minimumScrollOffset,
maximumScrollOffset: maximumScrollOffset)
}
scrollView.cachedAccessibilityElements = nil
}
private func updateVisibleViews(withVisibleItems visibleItems: Set<VisibleItem>) {
var viewsToHideForVisibleItems = visibleViewsForVisibleItems
visibleViewsForVisibleItems.removeAll(keepingCapacity: true)
let contexts = reuseManager.reusedViewContexts(
visibleItems: visibleItems,
reuseUnusedViews: !UIAccessibility.isVoiceOverRunning)
for context in contexts {
UIView.conditionallyPerformWithoutAnimation(when: !context.isReusedViewSameAsPreviousView) {
if context.view.superview == nil {
let insertionIndex = subviewInsertionIndexTracker.insertionIndex(
forSubviewWithCorrespondingItemType: context.visibleItem.itemType)
scrollView.insertSubview(context.view, at: insertionIndex)
}
context.view.isHidden = false
configureView(context.view, with: context.visibleItem)
}
visibleViewsForVisibleItems[context.visibleItem] = context.view
if context.isViewReused {
// Don't hide views that were reused
viewsToHideForVisibleItems.removeValue(forKey: context.visibleItem)
}
}
// Hide any old views that weren't reused. This is faster than adding / removing subviews.
// If VoiceOver is running, we remove the view to save memory (since views aren't reused).
for (visibleItem, viewToHide) in viewsToHideForVisibleItems {
if UIAccessibility.isVoiceOverRunning {
viewToHide.removeFromSuperview()
subviewInsertionIndexTracker.removedSubview(withCorrespondingItemType: visibleItem.itemType)
} else {
viewToHide.isHidden = true
}
}
}
private func configureView(_ view: ItemView, with visibleItem: VisibleItem) {
let calendarItemModel = visibleItem.calendarItemModel
view.calendarItemModel = calendarItemModel
view.itemType = visibleItem.itemType
view.frame = visibleItem.frame.alignedToPixels(forScreenWithScale: scale)
if traitCollection.layoutDirection == .rightToLeft {
view.transform = .init(scaleX: -1, y: 1)
} else {
view.transform = .identity
}
// Set up the selection handler
if case .layoutItemType(.day(let day)) = visibleItem.itemType {
view.selectionHandler = { [weak self] in
self?.daySelectionHandler?(day)
}
} else {
view.selectionHandler = nil
}
}
private func startScrollingTowardTargetItem() {
let scrollToItemDisplayLink = CADisplayLink(
target: self,
selector: #selector(scrollToItemDisplayLinkFired))
scrollToItemAnimationStartTime = CACurrentMediaTime()
if #available(iOS 15.0, *) {
#if swift(>=5.5) // Allows us to still build using Xcode 12
scrollToItemDisplayLink.preferredFrameRateRange = CAFrameRateRange(
minimum: 80,
maximum: 120,
preferred: 120)
#endif
}
scrollToItemDisplayLink.add(to: .main, forMode: .common)
self.scrollToItemDisplayLink = scrollToItemDisplayLink
}
private func finalizeScrollingTowardItem(for scrollToItemContext: ScrollToItemContext) {
self.scrollToItemContext = ScrollToItemContext(
targetItem: scrollToItemContext.targetItem,
scrollPosition: scrollToItemContext.scrollPosition,
animated: false)
}
@objc
private func scrollToItemDisplayLinkFired() {
guard
let scrollToItemContext,
let animationStartTime = scrollToItemAnimationStartTime
else {
preconditionFailure("""
Expected `scrollToItemContext`, `animationStartTime`, and `scrollMetricsMutator` to be
non-nil when animating toward an item.
""")
}
guard scrollToItemContext.animated else {
preconditionFailure(
"The scroll-to-item animation display link fired despite no animation being needed.")
}
guard isReadyForLayout else { return }
let positionBeforeLayout = positionRelativeToVisibleBounds(for: scrollToItemContext.targetItem)
let secondsSinceAnimationStart = CACurrentMediaTime() - animationStartTime
let offset = maximumPerAnimationTickOffset * CGFloat(min(secondsSinceAnimationStart / 5, 1))
switch positionBeforeLayout {
case .before:
scrollMetricsMutator.applyOffset(-offset)
case .after:
scrollMetricsMutator.applyOffset(offset)
case .partiallyOrFullyVisible(let frame):
let targetPosition: CGFloat
let currentPosition: CGFloat
switch content.monthsLayout {
case .vertical:
targetPosition = anchorLayoutItem(
for: scrollToItemContext,
visibleItemsProvider: visibleItemsProvider)
.frame.minY
currentPosition = frame.minY
case .horizontal:
targetPosition = anchorLayoutItem(
for: scrollToItemContext,
visibleItemsProvider: visibleItemsProvider)
.frame.minX
currentPosition = frame.minX
}
let distanceToTargetPosition = currentPosition - targetPosition
if distanceToTargetPosition <= -1 {
scrollMetricsMutator.applyOffset(max(-offset, distanceToTargetPosition))
} else if distanceToTargetPosition >= 1 {
scrollMetricsMutator.applyOffset(min(offset, distanceToTargetPosition))
} else {
finalizeScrollingTowardItem(for: scrollToItemContext)
}
case .none:
break
}
setNeedsLayout()
layoutIfNeeded()
// If we overshoot our target item, then finalize the animation immediately. In practice, this
// will only happen if the maximum per-animation-tick offset is greater than the viewport size.
let positionAfterLayout = positionRelativeToVisibleBounds(for: scrollToItemContext.targetItem)
switch (positionBeforeLayout, positionAfterLayout) {
case (.before, .after), (.after, .before):
finalizeScrollingTowardItem(for: scrollToItemContext)
// Force layout immediately to prevent the overshoot from being visible to the user.
setNeedsLayout()
layoutIfNeeded()
default:
break
}
}
private func maintainScrollPositionAfterBoundsOrMarginsChange() {
guard
!scrollView.isDragging,
let framesForVisibleMonths = visibleItemsDetails?.framesForVisibleMonths,
let firstVisibleMonth = visibleMonthRange?.lowerBound,
let frameOfFirstVisibleMonth = framesForVisibleMonths[firstVisibleMonth]
else {
return
}
let paddingFromFirstEdge: CGFloat
switch content.monthsLayout {
case .vertical:
paddingFromFirstEdge = frameOfFirstVisibleMonth.minY -
scrollView.contentOffset.y -
(visibleItemsDetails?.heightOfPinnedContent ?? 0)
case .horizontal:
paddingFromFirstEdge = frameOfFirstVisibleMonth.minX - scrollView.contentOffset.x
}
if let existingScrollToItemContext = scrollToItemContext {
let scrollPosition: CalendarViewScrollPosition
switch existingScrollToItemContext.scrollPosition {
case .firstFullyVisiblePosition:
scrollPosition = .firstFullyVisiblePosition(padding: paddingFromFirstEdge)
default:
scrollPosition = existingScrollToItemContext.scrollPosition
}
scrollToItemContext = ScrollToItemContext(
targetItem: existingScrollToItemContext.targetItem,
scrollPosition: scrollPosition,
animated: false)
} else {
scrollToItemContext = ScrollToItemContext(
targetItem: .month(firstVisibleMonth),
scrollPosition: .firstFullyVisiblePosition(padding: paddingFromFirstEdge),
animated: false)
}
}
private func configureMultiDaySelectionPanGestureRecognizer() {
if multiDaySelectionDragHandler == nil {
removeGestureRecognizer(multiDaySelectionLongPressGestureRecognizer)
removeGestureRecognizer(multiDaySelectionPanGestureRecognizer)
} else {
addGestureRecognizer(multiDaySelectionLongPressGestureRecognizer)
addGestureRecognizer(multiDaySelectionPanGestureRecognizer)
}
}
@objc
private func multiDaySelectionGestureRecognized(_ gestureRecognizer: UIGestureRecognizer) {
guard gestureRecognizer.state != .possible else { return }
// If the user interacts with the drag gesture, we should clear out any existing
// `scrollToItemContext` that might be leftover from the initial layout process.
scrollToItemContext = nil
updateSelectedDayRange(gestureRecognizer: gestureRecognizer)