Blame view

Pods/JTAppleCalendar/Sources/UICollectionViewDelegates.swift 10.3 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
  //
  //  UICollectionViewDelegates.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.
  //
  
  extension JTAppleCalendarView: UICollectionViewDelegate, UICollectionViewDataSource {
      /// Asks your data source object to provide a
      /// supplementary view to display in the collection view.
      public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
          guard
              let validDate = monthInfoFromSection(indexPath.section),
              let delegate = calendarDelegate else {
                  developerError(string: "Either date could not be generated or delegate was nil")
                  assert(false, "Date could not be generated for section. This is a bug. Contact the developer")
                  return UICollectionReusableView()
          }
          
          let headerView = delegate.calendar(self, headerViewForDateRange: validDate.range, at: indexPath)
          if #available(iOS 9.0, *) {
              headerView.transform.a = semanticContentAttribute == .forceRightToLeft ? -1 : 1
          }
          return headerView
      }
      
      public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
          if !pathsToReload.contains(indexPath) { return }
          pathsToReload.remove(indexPath)
          let cellState: CellState
          if let validCachedCellState = selectedCellData[indexPath]?.cellState {
              cellState = validCachedCellState
          } else {
              cellState = cellStateFromIndexPath(indexPath)
          }
          calendarDelegate!.calendar(self, willDisplay: cell as! JTAppleCell, forItemAt: cellState.date, cellState: cellState, indexPath: indexPath)
      }
      
      /// Asks your data source object for the cell that corresponds
      /// to the specified item in the collection view.
      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          guard let delegate = calendarDelegate else {
              print("Your delegate does not conform to JTAppleCalendarViewDelegate")
              assert(false)
              return UICollectionViewCell()
          }
          let cellState = cellStateFromIndexPath(indexPath)
          let configuredCell = delegate.calendar(self, cellForItemAt: cellState.date, cellState: cellState, indexPath: indexPath)
          
          pathsToReload.remove(indexPath)
          
          if #available(iOS 9.0, *) {
              configuredCell.transform.a = semanticContentAttribute == .forceRightToLeft ? -1 : 1
          }
          return configuredCell
      }
      
      /// Asks your data sourceobject for the number of sections in
      /// the collection view. The number of sections in collectionView.
      public func numberOfSections(in collectionView: UICollectionView) -> Int {
          return monthMap.count
      }
      
      /// Asks your data source object for the number of items in the
      /// specified section. The number of rows in section.
      public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          if calendarViewLayout.cellCache.isEmpty {return 0}
          guard let count =  calendarViewLayout.cellCache[section]?.count else {
              developerError(string: "cellCacheSection does not exist.")
              return 0
          }
          return count
      }
      
      /// Asks the delegate if the specified item should be selected.
      /// true if the item should be selected or false if it should not.
      public func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
          return handleShouldSelectionValueChange(collectionView, action: .shouldSelect, indexPath: indexPath, selectionType: .userInitiated)
      }
      
      /// Asks the delegate if the specified item should be deselected.
      /// true if the item should be deselected or false if it should not.
      public func collectionView(_ collectionView: UICollectionView, shouldDeselectItemAt indexPath: IndexPath) -> Bool {
          return handleShouldSelectionValueChange(collectionView, action: .shouldDeselect, indexPath: indexPath, selectionType: .userInitiated)
      }
  
      /// Tells the delegate that the item at the specified index
      /// path was selected. The collection view calls this method when the
      /// user successfully selects an item in the collection view.
      /// It does not call this method when you programmatically
      /// set the selection.
      public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          handleSelectionValueChanged(collectionView, action: .didSelect, indexPath: indexPath, selectionType: .userInitiated)
      }
      
      /// Tells the delegate that the item at the specified path was deselected.
      /// The collection view calls this method when the user successfully
      /// deselects an item in the collection view.
      /// It does not call this method when you programmatically deselect items.
      public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
          handleSelectionValueChanged(collectionView, action: .didDeselect, indexPath: indexPath, selectionType: .userInitiated)
      }
      
      public func sizeOfDecorationView(indexPath: IndexPath) -> CGRect {
          guard let size = calendarDelegate?.sizeOfDecorationView(indexPath: indexPath) else { return .zero }
          return size
      }
      
      func handleSelectionValueChanged(_ collectionView: UICollectionView, action: SelectionAction, indexPath: IndexPath, selectionType: SelectionType, shouldTriggerSelectionDelegate: Bool = true) {
          guard
              let delegate = calendarDelegate,
              let infoOfDate = dateOwnerInfoFromPath(indexPath) else {
                  return
          }
          // index paths to be reloaded should be index to the left and right of the selected index
          var localPathsToReload = isRangeSelectionUsed ? validForwardAndBackwordSelectedIndexes(forIndexPath: indexPath) : []
          
          let cell = collectionView.cellForItem(at: indexPath) as? JTAppleCell
          if !shouldTriggerSelectionDelegate || cell == nil {
              pathsToReload.insert(indexPath)
              localPathsToReload.insert(indexPath)
          }
          
          let isSelected = action == .didSelect ? true : false
          
          // If cell has a counterpart cell, then select it as well
          let cellState = cellStateFromIndexPath(indexPath, withDateInfo: infoOfDate, cell: cell, isSelected: isSelected, selectionType: selectionType)
          
          // Update model
          let cleanupAction: (IndexPath, Date, DateOwner) -> IndexPath?
          
          if action == .didSelect {
              cleanupAction = selectCounterPartCellIndexPath
              addCellToSelectedSet(indexPath, date: infoOfDate.date, cellState: cellState)
          } else {
              cleanupAction = deselectCounterPartCellIndexPath
              deleteCellFromSelectedSetIfSelected(indexPath)
          }
          
          // check if the paths to reload (forward&backward indexes) also have counterpart dates
          if !localPathsToReload.isEmpty {
              let reloadPaths = localPathsToReload
              for path in reloadPaths {
                  if let validCounterPath = selectedCellData[path]?.counterIndexPath {
                      localPathsToReload.insert(validCounterPath)
                  }
              }
          }
          
          if let counterPartIndexPath = cleanupAction(indexPath, infoOfDate.date, cellState.dateBelongsTo) {
              localPathsToReload.insert(counterPartIndexPath)
              let counterPathsToReload = isRangeSelectionUsed ? validForwardAndBackwordSelectedIndexes(forIndexPath: counterPartIndexPath) : []
              localPathsToReload.formUnion(counterPathsToReload)
          }
          
          if shouldTriggerSelectionDelegate {
              if action == .didSelect {
                  delegate.calendar(self, didSelectDate: infoOfDate.date, cell: cell, cellState: cellState)
              } else {
                  delegate.calendar(self, didDeselectDate: infoOfDate.date, cell: cell, cellState: cellState)
              }
          }
          
          if !localPathsToReload.isEmpty {
              batchReloadIndexPaths(Array(localPathsToReload))
          }
      }
      
      func handleShouldSelectionValueChange(_ collectionView: UICollectionView, action: ShouldSelectionAction, indexPath: IndexPath, selectionType: SelectionType) -> Bool {
          if let
              delegate = calendarDelegate,
              let infoOfDate = dateOwnerInfoFromPath(indexPath) {
              let cell = collectionView.cellForItem(at: indexPath) as? JTAppleCell
              let cellState = cellStateFromIndexPath(indexPath,
                                                     withDateInfo: infoOfDate,
                                                     selectionType: selectionType)
              switch action {
              case .shouldSelect:
                  return delegate.calendar(self, shouldSelectDate: infoOfDate.date, cell: cell, cellState: cellState)
              case .shouldDeselect:
                  return delegate.calendar(self, shouldDeselectDate: infoOfDate.date, cell: cell, cellState: cellState)
              }
          }
          return false
      }
  }