From f3763723198c89498dfe49706aaaaf51dffbc211 Mon Sep 17 00:00:00 2001 From: JayT Date: Sun, 5 Mar 2017 21:04:26 -0800 Subject: [PATCH] Fixed Leaks --- Sources/GlobalFunctionsAndExtensions.swift | 8 +- Sources/JTAppleCalendarLayout.swift | 25 +++-- Sources/JTAppleCalendarView.swift | 106 +++++++++----------- Sources/JTAppleCollectionReusableView.swift | 4 +- Sources/JTAppleDayCell.swift | 6 +- Sources/JTAppleReusableViewProtocol.swift | 4 +- Sources/UICollectionViewDelegates.swift | 16 +-- Sources/UIScrollViewDelegates.swift | 18 ++-- Sources/UserInteractionFunctions.swift | 77 +++++++------- 9 files changed, 121 insertions(+), 143 deletions(-) diff --git a/Sources/GlobalFunctionsAndExtensions.swift b/Sources/GlobalFunctionsAndExtensions.swift index a80272db..41ace25c 100644 --- a/Sources/GlobalFunctionsAndExtensions.swift +++ b/Sources/GlobalFunctionsAndExtensions.swift @@ -23,7 +23,7 @@ extension Calendar { func endOfMonth(for date: Date) -> Date? { guard let comp = dateFormatterComponents(from: date), - let day = self.range(of: .day, in: .month, for: date)?.count, + let day = range(of: .day, in: .month, for: date)?.count, let retVal = Calendar.formatter.date(from: "\(comp.year) \(comp.month) \(day)") else { return nil } @@ -33,10 +33,10 @@ extension Calendar { private func dateFormatterComponents(from date: Date) -> (month: Int, year: Int)? { // Setup the dateformatter to this instance's settings - Calendar.formatter.timeZone = self.timeZone - Calendar.formatter.locale = self.locale + Calendar.formatter.timeZone = timeZone + Calendar.formatter.locale = locale - let comp = self.dateComponents([.year, .month], from: date) + let comp = dateComponents([.year, .month], from: date) guard let month = comp.month, diff --git a/Sources/JTAppleCalendarLayout.swift b/Sources/JTAppleCalendarLayout.swift index 90517ed6..01257b57 100644 --- a/Sources/JTAppleCalendarLayout.swift +++ b/Sources/JTAppleCalendarLayout.swift @@ -47,7 +47,7 @@ open class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutP } } - weak var delegate: JTAppleCalendarDelegateProtocol! + unowned var delegate: JTAppleCalendarDelegateProtocol var currentHeader: (section: Int, size: CGSize)? // Tracks the current header size var currentCell: (section: Int, itemSize: CGSize)? // Tracks the current cell size var contentHeight: CGFloat = 0 // Content height of calendarView @@ -57,9 +57,18 @@ open class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutP var daysInSection: [Int: Int] = [:] // temporary caching init(withDelegate delegate: JTAppleCalendarDelegateProtocol) { - super.init() self.delegate = delegate + super.init() + + } + + /// Returns an object initialized from data in a given unarchiver. + /// self, initialized using the data in decoder. + required public init?(coder aDecoder: NSCoder) { + self.delegate = aDecoder.value(forKey: "delegate") as! JTAppleCalendarDelegateProtocol + super.init(coder: aDecoder) } + func indexPath(direction: SegmentDestination, of section:Int, item: Int) -> IndexPath? { switch direction { @@ -177,7 +186,7 @@ open class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutP section += 1 } } - contentHeight = self.collectionView!.bounds.size.height + contentHeight = collectionView!.bounds.size.height } func verticalStuff() { @@ -233,7 +242,7 @@ open class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutP section += 1 } } - contentWidth = self.collectionView!.bounds.size.width + contentWidth = collectionView!.bounds.size.width } /// Returns the width and height of the collection view’s contents. @@ -485,13 +494,7 @@ open class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutP } 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) { - super.init(coder: aDecoder) - } - + /// Returns the content offset to use after an animation /// layout update or change. /// - Parameter proposedContentOffset: The proposed point for the diff --git a/Sources/JTAppleCalendarView.swift b/Sources/JTAppleCalendarView.swift index bf37d0f8..ee094d83 100644 --- a/Sources/JTAppleCalendarView.swift +++ b/Sources/JTAppleCalendarView.swift @@ -83,8 +83,7 @@ open class JTAppleCalendarView: UIView { /// Enables/Disables multiple selection on JTAppleCalendar open var allowsMultipleSelection: Bool = false { didSet { - self.calendarView.allowsMultipleSelection = - allowsMultipleSelection + calendarView.allowsMultipleSelection = allowsMultipleSelection } } @@ -124,7 +123,7 @@ open class JTAppleCalendarView: UIView { /// Lays out subviews. override open func layoutSubviews() { - self.frame = super.frame + frame = super.frame } var lastIndexOffset: (IndexPath, UICollectionElementCategory)? @@ -142,13 +141,15 @@ open class JTAppleCalendarView: UIView { /// its superview coordinate system. override open var frame: CGRect { didSet { - calendarView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) + calendarView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height) updateLayoutItemSize() if calendarViewLayout.itemSize != lastSize { lastSize = calendarViewLayout.itemSize if !calendarIsAlreadyLoaded, delegate != nil { // This will only be set once calendarIsAlreadyLoaded = true - self.reloadData() { self.executeDelayedTasks() } + reloadData() {[unowned self] in + self.executeDelayedTasks() + } return } } @@ -263,7 +264,7 @@ open class JTAppleCalendarView: UIView { } } - lazy var calendarView: CustomCollectionView = { + lazy var calendarView: CustomCollectionView = {[unowned self] in let layout = JTAppleCalendarLayout(withDelegate: self) layout.scrollDirection = self.scrollDirection @@ -292,11 +293,11 @@ open class JTAppleCalendarView: UIView { // Invalidate the layout // Default Item height - var height: CGFloat = (self.calendarView.bounds.size.height - layout.headerReferenceSize.height) / CGFloat(cachedConfiguration.numberOfRows) + var height: CGFloat = (calendarView.bounds.size.height - layout.headerReferenceSize.height) / CGFloat(cachedConfiguration.numberOfRows) // Default Item width - var width: CGFloat = self.calendarView.bounds.size.width / CGFloat(maxNumberOfDaysInWeek) + var width: CGFloat = calendarView.bounds.size.width / CGFloat(maxNumberOfDaysInWeek) - if let userSetItemSize = self.itemSize { + if let userSetItemSize = itemSize { if scrollDirection == .vertical { height = userSetItemSize } else { @@ -320,7 +321,7 @@ open class JTAppleCalendarView: UIView { /// view object with the specified frame rectangle. public override init(frame: CGRect) { super.init(frame: frame) - self.initialSetup() + initialSetup() } func developerError(string: String) { @@ -338,23 +339,19 @@ open class JTAppleCalendarView: UIView { /// Prepares the receiver for service after it has been loaded /// from an Interface Builder archive, or nib file. override open func awakeFromNib() { - self.initialSetup() + initialSetup() } // MARK: Setup func initialSetup() { - self.clipsToBounds = true - self.calendarView.register(JTAppleDayCell.self, forCellWithReuseIdentifier: cellReuseIdentifier) - self.addSubview(self.calendarView) + clipsToBounds = true + calendarView.register(JTAppleDayCell.self, forCellWithReuseIdentifier: cellReuseIdentifier) + addSubview(calendarView) } func restoreSelectionStateForCellAtIndexPath(_ indexPath: IndexPath) { if theSelectedIndexPaths.contains(indexPath) { - calendarView.selectItem( - at: indexPath, - animated: false, - scrollPosition: UICollectionViewScrollPosition() - ) + calendarView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition()) } } @@ -374,29 +371,26 @@ open class JTAppleCalendarView: UIView { func scrollTo(indexPath: IndexPath, isAnimationEnabled: Bool, position: UICollectionViewScrollPosition, completionHandler: (() -> Void)?) { if let validCompletionHandler = completionHandler { - self.delayedExecutionClosure.append(validCompletionHandler) + delayedExecutionClosure.append(validCompletionHandler) } - self.calendarView.scrollToItem(at: indexPath, at: position, animated: isAnimationEnabled) + calendarView.scrollToItem(at: indexPath, at: position, animated: isAnimationEnabled) if isAnimationEnabled { if calendarOffsetIsAlreadyAtScrollPosition(forIndexPath: indexPath) { - self.scrollViewDidEndScrollingAnimation(self.calendarView) - self.scrollInProgress = false + scrollViewDidEndScrollingAnimation(calendarView) + scrollInProgress = false return } } } func targetPointForItemAt(indexPath: IndexPath) -> CGPoint? { - - guard let targetCellFrame = self.calendarView.layoutAttributesForItem(at: indexPath)?.frame else { - return nil - } + guard let targetCellFrame = calendarView.layoutAttributesForItem(at: indexPath)?.frame else { return nil } let theTargetContentOffset: CGFloat = scrollDirection == .horizontal ? targetCellFrame.origin.x : targetCellFrame.origin.y var fixedScrollSize: CGFloat = 0 switch scrollingMode { case .stopAtEachSection, .stopAtEachCalendarFrameWidth: - if self.scrollDirection == .horizontal || (scrollDirection == .vertical && !thereAreHeaders) { + if scrollDirection == .horizontal || (scrollDirection == .vertical && !thereAreHeaders) { // Horizontal has a fixed width. // Vertical with no header has fixed height fixedScrollSize = calendarViewLayout.sizeOfContentForSection(0) @@ -447,7 +441,7 @@ open class JTAppleCalendarView: UIView { // is already on the screen, then the didFinishScrollingAnimation // delegate will not get called. Once animation is on let's force // a scroll so the delegate MUST get caalled - if let attributes = self.calendarView.layoutAttributesForItem(at: indexPath) { + if let attributes = calendarView.layoutAttributesForItem(at: indexPath) { let layoutOffset: CGFloat let calendarOffset: CGFloat if scrollDirection == .horizontal { @@ -466,15 +460,13 @@ open class JTAppleCalendarView: UIView { /// Changes the calendar reading direction public func changeVisibleDirection(to orientation: ReadingOrientation) { if !calendarIsAlreadyLoaded { - delayedExecutionClosure.append { + delayedExecutionClosure.append {[unowned self] in self.changeVisibleDirection(to: orientation) } return } - if orientation == self.orientation { - return - } + if orientation == orientation { return } self.orientation = orientation calendarView.transform.a = orientation == .leftToRight ? 1 : -1 @@ -495,11 +487,9 @@ open class JTAppleCalendarView: UIView { if let validHandler = completionHandler { self.delayedExecutionClosure.append(validHandler) } - let topOfHeader = CGPoint(x: attributes.frame.origin.x, - y: attributes.frame.origin.y) + let topOfHeader = CGPoint(x: attributes.frame.origin.x, y: attributes.frame.origin.y) self.scrollInProgress = true - self.calendarView.setContentOffset(topOfHeader, - animated: animation) + self.calendarView.setContentOffset(topOfHeader, animated: animation) if !animation { self.scrollViewDidEndScrollingAnimation(self.calendarView) } else { @@ -522,14 +512,12 @@ open class JTAppleCalendarView: UIView { withAnimation animation: Bool = false, completionHandler: (() -> Void)? = nil) { - - // Reload the datasource if shouldCheckDelegateDatasource { reloadDelegateDataSource() } - self.layoutIfNeeded() + layoutIfNeeded() if layoutNeedsUpdating { calendarViewLayout.clearCache() @@ -542,10 +530,10 @@ open class JTAppleCalendarView: UIView { // Re-select the dates that were selected prior to the reload if !selectedDates.isEmpty { let selectedDates = self.selectedDates - var pathsAndCounterPaths = self.pathsFromDates(selectedDates) + var pathsAndCounterPaths = pathsFromDates(selectedDates) for date in selectedDates { - if let counterPath = self.indexPathOfdateCellCounterPart(date, dateOwner: .thisMonth) { + if let counterPath = indexPathOfdateCellCounterPart(date, dateOwner: .thisMonth) { pathsAndCounterPaths.append(counterPath) } } @@ -554,7 +542,7 @@ open class JTAppleCalendarView: UIView { } // Restore the selected index paths - let restoreAfterReload = { + let restoreAfterReload = {[unowned self] in // The bounds of visible cells might have shifted, so reset them for cell in self.calendarView.visibleCells { cell.bounds.origin = CGPoint(x: 0, y: 0) } for indexPath in self.theSelectedIndexPaths { self.restoreSelectionStateForCellAtIndexPath(indexPath) } @@ -564,7 +552,7 @@ open class JTAppleCalendarView: UIView { // If we have a valid anchor date, this means we want to // scroll // This scroll should happen after the reload above - calendarView.completionHandler = { + calendarView.completionHandler = {[unowned self] in if self.thereAreHeaders { self.scrollToHeaderForDate( validAnchorDate, @@ -583,24 +571,24 @@ open class JTAppleCalendarView: UIView { calendarView.reloadData() } else { guard let validCompletionHandler = completionHandler else { - self.calendarView.completionHandler = restoreAfterReload - self.calendarView.reloadData() + calendarView.completionHandler = restoreAfterReload + calendarView.reloadData() return } - if self.scrollInProgress { - self.delayedExecutionClosure.append({ + if scrollInProgress { + delayedExecutionClosure.append {[unowned self] in self.calendarView.completionHandler = { restoreAfterReload() validCompletionHandler() } self.calendarView.reloadData() - }) + } } else { - self.calendarView.completionHandler = { + calendarView.completionHandler = { restoreAfterReload() validCompletionHandler() } - self.calendarView.reloadData() + calendarView.reloadData() } } } @@ -907,23 +895,23 @@ extension JTAppleCalendarView { if pathsToReload.isEmpty { return } UICollectionView.performWithoutAnimation { - self.calendarView.performBatchUpdates({ + self.calendarView.performBatchUpdates({[unowned self] in self.calendarView.reloadItems(at: pathsToReload) - }, completion: nil) + }) } } func addCellToSelectedSetIfUnselected(_ indexPath: IndexPath, date: Date) { - if self.theSelectedIndexPaths.contains(indexPath) == false { - self.theSelectedIndexPaths.append(indexPath) - self.theSelectedDates.append(date) + if theSelectedIndexPaths.contains(indexPath) == false { + theSelectedIndexPaths.append(indexPath) + theSelectedDates.append(date) } } func deleteCellFromSelectedSetIfSelected(_ indexPath: IndexPath) { - if let index = self.theSelectedIndexPaths.index(of: indexPath) { - self.theSelectedIndexPaths.remove(at: index) - self.theSelectedDates.remove(at: index) + if let index = theSelectedIndexPaths.index(of: indexPath) { + theSelectedIndexPaths.remove(at: index) + theSelectedDates.remove(at: index) } } diff --git a/Sources/JTAppleCollectionReusableView.swift b/Sources/JTAppleCollectionReusableView.swift index f6e3a7e1..e85ca2c5 100644 --- a/Sources/JTAppleCollectionReusableView.swift +++ b/Sources/JTAppleCollectionReusableView.swift @@ -12,8 +12,8 @@ open class JTAppleCollectionReusableView: UICollectionReusableView, JTAppleReusa var view: JTAppleHeaderView? func update() { - view!.frame = self.frame - view!.center = CGPoint(x: self.bounds.size.width * 0.5, y: self.bounds.size.height * 0.5) + view!.frame = frame + view!.center = CGPoint(x: bounds.size.width * 0.5, y: bounds.size.height * 0.5) } override init(frame: CGRect) { diff --git a/Sources/JTAppleDayCell.swift b/Sources/JTAppleDayCell.swift index 6d2be871..201c76ca 100644 --- a/Sources/JTAppleDayCell.swift +++ b/Sources/JTAppleDayCell.swift @@ -13,10 +13,10 @@ open class JTAppleDayCell: UICollectionViewCell, JTAppleReusableViewProtocol { public var view: JTAppleDayCellView? func updateCellView(_ cellInsetX: CGFloat, cellInsetY: CGFloat) { - let vFrame = self.frame.insetBy(dx: cellInsetX, dy: cellInsetY) + let vFrame = frame.insetBy(dx: cellInsetX, dy: cellInsetY) view!.frame = vFrame - view!.center = CGPoint(x: self.bounds.size.width * 0.5, - y: self.bounds.size.height * 0.5) + view!.center = CGPoint(x: bounds.size.width * 0.5, + y: bounds.size.height * 0.5) } override init(frame: CGRect) { diff --git a/Sources/JTAppleReusableViewProtocol.swift b/Sources/JTAppleReusableViewProtocol.swift index 843b4d47..51db54b3 100644 --- a/Sources/JTAppleReusableViewProtocol.swift +++ b/Sources/JTAppleReusableViewProtocol.swift @@ -45,7 +45,7 @@ extension JTAppleReusableViewProtocol { assert(false) return } - self.view = theCellClass.init() + view = theCellClass.init() break case let .fromType(cellType): guard let theCellClass = cellType as? ViewType.Type else { @@ -54,7 +54,7 @@ extension JTAppleReusableViewProtocol { assert(false) return } - self.view = theCellClass.init() + view = theCellClass.init() break } guard let validView = view else { diff --git a/Sources/UICollectionViewDelegates.swift b/Sources/UICollectionViewDelegates.swift index 4fcee696..9d26b49e 100644 --- a/Sources/UICollectionViewDelegates.swift +++ b/Sources/UICollectionViewDelegates.swift @@ -58,17 +58,11 @@ extension JTAppleCalendarView: UICollectionViewDelegate, UICollectionViewDataSou } headerView.setupView(source, leftToRightOrientation: orientation) headerView.update() - self.delegate?.calendar( - self, - willDisplaySectionHeader: headerView.view!, - range: validDate.range, - identifier: reuseIdentifier) + delegate?.calendar(self, willDisplaySectionHeader: headerView.view!, range: validDate.range, identifier: reuseIdentifier) return headerView } /// Notifies the delegate that a cell is no longer on screen - public func collectionView(_ collectionView: UICollectionView, - didEndDisplaying cell: UICollectionViewCell, - forItemAt indexPath: IndexPath) { + public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { guard #available(iOS 10, *) else { guard let theCell = cell as? JTAppleDayCell, @@ -76,7 +70,7 @@ extension JTAppleCalendarView: UICollectionViewDelegate, UICollectionViewDataSou developerError(string: "Cell view was nil") return } - self.delegate?.calendar(self, willResetCell: cellView) + delegate?.calendar(self, willResetCell: cellView) return } } @@ -166,7 +160,7 @@ extension JTAppleCalendarView: UICollectionViewDelegate, UICollectionViewDataSou } } if pathsToReload.count > 0 { - self.batchReloadIndexPaths(pathsToReload) + batchReloadIndexPaths(pathsToReload) } delegate.calendar(self, didDeselectDate: dateInfoDeselectedByUser.date, cell: selectedCell?.view, cellState: cellState) } @@ -221,7 +215,7 @@ extension JTAppleCalendarView: UICollectionViewDelegate, UICollectionViewDataSou } } if pathsToReload.count > 0 { - self.batchReloadIndexPaths(pathsToReload) + batchReloadIndexPaths(pathsToReload) } delegate.calendar(self, didSelectDate: infoOfDateSelectedByUser.date, cell: selectedCell?.view, cellState: cellState) } diff --git a/Sources/UIScrollViewDelegates.swift b/Sources/UIScrollViewDelegates.swift index df042af1..13403fb0 100644 --- a/Sources/UIScrollViewDelegates.swift +++ b/Sources/UIScrollViewDelegates.swift @@ -10,13 +10,11 @@ extension JTAppleCalendarView: UIScrollViewDelegate { /// Inform the scrollViewDidEndDecelerating /// function that scrolling just occurred public func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { - self.scrollViewDidEndDecelerating(calendarView) + scrollViewDidEndDecelerating(calendarView) } public func saveLastContentOffset(_ offset: CGPoint) { - self.lastSavedContentOffset = scrollDirection == .horizontal ? - offset.x : - offset.y + lastSavedContentOffset = scrollDirection == .horizontal ? offset.x : offset.y } /// Tells the delegate when the user finishes scrolling the content. @@ -31,7 +29,7 @@ extension JTAppleCalendarView: UIScrollViewDelegate { var contentOffset: CGFloat = 0, theTargetContentOffset: CGFloat = 0, directionVelocity: CGFloat = 0 - let calendarLayout = self.calendarViewLayout + let calendarLayout = calendarViewLayout if scrollDirection == .horizontal { contentOffset = scrollView.contentOffset.x theTargetContentOffset = targetContentOffset.pointee.x @@ -100,7 +98,7 @@ extension JTAppleCalendarView: UIScrollViewDelegate { setTargetContentOffset(calculatedOffset) case .stopAtEachCalendarFrameWidth: #if os(tvOS) - let interval = self.scrollDirection == .horizontal ? scrollView.frame.width : scrollView.frame.height + let interval = scrollDirection == .horizontal ? scrollView.frame.width : scrollView.frame.height let calculatedOffset = calculatedCurrentFixedContentOffsetFrom(interval) setTargetContentOffset(calculatedOffset) @@ -174,15 +172,15 @@ extension JTAppleCalendarView: UIScrollViewDelegate { } let pathPoint = CGPoint( x: targetContentOffset.pointee.x, y: calculatedOffSet) let attribPath = IndexPath(item: 0, section: stopSection) - if contentOffset > 0, let path = self.calendarView.indexPathForItem(at: pathPoint) { - let attrib = self.calendarView.layoutAttributesForItem(at: path)! + if contentOffset > 0, let path = calendarView.indexPathForItem(at: pathPoint) { + let attrib = calendarView.layoutAttributesForItem(at: path)! if isScrollingForward() { calculatedOffSet = attrib.frame.origin.y + attrib.frame.size.height } else { calculatedOffSet = attrib.frame.origin.y } } else if thereAreHeaders, - let attrib = self.calendarView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: attribPath) { + let attrib = calendarView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: attribPath) { // change the final value to the end of the header if isScrollingForward() { calculatedOffSet = attrib.frame.origin.y + attrib.frame.size.height @@ -231,7 +229,7 @@ extension JTAppleCalendarView: UIScrollViewDelegate { /// Tells the delegate that the scroll view has /// ended decelerating the scrolling movement. public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { - visibleDates { dates in + visibleDates { [unowned self] dates in self.delegate?.calendar(self, didScrollToDateSegmentWith: dates) } } diff --git a/Sources/UserInteractionFunctions.swift b/Sources/UserInteractionFunctions.swift index 4b1daee0..ec5c3c16 100644 --- a/Sources/UserInteractionFunctions.swift +++ b/Sources/UserInteractionFunctions.swift @@ -131,7 +131,7 @@ extension JTAppleCalendarView { unregisterHeaders() for headerViewXibName in xibFileNames { registeredHeaderViews.append(JTAppleCalendarViewSource.fromXib(headerViewXibName, bundle)) - self.calendarView.register(JTAppleCollectionReusableView.self, + calendarView.register(JTAppleCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerViewXibName) } @@ -146,14 +146,10 @@ extension JTAppleCalendarView { if classStringNames.isEmpty { return } unregisterHeaders() for headerViewClassName in classStringNames { - registeredHeaderViews.append(JTAppleCalendarViewSource - .fromClassName(headerViewClassName, bundle)) - self.calendarView.register( - JTAppleCollectionReusableView.self, - forSupplementaryViewOfKind: - UICollectionElementKindSectionHeader, - withReuseIdentifier: headerViewClassName - ) + registeredHeaderViews.append(JTAppleCalendarViewSource.fromClassName(headerViewClassName, bundle)) + calendarView.register(JTAppleCollectionReusableView.self, + forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, + withReuseIdentifier: headerViewClassName) } } @@ -164,13 +160,10 @@ extension JTAppleCalendarView { if classTypeNames.isEmpty { return } unregisterHeaders() for aClass in classTypeNames { - registeredHeaderViews - .append(JTAppleCalendarViewSource.fromType(aClass)) - self.calendarView.register( - JTAppleCollectionReusableView.self, - forSupplementaryViewOfKind: - UICollectionElementKindSectionHeader, - withReuseIdentifier: aClass.description() + registeredHeaderViews.append(JTAppleCalendarViewSource.fromType(aClass)) + calendarView.register(JTAppleCollectionReusableView.self, + forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, + withReuseIdentifier: aClass.description() ) } } @@ -185,6 +178,8 @@ extension JTAppleCalendarView { public func reloadData(withAnchor date: Date? = nil, animation: Bool = false, completionHandler: (() -> Void)? = nil) { + return + print("") if !calendarIsAlreadyLoaded { if let validCompletionHandler = completionHandler { @@ -263,7 +258,7 @@ extension JTAppleCalendarView { if !calendarIsAlreadyLoaded { // If the calendar is not yet fully loaded. // Add the task to the delayed queue - delayedExecutionClosure.append { + delayedExecutionClosure.append {[unowned self] in self.selectDates( dates, triggerSelectionDelegate: triggerSelectionDelegate, @@ -341,14 +336,14 @@ extension JTAppleCalendarView { if !(firstDayOfDate >= startOfMonthCache! && firstDayOfDate <= endOfMonthCache!) { continue } - let pathFromDates = self.pathsFromDates([date]) + let pathFromDates = pathsFromDates([date]) // If the date path youre searching for, doesnt exist, return if pathFromDates.isEmpty { continue } let sectionIndexPath = pathFromDates[0] // Remove old selections - if self.calendarView.allowsMultipleSelection == false { + if calendarView.allowsMultipleSelection == false { // If single selection is ON - let selectedIndexPaths = self.theSelectedIndexPaths + let selectedIndexPaths = theSelectedIndexPaths // made a copy because the array is about to be mutated for indexPath in selectedIndexPaths { if indexPath != sectionIndexPath { @@ -375,7 +370,7 @@ extension JTAppleCalendarView { // Just add it to be reloaded } } - if self.theSelectedIndexPaths.contains(sectionIndexPath) { + if theSelectedIndexPaths.contains(sectionIndexPath) { // If this cell is already selected, then deselect it deSelectTheDate(sectionIndexPath) } else { @@ -390,7 +385,7 @@ extension JTAppleCalendarView { // called, we do want the cell refreshed. // Reload to call itemAtIndexPath if !triggerSelectionDelegate && !allIndexPathsToReload.isEmpty { - self.batchReloadIndexPaths(allIndexPathsToReload) + batchReloadIndexPaths(allIndexPathsToReload) } } @@ -401,7 +396,7 @@ extension JTAppleCalendarView { /// - Parameter completionHandler: A completion handler that will be executed at the end of the scroll animation public func scrollToSegment(_ destination: SegmentDestination, triggerScrollToDateDelegate: Bool = true, animateScroll: Bool = true, completionHandler: (() -> Void)? = nil) { if !calendarIsAlreadyLoaded { - delayedExecutionClosure.append { + delayedExecutionClosure.append {[unowned self] in self.scrollToSegment(destination, triggerScrollToDateDelegate: triggerScrollToDateDelegate, animateScroll: animateScroll, completionHandler: completionHandler) } } @@ -485,7 +480,7 @@ extension JTAppleCalendarView { preferredScrollPosition: UICollectionViewScrollPosition? = nil, completionHandler: (() -> Void)? = nil) { if !calendarIsAlreadyLoaded { - delayedExecutionClosure.append { + delayedExecutionClosure.append {[unowned self] in self.scrollToDate(date, triggerScrollToDateDelegate: triggerScrollToDateDelegate, animateScroll: animateScroll, @@ -527,29 +522,29 @@ extension JTAppleCalendarView { } else { let validPosition = position ?? .left scrollInProgress = true - self.scrollTo(indexPath: validIndexPath, isAnimationEnabled: isAnimationEnabled, position: validPosition, completionHandler: completionHandler) + scrollTo(indexPath: validIndexPath, isAnimationEnabled: isAnimationEnabled, position: validPosition, completionHandler: completionHandler) } } // Jt101 put this into a function to reduce code between // this and the scroll to header function if !isAnimationEnabled { - self.scrollViewDidEndScrollingAnimation(self.calendarView) + scrollViewDidEndScrollingAnimation(calendarView) } - self.scrollInProgress = false + scrollInProgress = false } // This part should be inside the mainRunLoop - if !((firstDayOfDate >= self.startOfMonthCache!) && (firstDayOfDate <= self.endOfMonthCache!)) { + if !((firstDayOfDate >= startOfMonthCache!) && (firstDayOfDate <= endOfMonthCache!)) { return } - let retrievedPathsFromDates = self.pathsFromDates([date]) + let retrievedPathsFromDates = pathsFromDates([date]) guard !retrievedPathsFromDates.isEmpty else { return } - let sectionIndexPath = self.pathsFromDates([date])[0] - var position: UICollectionViewScrollPosition = self.scrollDirection == .horizontal ? .left : .top - if !self.scrollingMode.pagingIsEnabled() { + let sectionIndexPath = pathsFromDates([date])[0] + var position: UICollectionViewScrollPosition = scrollDirection == .horizontal ? .left : .top + if !scrollingMode.pagingIsEnabled() { if let validPosition = preferredScrollPosition { - if self.scrollDirection == .horizontal { + if scrollDirection == .horizontal { if validPosition == .left || validPosition == .right || validPosition == .centeredHorizontally { position = validPosition } @@ -561,10 +556,10 @@ extension JTAppleCalendarView { } } var point: CGPoint? - switch self.scrollingMode { + switch scrollingMode { case .stopAtEach, .stopAtEachSection, .stopAtEachCalendarFrameWidth: - if self.scrollDirection == .horizontal || (self.scrollDirection == .vertical && !self.thereAreHeaders) { - point = self.targetPointForItemAt(indexPath: sectionIndexPath) + if scrollDirection == .horizontal || (scrollDirection == .vertical && !thereAreHeaders) { + point = targetPointForItemAt(indexPath: sectionIndexPath) } default: break @@ -579,12 +574,12 @@ extension JTAppleCalendarView { func scrollTo(point: CGPoint, triggerScrollToDateDelegate: Bool? = nil, isAnimationEnabled: Bool, completionHandler: (() -> Void)?) { if let validCompletionHandler = completionHandler { - self.delayedExecutionClosure.append(validCompletionHandler) + delayedExecutionClosure.append(validCompletionHandler) } self.triggerScrollToDateDelegate = triggerScrollToDateDelegate - self.scrollInProgress = true - self.calendarView.setContentOffset(point, animated: isAnimationEnabled) - self.scrollInProgress = false + scrollInProgress = true + calendarView.setContentOffset(point, animated: isAnimationEnabled) + scrollInProgress = false } func scrollTo(rect: CGRect, triggerScrollToDateDelegate: Bool? = nil, isAnimationEnabled: Bool, completionHandler: (() -> Void)?) { @@ -634,7 +629,7 @@ extension JTAppleCalendarView { /// - DateSegmentInfo public func visibleDates(_ completionHandler: @escaping (_ dateSegmentInfo: DateSegmentInfo) ->()) { if !calendarIsAlreadyLoaded { - delayedExecutionClosure.append { + delayedExecutionClosure.append {[unowned self] in self.visibleDates(completionHandler) } return