Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blackout #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions KDCalendar/CalendarView/CalendarDayCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ open class CalendarDayCell: UICollectionViewCell {
}
}

var isBlackout: Bool = false {
didSet {
switch isBlackout {
case true:
self.textLabel.textColor = CalendarView.Style.cellTextColorBlackout
self.bgView.backgroundColor = CalendarView.Style.cellColorBlackout
self.bgView.layer.borderWidth = CalendarView.Style.cellBorderWidthBlackout
case false:
self.bgView.backgroundColor = CalendarView.Style.cellColorDefault
self.bgView.layer.borderWidth = CalendarView.Style.cellBorderWidth
self.textLabel.textColor = CalendarView.Style.cellTextColorDefault
}
}
}

override open var isSelected: Bool {
didSet {
switch isSelected {
Expand Down
4 changes: 2 additions & 2 deletions KDCalendar/CalendarView/CalendarHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ open class CalendarHeaderView: UIView {

let weekdayLabel = UILabel()

weekdayLabel.font = UIFont(name: CalendarView.Style.headerFontName, size: 14.0)
weekdayLabel.font = UIFont(name: CalendarView.Style.headerDayFontName, size: CalendarView.Style.headerDayFontSize)

weekdayLabel.text = formatter.shortWeekdaySymbols[(index % 7)]

weekdayLabel.textColor = CalendarView.Style.headerTextColor
weekdayLabel.textColor = CalendarView.Style.headerDayTextColor
weekdayLabel.textAlignment = NSTextAlignment.center

v.addSubview(weekdayLabel)
Expand Down
2 changes: 1 addition & 1 deletion KDCalendar/CalendarView/CalendarView+DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension CalendarView: UICollectionViewDataSource {
}
if let delegate = self.delegate, let dateBeingSelected = self.dateFromIndexPath(indexPath) {
if delegate.calendar(self, canSelectDate: dateBeingSelected) == false {
dayCell.isWeekend = true
dayCell.isBlackout = true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to add backwards compatibility for isWeekend?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was originally trying to re-purpose isWeekend to give us our blackout behavior, and this is actually un-doing that. isWeekend should be back to normal now, and un-used by our app. We set .marksWeekends to false now.

}
}
if let eventsForDay = self.eventsByIndexPath[indexPath] {
Expand Down
9 changes: 4 additions & 5 deletions KDCalendar/CalendarView/CalendarView+Delegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ extension CalendarView: UICollectionViewDelegateFlowLayout {
guard let date = self.dateFromIndexPath(indexPath) else { return }

if let index = selectedIndexPaths.index(of: indexPath) {

delegate?.calendar(self, didDeselectDate: date)

selectedIndexPaths.remove(at: index)
selectedDates.remove(at: index)
// We want to prevent deselecting ship date
// delegate?.calendar(self, didDeselectDate: date)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could make this more mergeable with another delegate method:

if delegate?.calendar(self, canDeselectDate: date) ?? false {
  delegate?.calendar(self, didDeselectDate: date)
  selectedIndexPaths.remove(at: index)
  selectedDates.remove(at: index)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another "too big of a refactor" where I think the correct generalization would be a SelectionMode enum with an .alwaysOneSelectedDay option. The delegate method rings false since everything CAN be deselected, but only by selecting something else.

// selectedIndexPaths.remove(at: index)
// selectedDates.remove(at: index)

} else {

Expand Down
12 changes: 9 additions & 3 deletions KDCalendar/CalendarView/CalendarView+Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import UIKit

extension CalendarView {

/// This is the view model
public struct Style {

public enum CellShapeOptions {
case round
case square
Expand All @@ -37,12 +36,14 @@ extension CalendarView {
//Header
public static var headerHeight: CGFloat = 80.0
public static var headerTextColor = UIColor.gray
public static var headerDayTextColor = UIColor.gray
public static var headerFontName: String = "Helvetica"
public static var headerFontSize: CGFloat = 20.0
public static var headerDayFontName: String = "Helvetica"
public static var headerDayFontSize: CGFloat = 14.0

//Common
public static var cellShape = CellShapeOptions.bevel(4.0)

public static var firstWeekday = FirstWeekdayOptions.monday

//Default Style
Expand All @@ -66,5 +67,10 @@ extension CalendarView {
public static var cellBorderWidthWeekend = CGFloat(0.0)
public static var cellColorWeekend = UIColor(red:1.00, green:0.84, blue:0.65, alpha:1.00)

//Blackout Style
public static var cellTextColorBlackout = UIColor(red:1.00, green:0.84, blue:0.65, alpha:1.00)
public static var cellBorderWidthBlackout = CGFloat(0.0)
public static var cellColorBlackout = UIColor(red:1.00, green:0.84, blue:0.65, alpha:1.00)

}
}
29 changes: 26 additions & 3 deletions KDCalendar/CalendarView/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class CalendarView: UIView {

public lazy var calendar: Calendar = {
var gregorian = Calendar(identifier: .gregorian)
gregorian.timeZone = TimeZone(abbreviation: "UTC")!
//gregorian.timeZone = TimeZone(abbreviation: "UTC")!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the Calendar should always be in the user's current time zone?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be in the timezone of the shipping facility, if known, but this fixes an off-by-one error since it handles day-dates as "midnight". There is a lot of clean-up that could improve this, but it'd be a large refactor on the pod.

return gregorian
}()

Expand Down Expand Up @@ -329,8 +329,7 @@ extension CalendarView {
function: - scroll calendar at date (month/year) passed as parameter.
*/
public func setDisplayDate(_ date: Date, animated: Bool = false) {

guard (date >= startDateCache) && (date <= endDateCache) else { return }
guard (date >= startOfMonthCache) && (date <= endDateCache) else { return }
self.collectionView.setContentOffset(self.scrollViewOffset(for: date), animated: animated)
self.displayDateOnHeader(date)
}
Expand Down Expand Up @@ -372,6 +371,18 @@ extension CalendarView {
goToMonthWithOffet(1)
}

public func isLastMonth() -> Bool {
var dateComponents = DateComponents()
dateComponents.month = 1
guard let displayDate = self.displayDate else { return true }
guard let newDate = self.calendar.date(byAdding: dateComponents, to: displayDate) else { return true }
if newDate <= endDateCache {
return false
} else {
return true
}
}

/*
method: - goToPreviousMonth
function: - scroll the calendar by one month in the past
Expand All @@ -380,6 +391,18 @@ extension CalendarView {
goToMonthWithOffet(-1)
}

public func isFirstMonth() -> Bool {
var dateComponents = DateComponents()
dateComponents.month = -1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My favorite month of the year is -1

guard let displayDate = self.displayDate else { return true }
guard let newDate = self.calendar.date(byAdding: dateComponents, to: displayDate) else { return true }
if newDate >= startOfMonthCache {
return false
} else {
return true
}
}

public func loadEvents(onComplete: ((Error?) -> Void)? = nil) {

EventsManager.load(from: self.startDateCache, to: self.endDateCache) { // (events:[CalendarEvent]?) in
Expand Down