Skip to content

Commit

Permalink
fix #45 remove current calendar (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
UriyDevyataev authored Mar 13, 2024
1 parent 5c033df commit 4bd9b5e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Sources/Extensions/Date+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import Foundation

extension Date {

func startOfMonth(in calendar: Calendar = .current) -> Date {
func startOfMonth(in calendar: Calendar) -> Date {
calendar.date(from: calendar.dateComponents([.year, .month], from: calendar.startOfDay(for: self)))!.startOfDay(in: calendar)
}

func endOfMonth(in calendar: Calendar = .current) -> Date {
func endOfMonth(in calendar: Calendar) -> Date {
calendar.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth(in: calendar))!.endOfDay(in: calendar)
}

func isInSameDay(in calendar: Calendar = .current, date: Date) -> Bool {
func isInSameDay(in calendar: Calendar, date: Date) -> Bool {
calendar.isDate(self, equalTo: date, toGranularity: .day)
}

func isInSameMonth(in calendar: Calendar = .current, date: Date) -> Bool {
func isInSameMonth(in calendar: Calendar, date: Date) -> Bool {
calendar.component(.month, from: self) == calendar.component(.month, from: date)
}

func startOfDay(in calendar: Calendar = .current) -> Date {
func startOfDay(in calendar: Calendar) -> Date {
calendar.date(bySettingHour: 0, minute: 0, second: 0, of: self)!
}

func endOfDay(in calendar: Calendar = .current) -> Date {
func endOfDay(in calendar: Calendar) -> Date {
calendar.date(bySettingHour: 23, minute: 59, second: 59, of: self)!
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/Models/Shortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public struct FastisShortcut<Value: FastisValue>: Hashable {

internal func isEqual(to value: Value, calendar: Calendar) -> Bool {
if let date1 = self.action(calendar) as? Date, let date2 = value as? Date {
return date1.isInSameDay(date: date2)
return date1.isInSameDay(in: calendar, date: date2)
} else if let value1 = self.action(calendar) as? FastisRange, let value2 = value as? FastisRange {
return value1 == value2
}
Expand All @@ -69,9 +69,9 @@ public extension FastisShortcut where Value == FastisRange {

/// Range: from **`now.startOfDay`** to **`now.endOfDay`**
static var today: FastisShortcut {
FastisShortcut(name: "Today") { _ in
FastisShortcut(name: "Today") { calendar in
let now = Date()
return FastisRange(from: now.startOfDay(), to: now.endOfDay())
return FastisRange(from: now.startOfDay(in: calendar), to: now.endOfDay(in: calendar))
}
}

Expand All @@ -80,7 +80,7 @@ public extension FastisShortcut where Value == FastisRange {
FastisShortcut(name: "Last week") { calendar in
let now = Date()
let weekAgo = calendar.date(byAdding: .day, value: -7, to: now)!
return FastisRange(from: weekAgo.startOfDay(), to: now.endOfDay())
return FastisRange(from: weekAgo.startOfDay(in: calendar), to: now.endOfDay(in: calendar))
}
}

Expand All @@ -89,7 +89,7 @@ public extension FastisShortcut where Value == FastisRange {
FastisShortcut(name: "Last month") { calendar in
let now = Date()
let monthAgo = calendar.date(byAdding: .month, value: -1, to: now)!
return FastisRange(from: monthAgo.startOfDay(), to: now.endOfDay())
return FastisRange(from: monthAgo.startOfDay(in: calendar), to: now.endOfDay(in: calendar))
}
}

Expand Down
4 changes: 0 additions & 4 deletions Sources/Models/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public struct FastisRange: FastisValue, Hashable {
FastisRange(from: fromDate, to: toDate)
}

public var onSameDay: Bool {
self.fromDate.isInSameDay(date: self.toDate)
}

public func outOfRange(minDate: Date?, maxDate: Date?) -> Bool {
self.fromDate < minDate ?? self.fromDate || self.toDate > maxDate ?? self.toDate
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/Views/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
self.privateMinimumDate
}
set {
self.privateMinimumDate = newValue?.startOfDay()
self.privateMinimumDate = newValue?.startOfDay(in: self.config.calendar)
}
}

Expand All @@ -259,7 +259,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
self.privateMaximumDate
}
set {
self.privateMaximumDate = newValue?.endOfDay()
self.privateMaximumDate = newValue?.endOfDay(in: self.config.calendar)
}
}

Expand All @@ -272,6 +272,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
self.appearance = config.controller
self.dayFormatter.locale = config.calendar.locale
self.dayFormatter.calendar = config.calendar
self.dayFormatter.timeZone = config.calendar.timeZone
self.dayFormatter.dateFormat = "d"
super.init(nibName: nil, bundle: nil)
}
Expand Down Expand Up @@ -499,7 +500,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
}

let dateRangeChangesDisabled = !self.allowDateRangeChanges
let rangeSelected = !oldValue.fromDate.isInSameDay(date: oldValue.toDate)
let rangeSelected = !oldValue.fromDate.isInSameDay(in: self.config.calendar, date: oldValue.toDate)
if dateRangeChangesDisabled, rangeSelected {
return .from(date.startOfDay(in: self.config.calendar), to: date.endOfDay(in: self.config.calendar))
} else if date.isInSameDay(in: self.config.calendar, date: oldValue.fromDate) {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Views/CurrentValueView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final class CurrentValueView<Value: FastisValue>: UIView {
formatter.locale = self.calendar.locale
formatter.dateFormat = self.config.format
formatter.calendar = self.calendar
formatter.timeZone = self.calendar.timeZone
return formatter
}()

Expand Down Expand Up @@ -127,7 +128,7 @@ final class CurrentValueView<Value: FastisValue>: UIView {
self.clearButton.alpha = 1
self.clearButton.isUserInteractionEnabled = true

if value.onSameDay {
if value.fromDate.isInSameDay(in: self.calendar, date: value.toDate) {
self.label.text = self.dateFormatter.string(from: value.fromDate)
} else {
self.label.text = self.dateFormatter.string(from: value.fromDate) + "" + self.dateFormatter.string(from: value.toDate)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Views/DayCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ final class DayCell: JTACDayCell {

config.dateLabelText = state.text

if let minimumDate, state.date < minimumDate.startOfDay() {
if let minimumDate, state.date < minimumDate.startOfDay(in: calendar) {
config.isDateEnabled = false
return config
} else if let maximumDate, state.date > maximumDate.endOfDay() {
} else if let maximumDate, state.date > maximumDate.endOfDay(in: calendar) {
config.isDateEnabled = false
return config
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Views/MonthHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ final class MonthHeader: JTACMonthReusableView {
self.monthFormatter.calendar = calendar
self.monthFormatter.dateFormat = config.monthFormat
self.monthFormatter.locale = calendar.locale
self.monthFormatter.timeZone = calendar.timeZone
self.monthLabel.font = config.labelFont
self.monthLabel.textColor = config.labelColor
self.monthLabel.textAlignment = config.labelAlignment
Expand Down

0 comments on commit 4bd9b5e

Please sign in to comment.