Skip to content

Commit

Permalink
Fixed Leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
patchthecode committed Mar 6, 2017
1 parent 19a77e2 commit f376372
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 143 deletions.
8 changes: 4 additions & 4 deletions Sources/GlobalFunctionsAndExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
25 changes: 14 additions & 11 deletions Sources/JTAppleCalendarLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -177,7 +186,7 @@ open class JTAppleCalendarLayout: UICollectionViewLayout, JTAppleCalendarLayoutP
section += 1
}
}
contentHeight = self.collectionView!.bounds.size.height
contentHeight = collectionView!.bounds.size.height
}

func verticalStuff() {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
106 changes: 47 additions & 59 deletions Sources/JTAppleCalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)?
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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())
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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)
}
}
Expand All @@ -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) }
Expand All @@ -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,
Expand All @@ -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()
}
}
}
Expand Down Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/JTAppleCollectionReusableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/JTAppleDayCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit f376372

Please sign in to comment.