Skip to content

Commit a5a75ec

Browse files
author
quivr
committed
fix: dangerous implementation of daydate
1 parent 00232bf commit a5a75ec

File tree

6 files changed

+234
-165
lines changed

6 files changed

+234
-165
lines changed

QVRWeekView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'QVRWeekView'
11-
s.version = '0.14.1'
11+
s.version = '0.14.2'
1212
s.summary = 'QVRWeekView is a simple calendar week view with support for horizontal, vertical scrolling and zooming.'
1313
s.swift_version = '5'
1414

QVRWeekView/Classes/Common/DayDate.swift

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
import Foundation
1010
import UIKit
1111

12-
/**
13-
Enum stores the text mode that the day date should return.
14-
*/
12+
/// Enum stores the text mode that the day date should return.
1513
public enum TextMode {
1614
case large
1715
case normal
1816
case small
1917
}
2018

21-
/**
22-
Day date class is used as a reliable way to assign a day to things such as dayViewCells and dictionaries
23-
storing event and frame data. DayDates are not influenced by timezones and thus the date it has been given will
24-
remain. DayDates are also easy to compare, print as strings and are hashable.
25-
*/
19+
/// Day date class is used as a reliable way to assign a day to things such as dayViewCells and dictionaries
20+
/// storing event and frame data. DayDates are not influenced by timezones and thus the date it has been given will
21+
/// remain. DayDates are also easy to compare, print as strings and are hashable.
2622
public struct DayDate: Hashable, Comparable, CustomStringConvertible, Strideable {
2723
public let day: Int
2824
public let month: Int
@@ -71,15 +67,9 @@ public struct DayDate: Hashable, Comparable, CustomStringConvertible, Strideable
7167
self.era = cal.component(.era, from: date)
7268
}
7369

74-
public init() {
75-
self.day = -1
76-
self.month = -1
77-
self.year = -1
78-
self.era = -1
79-
}
80-
8170
public static func == (lhs: DayDate, rhs: DayDate) -> Bool {
82-
return lhs.day == rhs.day && lhs.month == rhs.month && lhs.year == rhs.year && lhs.era == rhs.era
71+
return lhs.day == rhs.day && lhs.month == rhs.month && lhs.year == rhs.year
72+
&& lhs.era == rhs.era
8373
}
8474

8575
public static func < (lhs: DayDate, rhs: DayDate) -> Bool {
@@ -88,14 +78,22 @@ public struct DayDate: Hashable, Comparable, CustomStringConvertible, Strideable
8878
if lhs.month == rhs.month {
8979
if lhs.day == rhs.day {
9080
return false
91-
} else { return lhs.day < rhs.day }
92-
} else { return lhs.month < rhs.month }
93-
} else { return lhs.year < rhs.year }
94-
} else { return lhs.era < rhs.era }
81+
} else {
82+
return lhs.day < rhs.day
83+
}
84+
} else {
85+
return lhs.month < rhs.month
86+
}
87+
} else {
88+
return lhs.year < rhs.year
89+
}
90+
} else {
91+
return lhs.era < rhs.era
92+
}
9593
}
9694

9795
static func + (lhs: DayDate, rhs: Int) -> DayDate {
98-
return DayDate(day: lhs.day + rhs, month: lhs.month, year: lhs.year, era: lhs.era)
96+
return lhs.getDayDateWith(daysAdded: rhs)
9997
}
10098

