Merge branch 'main' into feature_tvOS

This commit is contained in:
2026-02-27 11:35:24 +08:00
3 changed files with 25 additions and 19 deletions

View File

@@ -93,10 +93,6 @@ enum HTMLParser {
let h1 = try doc.select("h1").first()
let fullTitle = try h1?.text().trimmingCharacters(in: .whitespacesAndNewlines) ?? "未知标题"
//
let imgSrc = try doc.select("img.w-full.h-full.object-cover").first()?.attr("src") ?? ""
let posterURL = URL(string: imgSrc)
// === JSON-LD ===
var year = 0
var rating: Double?
@@ -105,12 +101,17 @@ enum HTMLParser {
var genres: [String] = []
var description = ""
var region = ""
var posterURL: URL?
if let jsonLDScript = try doc.select("script[type=application/ld+json]").first() {
let jsonText = try jsonLDScript.data()
if let data = jsonText.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
if let imageStr = json["image"] as? String {
posterURL = URL(string: imageStr)
}
if let published = json["datePublished"] as? String {
year = Int(published) ?? 0
}
@@ -140,6 +141,12 @@ enum HTMLParser {
// === HTML ===
// fallback: .flex-shrink-0
if posterURL == nil {
let imgSrc = try doc.select(".flex-shrink-0 img.object-cover").first()?.attr("src") ?? ""
posterURL = URL(string: imgSrc)
}
// fallback
if rating == nil {
let ratingText = try doc.select(".rating-display").text()

View File

@@ -1,6 +1,6 @@
import Foundation
@Observable
@Observable @MainActor
final class DetailViewModel {
var detail: ContentDetail?
var selectedSourceIndex = 0
@@ -27,23 +27,22 @@ final class DetailViewModel {
func loadDetail(path: String) async {
isLoading = true
error = nil
defer { isLoading = false }
do {
let html = try await APIClient.shared.fetchDetailPage(path: path)
let parsedDetail = try HTMLParser.parseContentDetail(html: html)
await MainActor.run {
self.detail = parsedDetail
self.selectedSourceIndex = 0
self.selectedEpisodeIndex = 0
}
} catch is CancellationError {
// loading
return
} catch let error as URLError where error.code == .cancelled {
return
} catch {
await MainActor.run { self.error = error.localizedDescription }
self.error = error.localizedDescription
}
isLoading = false
}
func selectSource(_ index: Int) {

View File

@@ -16,22 +16,22 @@ struct DetailView: View {
var body: some View {
Group {
if viewModel.isLoading {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if let error = viewModel.error {
errorView(error)
} else if let detail = viewModel.detail {
if let detail = viewModel.detail {
ScrollView {
detailContent(detail)
}
} else if let error = viewModel.error {
errorView(error)
} else {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.navigationTitle(item.title)
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.task {
.task(id: item.id) {
if viewModel.detail == nil {
await viewModel.loadDetail(path: item.detailURL)
}