Skip to content

Commit 1434d89

Browse files
Merge pull request #13 from SimformSolutionsPvtLtd/bug-fixes
Fix year reversion bug on month change
2 parents c475e9d + 76639c6 commit 1434d89

5 files changed

Lines changed: 45 additions & 11 deletions

File tree

Sources/SSDateTimePicker/SSDateTimePicker/Common/Date picker/SSDatePickerManager.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ final class SSDatePickerManager: ObservableObject, DatePickerConfigurationDirect
7474
currentMonth = currentMonth.getNextMonth(calendar) ?? currentMonth
7575
case .month:
7676
currentMonth = currentMonth.getNextYear(calendar) ?? currentMonth
77+
updateYearSelection(date: currentMonth)
7778
case .year:
7879
guard let year = self.yearRange.last else { return }
7980
self.updateYearRange(year: year+12)
@@ -87,12 +88,18 @@ final class SSDatePickerManager: ObservableObject, DatePickerConfigurationDirect
8788
currentMonth = currentMonth.getPreviousMonth(calendar) ?? currentMonth
8889
case .month:
8990
currentMonth = currentMonth.getPreviousYear(calendar) ?? currentMonth
91+
updateYearSelection(date: currentMonth)
9092
case .year:
9193
guard let year = self.yearRange.first else { return }
9294
self.updateYearRange(year: year-1)
9395
}
9496
}
95-
97+
98+
/// Updates the year selection based on the chosen year.
99+
func updateYearSelection(date: Date) {
100+
updateYearSelection(year: date.year(calendar))
101+
}
102+
96103
/// Checks if a given month is currently selected in the date picker.
97104
func isSelected(_ month: String) -> Bool {
98105
month.lowercased() == (selectedDate ?? currentMonth).fullMonth.lowercased()

Sources/SSDateTimePicker/SSDateTimePicker/Common/Time picker/SSTimePickerManager.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,20 @@ final class SSTimePickerManager: ObservableObject {
6060

6161
/// Updates the angle based on the current selected hour.
6262
func updateCurrentHourAngle() {
63-
angle = Double((Int(hourSelected) ?? 12) * 30)
63+
let hour = updateWithDefaultHourIfEmpty()
64+
angle = Double(hour * 30)
6465
}
6566

6667
/// Updates the angle based on the current selected minute.
6768
func updateCurrentMinuteAngle() {
68-
angle = Double((Int(minutesSelected) ?? 00) * 6)
69+
let minute = updateWithDefaultMinuteIfEmpty()
70+
angle = Double(minute * 6)
6971
}
7072

7173
/// Updates the selected time based on the current hour, minute, and time format.
7274
func updateSelectedTime() {
75+
_ = updateWithDefaultMinuteIfEmpty()
76+
_ = updateWithDefaultHourIfEmpty()
7377
let format = DateFormatter.configure(with: DateFormat.twentyFourHourFormat)
7478
// get hours for 24-hrs format
7579
let hour = (Int(hourSelected) ?? 12)
@@ -79,7 +83,25 @@ final class SSTimePickerManager: ObservableObject {
7983
handleCallback()
8084
isMinuteClock = false
8185
}
82-
86+
87+
// In case if textfield is empty set default minute
88+
func updateWithDefaultMinuteIfEmpty() -> Int {
89+
let minute = Int(minutesSelected) ?? 00
90+
if minutesSelected == "" {
91+
minutesSelected = "00"
92+
}
93+
return minute
94+
}
95+
96+
// In case if textfield is empty set default hour
97+
func updateWithDefaultHourIfEmpty() -> Int {
98+
let hour = Int(hourSelected) ?? 12
99+
if hourSelected == "" {
100+
hourSelected = "12"
101+
}
102+
return hour
103+
}
104+
83105
/// Sets up the initial time, angle, and clock view based on the selected time.
84106
func setUpTimeAndAngle() {
85107
let calender = Calendar.current

Sources/SSDateTimePicker/SSDateTimePicker/Views/Time Picker/SSClockPicker.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ struct SSClockPicker: View, TimePickerConfigurationDirectAccess {
102102
private func actionClockNumberSelection(number: Int) {
103103
if timePickerManager.isMinuteClock {
104104
let minute = number * 5
105-
timePickerManager.minutesSelected = (minute == 60 ? 0 : minute).formattedTime
105+
timePickerManager.minutesSelected = (minute == 60 ? 00 : minute).formattedTime
106106
timePickerManager.updateCurrentMinuteAngle()
107107
} else {
108108
timePickerManager.hourSelected = number.formattedTime
@@ -130,6 +130,7 @@ extension SSClockPicker {
130130
// updating angle for hour if the angle is in between hours
131131
let roundValue = Int(SSPickerConstants.clockNumberRotationDegree) * Int(round(angle/SSPickerConstants.clockNumberRotationDegree))
132132
timePickerManager.angle = Double(roundValue)
133+
updateHour()
133134
} else {
134135
// updating minutes
135136
let progress = angle / threeSixtyDegree
@@ -139,17 +140,21 @@ extension SSClockPicker {
139140

140141
private func onEnd(value: DragGesture.Value) {
141142
if !timePickerManager.isMinuteClock {
142-
// updating hour value
143-
let hour = Int(timePickerManager.angle / SSPickerConstants.clockNumberRotationDegree)
144-
timePickerManager.hourSelected = (hour == 0 ? 12 : hour).formattedTime
143+
updateHour()
145144
// updating picker to minutes
146145
withAnimation {
147-
timePickerManager.angle = Double((Int(timePickerManager.minutesSelected) ?? 1) * Int(SSPickerConstants.minuteRotationDegree))
146+
timePickerManager.angle = Double((Int(timePickerManager.minutesSelected) ?? 00) * Int(SSPickerConstants.minuteRotationDegree))
148147
timePickerManager.isMinuteClock = true
149148
}
150149
}
151150
}
152-
151+
152+
// updating hour value
153+
private func updateHour() {
154+
let hour = Int(timePickerManager.angle / SSPickerConstants.clockNumberRotationDegree)
155+
timePickerManager.hourSelected = (hour == 0 ? 12 : hour).formattedTime
156+
}
157+
153158
}
154159

155160

Sources/SSDateTimePicker/SSDateTimePicker/Views/Time Picker/SSTimeTextField.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct SSTimeTextField: View, TimePickerConfigurationDirectAccess {
6161
.fixedSize()
6262
.cornerRadius(SSPickerConstants.timeFieldCornerRadius)
6363
.onChange(of: time) { [time] newTime in
64-
if Int(newTime) ?? 0 > (isHourField ? 12 : 59) {
64+
if Int(newTime) ?? 00 > (isHourField ? 12 : 59) {
6565
self.time = time
6666
}
6767
if self.time.count > charLimit {

0 commit comments

Comments
 (0)