10199
public func hash(into hasher: inout Hasher) {

QVRWeekView/Classes/Common/EventLayer.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ class EventLayer: CALayer {
2929
let eventTextLayer = CATextLayer()
3030
eventTextLayer.isWrapped = true
3131
eventTextLayer.contentsScale = UIScreen.main.scale
32-
eventTextLayer.string = event.getDisplayString(withMainFont: layout.eventLabelFont,
33-
infoFont: layout.eventLabelInfoFont,
34-
andColor: layout.eventLabelTextColor)
32+
eventTextLayer.string = event.getDisplayString(
33+
withMainFont: layout.eventLabelFont,
34+
infoFont: layout.eventLabelInfoFont,
35+
andColor: layout.eventLabelTextColor)
3536

3637
let xPadding = layout.eventLabelHorizontalTextPadding
3738
let yPadding = layout.eventLabelVerticalTextPadding
38-
eventTextLayer.frame = CGRect(x: frame.origin.x + xPadding,
39-
y: frame.origin.y + yPadding,
40-
width: frame.width - 2*xPadding,
41-
height: frame.height - 2*yPadding)
39+
eventTextLayer.frame = CGRect(
40+
x: frame.origin.x + xPadding,
41+
y: frame.origin.y + yPadding,
42+
width: frame.width - 2 * xPadding,
43+
height: frame.height - 2 * yPadding)
4244
self.addSublayer(eventTextLayer)
4345
}
4446

QVRWeekView/Classes/Views/DayScrollView.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ UICollectionViewDelegate, UICollectionViewDataSource, DayViewCellDelegate, Frame
211211
let cvLeft = CGPoint(x: collectionView.contentOffset.x, y: collectionView.center.y + collectionView.contentOffset.y)
212212
if let path = collectionView.indexPathForItem(at: cvLeft),
213213
let dayViewCell = collectionView.cellForItem(at: path) as? DayViewCell,
214-
!scrollingToDay, activeDay != dayViewCell.date {
214+
let dayViewCellDate = dayViewCell.date,
215+
!scrollingToDay, activeDay != dayViewCellDate {
215216

216-
self.activeDay = dayViewCell.date
217+
self.activeDay = dayViewCellDate
217218
if activeDay > currentPeriod.lateMidLimit {
218219
updatePeriod()
219220
}
@@ -264,8 +265,7 @@ UICollectionViewDelegate, UICollectionViewDataSource, DayViewCellDelegate, Frame
264265
}
265266

266267
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
267-
if let dayViewCell = cell as? DayViewCell {
268-
let dayDate = dayViewCell.date
268+
if let dayViewCell = cell as? DayViewCell, let dayDate = dayViewCell.date {
269269
self.weekView?.addDayLabel(forIndexPath: indexPath, withDate: dayDate)
270270
if let allDayEvents = allDayEventsData[dayDate] {
271271
self.weekView?.addAllDayEvents(allDayEvents, forIndexPath: indexPath, withDate: dayDate)
@@ -274,8 +274,7 @@ UICollectionViewDelegate, UICollectionViewDataSource, DayViewCellDelegate, Frame
274274
}
275275

276276
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
277-
if let dayViewCell = cell as? DayViewCell {
278-
let dayDate = dayViewCell.date
277+
if let dayViewCell = cell as? DayViewCell, let dayDate = dayViewCell.date {
279278
self.weekView?.discardDayLabel(withDate: dayDate)
280279
if self.weekView?.hasAllDayEvents(forDate: dayDate) == true {
281280
self.weekView?.removeAllDayEvents(forDate: dayDate)
@@ -468,8 +467,7 @@ UICollectionViewDelegate, UICollectionViewDataSource, DayViewCellDelegate, Frame
468467
self.eventsData = newEventsData
469468
self.allDayEventsData = newAllDayEvents
470469
for cell in self.dayCollectionView.visibleCells {
471-
if let dayViewCell = cell as? DayViewCell {
472-
let dayDate = dayViewCell.date
470+
if let dayViewCell = cell as? DayViewCell, let dayDate = dayViewCell.date {
473471
let allThisDayEvents = self.allDayEventsData[dayDate]
474472
if allThisDayEvents == nil && self.weekView?.hasAllDayEvents(forDate: dayDate) == true {
475473
self.weekView?.removeAllDayEvents(forDate: dayDate)
@@ -490,10 +488,12 @@ UICollectionViewDelegate, UICollectionViewDataSource, DayViewCellDelegate, Frame
490488
for dayDate in sortedChangedDays {
491489
self.processEventsData(forDayDate: dayDate)
492490
}
493-
// Redraw days with no changed data
494-
for (_, dayViewCell) in self.dayViewCells where !sortedChangedDays.contains(dayViewCell.date) {
495-
dayViewCell.setNeedsLayout()
491+
for (_, dayViewCell) in self.dayViewCells {
492+
if let date = dayViewCell.date, !sortedChangedDays.contains(date) {
493+
dayViewCell.setNeedsLayout()
494+
}
496495
}
496+
497497
}
498498

499499
func requestEvents() {

0 commit comments

Comments
 (0)