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

[APP-83] The first day of week in the Calendar based on the User Localisation #2110

Merged
merged 5 commits into from
Nov 21, 2024
Merged
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
9 changes: 5 additions & 4 deletions Backpack-SwiftUI/Calendar/Classes/CalendarHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ struct CalendarHeader: View {
}

private var weekdays: [String] {
var weekdaySymbols = calendar.veryShortStandaloneWeekdaySymbols
// We treat Sunday as the last day of the week, so we rotate the symbols array to match.
weekdaySymbols.append(weekdaySymbols.remove(at: weekdaySymbols.startIndex))
return weekdaySymbols
let weekdaySymbols = calendar.veryShortStandaloneWeekdaySymbols

// Rotate the array to start from the locale's first weekday
let startIndex = calendar.firstWeekday - 1 // `firstWeekday` is 1-based
return Array(weekdaySymbols[startIndex...] + weekdaySymbols[..<startIndex])
}
}

Expand Down
13 changes: 6 additions & 7 deletions Backpack-SwiftUI/Calendar/Classes/Core/CalendarMonthGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ struct CalendarMonthGrid<
private let daysInAWeek = 7

var body: some View {
// Sunday is the first day of the week in the Calendar, so we need to offset (rotate the array) the days to
// make Monday the first day
let weekdaysOffset = calendar.component(.weekday, from: monthDate) - 2
// If the offset is -1 (Sunday), we want it to be 6 (the last day of the week),
// otherwise we keep the offset as it is.
// This is because we need to fill the first row with empty cells if the month doesn't start on a Monday.
let daysFromPreviousMonth = weekdaysOffset == -1 ? 6 : weekdaysOffset
let firstWeekday = calendar.firstWeekday // Locale-aware first day of the week
let weekdayOfMonthStart = calendar.component(.weekday, from: monthDate)
// Calculate the offset based on the first weekday
let weekdaysOffset = (weekdayOfMonthStart - firstWeekday + daysInAWeek) % daysInAWeek
let daysFromPreviousMonth = weekdaysOffset

LazyVGrid(
columns: Array(repeating: GridItem(spacing: BPKSpacing.none.value), count: daysInAWeek),
spacing: BPKSpacing.lg.value
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading