Advertisement
Guest User

Ordered User Keys

a guest
Feb 23rd, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.46 KB | None | 0 0
  1. import UIKit
  2.  
  3. struct CommentModel {
  4.     var postId: String?
  5.     var userUid: String?
  6.     var postText: String?
  7.     var postDate: Double?
  8.     init(dictionary: [String: Any]) {
  9.         postId = dictionary["postId"] as? String
  10.         userUid = dictionary["userUid"] as? String
  11.         postText = dictionary["postText"] as? String
  12.         postDate = dictionary["postDate"] as? Double
  13.     }
  14. }
  15.  
  16. // Returns a random date between the start and end times,
  17. // truncated to the nearest second.
  18. func randomDate(between start: Date, and end: Date) -> Date {
  19.     let delta = end.timeIntervalSince(start)
  20.     let randomDelta = arc4random_uniform(UInt32(delta))
  21.     return start.addingTimeInterval(TimeInterval(randomDelta))
  22. }
  23.  
  24. func randomDateInPast24Hours() -> Date {
  25.     // TimeInterval is measured in seconds
  26.     let twentyFourHours: TimeInterval = 24 /* hours */ * 60 /* minutes */ * 60 /* seconds */
  27.     let now = Date()
  28.     let twentyFourHoursAgo = now.addingTimeInterval(-1 * twentyFourHours)
  29.     return randomDate(between: twentyFourHoursAgo, and: now)
  30. }
  31.  
  32. func generateMockComment(userUid: String?) -> CommentModel {
  33.     let dictionary: [String: Any] = [
  34.         "postId": UUID().uuidString,
  35.         "userUid": userUid ?? UUID().uuidString,
  36.         "postText": "Hello World!",
  37.         "postDate": randomDateInPast24Hours().timeIntervalSince1970
  38.     ]
  39.     return CommentModel(dictionary: dictionary)
  40. }
  41.  
  42.  
  43. let aliceUid = "9F463FC9-8729-47D0-8AAA-1B43F6D4121F"
  44. let bobUid = "A92F5217-FA19-49B4-A79F-72EF07CB96D3"
  45. let charlieUid = "932CAA5A-DFFF-4BD6-9AA2-79A439069049"
  46.  
  47. /**
  48.  # Step 1: Collect the comments
  49.  */
  50.  
  51. let allComments = [
  52.     generateMockComment(userUid: aliceUid),
  53.     generateMockComment(userUid: bobUid),
  54.     generateMockComment(userUid: charlieUid),
  55.     generateMockComment(userUid: aliceUid),
  56.     generateMockComment(userUid: bobUid),
  57.     generateMockComment(userUid: charlieUid),
  58.     generateMockComment(userUid: aliceUid),
  59.     generateMockComment(userUid: bobUid),
  60.     generateMockComment(userUid: charlieUid),
  61. ]
  62.  
  63. /**
  64.  # Step 2: Sort the comments
  65.  */
  66.  
  67. let sortedComments = allComments.sorted { (lhs, rhs) -> Bool in
  68.     return lhs.postDate ?? 0 > rhs.postDate ?? 0
  69. }
  70.  
  71. /**
  72.  # Step 3: Get the unique users, ordered by post date
  73.  */
  74.  
  75. var orderedUsers = [String]()
  76.  
  77. for comment in sortedComments {
  78.     guard let userUid = comment.userUid else { continue }
  79.    
  80.     // If the ordered users array *does not* contain the UID
  81.     // add it to the array
  82.     if !orderedUsers.contains(userUid) {
  83.         orderedUsers.append(userUid)
  84.     }
  85. }
  86.  
  87. print("orderedUsers", orderedUsers)
  88.  
  89. /**
  90.  # Step 4: Map the comments using the index of this user
  91.  */
  92.  
  93. // I created a wrapper struct for this step so I wouldn't have
  94. // to modify the core CommentModel struct.
  95. struct UserKeyedCommentModel {
  96.     var userOrder: Int
  97.     var comment: CommentModel
  98.    
  99.     init(userOrder: Int, comment: CommentModel) {
  100.         self.userOrder = userOrder
  101.         self.comment = comment
  102.     }
  103. }
  104.  
  105. // Uses compact map so posts with a nil userUid get skipped.
  106. let mappedComments: [UserKeyedCommentModel] = sortedComments.compactMap { (comment) -> UserKeyedCommentModel? in
  107.     guard let userUid = comment.userUid else { return nil }
  108.     guard let index = orderedUsers.firstIndex(of: userUid) else { return nil }
  109.     return UserKeyedCommentModel(userOrder: index, comment: comment)
  110. }
  111.  
  112. /**
  113.  # Step 5: Display these comments in the table view
  114.  */
  115.  
  116. mappedComments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement