Skip to content

Commit

Permalink
feat: split WorldModel and FavoriteWorldModel
Browse files Browse the repository at this point in the history
  • Loading branch information
makinosp committed Sep 28, 2024
1 parent 28eba98 commit e5f43d1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
42 changes: 39 additions & 3 deletions Sources/VRCKit/Models/FavoriteWorldModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,48 @@
//

import Foundation
import MemberwiseInit

struct AnyCodable: Codable {}

public struct FavoriteWorldWrapper: Sendable {
public let worlds: [World]
public let worlds: [FavoriteWorld]
}

@MemberwiseInit(.public)
public struct FavoriteWorld: Codable, Sendable, Identifiable, Hashable {
public let id: String
public let name: String
public let description: String?
public let featured: Bool
public let authorId: String
public let authorName: String
public let capacity: Int
public let tags: [String]
public let releaseStatus: World.ReleaseStatus
public let imageUrl: URL?
public let thumbnailImageUrl: URL?
public let namespace: String?
public let organization: String
public let previewYoutubeId: String?
public let favorites: Int?
public let createdAt: Date
public let updatedAt: Date
public let publicationDate: OptionalISO8601Date
public let labsPublicationDate: OptionalISO8601Date
public let visits: Int
public let popularity: Int
public let heat: Int
public let favoriteGroup: String
public let version: Int?
}

extension FavoriteWorldWrapper: Decodable {
public init(from decoder: any Decoder) throws {
var container = try decoder.unkeyedContainer()
var worlds: [World] = []
var worlds: [FavoriteWorld] = []
while !container.isAtEnd {
if let world = try? container.decode(World.self) {
if let world = try? container.decode(FavoriteWorld.self) {
worlds.append(world)
} else {
_ = try container.decode(AnyCodable.self)
Expand All @@ -27,3 +56,10 @@ extension FavoriteWorldWrapper: Decodable {
self.worlds = worlds
}
}

extension FavoriteWorld: ImageUrlRepresentable {
public func imageUrl(_ resolution: ImageResolution) -> URL? {
guard let url = thumbnailImageUrl else { return nil }
return replaceImageUrl(url: url, resolution: resolution)
}
}
1 change: 0 additions & 1 deletion Sources/VRCKit/Models/WorldModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public struct World: Codable, Sendable, Identifiable, Hashable {
public let visits: Int
public let popularity: Int
public let heat: Int
public let favoriteGroup: String?
public let version: Int?

public enum ReleaseStatus: String, Codable, Sendable {
Expand Down
2 changes: 1 addition & 1 deletion Sources/VRCKit/Protocols/WorldServiceProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public protocol WorldServiceProtocol: Sendable {
func fetchWorld(worldId: String) async throws -> World
func fetchFavoritedWorlds(n: Int) async throws -> [World]
func fetchFavoritedWorlds(n: Int) async throws -> [FavoriteWorld]
}
2 changes: 1 addition & 1 deletion Sources/VRCKit/Services/WorldService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final actor WorldService: APIService, WorldServiceProtocol {
return try await Serializer.shared.decode(response.data)
}

public func fetchFavoritedWorlds(n: Int = 100) async throws -> [World] {
public func fetchFavoritedWorlds(n: Int = 100) async throws -> [FavoriteWorld] {
let queryItems = [URLQueryItem(name: "n", value: n.description)]
let response = try await client.request(path: "\(path)/favorites", method: .get, queryItems: queryItems)
let favoriteWorldWrapper: FavoriteWorldWrapper = try await Serializer.shared.decode(response.data)
Expand Down

0 comments on commit e5f43d1

Please sign in to comment.