add a refresh action
This commit is contained in:
@@ -10,6 +10,7 @@ final class CatalogViewModel: ObservableObject {
|
||||
|
||||
private var baseURL = AppConfig.serverBaseURL
|
||||
private var hasLoaded = false
|
||||
private var boundaryDocuments: [String: SentenceBoundaryDocument] = [:]
|
||||
|
||||
func loadIfNeeded() async {
|
||||
guard !hasLoaded else {
|
||||
@@ -31,6 +32,38 @@ final class CatalogViewModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func preloadBoundaries(for videos: [TrainingVideoSummary]) async -> (
|
||||
videos: [TrainingVideoSummary],
|
||||
succeeded: Int,
|
||||
failed: Int
|
||||
) {
|
||||
var updatedVideos = videos
|
||||
var succeeded = 0
|
||||
var failed = 0
|
||||
|
||||
for (index, video) in videos.enumerated() {
|
||||
do {
|
||||
let document = try await SentenceBoundaryAPI.fetch(videoHash: video.videoHash)
|
||||
boundaryDocuments[document.videoHash] = document
|
||||
updatedVideos[index] = video.copy(
|
||||
durationMs: document.durationMs,
|
||||
sentenceCount: document.sentences.count
|
||||
)
|
||||
if activeVideoHash == video.videoHash {
|
||||
controller?.updateBoundaries(document.sentences)
|
||||
controller?.boundaryState = .loaded(count: document.sentences.count)
|
||||
controller?.showTransientMessage("课程已就绪:\(document.sentences.count) 句")
|
||||
}
|
||||
succeeded += 1
|
||||
} catch {
|
||||
failed += 1
|
||||
}
|
||||
}
|
||||
|
||||
videos = updatedVideos
|
||||
return (updatedVideos, succeeded, failed)
|
||||
}
|
||||
|
||||
/// 与安卓端 loadRemoteVideo() 一致:先以无句边界装载,再拉取句边界后重新装载。
|
||||
func select(_ video: TrainingVideoSummary) {
|
||||
guard let controller = controller else {
|
||||
@@ -41,6 +74,19 @@ final class CatalogViewModel: ObservableObject {
|
||||
controller.boundaryState = .failed(message: "无法解析视频流地址")
|
||||
return
|
||||
}
|
||||
if let document = boundaryDocuments[video.videoHash] {
|
||||
let item = TrainingMediaItem(
|
||||
id: video.videoHash,
|
||||
title: video.title,
|
||||
url: url,
|
||||
sentences: document.sentences
|
||||
)
|
||||
controller.load(item)
|
||||
controller.boundaryState = .loaded(count: document.sentences.count)
|
||||
controller.showTransientMessage("课程已就绪:\(document.sentences.count) 句")
|
||||
return
|
||||
}
|
||||
|
||||
let loadingItem = TrainingMediaItem(id: video.videoHash, title: video.title, url: url)
|
||||
controller.load(loadingItem)
|
||||
controller.boundaryState = .loading(isLocal: false)
|
||||
@@ -51,6 +97,7 @@ final class CatalogViewModel: ObservableObject {
|
||||
guard activeVideoHash == video.videoHash else {
|
||||
return
|
||||
}
|
||||
boundaryDocuments[document.videoHash] = document
|
||||
controller.updateBoundaries(document.sentences)
|
||||
controller.boundaryState = .loaded(count: document.sentences.count)
|
||||
controller.showTransientMessage("课程已就绪:\(document.sentences.count) 句")
|
||||
|
||||
Reference in New Issue
Block a user