Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanStepanok committed Jul 11, 2024
1 parent 17b4127 commit 0bd4f6d
Showing 1 changed file with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,36 @@ public class CalendarManager: CalendarManagerProtocol {
let events = generateEvents(for: dateBlocks, courseName: courseName, calendar: calendar)
var saveSuccessful = true
events.forEach { event in
do {
try eventStore.save(event, span: .thisEvent)
persistence.saveCourseCalendarEvent(
CourseCalendarEvent(courseID: courseID, eventIdentifier: event.eventIdentifier)
)
} catch {
saveSuccessful = false
if !eventExists(event, in: calendar) {
do {
try eventStore.save(event, span: .thisEvent)
persistence.saveCourseCalendarEvent(
CourseCalendarEvent(courseID: courseID, eventIdentifier: event.eventIdentifier)
)
} catch {
saveSuccessful = false
}
}
}
return saveSuccessful
}

private func eventExists(_ event: EKEvent, in calendar: EKCalendar) -> Bool {
let predicate = eventStore.predicateForEvents(
withStart: event.startDate,
end: event.endDate,
calendars: [calendar]
)
let existingEvents = eventStore.events(matching: predicate)

return existingEvents.contains { existingEvent in
existingEvent.title == event.title &&
existingEvent.startDate == event.startDate &&
existingEvent.endDate == event.endDate &&
existingEvent.notes == event.notes
}
}

public func filterCoursesBySelected(fetchedCourses: [CourseForSync]) async -> [CourseForSync] {
let courseCalendarStates = persistence.getAllCourseStates()
if !courseCalendarStates.isEmpty {
Expand Down

0 comments on commit 0bd4f6d

Please sign in to comment.