Skip to content
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
2 changes: 2 additions & 0 deletions Sources/Internal/Data/Data.MonthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extension [Data.MonthView] {
createDatesRange()
.map(createMonthDate)
.map(createMonthViewData)
.reversed()
}
}
private extension [Data.MonthView] {
Expand Down Expand Up @@ -93,3 +94,4 @@ private extension Data.MonthView {
private extension Data.MonthView {
static var weekdaysNumber: Int { MWeekday.allCases.count }
}

26 changes: 26 additions & 0 deletions Sources/Public/Configurables/Public+CalendarConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ public extension CalendarConfig {
/// Sets the first day of the week.
/// DEFAULT:: Monday
func firstWeekday(_ value: MWeekday) -> Self { MCalendar.firstWeekday = value; return self }

/// Sets the first day of the system week.
/// DEFAULT:: System weekday
func firstSystemWeekday() -> Self {
return firstWeekday(MWeekday(rawValue: Calendar.current.firstWeekday) ?? .monday)
}

/// Sets the first day of the week based on the country Code
/// DEFAULT:: US
func firstWeekday(for countryCode: String = "US") -> Self {
let weekday = WeekdayRule.firstWeekday(for: countryCode)
return firstWeekday(weekday)
}

/// Sets the first day of the week based on the device locale
/// DEFAULT:: Current
func firstWeekday(for locale: Locale = Locale.current) -> Self {
var countryCode: String

if #available(iOS 16, *) {
countryCode = locale.region?.identifier ?? "US"
} else {
countryCode = locale.identifier
}
return firstWeekday(for: countryCode)
}

/// Sets the locale of the calendar.
/// DEFAULT: AutoupdatingCurrent
Expand Down
49 changes: 49 additions & 0 deletions Sources/Public/Enums/Public+MWeekday.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,52 @@


public enum MWeekday: Int { case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday }

public enum WeekdayRule {
enum StartDay {
case sunday, saturday, monday
}

static func firstWeekday(for countryCode: String) -> MWeekday {
let startDay = startDay(for: countryCode)
switch startDay {
case .sunday: return .sunday
case .saturday: return .saturday
case .monday: return .monday
}
}

private static func startDay(for countryCode: String) -> StartDay {
let country = countryCode.uppercased()

// 🇺🇸 SUNDAY - ~50% of the world
let sundayCountries = [
// North America
"US", "CA", "MX",
// South America
"BR", "AR", "CL", "CO", "PE", "VE", "BO", "UY", "PY", "EC", "GY", "SR", "GF",
// Asia
"IN", "CN", "JP", "KR", "PH", "VN", "TH", "ID", "MY", "SG",
// Middle East
"IL",
// Oceania
"AU", "NZ"
]

// 🇸🇦 SATURDAY - Muslim countries
let saturdayCountries = [
// Persian Gulf
"SA", "QA", "KW", "OM", "BH", "AE", // UAE partially switched to Sunday
// North Africa
"EG", "LY", "DZ", "MA", "TN",
// Others
"AF", "IQ", "YE", "SO"
]

if sundayCountries.contains(country) { return .sunday }
if saturdayCountries.contains(country) { return .saturday }

// Default: Monday (Europe, Russia, majority)
return .monday
}
}
7 changes: 5 additions & 2 deletions Sources/Public/View Protocols/Public+DayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ private extension DayView {
}
private extension DayView {
var rangeSelectionViewCorners: RoundedRectangle.Corner {
if isBeginningOfRange() { return [.topLeft, .bottomLeft] }
if isEndOfRange() { return [.topRight, .bottomRight] }
if isBeginningOfRange() && isInterfaceLTR() { return [.topLeft, .bottomLeft] }
if isEndOfRange() && isInterfaceLTR() { return [.topRight, .bottomRight] }
if isBeginningOfRange() && !isInterfaceLTR() { return [.topRight, .bottomRight] }
if isEndOfRange() && !isInterfaceLTR() { return [.topLeft, .bottomLeft] }

return []
}
Expand Down Expand Up @@ -99,6 +101,7 @@ public extension DayView {
func isBeginningOfRange() -> Bool { date.isSame(.day, as: selectedRange?.wrappedValue?.getRange()?.lowerBound) }
func isEndOfRange() -> Bool { date.isSame(.day, as: selectedRange?.wrappedValue?.getRange()?.upperBound) }
func isWithinRange() -> Bool { selectedRange?.wrappedValue?.isRangeCompleted() == true && selectedRange?.wrappedValue?.contains(date) == true }
func isInterfaceLTR() -> Bool { UIApplication.shared.userInterfaceLayoutDirection == .leftToRight }
}

// MARK: - Others
Expand Down