HXXXXJ

Lay out meetings in a calendar view

May 24th, 2022 (edited)
332
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Event {
  2.     let start : Date
  3.     let end : Date
  4. }
  5.  
  6. struct TimeRect {
  7.     let top : Double
  8.     let left : Double
  9.     let bottom : Double
  10.     let right : Double
  11. }
  12.  
  13. func getCalendar(_ array : [Event], _ maxWidth: Double) -> [TimeRect]{
  14.     guard array.count > 0 else { return []}
  15.     var res = [TimeRect]()
  16.     // 1. sort the [Event] by start time
  17.     sortEvent(array)
  18.     i = 0
  19.     // 2. Find overlapping events group.
  20.    // Within the group, layout the events and add to res  
  21.     while i < array.count {
  22.        var overlappingEvents = [Event]()
  23.        let event = array[i]
  24.        overlappingEvents.append(event)
  25.        let maxCurrentEndTime = event.end
  26.        i += 1
  27.        while i < array.count {
  28.          nextEvent = array[i]
  29.          if (nextEvent.start < maxCurrentEndTime){
  30.             i += 1
  31.             overlappingEvents.append(event)
  32.             maxCurrentEndTime = max(maxCurrentEndTime,nextEvent.end )
  33.           } else {
  34.             break
  35.           }
  36.         }
  37.       // Handle overlapping event. it has at least 1 event
  38.       HandleOverlap(overlappingEvents, maxWidth, res)
  39.    }
  40.    return res
  41. }
  42.  
  43.  
  44. func HandleOverlap(_ overlappingEvents : [Event], _ maxWidth: Double, _ res: inout [TimeRect]) {
  45.     guard overlappingEvents.count > 0 else { return }
  46.     let left = 0
  47.     let width = maxWidth / overlappingEvents.count
  48.     while i < overlappingEvents.count {
  49.        let event = overlappingEvents[i]
  50.        let h = event.end - event.start
  51.        y = event.start
  52.        res.append(TimeRect(left:left, top: event.start, bottom: event.end, right: left + width))
  53.        left += width
  54.        i += 1
  55.     }
  56. }
RAW Paste Data Copied