Skip to content

Commit fab9dbe

Browse files
committed
feat: adopt UserModel to Encodable, Equatable and RawRepresentable
1 parent ec6e62b commit fab9dbe

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// UserModel+Encodable.swift
3+
// VRCKit
4+
//
5+
// Created by makinosp on 2024/10/27.
6+
//
7+
8+
import Foundation
9+
10+
extension User: Encodable {
11+
public func encode(to encoder: any Encoder) throws {
12+
var container = encoder.container(keyedBy: UserCodingKeys.self)
13+
try container.encode(activeFriends, forKey: .activeFriends)
14+
try container.encode(allowAvatarCopying, forKey: .allowAvatarCopying)
15+
try container.encodeIfPresent(bio, forKey: .bio)
16+
try container.encode(bioLinks.wrappedValue, forKey: .bioLinks)
17+
try container.encode(currentAvatar, forKey: .currentAvatar)
18+
try container.encodeIfPresent(avatarImageUrl, forKey: .currentAvatarImageUrl)
19+
try container.encodeIfPresent(avatarThumbnailUrl, forKey: .currentAvatarThumbnailImageUrl)
20+
if let dateJoined = dateJoined {
21+
let dateJoinedString = DateFormatter.dateStringFormat.string(from: dateJoined)
22+
try container.encode(dateJoinedString, forKey: .dateJoined)
23+
}
24+
try container.encode(displayName, forKey: .displayName)
25+
try container.encode(friendKey, forKey: .friendKey)
26+
try container.encode(friends, forKey: .friends)
27+
try container.encode(homeLocation, forKey: .homeLocation)
28+
try container.encode(id, forKey: .id)
29+
try container.encode(isFriend, forKey: .isFriend)
30+
try container.encode(lastActivity, forKey: .lastActivity)
31+
try container.encode(lastLogin, forKey: .lastLogin)
32+
try container.encode(lastPlatform, forKey: .lastPlatform)
33+
try container.encode(offlineFriends, forKey: .offlineFriends)
34+
try container.encode(onlineFriends, forKey: .onlineFriends)
35+
try container.encode(pastDisplayNames, forKey: .pastDisplayNames)
36+
try container.encodeIfPresent(profilePicOverride, forKey: .profilePicOverride)
37+
try container.encode(state, forKey: .state)
38+
try container.encode(status, forKey: .status)
39+
try container.encode(statusDescription, forKey: .statusDescription)
40+
try container.encode(tags, forKey: .tags)
41+
try container.encode(twoFactorAuthEnabled, forKey: .twoFactorAuthEnabled)
42+
try container.encodeIfPresent(userIcon, forKey: .userIcon)
43+
try container.encodeIfPresent(userLanguage, forKey: .userLanguage)
44+
try container.encodeIfPresent(userLanguageCode, forKey: .userLanguageCode)
45+
try container.encode(presence, forKey: .presence)
46+
}
47+
}

Sources/VRCKit/Models/User/UserModel.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ public struct User: Sendable, ProfileDetailRepresentable {
5151
}
5252
}
5353

54+
extension User: Equatable {
55+
public static func == (lhs: Self, rhs: Self) -> Bool {
56+
lhs.hashValue == rhs.hashValue
57+
}
58+
}
59+
60+
extension User: RawRepresentable {
61+
public init?(rawValue: String) {
62+
guard let data = rawValue.data(using: .utf8) else { return nil }
63+
guard let decoded: User = try? Serializer.shared.decode(data) else { return nil }
64+
self = decoded
65+
}
66+
67+
public var rawValue: String {
68+
guard let data = try? Serializer.shared.encode(self) else { return "" }
69+
guard let encoded = String(data: data, encoding: .utf8) else { return "" }
70+
return encoded
71+
}
72+
}
73+
5474
public extension User {
5575
var platform: UserPlatform { presence.platform }
5676
}

0 commit comments

Comments
 (0)