Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.27 KB | None | 0 0
  1.    /**
  2.      Assembles and returns a feed for the provided band.
  3.      This is not a pure getter - it splices chats and shows into the feed and sorts when called.
  4.      This is why the result is stored in the feed controller and only recalled again after an update
  5.      to either chat, feed, or shows.
  6.      */
  7.     func getFeedForBand(band:Band) -> [FeedItem]
  8.     {
  9.         var feedItems = feedItemsByBand[band.id] ?? [FeedItem]()
  10.        
  11.         //We already have the FB/Twitter items in this array, but we need to add chats.
  12.         //Doing this at this step because we retrieve chats FAR more often than FB/Twitter items
  13.         //Future efficiency todo would be to always maintain this array and tack new chats to the end of it when they arrive.
  14.         addChatsToFeed(feed: &feedItems, forBand: band)
  15.        
  16.         feedItems.sort(by: { x, y in x.created > y.created })
  17.        
  18.         addShowsToFeed(feed: &feedItems, forBand: band)
  19.        
  20.         return feedItems
  21.     }
  22.    
  23.     /**
  24.      Splices chats from band X into the feed provided.
  25.      No ordering - we'll sort the feed only after all items have been added.
  26.     */
  27.     private func addChatsToFeed(feed feedItems:inout [FeedItem], forBand band:Band)
  28.     {
  29.         for c in ChatManager.instance.currentBandChats
  30.         {
  31.             if c.bandId == band.id
  32.             {
  33.                 feedItems.append(FeedItem(chat: c))
  34.             }
  35.         }
  36.     }
  37.    
  38.     /**
  39.     Add shows to feed at an interval.
  40.     These are little reminder stubs.
  41.     The interval is configured via server config (it's a get property to Config.getValue())
  42.     */
  43.     private func addShowsToFeed(feed feedItems:inout [FeedItem], forBand band:Band)
  44.     {
  45.         let shows = ShowManager.instance.upcomingVisibleShows
  46.         var feedItemIndex = 0
  47.        
  48.         for show in shows
  49.         {
  50.             if feedItemIndex >= feedItems.count
  51.             {
  52.                 feedItems.append(FeedItem(show:show))
  53.                 break
  54.             }
  55.                 // Last line will return early but using explicit 'else' here for readability
  56.             else
  57.             {
  58.                 feedItems.insert(FeedItem(show: show), at: feedItemIndex)
  59.                 feedItemIndex += showFeedInterval
  60.             }
  61.         }
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement