Skip to content

Commit 366a7b1

Browse files
committed
refact: code refactoring Favorite models
1 parent 65289b0 commit 366a7b1

File tree

6 files changed

+72
-49
lines changed

6 files changed

+72
-49
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// FavoriteModel.swift
3+
// VRCKit
4+
//
5+
// Created by makinosp on 2024/10/06.
6+
//
7+
8+
import MemberwiseInit
9+
10+
@MemberwiseInit(.public)
11+
public struct FavoriteGroup: Codable, Sendable, Identifiable, Hashable {
12+
public let id: String
13+
public let displayName: String
14+
public let name: String
15+
public let ownerId: String
16+
public let tags: [String]
17+
public let type: FavoriteType
18+
public let visibility: Visibility
19+
20+
public enum Visibility: String, Codable, Sendable {
21+
case `private`, friends, `public`
22+
}
23+
}
24+
25+
@MemberwiseInit
26+
struct RequestToUpdateFavoriteGroup: Codable, Sendable {
27+
let displayName: String?
28+
let visibility: FavoriteGroup.Visibility?
29+
@Init(default: []) let tags: [String]
30+
}
31+
32+
public struct FavoriteGroupParams: Sendable {
33+
let type: FavoriteType
34+
let name: String
35+
let userId: String
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// FavoriteList.swift
3+
// VRCKit
4+
//
5+
// Created by makinosp on 2024/10/06.
6+
//
7+
8+
public struct FavoriteList: Sendable, Identifiable {
9+
public let id: String
10+
public let favorites: [Favorite]
11+
}
12+
13+
public extension FavoriteList {
14+
func allFavoritesAre(_ type: FavoriteType) -> Bool {
15+
favorites.allSatisfy { $0.type == type }
16+
}
17+
}

Sources/VRCKit/Models/Favorite/FavoriteModel.swift

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77

88
import MemberwiseInit
99

10-
public enum FavoriteType: String, Codable, Sendable, CaseIterable {
11-
case world, avatar, friend
12-
}
13-
14-
extension FavoriteType: Identifiable {
15-
public var id: Int { hashValue }
16-
}
17-
1810
@MemberwiseInit(.public)
1911
public struct Favorite: Codable, Sendable, Identifiable {
2012
public let id: String
@@ -23,41 +15,8 @@ public struct Favorite: Codable, Sendable, Identifiable {
2315
public let type: FavoriteType
2416
}
2517

26-
public struct FavoriteDetail: Sendable, Identifiable {
27-
public let id: String
28-
public let favorites: [Favorite]
29-
}
30-
31-
public extension FavoriteDetail {
32-
func allFavoritesAre(_ type: FavoriteType) -> Bool {
33-
favorites.allSatisfy { $0.type == type }
34-
}
35-
}
36-
37-
@MemberwiseInit(.public)
38-
public struct FavoriteGroup: Codable, Sendable, Identifiable, Hashable {
39-
public let id: String
40-
public let displayName: String
41-
public let name: String
42-
public let ownerId: String
43-
public let tags: [String]
44-
public let type: FavoriteType
45-
public let visibility: Visibility
46-
47-
public enum Visibility: String, Codable, Sendable {
48-
case `private`, friends, `public`
49-
}
50-
}
51-
5218
struct RequestToAddFavorite: Codable, Sendable {
5319
let type: FavoriteType
5420
let favoriteId: String
5521
let tags: [String]
5622
}
57-
58-
@MemberwiseInit
59-
struct RequestToUpdateFavoriteGroup: Codable, Sendable {
60-
let displayName: String?
61-
let visibility: FavoriteGroup.Visibility?
62-
@Init(default: []) let tags: [String]
63-
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// FavoriteModel.swift
3+
// VRCKit
4+
//
5+
// Created by makinosp on 2024/10/06.
6+
//
7+
8+
public enum FavoriteType: String, Codable, Sendable, CaseIterable {
9+
case world, avatar, friend
10+
}
11+
12+
extension FavoriteType: Identifiable {
13+
public var id: Int { hashValue }
14+
}

Sources/VRCKit/Protocols/FavoriteServiceProtocol.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
//
77

88
public protocol FavoriteServiceProtocol: Sendable {
9-
typealias FavoriteGroupParams = (type: FavoriteType, name: String, userId: String)
109
func listFavoriteGroups() async throws -> [FavoriteGroup]
1110
func listFavorites(n: Int, type: FavoriteType, tag: String?) async throws -> [Favorite]
12-
func fetchFavoriteGroupDetails(favoriteGroups: [FavoriteGroup]) async throws -> [FavoriteDetail]
11+
func fetchFavoriteList(favoriteGroups: [FavoriteGroup]) async throws -> [FavoriteList]
1312
func addFavorite(type: FavoriteType, favoriteId: String, tag: String) async throws -> Favorite
1413
func updateFavoriteGroup(
1514
params: FavoriteGroupParams,

Sources/VRCKit/Services/FavoriteService.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ public final actor FavoriteService: APIService, FavoriteServiceProtocol {
4949
/// Fetches details of favorite groups asynchronously.
5050
/// - Parameter favoriteGroups: An array of `FavoriteGroup` objects.
5151
/// - Returns: An array of `FavoriteDetail` objects containing detailed information about the favorite groups.
52-
public func fetchFavoriteGroupDetails(
53-
favoriteGroups: [FavoriteGroup]
54-
) async throws -> [FavoriteDetail] {
55-
var results: [FavoriteDetail] = []
56-
try await withThrowingTaskGroup(of: FavoriteDetail.self) { taskGroup in
52+
public func fetchFavoriteList(favoriteGroups: [FavoriteGroup]) async throws -> [FavoriteList] {
53+
var results: [FavoriteList] = []
54+
try await withThrowingTaskGroup(of: FavoriteList.self) { taskGroup in
5755
for favoriteGroup in favoriteGroups.filter({ $0.type == .friend }) {
5856
taskGroup.addTask { [weak self] in
5957
guard let self = self else {
@@ -63,7 +61,7 @@ public final actor FavoriteService: APIService, FavoriteServiceProtocol {
6361
type: .friend,
6462
tag: favoriteGroup.name
6563
)
66-
return FavoriteDetail(id: favoriteGroup.id, favorites: favorites)
64+
return FavoriteList(id: favoriteGroup.id, favorites: favorites)
6765
}
6866
}
6967
for try await favoriteGroupDetail in taskGroup {

0 commit comments

Comments
 (0)