Skip to content

Commit

Permalink
refact: code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
makinosp committed Oct 12, 2024
1 parent 5c1f1c3 commit 770a4e0
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 149 deletions.
4 changes: 1 addition & 3 deletions Sources/VRCKit/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,5 @@ public final actor APIClient {
}

extension APIClient.Method: CustomStringConvertible {
var description: String {
self.rawValue.uppercased()
}
var description: String { rawValue.uppercased() }
}
2 changes: 1 addition & 1 deletion Sources/VRCKit/Models/Favorite/FavoriteTypeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public enum FavoriteType: String, Codable, Sendable, CaseIterable {
}

extension FavoriteType: Identifiable {
public var id: Int { hashValue }
public var id: String { rawValue }
}
24 changes: 1 addition & 23 deletions Sources/VRCKit/Models/Friend/FriendModel+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

extension Friend: Decodable {
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let container = try decoder.container(keyedBy: UserCodingKeys.self)
bio = try container.decodeIfPresent(String.self, forKey: .bio)
bioLinks = try container.decodeSafeNullableArray(URL.self, forKey: .bioLinks)
avatarImageUrl = try container.decodeIfPresent(URL.self, forKey: .currentAvatarImageUrl)
Expand All @@ -29,25 +29,3 @@ extension Friend: Decodable {
platform = try container.decode(UserPlatform.self, forKey: .platform)
}
}

