Skip to content

Commit

Permalink
refactor: Simplified showed scheduled option logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lebojo committed Dec 2, 2024
1 parent 733319b commit 4af1490
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,14 @@ struct ScheduleFloatingPanelView: View {
let lastScheduleInterval: Double
let setScheduleAction: (Date) -> Void

private var isWeekend: Bool {
[1, 7].contains(Calendar.current.component(.weekday, from: Date.now))
}

private var scheduleOptions: [ScheduleSendOption] {
guard !isWeekend else { return [.nextMondayMorning, .nextMondayAfternoon] }

switch Calendar.current.component(.hour, from: Date.now) {
case 0 ... 7:
return [.laterThisMorning, .tomorrowMorning, .nextMonday]
case 8 ... 13:
return [.thisAfternoon, .tomorrowMorning, .nextMonday]
case 14 ... 19:
return [.thisEvening, .tomorrowMorning, .nextMonday]
case 20 ... 23:
return [.tomorrowMorning, .nextMondayMorning]
default:
return []
}
var allCases = ScheduleSendOption.allCases
allCases.insert(ScheduleSendOption.lastSchedule(value: Date(timeIntervalSince1970: lastScheduleInterval)), at: 0)
return allCases.filter { $0.shouldBeDisplayedNow }
}

var body: some View {
VStack(spacing: IKPadding.small) {
if lastScheduleInterval > Date.minimumScheduleDelay.timeIntervalSince1970 {
ScheduleOptionButtonRow(
option: .lastSchedule(value: Date(timeIntervalSince1970: lastScheduleInterval)),
setScheduleAction: setScheduleAction
)
}
ForEach(scheduleOptions) { option in
ScheduleOptionButtonRow(option: option, setScheduleAction: setScheduleAction)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@ enum ScheduleSendOption: Identifiable, Equatable {
}
}

var shouldBeDisplayedNow: Bool {
let weekday = Calendar.current.component(.weekday, from: Date.now)
let hour = Calendar.current.component(.hour, from: .now)
let minute = Calendar.current.component(.minute, from: .now)

switch self {
case .laterThisMorning:
return hour < 7 || (hour == 7 && minute < 55)
case .thisAfternoon:
return hour < 13 || (hour == 13 && minute < 55)
case .thisEvening:
return hour < 17 || (hour == 17 && minute < 55)
case .tomorrowMorning, .nextMonday:
return true
case .nextMondayMorning, .nextMondayAfternoon:
return weekday == 1 || weekday == 7
case .lastSchedule(let value):
return value > .minimumScheduleDelay
}
}

static var allCases: [ScheduleSendOption] = [
.laterThisMorning,
.thisAfternoon,
.thisEvening,
.tomorrowMorning,
.nextMonday,
.nextMondayMorning,
.nextMondayAfternoon
]

private func dateFromNow(setHour: Int, tomorrow: Bool = false) -> Date? {
let startOfDay = Calendar.current.startOfDay(for: .now)
guard let dateWithDay = Calendar.current.date(byAdding: .day, value: tomorrow ? 1 : 0, to: startOfDay) else { return nil }
Expand Down

0 comments on commit 4af1490

Please sign in to comment.