SHARE
TWEET

Untitled

a guest Aug 25th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func getTimeSlots(bookings: [BookingDomainDataModel]) -> [SlotModel] {
  2.         let generatedSlots: [SlotModel] = generateSlots()
  3.         let regeneratedBookings: [BookingDomainDataModel] = reGenerateBookingSlots(bookings: bookings)
  4.         var availableSlots: [SlotModel] = []
  5.        
  6.         generatedSlots.forEach { (slot) in
  7.             let startSlotDate = slot.startDate
  8.             let endSlotDate = slot.endDate
  9.            
  10.             if let slotHour = Calendar.current.dateComponents([.hour], from: startSlotDate).hour {
  11.                 if regeneratedBookings.filter({ slotHour == Calendar.current.dateComponents([.hour], from: $0.bookingStartDate).hour }).isEmpty != true {
  12.                     let filteredBooking = regeneratedBookings.filter({ slotHour == Calendar.current.dateComponents([.hour], from: $0.bookingStartDate).hour })[0]
  13.                     availableSlots.append(SlotModel(startDate: startSlotDate.toTheNearestHour(), endDate: endSlotDate.toTheNearestHour(), isBusy: true, bookingData: filteredBooking))
  14.                 } else {
  15.                     availableSlots.append(SlotModel(startDate: startSlotDate.toTheNearestHour(), endDate: endSlotDate.toTheNearestHour(), isBusy: false, bookingData: nil))
  16.                 }
  17.             }
  18.         }
  19.        
  20.         return availableSlots
  21.     }
  22.    
  23.     //MARK: - Private Methods
  24.     private func reGenerateBookingSlots(bookings: [BookingDomainDataModel]) -> [BookingDomainDataModel] {
  25.         var availableBookings: [BookingDomainDataModel] = []
  26.        
  27.         bookings.forEach { (bookingData) in
  28.             if bookingData.bookindDuration > Constants().slotDuration {
  29.                 let intervalComponents = Calendar.current.dateComponents([.hour], from: bookingData.bookingStartDate, to: bookingData.bookingEndDate)
  30.                 if let hoursInterval = intervalComponents.hour {
  31.                    
  32.                     for i in 0...hoursInterval {
  33.                         let startDate = bookingData.bookingStartDate.addingTimeInterval(TimeInterval(i*Constants().slotDuration))
  34.                         let endDate = bookingData.bookingEndDate.addingTimeInterval(TimeInterval(i*Constants().slotDuration))
  35.                        
  36.                         if let bookingEndMinute = Calendar.current.dateComponents([.minute], from: endDate).minute {
  37.                             if i == hoursInterval && bookingEndMinute == 0 {
  38.                                 break
  39.                             }
  40.                         }
  41.                        
  42.                         availableBookings.append(BookingDomainDataModel(bookingId: bookingData.bookingId, userID: bookingData.userID, userName: bookingData.userName, bookingStartDate: startDate, bookingEndDate: endDate, bookindDuration: bookingData.bookindDuration))
  43.                        
  44.                     }
  45.                 }
  46.             } else {
  47.                 availableBookings.append(bookingData)
  48.             }
  49.         }
  50.        
  51.         return availableBookings
  52.     }
  53.    
  54.     private func generateSlots() -> [SlotModel] {
  55.         if let startDate = Calendar.current.date(from: Constants().dayStart) {
  56.             let countOfSlots = Constants().workingDayDuration/Constants().slotDuration
  57.             var availableSlots: [SlotModel] = []
  58.            
  59.             for i in 0...countOfSlots {
  60.                 let startSlotDate = startDate.addingTimeInterval(TimeInterval(i*Constants().slotDuration))
  61.                 let endSlotDate = startSlotDate.addingTimeInterval(TimeInterval(Constants().slotDuration))
  62.                
  63.                 if let endTimeHour = Calendar.current.dateComponents([.hour], from: startSlotDate).hour {
  64.                     if endTimeHour == 20 {
  65.                         break
  66.                     }
  67.                 }
  68.                
  69.                 availableSlots.append(SlotModel(startDate: startSlotDate, endDate: endSlotDate, isBusy: false, bookingData: nil))
  70.             }
  71.             return availableSlots
  72.         }
  73.         return []
  74.     }
  75.    
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top