Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- struct CommentModel {
- var postId: String?
- var userUid: String?
- var postText: String?
- var postDate: Double?
- init(dictionary: [String: Any]) {
- postId = dictionary["postId"] as? String
- userUid = dictionary["userUid"] as? String
- postText = dictionary["postText"] as? String
- postDate = dictionary["postDate"] as? Double
- }
- }
- // Returns a random date between the start and end times,
- // truncated to the nearest second.
- func randomDate(between start: Date, and end: Date) -> Date {
- let delta = end.timeIntervalSince(start)
- let randomDelta = arc4random_uniform(UInt32(delta))
- return start.addingTimeInterval(TimeInterval(randomDelta))
- }
- func randomDateInPast24Hours() -> Date {
- // TimeInterval is measured in seconds
- let twentyFourHours: TimeInterval = 24 /* hours */ * 60 /* minutes */ * 60 /* seconds */
- let now = Date()
- let twentyFourHoursAgo = now.addingTimeInterval(-1 * twentyFourHours)
- return randomDate(between: twentyFourHoursAgo, and: now)
- }
- func generateMockComment(userUid: String?) -> CommentModel {
- let dictionary: [String: Any] = [
- "postId": UUID().uuidString,
- "userUid": userUid ?? UUID().uuidString,
- "postText": "Hello World!",
- "postDate": randomDateInPast24Hours().timeIntervalSince1970
- ]
- return CommentModel(dictionary: dictionary)
- }
- let aliceUid = "9F463FC9-8729-47D0-8AAA-1B43F6D4121F"
- let bobUid = "A92F5217-FA19-49B4-A79F-72EF07CB96D3"
- let charlieUid = "932CAA5A-DFFF-4BD6-9AA2-79A439069049"
- /**
- # Step 1: Collect the comments
- */
- let allComments = [
- generateMockComment(userUid: aliceUid),
- generateMockComment(userUid: bobUid),
- generateMockComment(userUid: charlieUid),
- generateMockComment(userUid: aliceUid),
- generateMockComment(userUid: bobUid),
- generateMockComment(userUid: charlieUid),
- generateMockComment(userUid: aliceUid),
- generateMockComment(userUid: bobUid),
- generateMockComment(userUid: charlieUid),
- ]
- /**
- # Step 2: Sort the comments
- */
- let sortedComments = allComments.sorted { (lhs, rhs) -> Bool in
- return lhs.postDate ?? 0 > rhs.postDate ?? 0
- }
- /**
- # Step 3: Get the unique users, ordered by post date
- */
- var orderedUsers = [String]()
- for comment in sortedComments {
- guard let userUid = comment.userUid else { continue }
- // If the ordered users array *does not* contain the UID
- // add it to the array
- if !orderedUsers.contains(userUid) {
- orderedUsers.append(userUid)
- }
- }
- print("orderedUsers", orderedUsers)
- /**
- # Step 4: Map the comments using the index of this user
- */
- // I created a wrapper struct for this step so I wouldn't have
- // to modify the core CommentModel struct.
- struct UserKeyedCommentModel {
- var userOrder: Int
- var comment: CommentModel
- init(userOrder: Int, comment: CommentModel) {
- self.userOrder = userOrder
- self.comment = comment
- }
- }
- // Uses compact map so posts with a nil userUid get skipped.
- let mappedComments: [UserKeyedCommentModel] = sortedComments.compactMap { (comment) -> UserKeyedCommentModel? in
- guard let userUid = comment.userUid else { return nil }
- guard let index = orderedUsers.firstIndex(of: userUid) else { return nil }
- return UserKeyedCommentModel(userOrder: index, comment: comment)
- }
- /**
- # Step 5: Display these comments in the table view
- */
- mappedComments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement