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
|
//
// InternalActionFunctions.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 {
/// Lays out subviews.
override open func layoutSubviews() {
super.layoutSubviews()
if !generalDelayedExecutionClosure.isEmpty, isCalendarLayoutLoaded {
executeDelayedTasks(.general)
}
}
func setupMonthInfoAndMap(with data: ConfigurationParameters? = nil) {
theData = setupMonthInfoDataForStartAndEndDate(with: data)
}
func developerError(string: String) {
print(string)
print(developerErrorMessage)
assert(false)
}
func setupNewLayout(from oldLayout: JTAppleCalendarLayoutProtocol) {
let newLayout = JTAppleCalendarLayout(withDelegate: self)
newLayout.scrollDirection = oldLayout.scrollDirection
newLayout.sectionInset = oldLayout.sectionInset
newLayout.minimumInteritemSpacing = oldLayout.minimumInteritemSpacing
newLayout.minimumLineSpacing = oldLayout.minimumLineSpacing
collectionViewLayout = newLayout
scrollDirection = newLayout.scrollDirection
sectionInset = newLayout.sectionInset
minimumLineSpacing = newLayout.minimumLineSpacing
minimumInteritemSpacing = newLayout.minimumInteritemSpacing
if #available(iOS 9.0, *) {
transform.a = semanticContentAttribute == .forceRightToLeft ? -1 : 1
}
super.dataSource = self
super.delegate = self
decelerationRate = .fast
#if os(iOS)
if isPagingEnabled {
scrollingMode = .stopAtEachCalendarFrame
} else {
scrollingMode = .none
}
#endif
}
func scrollTo(indexPath: IndexPath, triggerScrollToDateDelegate: Bool, isAnimationEnabled: Bool, position: UICollectionView.ScrollPosition, extraAddedOffset: CGFloat, completionHandler: (() -> Void)?) {
isScrollInProgress = true
if let validCompletionHandler = completionHandler { scrollDelayedExecutionClosure.append(validCompletionHandler) }
self.triggerScrollToDateDelegate = triggerScrollToDateDelegate
DispatchQueue.main.async {
self.scrollToItem(at: indexPath, at: position, animated: isAnimationEnabled)
if (isAnimationEnabled && self.calendarOffsetIsAlreadyAtScrollPosition(forIndexPath: indexPath)) ||
!isAnimationEnabled {
self.scrollViewDidEndScrollingAnimation(self)
}
self.isScrollInProgress = false
}
}
func scrollToHeaderInSection(_ section: Int,
triggerScrollToDateDelegate: Bool = false,
withAnimation animation: Bool = true,
extraAddedOffset: CGFloat,
completionHandler: (() -> Void)? = nil) {
if !calendarViewLayout.thereAreHeaders { return }
let indexPath = IndexPath(item: 0, section: section)
guard let attributes = calendarViewLayout.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: indexPath) else { return }
isScrollInProgress = true
if let validHandler = completionHandler { scrollDelayedExecutionClosure.append(validHandler) }
self.triggerScrollToDateDelegate = triggerScrollToDateDelegate
let maxYCalendarOffset = max(0, self.contentSize.height - self.frame.size.height)
var topOfHeader = CGPoint(x: attributes.frame.origin.x,y: min(maxYCalendarOffset, attributes.frame.origin.y))
if scrollDirection == .horizontal { topOfHeader.x += extraAddedOffset} else { topOfHeader.y += extraAddedOffset }
DispatchQueue.main.async {
self.setContentOffset(topOfHeader, animated: animation)
if (animation && self.calendarOffsetIsAlreadyAtScrollPosition(forOffset: topOfHeader)) ||
!animation {
self.scrollViewDidEndScrollingAnimation(self)
}
self.isScrollInProgress = false
}
}
// Subclasses cannot use this function
@available(*, unavailable)
open override func reloadData() {
super.reloadData()
}
func handleScroll(point: CGPoint? = nil,
indexPath: IndexPath? = nil,
triggerScrollToDateDelegate: Bool = true,
isAnimationEnabled: Bool,
position: UICollectionView.ScrollPosition? = .left,
extraAddedOffset: CGFloat = 0,
completionHandler: (() -> Void)?) {
if isScrollInProgress { return }
// point takes preference
if let validPoint = point {
scrollTo(point: validPoint,
triggerScrollToDateDelegate: triggerScrollToDateDelegate,
isAnimationEnabled: isAnimationEnabled,
extraAddedOffset: extraAddedOffset,
completionHandler: completionHandler)
} else {
guard let validIndexPath = indexPath else { return }
var isNonConinuousScroll = true
switch scrollingMode {
case .none, .nonStopToCell: isNonConinuousScroll = false
default: break
}
if calendarViewLayout.thereAreHeaders,
scrollDirection == .vertical,
isNonConinuousScroll {
scrollToHeaderInSection(validIndexPath.section,
triggerScrollToDateDelegate: triggerScrollToDateDelegate,
withAnimation: isAnimationEnabled,
extraAddedOffset: extraAddedOffset,
completionHandler: completionHandler)
} else {
scrollTo(indexPath:validIndexPath,
triggerScrollToDateDelegate: triggerScrollToDateDelegate,
isAnimationEnabled: isAnimationEnabled,
position: position ?? .left,
extraAddedOffset: extraAddedOffset,
completionHandler: completionHandler)
}
}
}
func scrollTo(point: CGPoint, triggerScrollToDateDelegate: Bool? = nil, isAnimationEnabled: Bool, extraAddedOffset: CGFloat, completionHandler: (() -> Void)?) {
isScrollInProgress = true
if let validCompletionHandler = completionHandler { scrollDelayedExecutionClosure.append(validCompletionHandler) }
self.triggerScrollToDateDelegate = triggerScrollToDateDelegate
var point = point
if scrollDirection == .horizontal { point.x += extraAddedOffset } else { point.y += extraAddedOffset }
DispatchQueue.main.async() {
self.setContentOffset(point, animated: isAnimationEnabled)
if (isAnimationEnabled && self.calendarOffsetIsAlreadyAtScrollPosition(forOffset: point)) ||
!isAnimationEnabled {
self.scrollViewDidEndScrollingAnimation(self)
}
}
}
func setupMonthInfoDataForStartAndEndDate(with config: ConfigurationParameters? = nil) -> CalendarData {
var months = [Month]()
var monthMap = [Int: Int]()
var totalSections = 0
var totalDays = 0
var validConfig = config
if validConfig == nil { validConfig = calendarDataSource?.configureCalendar(self) }
if let validConfig = validConfig {
let comparison = validConfig.calendar.compare(validConfig.startDate, to: validConfig.endDate, toGranularity: .nanosecond)
if comparison == ComparisonResult.orderedDescending {
assert(false, "Error, your start date cannot be greater than your end date\n")
return (CalendarData(months: [], totalSections: 0, sectionToMonthMap: [:], totalDays: 0))
}
// Set the new cache
_cachedConfiguration = validConfig
if let
startMonth = calendar.startOfMonth(for: validConfig.startDate),
let endMonth = calendar.endOfMonth(for: validConfig.endDate) {
startOfMonthCache = startMonth
endOfMonthCache = endMonth
// Create the parameters for the date format generator
let parameters = ConfigurationParameters(startDate: startOfMonthCache,
endDate: endOfMonthCache,
numberOfRows: validConfig.numberOfRows,
calendar: calendar,
generateInDates: validConfig.generateInDates,
generateOutDates: validConfig.generateOutDates,
firstDayOfWeek: validConfig.firstDayOfWeek,
hasStrictBoundaries: validConfig.hasStrictBoundaries)
let generatedData = dateGenerator.setupMonthInfoDataForStartAndEndDate(parameters)
months = generatedData.months
monthMap = generatedData.monthMap
totalSections = generatedData.totalSections
totalDays = generatedData.totalDays
}
}
let data = CalendarData(months: months, totalSections: totalSections, sectionToMonthMap: monthMap, totalDays: totalDays)
return data
}
func batchReloadIndexPaths(_ indexPaths: [IndexPath]) {
let visiblePaths = indexPathsForVisibleItems
var visibleCellsToReload: [JTAppleCell: IndexPath] = [:]
for path in indexPaths {
if calendarViewLayout.cachedValue(for: path.item, section: path.section) == nil { continue }
pathsToReload.insert(path)
if visiblePaths.contains(path) {
visibleCellsToReload[cellForItem(at: path) as! JTAppleCell] = path
}
}
// Reload the visible paths
if !visibleCellsToReload.isEmpty {
for (cell, path) in visibleCellsToReload {
self.collectionView(self, willDisplay: cell, forItemAt: path)
}
}
}
func addCellToSelectedSet(_ indexPath: IndexPath, date: Date, cellState: CellState) {
selectedCellData[indexPath] = SelectedCellData(indexPath: indexPath, date: date, cellState: cellState)
}
func deleteCellFromSelectedSetIfSelected(_ indexPath: IndexPath) {
selectedCellData.removeValue(forKey: indexPath)
}
// Returns an indexPath if valid one was found
func deselectCounterPartCellIndexPath(_ indexPath: IndexPath, date: Date, dateOwner: DateOwner) -> IndexPath? {
guard let counterPartCellIndexPath = indexPathOfdateCellCounterPath(date, dateOwner: dateOwner) else { return nil }
deleteCellFromSelectedSetIfSelected(counterPartCellIndexPath)
deselectItem(at: counterPartCellIndexPath, animated: false)
return counterPartCellIndexPath
}
func selectCounterPartCellIndexPath(_ indexPath: IndexPath, date: Date, dateOwner: DateOwner) -> IndexPath? {
guard let counterPartCellIndexPath = indexPathOfdateCellCounterPath(date, dateOwner: dateOwner) else { return nil }
let counterPartCellState = cellStateFromIndexPath(counterPartCellIndexPath, isSelected: true)
addCellToSelectedSet(counterPartCellIndexPath, date: date, cellState: counterPartCellState)
// Update the selectedCellData counterIndexPathData
selectedCellData[indexPath]?.counterIndexPath = counterPartCellIndexPath
selectedCellData[counterPartCellIndexPath]?.counterIndexPath = indexPath
if allowsMultipleSelection {
// only if multiple selection is enabled. With single selection, we do not want the counterpart cell to be
// selected in place of the main cell. With multiselection, however, all can be selected
selectItem(at: counterPartCellIndexPath, animated: false, scrollPosition: [])
}
return counterPartCellIndexPath
}
func executeDelayedTasks(_ type: DelayedTaskType) {
let tasksToExecute: [(() -> Void)]
switch type {
case .scroll:
tasksToExecute = scrollDelayedExecutionClosure
scrollDelayedExecutionClosure.removeAll()
case .general:
tasksToExecute = generalDelayedExecutionClosure
generalDelayedExecutionClosure.removeAll()
}
for aTaskToExecute in tasksToExecute { aTaskToExecute() }
}
// Only reload the dates if the datasource information has changed
func reloadDelegateDataSource() -> (shouldReload: Bool, configParameters: ConfigurationParameters?) {
var retval: (Bool, ConfigurationParameters?) = (false, nil)
if let
newDateBoundary = calendarDataSource?.configureCalendar(self) {
// Jt101 do a check in each var to see if
// user has bad star/end dates
let newStartOfMonth = calendar.startOfMonth(for: newDateBoundary.startDate)
let newEndOfMonth = calendar.endOfMonth(for: newDateBoundary.endDate)
let oldStartOfMonth = calendar.startOfMonth(for: startDateCache)
let oldEndOfMonth = calendar.endOfMonth(for: endDateCache)
let newLastMonth = sizesForMonthSection()
let calendarLayout = calendarViewLayout
if
// ConfigParameters were changed
newStartOfMonth != oldStartOfMonth ||
newEndOfMonth != oldEndOfMonth ||
newDateBoundary.calendar != _cachedConfiguration.calendar ||
newDateBoundary.numberOfRows != _cachedConfiguration.numberOfRows ||
newDateBoundary.generateInDates != _cachedConfiguration.generateInDates ||
newDateBoundary.generateOutDates != _cachedConfiguration.generateOutDates ||
newDateBoundary.firstDayOfWeek != _cachedConfiguration.firstDayOfWeek ||
newDateBoundary.hasStrictBoundaries != _cachedConfiguration.hasStrictBoundaries ||
// Other layout information were changed
minimumInteritemSpacing != calendarLayout.minimumInteritemSpacing ||
minimumLineSpacing != calendarLayout.minimumLineSpacing ||
sectionInset != calendarLayout.sectionInset ||
lastMonthSize != newLastMonth ||
allowsDateCellStretching != calendarLayout.allowsDateCellStretching ||
scrollDirection != calendarLayout.scrollDirection ||
calendarLayout.isDirty {
lastMonthSize = newLastMonth
retval = (true, newDateBoundary)
}
}
return retval
}
func remapSelectedDatesWithCurrentLayout() -> (selected:(indexPaths:[IndexPath], counterPaths:[IndexPath]), selectedDates: [Date]) {
var retval = (selected:(indexPaths:[IndexPath](), counterPaths:[IndexPath]()), selectedDates: [Date]())
if !selectedDates.isEmpty {
let selectedDates = self.selectedDates
// Get the new paths
let newPaths = pathsFromDates(selectedDates)
// Get the new counter Paths
var newCounterPaths: [IndexPath] = []
for date in selectedDates {
if let counterPath = indexPathOfdateCellCounterPath(date, dateOwner: .thisMonth) {
newCounterPaths.append(counterPath)
}
}
// Append paths
retval.selected.indexPaths.append(contentsOf: newPaths)
retval.selected.counterPaths.append(contentsOf: newCounterPaths)
// Append dates to retval
for allPaths in [newPaths, newCounterPaths] {
for path in allPaths {
guard let dateFromPath = dateOwnerInfoFromPath(path)?.date else { continue }
retval.selectedDates.append(dateFromPath)
}
}
}
return retval
}
}
|