Skip to content

Commit

Permalink
Merge pull request #1 from jvisenti/fix/force-unwrap-crash
Browse files Browse the repository at this point in the history
Converted some properties from implicitly unwrapped optionals to address crash in patchthecode#1217
  • Loading branch information
Dean151 committed Sep 26, 2023
2 parents 718f0ab + 06575f1 commit 2d39c36
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 25 deletions.
Expand Up @@ -433,7 +433,7 @@ extension JTACMonthView {

switch scrollDirection {
case .horizontal:
if calendarViewLayout.thereAreHeaders || _cachedConfiguration.generateOutDates == .tillEndOfGrid {
if calendarViewLayout.thereAreHeaders || _cachedConfiguration?.generateOutDates == .tillEndOfGrid {
fixedScrollSize = calendarViewLayout.sizeOfContentForSection(0)
} else {
fixedScrollSize = frame.width
Expand Down
12 changes: 6 additions & 6 deletions Sources/JTAppleCalendar/JTACMonthActionFunctions.swift
Expand Up @@ -249,12 +249,12 @@ extension JTACMonthView {
// 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 ||
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 ||
Expand Down
2 changes: 1 addition & 1 deletion Sources/JTAppleCalendar/JTACMonthDelegateProtocol.swift
Expand Up @@ -27,7 +27,7 @@ import UIKit
protocol JTACMonthDelegateProtocol: AnyObject {
// Variables
var allowsDateCellStretching: Bool {get set}
var _cachedConfiguration: ConfigurationParameters! {get set}
var _cachedConfiguration: ConfigurationParameters? {get set}
var calendarDataSource: JTACMonthViewDataSource? {get set}
var cellSize: CGFloat {get set}
var anchorDate: Date? {get set}
Expand Down
24 changes: 14 additions & 10 deletions Sources/JTAppleCalendar/JTACMonthLayout.swift
Expand Up @@ -34,7 +34,7 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {
var lastSetCollectionViewSize: CGRect = .zero

var cellSize: CGSize = CGSize.zero
var shouldUseUserItemSizeInsteadOfDefault: Bool { return delegate.cellSize == 0 ? false: true }
var shouldUseUserItemSizeInsteadOfDefault: Bool { return (delegate?.cellSize ?? 0) == 0 ? false: true }
var scrollDirection: UICollectionView.ScrollDirection = .horizontal
var maxMissCount: Int = 0
var cellCache: [Int: [(item: Int, section: Int, xOffset: CGFloat, yOffset: CGFloat, width: CGFloat, height: CGFloat)]] = [:]
Expand All @@ -49,15 +49,18 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {
var headerSizes: [AnyHashable:CGFloat] = [:]
var focusIndexPath: IndexPath?
var isCalendarLayoutLoaded: Bool { return !cellCache.isEmpty }
var layoutIsReadyToBePrepared: Bool { return !(!cellCache.isEmpty || delegate.calendarDataSource == nil) }
var layoutIsReadyToBePrepared: Bool {
guard let delegate = delegate else { return false }
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: JTACMonthDelegateProtocol!
weak var delegate: JTACMonthDelegateProtocol?

var currentHeader: (section: Int, size: CGSize)? // Tracks the current header size
var currentCell: (section: Int, width: CGFloat, height: CGFloat)? // Tracks the current cell size
Expand Down Expand Up @@ -89,7 +92,7 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {
}

var updatedLayoutCellSize: CGSize {
guard let cachedConfiguration = delegate._cachedConfiguration else { return .zero }
guard let delegate = delegate, let cachedConfiguration = delegate._cachedConfiguration else { return .zero }

// Default Item height and width
var height: CGFloat = collectionView!.bounds.size.height / CGFloat(cachedConfiguration.numberOfRows)
Expand Down Expand Up @@ -128,8 +131,8 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {
lastSetCollectionViewSize = collectionView!.frame

if !layoutIsReadyToBePrepared {
// Layoout may not be ready, but user might have reloaded with an anchor date
let requestedOffset = delegate.requestedContentOffset
// Layout may not be ready, but user might have reloaded with an anchor date
let requestedOffset = delegate?.requestedContentOffset ?? .zero
if requestedOffset != .zero { collectionView!.setContentOffset(requestedOffset, animated: false) }

// execute any other delayed tasks
Expand All @@ -152,7 +155,7 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {
}

// Set the first content offset only once. This will prevent scrolling animation on viewDidload.
if !firstContentOffsetWasSet {
if !firstContentOffsetWasSet, let delegate = delegate {
firstContentOffsetWasSet = true
let firstContentOffset = delegate.requestedContentOffset
collectionView!.setContentOffset(firstContentOffset, animated: false)
Expand All @@ -163,10 +166,11 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {
}

func setupDataFromDelegate() {
guard let delegate = delegate, let cachedConfiguration = delegate._cachedConfiguration else { return }
// 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
strictBoundaryRulesShouldApply = thereAreHeaders || cachedConfiguration.hasStrictBoundaries
numberOfRows = cachedConfiguration.numberOfRows
monthMap = delegate.monthMap
allowsDateCellStretching = delegate.allowsDateCellStretching
monthInfo = delegate.monthInfo
Expand Down Expand Up @@ -369,7 +373,7 @@ class JTACMonthLayout: UICollectionViewLayout, JTACMonthLayoutProtocol {

let retval = UICollectionViewLayoutAttributes(forDecorationViewOfKind: decorationViewID, with: indexPath)
decorationCache[indexPath] = retval
retval.frame = delegate.sizeOfDecorationView(indexPath: indexPath)
retval.frame = delegate?.sizeOfDecorationView(indexPath: indexPath) ?? .zero
retval.zIndex = -1
return retval
}
Expand Down
Expand Up @@ -65,7 +65,7 @@ extension JTACMonthLayout {
xStride = contentWidth
endOfSectionOffsets.append(contentWidth)
} else {
if totalDayCounter >= delegate.totalDays {
if totalDayCounter >= delegate?.totalDays ?? 0 {
contentWidth += (attribute.width * 7) + endSeparator
endOfSectionOffsets.append(contentWidth)
}
Expand Down
Expand Up @@ -77,7 +77,7 @@ extension JTACMonthLayout {
endOfSectionOffsets.append(contentHeight - sectionInset.top)

} else {
if totalDayCounter >= delegate.totalDays {
if totalDayCounter >= delegate?.totalDays ?? 0 {
yCellOffset += attribute.height + sectionInset.top
contentHeight = yCellOffset
endOfSectionOffsets.append(contentHeight - sectionInset.top)
Expand Down
8 changes: 4 additions & 4 deletions Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift
Expand Up @@ -135,9 +135,9 @@ extension JTACMonthView {
}

func indexPathOfdateCellCounterPath(_ date: Date, dateOwner: DateOwner) -> IndexPath? {
if (_cachedConfiguration.generateInDates == .off ||
_cachedConfiguration.generateInDates == .forFirstMonthOnly) &&
_cachedConfiguration.generateOutDates == .off {
if (_cachedConfiguration?.generateInDates == .off ||
_cachedConfiguration?.generateInDates == .forFirstMonthOnly) &&
_cachedConfiguration?.generateOutDates == .off {
return nil
}
var retval: IndexPath?
Expand Down Expand Up @@ -377,7 +377,7 @@ extension JTACMonthView {
}
if let monthDate = calendar.date(byAdding: .month, value: monthIndex, to: startDateCache) {
let monthNumber = calendar.dateComponents([.month], from: monthDate)
let numberOfRowsForSection = monthData.numberOfRows(for: section, developerSetRows: _cachedConfiguration.numberOfRows)
let numberOfRowsForSection = monthData.numberOfRows(for: section, developerSetRows: _cachedConfiguration?.numberOfRows ?? 0)
return ((startDate, endDate), monthNumber.month!, numberOfRowsForSection)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/JTAppleCalendar/JTACMonthView.swift
Expand Up @@ -110,7 +110,7 @@ open class JTACMonthView: UICollectionView {
}

// Configuration parameters from the dataSource
var _cachedConfiguration: ConfigurationParameters!
var _cachedConfiguration: ConfigurationParameters?
// Set the start of the month
var startOfMonthCache: Date!
// Set the end of month
Expand Down

0 comments on commit 2d39c36

Please sign in to comment.