extension Friend {
private enum CodingKeys: String, CodingKey {
case bio
case bioLinks
case currentAvatarImageUrl
case currentAvatarThumbnailImageUrl
case displayName
case id
case isFriend
case lastLogin
case lastPlatform
case profilePicOverride
case status
case statusDescription
case tags
case userIcon
case location
case friendKey
case platform
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CommonModels.swift
// GenericResponseModels.swift
// VRCKit
//
// Created by makinosp on 2024/08/03.
Expand Down
2 changes: 0 additions & 2 deletions Sources/VRCKit/Models/Tag/SystemTagModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by makinosp on 2024/08/22.
//

import Foundation

public enum SystemTag: String, Hashable, Codable, Sendable {
case adminAvatarAccess = "admin_avatar_access"
case adminCanGrantLicenses = "admin_can_grant_licenses"
Expand Down
42 changes: 42 additions & 0 deletions Sources/VRCKit/Models/User/UserCodingKeys.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// UserCodingKeys.swift
// VRCKit
//
// Created by makinosp on 2024/10/12.
//

public enum UserCodingKeys: String, CodingKey {
case bio
case bioLinks
case currentAvatarImageUrl
case currentAvatarThumbnailImageUrl
case displayName
case id
case isFriend
case lastLogin
case lastPlatform
case profilePicOverride
case state
case status
case statusDescription
case tags
case userIcon
case location
case friendKey
case dateJoined
case note
case lastActivity
case platform
case activeFriends
case allowAvatarCopying
case currentAvatar
case friends
case homeLocation
case offlineFriends
case onlineFriends
case pastDisplayNames
case presence
case twoFactorAuthEnabled
case userLanguage
case userLanguageCode
}
32 changes: 3 additions & 29 deletions Sources/VRCKit/Models/User/UserDetailModel+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Foundation

extension UserDetail: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let container = try decoder.container(keyedBy: UserCodingKeys.self)
bio = try container.decodeIfPresent(String.self, forKey: .bio)
bioLinks = try container.decodeSafeNullableArray(URL.self, forKey: .bioLinks)
avatarImageUrl = try? container.decodeIfPresent(URL.self, forKey: .avatarImageUrl)
avatarThumbnailUrl = try? container.decodeIfPresent(URL.self, forKey: .avatarThumbnailUrl)
avatarImageUrl = try? container.decodeIfPresent(URL.self, forKey: .currentAvatarImageUrl)
avatarThumbnailUrl = try? container.decodeIfPresent(URL.self, forKey: .currentAvatarThumbnailImageUrl)
displayName = try container.decode(String.self, forKey: .displayName)
id = try container.decode(UserDetail.ID.self, forKey: .id)
isFriend = try container.decode(Bool.self, forKey: .isFriend)
Expand All @@ -34,29 +34,3 @@ extension UserDetail: Decodable {
platform = try container.decode(UserPlatform.self, forKey: .platform)
}
}

extension UserDetail {
private enum CodingKeys: String, CodingKey {
case bio
case bioLinks
case avatarImageUrl = "currentAvatarImageUrl"
case avatarThumbnailUrl = "currentAvatarThumbnailImageUrl"
case displayName
case id
case isFriend
case lastLogin
case lastPlatform
case profilePicOverride
case state
case status
case statusDescription
case tags
case userIcon
case location
case friendKey
case dateJoined
case note
case lastActivity
case platform
}
}
37 changes: 1 addition & 36 deletions Sources/VRCKit/Models/User/UserModel+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

extension User: Decodable {
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let container = try decoder.container(keyedBy: UserCodingKeys.self)
activeFriends = try container.decode([String].self, forKey: .activeFriends)
allowAvatarCopying = try container.decode(Bool.self, forKey: .allowAvatarCopying)
bio = try container.decodeIfPresent(String.self, forKey: .bio)
Expand Down Expand Up @@ -43,38 +43,3 @@ extension User: Decodable {
presence = try container.decode(Presence.self, forKey: .presence)
}
}

extension User {
private enum CodingKeys: String, CodingKey {
case activeFriends
case allowAvatarCopying
case bio
case bioLinks
case currentAvatar
case currentAvatarImageUrl
case currentAvatarThumbnailImageUrl
case dateJoined
case displayName
case friendKey
case friends
case homeLocation
case id
case isFriend
case lastActivity
case lastLogin
case lastPlatform
case offlineFriends
case onlineFriends
case pastDisplayNames
case presence
case profilePicOverride
case state
case status
case statusDescription
case tags
case twoFactorAuthEnabled
case userIcon
case userLanguage
case userLanguageCode
}
}
23 changes: 6 additions & 17 deletions Sources/VRCKit/Models/User/UserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public struct User: Sendable, ProfileDetailRepresentable {
public let userLanguageCode: String?
public let presence: Presence

@MemberwiseInit(.public)
public struct DisplayName: Codable, Sendable, Hashable {
public let displayName: String
public let updatedAt: Date
Expand All @@ -55,6 +56,7 @@ public struct User: Sendable, ProfileDetailRepresentable {
case offline
}

@MemberwiseInit(.public)
public struct Presence: Codable, Hashable, Sendable {
public let groups: [String]
public let id: String
Expand All @@ -69,24 +71,11 @@ public struct User: Sendable, ProfileDetailRepresentable {
}

public extension User {
var platform: UserPlatform {
presence.platform
}
var url: URL? {
URL(string: [Const.homeBaseUrl, "user", id].joined(separator: "/"))
}
var platform: UserPlatform { presence.platform }
}

public extension User.Presence {
init() {
groups = []
id = UUID().uuidString
instance = ""
instanceType = ""
platform = .blank
status = .offline
travelingToInstance = ""
travelingToWorld = ""
world = ""
public extension User {
var url: URL? {
URL(string: [Const.homeBaseUrl, "user", id].joined(separator: "/"))
}
}
4 changes: 2 additions & 2 deletions Sources/VRCKit/Models/User/UserNoteModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Foundation
import MemberwiseInit

struct UserNoteRequest: Codable, Sendable {
var targetUserId: String
var note: String
let targetUserId: String
let note: String
}

@MemberwiseInit(.public)
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 @@ -15,7 +15,7 @@ public protocol ProfileElementRepresentable: Sendable, Hashable, Identifiable, I
var avatarImageUrl: URL? { get }
var avatarThumbnailUrl: URL? { get }
var displayName: String { get }
var id: String { get }
var id: Self.ID { get }
var isFriend: Bool { get }
var lastLogin: Date { get }
var lastPlatform: String { get }
Expand Down
7 changes: 2 additions & 5 deletions Sources/VRCKit/Services/AuthenticationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
//

import Foundation
import MemberwiseInit

@MemberwiseInit(.public)
public final actor AuthenticationService: APIService, AuthenticationServiceProtocol {
public let client: APIClient
private let authPath = "auth"

// Initializes the AuthenticationService with an APIClient instance
public init(client: APIClient) {
self.client = client
}

/// Check if a user exists by their user ID.
/// - Parameter userId: The ID of the user to check.
/// - Returns: A boolean indicating if the user exists.
Expand Down
7 changes: 2 additions & 5 deletions Sources/VRCKit/Services/FavoriteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
//

import Foundation
import MemberwiseInit

@MemberwiseInit(.public)
public final actor FavoriteService: APIService, FavoriteServiceProtocol {
public let client: APIClient

// Initializes the AuthenticationService with an APIClient instance
public init(client: APIClient) {
self.client = client
}

/// Asynchronously retrieves a list of favorite groups from the server.
/// - Returns: An array of `FavoriteGroup` objects.
public func listFavoriteGroups() async throws -> [FavoriteGroup] {
Expand Down
6 changes: 2 additions & 4 deletions Sources/VRCKit/Services/FriendService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
//

import Foundation
import MemberwiseInit

@MemberwiseInit(.public)
public final actor FriendService: APIService, FriendServiceProtocol {
public let client: APIClient
private let path = "auth/user/friends"

public init(client: APIClient) {
self.client = client
}

/// List information about friends.
public func fetchFriends(offset: Int, n: Int = 60, offline: Bool) async throws -> [Friend] {
let queryItems = [
Expand Down
7 changes: 3 additions & 4 deletions Sources/VRCKit/Services/InstanceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
// Created by makinosp on 2024/02/18.
//

import MemberwiseInit

@MemberwiseInit(.public)
public final actor InstanceService: APIService, InstanceServiceProtocol {
public let client: APIClient
private let path = "instances"

public init(client: APIClient) {
self.client = client
}

/// Fetches an instance of a world using the specified world ID and instance ID.
/// - Parameters:
/// - worldId: The ID of the world to fetch the instance from.
Expand Down
8 changes: 3 additions & 5 deletions Sources/VRCKit/Services/UserNoteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
// Created by makinosp on 2024/03/17.
//

import MemberwiseInit

@MemberwiseInit(.public)
public final actor UserNoteService: APIService, UserNoteServiceProtocol {
public let client: APIClient
private let path = "userNotes"

// Initializes the AuthenticationService with an APIClient instance
public init(client: APIClient) {
self.client = client
}

/// Updates the note for a specific user by sending the note to the API.
/// - Parameters:
/// - targetUserId: The ID of the user for whom the note is being updated.
Expand Down
7 changes: 3 additions & 4 deletions Sources/VRCKit/Services/UserService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
// Created by makinosp on 2024/02/18.
//

import MemberwiseInit

@MemberwiseInit(.public)
public final actor UserService: APIService, UserServiceProtocol {
public let client: APIClient
private let path = "users"

public init(client: APIClient) {
self.client = client
}

/// Fetch a user
public func fetchUser(userId: String) async throws -> UserDetail {
let response = try await client.request(path: "\(path)/\(userId)", method: .get)
Expand Down
6 changes: 2 additions & 4 deletions Sources/VRCKit/Services/WorldService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
//

import Foundation
import MemberwiseInit

@MemberwiseInit(.public)
public final actor WorldService: APIService, WorldServiceProtocol {
public let client: APIClient
private let path = "worlds"

public init(client: APIClient) {
self.client = client
}

public func fetchWorld(worldId: String) async throws -> World {
let response = try await client.request(path: "\(path)/\(worldId)", method: .get)
return try await Serializer.shared.decode(response.data)
Expand Down
Loading

0 comments on commit 770a4e0

Please sign in to comment.