{ "extension": ".swift", "source": "import Foundation\n\n/// A notification about activities in a rant or a comment.\npublic struct Notification: Hashable, Identifiable, Sendable {\n public enum Kind: String, Sendable {\n /// An upvote for a rant.\n case rantUpvote = "content_vote"\n \n /// An upvote for a comment.\n case commentUpvote = "comment_vote"\n \n /// A new comment in one of the logged in user's rants.\n case newCommentInOwnRant = "comment_content"\n \n /// A new comment in a rant that the logged in user has commented in.\n case newComment = "comment_discuss"\n \n /// A mention of the logged in user in a comment.\n case mentionInComment = "comment_mention"\n \n /// A new rant posted by someone that the logged in user is subscribed to.\n case newRantOfSubscribedUser = "rant_sub"\n }\n \n /// The id of the rant associated with this notification.\n public let rantId: Int\n \n /// The id of the comment associated with this notification, if this notification is for a comment.\n public let commentId: Int?\n \n /// The time when this notification was created.\n public let created: Date\n \n /// True if the user has already read this notification.\n public let read: Bool\n \n /// The type of this notification.\n public let kind: Kind\n \n /// The id of the user who triggered the notification.\n public let userId: Int\n \n public var id: String {\n [\n String(rantId),\n commentId.flatMap{ String($0) } ?? "-",\n String(Int(created.timeIntervalSince1970)),\n String(read),\n kind.rawValue,\n String(userId)\n ].joined(separator: "|")\n }\n \n public init(rantId: Int, commentId: Int?, created: Date, read: Bool, kind: Notification.Kind, userId: Int) {\n self.rantId = rantId\n self.commentId = commentId\n self.created = created\n self.read = read\n self.kind = kind\n self.userId = userId\n }\n}\n\nextension Notification {\n struct CodingData: Codable {\n let rant_id: Int\n let comment_id: Int?\n let created_time: Int\n let read: Int\n let type: String\n let uid: Int\n }\n}\n\nextension Notification.CodingData {\n var decoded: Notification {\n .init(\n rantId: rant_id,\n commentId: comment_id,\n created: Date(timeIntervalSince1970: TimeInterval(created_time)),\n read: read != 0,\n kind: .init(rawValue: type) ?? .newComment,\n userId: uid\n )\n }\n}\n", "review": "8\n\n### Bugs\n- No significant bugs identified in this code snippet.\n\n### Optimizations\n- The id generation logic could potentially be simplified using string interpolation.\n- Consider using enum cases directly for type safety and clarity in decoding logic.\n- Mapping the raw type to Kind could be more exhaustive or safeguarded with a fallback or error handling if unknown values arise.\n\n### Good points\n- The structuring using nested enumerations and extensions leads to clear organization.\n- Conformance to Swift protocols like Hashable, Identifiable, Sendable, and Codable enhances usability and makes it versatile in different contexts.\n- The use of flatMap facilitates clean handling of optional values.\n- Utilization of descriptive comments for each aspect of the notification is helpful for understanding.\n\n### Summary\nThis code defines a Notification struct with complete interoperability, providing encoding and decoding functionality seamlessly. The struct is well-organized with clear enums and comments to provide context and functionality. It efficiently uses Swift's optional handling and protocol conformance to offer a robust, clear structure for representing user notifications.\n\n### Open source alternatives\n- SwiftNotificationBanner: A lightweight library to display notification bars in a customizable and quick manner.\n- Noty: A simple in-app notification framework inspired by iOS system notifications.\n- MessageKit: A community-driven replacement for JSQMessagesViewController that builds upon Apple's new UI elements for dealing with notifications.", "filename": "Notification.swift", "path": "Sources/SwiftDevRant/Models/Notification.swift", "directory": "Models", "grade": 8, "size": 2614, "line_count": 87 }