add save function of explanation

This commit is contained in:
2026-09-14 16:27:55 +08:00
parent 9c78bb9b04
commit 3aaa9f4229
9 changed files with 200 additions and 14 deletions

View File

@@ -97,6 +97,7 @@ struct FullscreenPlayerView: View {
}
.sheet(item: $explanationSentence) { sentence in
SentenceExplanationSheet(
mediaId: controller.currentItem?.id ?? "",
courseTitle: controller.currentItem?.title ?? "口语训练课程",
sentence: sentence
)

View File

@@ -22,6 +22,7 @@ struct PlayerSectionView: View {
.padding(.horizontal, 16)
.sheet(item: $explanationSentence) { sentence in
SentenceExplanationSheet(
mediaId: controller.currentItem?.id ?? "",
courseTitle: controller.currentItem?.title ?? "口语训练课程",
sentence: sentence
)
@@ -96,7 +97,7 @@ struct PlayerSectionView: View {
Text(sentenceTextText)
.font(.system(size: 15))
.foregroundColor(.white)
.lineLimit(2)
.lineLimit(4)
Spacer()
Button {
guard let sentence = controller.currentSentence, !(sentence.text ?? "").isEmpty else {
@@ -230,12 +231,14 @@ struct PlayerSectionView: View {
}
struct SentenceExplanationSheet: View {
let mediaId: String
let courseTitle: String
let sentence: SentenceBoundary
@Environment(\.dismiss) private var dismiss
@State private var explanation = ""
@State private var errorMessage: String?
@State private var saved = false
var body: some View {
NavigationView {
@@ -267,18 +270,35 @@ struct SentenceExplanationSheet: View {
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
dismiss()
} label: {
Image(systemName: "xmark.circle.fill")
.font(.system(size: 22, weight: .semibold))
HStack(spacing: 6) {
Button(saved ? "已保存" : "保存") {
saveExplanation()
}
.disabled(saved || explanation.isEmpty || errorMessage != nil)
Text(saved ? "(已保存本地)" : "(下次直接看本地)")
.font(.system(size: 12))
.foregroundColor(Theme.textMuted)
Button {
dismiss()
} label: {
Image(systemName: "xmark.circle.fill")
.font(.system(size: 22, weight: .semibold))
.foregroundColor(Theme.textMuted)
}
.accessibilityLabel("关闭句子讲解")
}
.accessibilityLabel("关闭句子讲解")
}
}
}
.task {
if let cached = SentenceExplanationStore.shared.load(
mediaId: mediaId,
sentenceIndex: sentence.index
) {
explanation = cached
saved = true
return
}
guard explanation.isEmpty, errorMessage == nil else { return }
do {
explanation = try await SentenceBoundaryAPI.explain(
@@ -290,4 +310,13 @@ struct SentenceExplanationSheet: View {
}
}
}
private func saveExplanation() {
guard !saved, !explanation.isEmpty, errorMessage == nil else { return }
saved = SentenceExplanationStore.shared.save(
mediaId: mediaId,
sentenceIndex: sentence.index,
explanation: explanation
)
}
}