From ff34617505b5b9a3101a69aef95165e66d580040 Mon Sep 17 00:00:00 2001 From: JayT Date: Sat, 22 Oct 2016 21:02:22 -0700 Subject: [PATCH] Added synchronous version to visibleDates function --- Sources/UserInteractionFunctions.swift | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Sources/UserInteractionFunctions.swift b/Sources/UserInteractionFunctions.swift index 61438479..4c614652 100644 --- a/Sources/UserInteractionFunctions.swift +++ b/Sources/UserInteractionFunctions.swift @@ -59,17 +59,15 @@ extension JTAppleCalendarView { /// Returns the visible dates of the calendar. /// - returns: /// - DateSegmentInfo - public func visibleDates(_ completionHandler: @escaping (_ dateSegmentInfo: DateSegmentInfo) ->()) { + public func visibleDates()-> DateSegmentInfo { + let emptySegment = DateSegmentInfo(indates: [], monthDates: [], outdates: []) + if !calendarIsAlreadyLoaded { - delayedExecutionClosure.append { - self.visibleDates(completionHandler) - } - return + return emptySegment } - let rect = CGRect(x: calendarView.contentOffset.x + 1, y: calendarView.contentOffset.y + 1, width: calendarView.frame.width - 2, height: calendarView.frame.height - 2) guard let attributes = calendarViewLayout.layoutAttributesForElements(in: rect), attributes.count > 0 else { - return + return emptySegment } let indexPaths: [IndexPath] = Array(Set(attributes.map { $0.indexPath })).sorted() @@ -77,7 +75,7 @@ extension JTAppleCalendarView { var inDates = [Date]() var monthDates = [Date]() var outDates = [Date]() - + for indexPath in indexPaths { let info = dateInfoFromPath(indexPath) @@ -94,6 +92,17 @@ extension JTAppleCalendarView { } let retval = DateSegmentInfo(indates: inDates, monthDates: monthDates, outdates: outDates) + return retval + } + + public func visibleDates(_ completionHandler: @escaping (_ dateSegmentInfo: DateSegmentInfo) ->()) { + if !calendarIsAlreadyLoaded { + delayedExecutionClosure.append { + self.visibleDates(completionHandler) + } + return + } + let retval = visibleDates() completionHandler(retval) }