Blame view

Pods/JTAppleCalendar/Sources/JTAppleCalendarLayout.swift 30.2 KB
75d24c15   yangbin   123
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
  //
  //  JTAppleCalendarLayout.swift
  //
  //  Copyright (c) 2016-2017 JTAppleCalendar (https://github.com/patchthecode/JTAppleCalendar)
  //
  //  Permission is hereby granted, free of charge, to any person obtaining a copy
  //  of this software and associated documentation files (the "Software"), to deal
  //  in the Software without restriction, including without limitation the rights
  //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  //  copies of the Software, and to permit persons to whom the Software is
  //  furnished to do so, subject to the following conditions:
  //
  //  The above copyright notice and this permission notice shall be included in
  //  all copies or substantial portions of the Software.
  //
  //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  //  THE SOFTWARE.
  //
  
  /// Methods in this class are meant to be overridden and will be called by its collection view to gather layout information.
  class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutProtocol {
      
      var allowsDateCellStretching = true
      var firstContentOffsetWasSet = false
      
      var lastSetCollectionViewSize: CGRect = .zero
      
      var cellSize: CGSize = CGSize.zero
      var shouldUseUserItemSizeInsteadOfDefault: Bool { return delegate.cellSize == 0 ? false: true }
      var scrollDirection: UICollectionView.ScrollDirection = .horizontal
      var maxMissCount: Int = 0
      var cellCache: [Int: [(Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)]] = [:]
      var headerCache: [Int: (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)] = [:]
      var decorationCache: [IndexPath:UICollectionViewLayoutAttributes] = [:]
      var sectionSize: [CGFloat] = []
      var lastWrittenCellAttribute: (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)!
      var stride: CGFloat = 0
      var minimumInteritemSpacing: CGFloat = 0
      var minimumLineSpacing: CGFloat = 0
      var sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
      var headerSizes: [AnyHashable:CGFloat] = [:]
      var focusIndexPath: IndexPath?
      var isCalendarLayoutLoaded: Bool { return !cellCache.isEmpty }
      var layoutIsReadyToBePrepared: Bool { return !(!cellCache.isEmpty  || delegate.calendarDataSource == nil) }
  
      var monthMap: [Int: Int] = [:]
      var numberOfRows: Int = 0
      var strictBoundaryRulesShouldApply: Bool = false
      var thereAreHeaders: Bool { return !headerSizes.isEmpty }
      var thereAreDecorationViews = false
      
      weak var delegate: JTAppleCalendarDelegateProtocol!
      
      var currentHeader: (section: Int, size: CGSize)? // Tracks the current header size
      var currentCell: (section: Int, width: CGFloat, height: CGFloat)? // Tracks the current cell size
      var contentHeight: CGFloat = 0 // Content height of calendarView
      var contentWidth: CGFloat = 0 // Content wifth of calendarView
      var xCellOffset: CGFloat = 0
      var yCellOffset: CGFloat = 0
      var endSeparator: CGFloat = 0
      
      var delayedExecutionClosure: [(() -> Void)] = []
      func executeDelayedTasks() {
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
              let tasksToExecute = self.delayedExecutionClosure
              self.delayedExecutionClosure.removeAll()
              
              for aTaskToExecute in tasksToExecute {
                  aTaskToExecute()
              }
          }
      }
      
      var daysInSection: [Int: Int] = [:] // temporary caching
      var monthInfo: [Month] = []
      
      var reloadWasTriggered = false
      var isDirty: Bool {
          return updatedLayoutCellSize != cellSize
      }
      
      var updatedLayoutCellSize: CGSize {
          guard let cachedConfiguration = delegate._cachedConfiguration else { return .zero }
          
          // Default Item height and width
          var height: CGFloat = collectionView!.bounds.size.height / CGFloat(cachedConfiguration.numberOfRows)
          var width: CGFloat = collectionView!.bounds.size.width / CGFloat(maxNumberOfDaysInWeek)
          
          if shouldUseUserItemSizeInsteadOfDefault { // If delegate item size was set
              if scrollDirection == .horizontal {
                  width = delegate.cellSize
              } else {
                  height = delegate.cellSize
              }
          }
          
          return CGSize(width: width, height: height)
      }
      
      open override func register(_ nib: UINib?, forDecorationViewOfKind elementKind: String) {
          super.register(nib, forDecorationViewOfKind: elementKind)
          thereAreDecorationViews = true
      }
      
      open override func register(_ viewClass: AnyClass?, forDecorationViewOfKind elementKind: String) {
          super.register(viewClass, forDecorationViewOfKind: elementKind)
          thereAreDecorationViews = true
      }
  
      init(withDelegate delegate: JTAppleCalendarDelegateProtocol) {
          super.init()
          self.delegate = delegate
      }
      /// Tells the layout object to update the current layout.
      open override func prepare() {
          
          // set the last content size before the if statement which can possible return if layout is not yet ready to be prepared. Avoids inf loop
          // with layout subviews
          lastSetCollectionViewSize = collectionView!.frame
          
          if !layoutIsReadyToBePrepared {
              // Layoout may not be ready, but user might have reloaded with an anchor date
              let requestedOffset = delegate.requestedContentOffset
              if requestedOffset != .zero { collectionView!.setContentOffset(requestedOffset, animated: false) }
              
              // execute any other delayed tasks
              executeDelayedTasks()
              return
          }
          
          setupDataFromDelegate()
          
          if scrollDirection == .vertical {
              configureVerticalLayout()
          } else {
              configureHorizontalLayout()
          }
          
          // Get rid of header data if dev didnt register headers.
          // They were used for calculation but are not needed to be displayed
          if !thereAreHeaders {
              headerCache.removeAll()
          }
          
          // Set the first content offset only once. This will prevent scrolling animation on viewDidload.
          if !firstContentOffsetWasSet {
              firstContentOffsetWasSet = true
              let firstContentOffset = delegate.requestedContentOffset
              collectionView!.setContentOffset(firstContentOffset, animated: false)
          }
          daysInSection.removeAll() // Clear chache
          reloadWasTriggered = false
          executeDelayedTasks()
      }
      
      func setupDataFromDelegate() {
          // get information from the delegate
          headerSizes = delegate.sizesForMonthSection() // update first. Other variables below depend on it
          strictBoundaryRulesShouldApply = thereAreHeaders || delegate._cachedConfiguration.hasStrictBoundaries
          numberOfRows = delegate._cachedConfiguration.numberOfRows
          monthMap = delegate.monthMap
          allowsDateCellStretching = delegate.allowsDateCellStretching
          monthInfo = delegate.monthInfo
          scrollDirection = delegate.scrollDirection
          maxMissCount = scrollDirection == .horizontal ? maxNumberOfRowsPerMonth : maxNumberOfDaysInWeek
          minimumInteritemSpacing = delegate.minimumInteritemSpacing
          minimumLineSpacing = delegate.minimumLineSpacing
          sectionInset = delegate.sectionInset
          cellSize = updatedLayoutCellSize
      }
      
      func indexPath(direction: SegmentDestination, of section:Int, item: Int) -> IndexPath? {
          var retval: IndexPath?
          switch direction {
          case .next:
              if let data = cellCache[section], !data.isEmpty, 0..<data.count ~= item + 1 {
                  retval = IndexPath(item: item + 1, section: section)
              } else if let data = cellCache[section + 1], !data.isEmpty {
                  retval = IndexPath(item: 0, section: section + 1)
              }
          case .previous:
              if let data = cellCache[section], !data.isEmpty, 0..<data.count ~= item - 1 {
                  retval = IndexPath(item: item - 1, section: section)
              } else if let data = cellCache[section - 1], !data.isEmpty {
                  retval = IndexPath(item: data.count - 1, section: section - 1)
              }
          default:
              break
          }
          return retval
      }
  
      
      func configureHorizontalLayout() {
          var section = 0
          var totalDayCounter = 0
          var headerGuide = 0
          let fullSection = numberOfRows * maxNumberOfDaysInWeek
          var extra = 0
          
          
          xCellOffset = sectionInset.left
          endSeparator = sectionInset.left + sectionInset.right
          
          
          for aMonth in monthInfo {
              for numberOfDaysInCurrentSection in aMonth.sections {
                  // Generate and cache the headers
                  if let aHeaderAttr = determineToApplySupplementaryAttribs(0, section: section) {
                      headerCache[section] = aHeaderAttr
                      if strictBoundaryRulesShouldApply {
                          contentWidth += aHeaderAttr.4
                          yCellOffset = aHeaderAttr.5
                      }
                  }
                  // Generate and cache the cells
                  for item in 0..<numberOfDaysInCurrentSection {
                      guard let attribute = determineToApplyAttribs(item, section: section)  else { continue }
                      if cellCache[section] == nil {
                          cellCache[section] = []
                      }
                      cellCache[section]!.append(attribute)
                      lastWrittenCellAttribute = attribute
                      xCellOffset += attribute.4
                      
                      if strictBoundaryRulesShouldApply {
                          headerGuide += 1
                          if numberOfDaysInCurrentSection - 1 == item || headerGuide % maxNumberOfDaysInWeek == 0 {
                              // We are at the last item in the section
                              // && if we have headers
                              headerGuide = 0
                              xCellOffset = sectionInset.left
                              yCellOffset += attribute.5
                          }
                      } else {
                          totalDayCounter += 1
                          extra += 1
                          if totalDayCounter % fullSection == 0 { // If you have a full section
                              xCellOffset = sectionInset.left
                              yCellOffset = 0
                              contentWidth += attribute.4 * 7
                              stride = contentWidth
                              sectionSize.append(contentWidth)
                          } else {
                              if totalDayCounter >= delegate.totalDays {
                                  contentWidth += attribute.4 * 7
                                  sectionSize.append(contentWidth)
                              }
                              
                              if totalDayCounter % maxNumberOfDaysInWeek == 0 {
                                  xCellOffset = sectionInset.left
                                  yCellOffset += attribute.5
                              }
                          }
                      }
                      
                  }
                  // Save the content size for each section
                  contentWidth += endSeparator
                  if strictBoundaryRulesShouldApply {
                      sectionSize.append(contentWidth)
                      stride = sectionSize[section]
                  }
                  section += 1
              }
          }
          contentHeight = self.collectionView!.bounds.size.height
      }
      
      func configureVerticalLayout() {
          var section = 0
          var totalDayCounter = 0
          var headerGuide = 0
          
          xCellOffset = sectionInset.left
          yCellOffset = sectionInset.top
          endSeparator = sectionInset.top + sectionInset.bottom
          
          for aMonth in monthInfo {
              for numberOfDaysInCurrentSection in aMonth.sections {
                  // Generate and cache the headers
                  if strictBoundaryRulesShouldApply {
                      if let aHeaderAttr = determineToApplySupplementaryAttribs(0, section: section) {
                          headerCache[section] = aHeaderAttr
                          yCellOffset += aHeaderAttr.5
                          contentHeight += aHeaderAttr.5
                      }
                  }
                  // Generate and cache the cells
                  for item in 0..<numberOfDaysInCurrentSection {
                      guard let attribute = determineToApplyAttribs(item, section: section) else { continue }
                      if cellCache[section] == nil {
                          cellCache[section] = []
                      }
                      cellCache[section]!.append(attribute)
                      lastWrittenCellAttribute = attribute
                      xCellOffset += attribute.4
                      if strictBoundaryRulesShouldApply {
                          headerGuide += 1
                          if headerGuide % maxNumberOfDaysInWeek == 0 || numberOfDaysInCurrentSection - 1 == item {
                              // We are at the last item in the
                              // section && if we have headers
                              headerGuide = 0
                              xCellOffset = sectionInset.left
                              yCellOffset += attribute.5
                              contentHeight += attribute.5
                          }
                      } else {
                          totalDayCounter += 1
                          if totalDayCounter % maxNumberOfDaysInWeek == 0 {
                              xCellOffset = sectionInset.left
                              yCellOffset += attribute.5
                              contentHeight += attribute.5
                          } else if totalDayCounter == delegate.totalDays {
                              contentHeight += attribute.5
                          }
                      }
                  }
                  // Save the content size for each section
                  contentHeight += endSeparator
                  yCellOffset += endSeparator
                  sectionSize.append(contentHeight)
                  section += 1
              }
          }
          contentWidth = self.collectionView!.bounds.size.width
      }
      
      /// Returns the width and height of the collection view’s contents.
      /// The width and height of the collection view’s contents.
      open override var collectionViewContentSize: CGSize {
          return CGSize(width: contentWidth, height: contentHeight)
      }
      override func invalidateLayout() {
          super.invalidateLayout()
          
          if isDirty && reloadWasTriggered {
              clearCache()
          }
      }
      
      override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
          return
              abs(lastSetCollectionViewSize.height - newBounds.height) > errorDelta ||
              abs(lastSetCollectionViewSize.width - newBounds.width) > errorDelta
      }
      
      /// Returns the layout attributes for all of the cells
      /// and views in the specified rectangle.
      override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
          let startSectionIndex = startIndexFrom(rectOrigin: rect.origin)
          // keep looping until there were no interception rects
          var attributes: [UICollectionViewLayoutAttributes] = []
          var beganIntercepting = false
          var missCount = 0
          
          outterLoop: for sectionIndex in startSectionIndex..<cellCache.count {
              if let validSection = cellCache[sectionIndex], !validSection.isEmpty {
                  if thereAreDecorationViews {
                      let attrib = layoutAttributesForDecorationView(ofKind: decorationViewID, at: IndexPath(item: 0, section: sectionIndex))!
                      attributes.append(attrib)
                  }
                  
                  // Add header view attributes
                  if thereAreHeaders {
                      let data = headerCache[sectionIndex]!
  
                      if CGRect(x: data.2, y: data.3, width: data.4, height: data.5).intersects(rect) {
                          let attrib = layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath(item: data.0, section: data.1))
                          attributes.append(attrib!)
                      }
                  }
                  
                  for val in validSection {
                      if CGRect(x: val.2, y: val.3, width: val.4, height: val.5).intersects(rect) {
                          missCount = 0
                          beganIntercepting = true
                          let attrib = layoutAttributesForItem(at: IndexPath(item: val.0, section: val.1))
                          attributes.append(attrib!)
                      } else {
                          missCount += 1
                          // If there are at least 8 misses in a row
                          // since intercepting began, then this
                          // section has no more interceptions.
                          // So break
                          if missCount > maxMissCount && beganIntercepting { break outterLoop }
                      }
                  }
              }
          }
          return attributes
      }
      
      /// Returns the layout attributes for the item at the specified index
      /// path. A layout attributes object containing the information to apply
      /// to the item’s cell.
      override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
          // If this index is already cached, then return it else,
          // apply a new layout attribut to it
          if let alreadyCachedCellAttrib = cellAttributeFor(indexPath.item, section: indexPath.section) {
              return alreadyCachedCellAttrib
          }
          return nil//deterimeToApplyAttribs(indexPath.item, section: indexPath.section)
      }
      
      func supplementaryAttributeFor(item: Int, section: Int, elementKind: String) -> UICollectionViewLayoutAttributes? {
          var retval: UICollectionViewLayoutAttributes?
          if let cachedData = headerCache[section] {
              
              let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: IndexPath(item: item, section: section))
              attributes.frame = CGRect(x: cachedData.2, y: cachedData.3, width: cachedData.4, height: cachedData.5)
              retval = attributes
          }
          return retval
      }
      
      func cachedValue(for item: Int, section: Int) -> (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)? {
          if
              let alreadyCachedCellAttrib = cellCache[section],
              item < alreadyCachedCellAttrib.count,
              item >= 0 {
              
              return alreadyCachedCellAttrib[item]
          }
          return nil
      }
      func cellAttributeFor(_ item: Int, section: Int) -> UICollectionViewLayoutAttributes? {
          guard let cachedValue = cachedValue(for: item, section: section) else { return nil }
          let attrib = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: item, section: section))
          
          attrib.frame = CGRect(x: cachedValue.2, y: cachedValue.3, width: cachedValue.4, height: cachedValue.5)
          if minimumInteritemSpacing > -1, minimumLineSpacing > -1 {
              var frame = attrib.frame.insetBy(dx: minimumInteritemSpacing, dy: minimumLineSpacing)
              if frame == .null {
                  frame = attrib.frame.insetBy(dx: 0, dy: 0)
              }
              attrib.frame = frame
          }
          return attrib
      }
      
      func determineToApplyAttribs(_ item: Int, section: Int) -> (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)? {
          let monthIndex = monthMap[section]!
          let numberOfDays = numberOfDaysInSection(monthIndex)
          // return nil on invalid range
          if !(0...monthMap.count ~= section) || !(0...numberOfDays  ~= item) { return nil }
          
          let size = sizeForitemAtIndexPath(item, section: section)
          let y = scrollDirection == .horizontal ? yCellOffset + sectionInset.top : yCellOffset
          return (item, section, xCellOffset + stride, y, size.width, size.height)
      }
      
      func determineToApplySupplementaryAttribs(_ item: Int, section: Int) -> (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)? {
          var retval: (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)?
          
          let headerHeight = cachedHeaderHeightForSection(section)
          
          switch scrollDirection {
          case .horizontal:
              let modifiedSize = sizeForitemAtIndexPath(item, section: section)
              let width = (modifiedSize.width * 7)
              retval = (item, section, contentWidth + sectionInset.left, sectionInset.top, width , headerHeight)
          case .vertical:
              // Use the calculaed header size and force the width
              // of the header to take up 7 columns
              // We cache the header here so we dont call the
              // delegate so much
              
              let modifiedSize = (width: collectionView!.frame.width, height: headerHeight)
              retval = (item, section, sectionInset.left, yCellOffset , modifiedSize.width - (sectionInset.left + sectionInset.right), modifiedSize.height)
          }
          if retval?.4 == 0, retval?.5 == 0 {
              return nil
          }
          return retval
      }
      
      open override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
          if let alreadyCachedVal = decorationCache[indexPath] { return alreadyCachedVal }
          
          let retval = UICollectionViewLayoutAttributes(forDecorationViewOfKind: decorationViewID, with: indexPath)
          decorationCache[indexPath] = retval
          retval.frame = delegate.sizeOfDecorationView(indexPath: indexPath)
          retval.zIndex = -1
          return retval
      }
      
      
      /// Returns the layout attributes for the specified supplementary view.
      open override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
          if let alreadyCachedHeaderAttrib = supplementaryAttributeFor(item: indexPath.item, section: indexPath.section, elementKind: elementKind) {
              return alreadyCachedHeaderAttrib
          }
          
          return nil
      }
      
      func numberOfDaysInSection(_ index: Int) -> Int {
          if let days = daysInSection[index] {
              return days
          }
          let days = monthInfo[index].numberOfDaysInMonthGrid
          daysInSection[index] = days
          return days
      }
      
      func cachedHeaderHeightForSection(_ section: Int) -> CGFloat {
          var retval: CGFloat = 0
          // We look for most specific to less specific
          // Section = specific dates
          // Months = generic months
          // Default = final resort
          
          if let height = headerSizes[section] {
              retval = height
          } else {
              let monthIndex = monthMap[section]!
              let monthName = monthInfo[monthIndex].name
              if let height = headerSizes[monthName] {
                  retval = height
              } else if let height = headerSizes["default"] {
                  retval = height
              }
          }
  
          return retval
      }
      
      func sizeForitemAtIndexPath(_ item: Int, section: Int) -> (width: CGFloat, height: CGFloat) {
          if let cachedCell  = currentCell,
              cachedCell.section == section {
              
              if !strictBoundaryRulesShouldApply, scrollDirection == .horizontal,
                  !cellCache.isEmpty {
                  
                  if let x = cellCache[0]?[0] {
                      return (x.4, x.5)
                  } else {
                      return (0, 0)
                  }
              } else {
                  return (cachedCell.width, cachedCell.height)
              }
          }
          let width = cellSize.width - ((sectionInset.left / 7) + (sectionInset.right / 7))
          var size: (width: CGFloat, height: CGFloat) = (width, cellSize.height)
          if shouldUseUserItemSizeInsteadOfDefault {
              if scrollDirection == .vertical {
                  size.height = cellSize.height
              } else {
                  size.width = cellSize.width
                  let headerHeight =  strictBoundaryRulesShouldApply ? cachedHeaderHeightForSection(section) : 0
                  let currentMonth = monthInfo[monthMap[section]!]
                  let recalculatedNumOfRows = allowsDateCellStretching ? CGFloat(currentMonth.maxNumberOfRowsForFull(developerSetRows: numberOfRows)) : CGFloat(maxNumberOfRowsPerMonth)
                  size.height = (collectionView!.frame.height - headerHeight - sectionInset.top - sectionInset.bottom) / recalculatedNumOfRows
                  currentCell = (section: section, width: size.width, height: size.height)
              }
          } else {
              // Get header size if it already cached
              let headerHeight =  strictBoundaryRulesShouldApply ? cachedHeaderHeightForSection(section) : 0
              var height: CGFloat = 0
              let currentMonth = monthInfo[monthMap[section]!]
              let numberOfRowsForSection: Int
              if allowsDateCellStretching {
                  numberOfRowsForSection
                      = strictBoundaryRulesShouldApply ? currentMonth.maxNumberOfRowsForFull(developerSetRows: numberOfRows) : numberOfRows
              } else {
                  numberOfRowsForSection = maxNumberOfRowsPerMonth
              }
              height      = (collectionView!.frame.height - headerHeight - sectionInset.top - sectionInset.bottom) / CGFloat(numberOfRowsForSection)
              size.height = height > 0 ? height : 0
              currentCell = (section: section, width: size.width, height: size.height)
          }
          return size
      }
      
      func numberOfRowsForMonth(_ index: Int) -> Int {
          let monthIndex = monthMap[index]!
          return monthInfo[monthIndex].rows
      }
      
      func startIndexFrom(rectOrigin offset: CGPoint) -> Int {
          let key =  scrollDirection == .horizontal ? offset.x : offset.y
          return startIndexBinarySearch(sectionSize, offset: key)
      }
      
      func sizeOfContentForSection(_ section: Int) -> CGFloat {
          switch scrollDirection {
          case .horizontal:
              return cellCache[section]![0].4 * CGFloat(maxNumberOfDaysInWeek) + sectionInset.left + sectionInset.right
          case .vertical:
              let headerSizeOfSection = !headerCache.isEmpty ? headerCache[section]!.5 : 0
              return cellCache[section]![0].5 * CGFloat(numberOfRowsForMonth(section)) + headerSizeOfSection
          }
      }
      
      func sectionFromOffset(_ theOffSet: CGFloat) -> Int {
          var val: Int = 0
          for (index, sectionSizeValue) in sectionSize.enumerated() {
              if abs(theOffSet - sectionSizeValue) < errorDelta {
                  continue
              }
              if theOffSet < sectionSizeValue {
                  val = index
                  break
              }
          }
          return val
      }
      
      func startIndexBinarySearch<T: Comparable>(_ val: [T], offset: T) -> Int {
          if val.count < 3 {
              return 0
          } // If the range is less than 2 just break here.
          var midIndex: Int = 0
          var startIndex = 0
          var endIndex = val.count - 1
          while startIndex < endIndex {
              midIndex = startIndex + (endIndex - startIndex) / 2
              if midIndex + 1  >= val.count || offset >= val[midIndex] &&
                  offset < val[midIndex + 1] ||  val[midIndex] == offset {
                  break
              } else if val[midIndex] < offset {
                  startIndex = midIndex + 1
              } else {
                  endIndex = midIndex
              }
          }
          return midIndex
      }
      
      /// Returns an object initialized from data in a given unarchiver.
      /// self, initialized using the data in decoder.
      required public init?(coder aDecoder: NSCoder) {
          delegate = (aDecoder.value(forKey: "delegate") as! JTAppleCalendarDelegateProtocol)
          cellCache = aDecoder.value(forKey: "delegate") as! [Int : [(Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)]]
          headerCache = aDecoder.value(forKey: "delegate") as! [Int : (Int, Int, CGFloat, CGFloat, CGFloat, CGFloat)]
          headerSizes = aDecoder.value(forKey: "delegate") as! [AnyHashable:CGFloat]
          super.init(coder: aDecoder)
      }
      
      // This function ignores decoration views //JT101 for setting proposal
      func minimumVisibleIndexPaths() -> (cellIndex: IndexPath?, headerIndex: IndexPath?) {
          let visibleItems: [UICollectionViewLayoutAttributes] = scrollDirection == .horizontal ? elementsAtRect(excludeHeaders: true) : elementsAtRect()
          
          var cells: [IndexPath] = []
          var headers: [IndexPath] = []
          for item in visibleItems {
              switch item.representedElementCategory {
              case .cell:
                  cells.append(item.indexPath)
              case .supplementaryView:
                  headers.append(item.indexPath)
              case .decorationView:
                  break
              }
          }
          return (cells.min(), headers.min())
      }
      
      func elementsAtRect(excludeHeaders: Bool? = false, from rect: CGRect? = nil) -> [UICollectionViewLayoutAttributes] {
          let aRect = rect ?? CGRect(x: collectionView!.contentOffset.x + 1, y: collectionView!.contentOffset.y + 1, width: collectionView!.frame.width - 2, height: collectionView!.frame.height - 2)
          guard let attributes = layoutAttributesForElements(in: aRect), !attributes.isEmpty else {
              return []
          }
          if excludeHeaders == true {
              return attributes.filter { $0.representedElementKind != UICollectionView.elementKindSectionHeader }
          }
          return attributes
      }
  
      func clearCache() {
          headerCache.removeAll()
          cellCache.removeAll()
          sectionSize.removeAll()
          decorationCache.removeAll()
          currentHeader = nil
          currentCell = nil
          lastWrittenCellAttribute = nil
          xCellOffset = 0
          yCellOffset = 0
          contentHeight = 0
          contentWidth = 0
          stride = 0
          firstContentOffsetWasSet = false
      }
  }