Skip to content

Commit 0889a10

Browse files
committed
feat: tags 显示
1 parent 50e38d4 commit 0889a10

8 files changed

Lines changed: 47 additions & 14 deletions

File tree

PCL.Mac.Core/Models/Resource.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,29 @@ public struct Mod: Codable, Hashable, Equatable {
2828
public let description: String?
2929
public let icon: ResourceIcon?
3030
public let loaders: [ModLoader]
31+
public let tags: [String]
3132
public let sources: [Source]
3233

33-
public init(name: String, version: String, description: String?, icon: ResourceIcon?, loaders: [ModLoader], sources: [Source]) {
34+
public init(
35+
name: String,
36+
version: String,
37+
description: String?,
38+
icon: ResourceIcon?,
39+
loaders: [ModLoader],
40+
tags: [String],
41+
sources: [Source]
42+
) {
3443
self.name = name
3544
self.version = version
3645
self.description = description
3746
self.icon = icon
3847
self.loaders = loaders
48+
self.tags = tags
3949
self.sources = sources
4050
}
4151

4252
private enum CodingKeys: CodingKey {
4353
// 不持久化存储 id
44-
case name, version, description, icon, loaders, sources
54+
case name, version, description, icon, loaders, tags, sources
4555
}
4656
}

PCL.Mac.Core/Services/Mod/ModLoadService.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class ModLoadService {
1313
private let remoteLookupService: ModRemoteLookupService
1414
private let cache: ModCache
1515
private let tomlDecoder: TOMLDecoder = .init()
16+
private let validPathExtensions = ["jar", "disabled"]
1617

1718
public init(remoteLookupService: ModRemoteLookupService, cache: ModCache) {
1819
self.remoteLookupService = remoteLookupService
@@ -71,7 +72,7 @@ public class ModLoadService {
7172

7273
func enumerateFiles(body: (URL) throws -> Void) {
7374
for case let fileURL as URL in enumerator {
74-
guard fileURL.pathExtension == "jar" && (try? fileURL.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true else { continue }
75+
guard validPathExtensions.contains(fileURL.pathExtension) && (try? fileURL.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true else { continue }
7576
do {
7677
try body(fileURL)
7778
} catch {
@@ -226,6 +227,7 @@ public class ModLoadService {
226227
description: meta.description ?? remoteInfo?.description,
227228
icon: icon,
228229
loaders: loaders,
230+
tags: remoteInfo?.tags ?? [],
229231
sources: (remoteInfo?.source).map { [$0] } ?? []
230232
)
231233
}

PCL.Mac.Core/Services/Mod/ModRemoteLookupService.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ public class ModRemoteLookupService {
5353
public struct RemoteModInfo {
5454
public let name: String
5555
public let description: String
56+
public let tags: [String]
5657
public let icon: URL?
5758
public let source: Mod.Source
5859

59-
public init(name: String, description: String, icon: URL?, source: Mod.Source) {
60+
public init(name: String, description: String, tags: [String], icon: URL?, source: Mod.Source) {
6061
self.name = name
6162
self.description = description
63+
self.tags = tags
6264
self.icon = icon
6365
self.source = source
6466
}
@@ -67,6 +69,7 @@ public class ModRemoteLookupService {
6769
self.init(
6870
name: modrinthProject.title,
6971
description: modrinthProject.description,
72+
tags: modrinthProject.categories,
7073
icon: modrinthProject.iconURL,
7174
source: .modrinth(projectId: modrinthProject.id)
7275
)
@@ -76,6 +79,7 @@ public class ModRemoteLookupService {
7679
self.init(
7780
name: curseforgeMod.name,
7881
description: curseforgeMod.summary,
82+
tags: [],
7983
icon: curseforgeMod.logo.thumbnailURL,
8084
source: .curseforge(id: curseforgeMod.id)
8185
)

PCL.Mac/Components/MyListItem.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct MyListItem<Content: View>: View {
6767
RoundedRectangle(cornerRadius: 4)
6868
.fill(hovered ? Color.color2.opacity(0.1) : .clear)
6969
.scaleEffect(backgroundScale)
70+
.allowsHitTesting(false)
7071
}
7172
.onHover { hovered in
7273
withAnimation(.spring(response: 0.2)) {

PCL.Mac/Models/ProjectListItemModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ struct ProjectListItemModel: Identifiable, Equatable, Hashable {
6767
}
6868
}
6969

70+
public static func localizeTag(_ key: String) -> String {
71+
return tagMap[key] ?? key
72+
}
73+
7074
private static func generateSupportDescription(for project: ModrinthProject) -> (String, Bool) {
7175
var description: String = ""
7276

PCL.Mac/Models/ResourceDisplayModel.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ struct ModDisplayModel {
1414
let name: String
1515
let version: String
1616
let description: String
17+
let tags: [String]
1718
let icon: ListItem.Image
19+
let fileName: String
1820

19-
init(name: String, version: String, description: String, icon: ListItem.Image?) {
21+
init(name: String, version: String, description: String, tags: [String], icon: ListItem.Image?, fileName: String) {
2022
self.name = name
2123
self.version = version
2224
self.description = description
25+
self.tags = tags
2326
self.icon = icon ?? .resource(.iconModLogo)
27+
self.fileName = fileName
2428
}
2529

26-
init(_ mod: Mod) {
30+
init(_ url: URL, _ mod: Mod) {
2731
let icon: ListItem.Image?
2832
if let modIcon = mod.icon {
2933
switch modIcon {
@@ -45,7 +49,9 @@ struct ModDisplayModel {
4549
name: mod.name,
4650
version: mod.version,
4751
description: mod.description ?? "",
48-
icon: icon
52+
tags: mod.tags.map(ProjectListItemModel.localizeTag(_:)),
53+
icon: icon,
54+
fileName: url.lastPathComponent
4955
)
5056
}
5157
}

PCL.Mac/ViewModels/Instance/InstalledModsViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class InstalledModsViewModel: ObservableObject {
7272
let resources = try loadResult[start..<end]
7373
.map {
7474
try Task.checkCancellation()
75-
return ModDisplayModel($0.1)
75+
return ModDisplayModel($0.0, $0.1)
7676
}
7777
await MainActor.run {
7878
self.resources = resources

PCL.Mac/Views/Launch/InstanceSettings/InstalledModsPage.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,22 @@ private struct ResourceListItem: View {
106106
.foregroundStyle(Color.color1)
107107

108108
VStack(alignment: .leading) {
109-
HStack(spacing: 0) {
109+
HStack(alignment: .center, spacing: 0) {
110110
MyText(resource.name)
111-
.lineLimit(1)
112-
MyText(" | \(resource.version)", color: .colorGray3)
111+
MyText(" | \(resource.fileName)", size: 12, color: .colorGray3)
112+
HStack(spacing: 3) {
113+
ForEach(resource.tags, id: \.self) { tag in
114+
MyTag(tag, labelColor: .colorGray2, backgroundColor: .black.opacity(0.05), size: 12)
115+
}
116+
}
117+
.padding(.leading, 3)
113118
}
114-
MyText("\(resource.version) | \(resource.description)", color: .colorGray3)
115-
.lineLimit(1)
119+
MyText("\(resource.version) | \(resource.description)", size: 12, color: .colorGray3)
116120
}
121+
.lineLimit(1)
122+
.textSelection(.enabled)
117123

118-
Spacer()
124+
Spacer(minLength: 0)
119125
}
120126
}
121127
}

0 commit comments

Comments
 (0)