Skip to content

Commit

Permalink
feat: decode date in string format to Date
Browse files Browse the repository at this point in the history
  • Loading branch information
makinosp committed Aug 16, 2024
1 parent 02458a6 commit 4a4d540
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Sources/VRCKit/Extensions/DateFormatter+iso8601Full.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ public extension DateFormatter {
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()

static let dateStringFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyy-MM-dd"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
}
5 changes: 3 additions & 2 deletions Sources/VRCKit/Models/UserDetailModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct UserDetail: ProfileDetailRepresentable, LocationRepresentable {
public let userIcon: URL?
public let location: String
public let friendKey: String
public let dateJoined: String
public let dateJoined: Date?
public var note: String
public let lastActivity: Date
}
Expand All @@ -50,7 +50,8 @@ extension UserDetail: Codable {
userIcon = try? container.decodeIfPresent(URL.self, forKey: .userIcon)
location = try container.decode(String.self, forKey: .location)
friendKey = try container.decode(String.self, forKey: .friendKey)
dateJoined = try container.decode(String.self, forKey: .dateJoined)
let dateJoinedString = try container.decode(String.self, forKey: .dateJoined)
dateJoined = DateFormatter.dateStringFormat.date(from: dateJoinedString)
note = try container.decode(String.self, forKey: .note)
lastActivity = try container.decode(Date.self, forKey: .lastActivity)
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/VRCKit/Models/UserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct User: ProfileDetailRepresentable {
public let currentAvatar: String
public let avatarImageUrl: URL?
public let avatarThumbnailUrl: URL?
public let dateJoined: String
public let dateJoined: Date?
public let displayName: String
public let friendKey: String
public let friends: [String]
Expand Down Expand Up @@ -63,7 +63,8 @@ extension User: Codable {
currentAvatar = try container.decode(String.self, forKey: .currentAvatar)
avatarImageUrl = try? container.decodeIfPresent(URL.self, forKey: .avatarImageUrl)
avatarThumbnailUrl = try? container.decodeIfPresent(URL.self, forKey: .avatarThumbnailUrl)
dateJoined = try container.decode(String.self, forKey: .dateJoined)
let dateJoinedString = try container.decode(String.self, forKey: .dateJoined)
dateJoined = DateFormatter.dateStringFormat.date(from: dateJoinedString)
displayName = try container.decode(String.self, forKey: .displayName)
friendKey = try container.decode(String.self, forKey: .friendKey)
friends = try container.decode([String].self, forKey: .friends)
Expand Down
4 changes: 2 additions & 2 deletions Sources/VRCKit/PreviewServices/PreviewDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class PreviewDataProvider {
currentAvatar: "",
avatarImageUrl: nil,
avatarThumbnailUrl: nil,
dateJoined: "2024/07/01",
dateJoined: Date(),
displayName: "usr_\(previewUserId.uuidString.prefix(8))",
friendKey: "",
friends: friends.map(\.id),
Expand Down Expand Up @@ -162,7 +162,7 @@ final class PreviewDataProvider {
userIcon: URL(string: "https://ul.h3z.jp/9gGIcerr.png"),
location: location,
friendKey: "",
dateJoined: "",
dateJoined: Date(),
note: "",
lastActivity: Date()
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/VRCKit/Protocols/ProfileProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public protocol ProfileElementRepresentable: Hashable, Identifiable {
/// A protocol representing detailed profile properties for users.
/// This protocol extends ProfileElementRepresentable and can be adopted by structures like User and UserDetail.
public protocol ProfileDetailRepresentable: ProfileElementRepresentable {
var dateJoined: String { get }
var dateJoined: Date? { get }
var lastActivity: Date { get }
var state: User.State { get }
}

0 comments on commit 4a4d540

Please sign in to comment.