Models WIP

This commit is contained in:
Wilhelm Oks 2024-12-11 17:00:16 +01:00
parent aad877c64c
commit 19c645e59c
3 changed files with 59 additions and 1 deletions

View File

@ -45,3 +45,47 @@ public struct Comment: Identifiable, Hashable {
self.image = image
}
}
extension Comment {
struct CodingData: Codable {
let id: Int
let rant_id: Int
let body: String
let score: Int
let created_time: Int
let vote_state: Int
let links: [Link.CodingData]?
let user_id: Int
let user_username: String
let user_score: Int
let user_avatar: User.Avatar.CodingData
//let user_avatar_lg: User.Avatar.CodingData //TODO: check if this exists in the JSON data
let user_dpp: Int?
let attached_image: AttachedImage.CodingData?
let edited: Bool?
}
}
extension Comment.CodingData {
var decoded: Comment {
.init(
id: id,
rantId: rant_id,
voteState: .init(rawValue: vote_state) ?? .unvoted,
score: score,
author: .init(
id: user_id,
name: user_username,
score: user_score,
devRantSupporter: (user_dpp ?? 0) != 0,
avatar: user_avatar.decoded,
avatarLarge: nil
),
created: Date(timeIntervalSince1970: TimeInterval(created_time)),
isEdited: edited ?? false,
text: body,
linksInText: links?.map(\.decoded) ?? [],
image: attached_image?.decoded
)
}
}

View File

@ -7,6 +7,11 @@ public extension User {
public var imageUrl: String? {
imageUrlPath.flatMap { "https://avatars.devrant.com/\($0)" }
}
public init(colorHex: String, imageUrlPath: String?) {
self.colorHex = colorHex
self.imageUrlPath = imageUrlPath
}
}
}

View File

@ -8,5 +8,14 @@ public struct User: Identifiable, Hashable {
public let avatar: Avatar
/// A large avatar for the profile view.
public let avatarLarge: Avatar
public let avatarLarge: Avatar?
public init(id: Int, name: String, score: Int, devRantSupporter: Bool, avatar: User.Avatar, avatarLarge: User.Avatar?) {
self.id = id
self.name = name
self.score = score
self.devRantSupporter = devRantSupporter
self.avatar = avatar
self.avatarLarge = avatarLarge
}
}