Skip to content

Commit

Permalink
Update week mode to start from start of week
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullah-original committed Jul 15, 2024
1 parent cdc6f02 commit 258da86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
33 changes: 20 additions & 13 deletions Sources/ExtendedDatePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,36 +129,43 @@ private extension ExtendedDatePicker {

// MARK: - Preview

@available(iOS 17, macOS 14, macCatalyst 17, *)
fileprivate struct ExtendedDatePickerPreview: View {
@State var selectedDate = Date()

let calendar: Calendar = {
var tempCal = Calendar.current
tempCal.timeZone = .init(abbreviation: "CET")!
tempCal.locale = .init(identifier: "ar-UAE")
tempCal.locale = .init(identifier: "en-GB")
return tempCal
}()

let range = Date()...(Date().addingTimeInterval(60*60*24*79))
let range = Date()...(Calendar.current.date(
byAdding: .day,
value: 22,
to: .now
) ?? .now)


var body: some View {
if #available(iOS 17, macOS 14, macCatalyst 17, *) {
ExtendedDatePicker(
selectedDate: $selectedDate,
dateRange: range,
mode: .monthYear,
calendar: self.calendar
)
.onChange(of: selectedDate, initial: true) {
print(selectedDate)
}
ExtendedDatePicker(
selectedDate: $selectedDate,
dateRange: range,
mode: .week,
calendar: self.calendar
)
.onChange(of: selectedDate, initial: true) {
print(selectedDate)
}
}
}


#Preview {
ExtendedDatePickerPreview()
if #available(iOS 17, *) {
ExtendedDatePickerPreview()
} else {
EmptyView()
}
}
#endif
13 changes: 11 additions & 2 deletions Sources/ExtendedDatePickerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,18 @@ final class ExtendedDatePickerModel: ObservableObject {
func weekOptions() -> [Date: String] {
var result = [Date: String]()

var startOfWeek = dateRange.lowerBound
let dateComponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: dateRange.lowerBound)

guard var startOfWeek = calendar.date(
from: .init(
weekOfYear: dateComponents.weekOfYear,
yearForWeekOfYear: dateComponents.yearForWeekOfYear
)
) else {
return [:]
}

while calendar.compare(startOfWeek, to: dateRange.upperBound, toGranularity: .day) == .orderedAscending {
while calendar.compare(startOfWeek, to: dateRange.upperBound, toGranularity: .day) != .orderedDescending {
result[startOfWeek] = dateFormatter.format(from: startOfWeek, mode: .week)
startOfWeek = getNext(component: .weekOfYear, for: startOfWeek, forward: true)
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/ExtendedDatePickerModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class ExtendedDatePickerModelTests: XCTestCase {
override func setUp() {
super.setUp()
calendar = .current
calendar.locale = .init(identifier: "en-US")
calendar.locale = .init(identifier: "en-GB")

selectedDate = Date()
}
Expand Down Expand Up @@ -126,11 +126,11 @@ final class ExtendedDatePickerModelTests: XCTestCase {
XCTAssertEqual(result.sorted(by: <).last?.value, "May 2024")
}

func testWeekOptions_whenDateRangeIs14March2024To5May2024_shouldReturn8Weeks() {
func testWeekOptions_whenDateRangeIs14March2024To2May2024_shouldReturn8Weeks() {
let input = ClosedRange(
uncheckedBounds: (
lower: DateComponents(calendar: calendar, year: 2024, month: 3, day: 14).date!,
upper: DateComponents(calendar: calendar, year: 2024, month: 5, day: 5).date!
upper: DateComponents(calendar: calendar, year: 2024, month: 5, day: 2).date!
)
)

Expand All @@ -146,11 +146,11 @@ final class ExtendedDatePickerModelTests: XCTestCase {
XCTAssertEqual(result.count, 8)
}

func testWeekOptions_whenDateRangeIs14March2024To2May2024_shouldReturn7Weeks() {
func testWeekOptions_whenDateRangeIs11March2024To5May2024_shouldReturn8Weeks() {
let input = ClosedRange(
uncheckedBounds: (
lower: DateComponents(calendar: calendar, year: 2024, month: 3, day: 14).date!,
upper: DateComponents(calendar: calendar, year: 2024, month: 5, day: 2).date!
lower: DateComponents(calendar: calendar, year: 2024, month: 3, day: 17).date!,
upper: DateComponents(calendar: calendar, year: 2024, month: 5, day: 5).date!
)
)

Expand All @@ -163,14 +163,14 @@ final class ExtendedDatePickerModelTests: XCTestCase {

let result = sut.weekOptions()

XCTAssertEqual(result.count, 7)
XCTAssertEqual(result.count, 8)
}

func testWeekOptions_whenDateRangeIs14March2024To1May2024_shouldReturn7Weeks() {
let input = ClosedRange(
uncheckedBounds: (
lower: DateComponents(calendar: calendar, year: 2024, month: 3, day: 14).date!,
upper: DateComponents(calendar: calendar, year: 2024, month: 5, day: 1).date!
upper: DateComponents(calendar: calendar, year: 2024, month: 4, day: 29).date!
)
)

Expand All @@ -183,7 +183,7 @@ final class ExtendedDatePickerModelTests: XCTestCase {

let result = sut.weekOptions()

XCTAssertEqual(result.count, 7)
XCTAssertEqual(result.count, 8)
}

func testHourOptions_whenDateRangeIs2February20241700To1February20242130_shouldReturn5Hours() {
Expand Down

0 comments on commit 258da86

Please sign in to comment.