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

@@ -10,6 +10,7 @@
AA0000000000000000000090 /* AuthAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0000000000000000000091 /* AuthAPI.swift */; };
AA00000000000000000000A1 /* UserHistory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00000000000000000000A2 /* UserHistory.swift */; };
AA00000000000000000000A3 /* TrainingHistoryStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00000000000000000000A4 /* TrainingHistoryStore.swift */; };
AA00000000000000000000B0 /* SentenceExplanationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00000000000000000000B1 /* SentenceExplanationStore.swift */; };
AA0000000000000000000092 /* AuthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0000000000000000000093 /* AuthView.swift */; };
AA0000000000000000000040 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AA0000000000000000000020 /* Assets.xcassets */; };
AA0000000000000000000042 /* OralTrainerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0000000000000000000022 /* OralTrainerApp.swift */; };
@@ -41,6 +42,7 @@
AA0000000000000000000091 /* AuthAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthAPI.swift; sourceTree = "<group>"; };
AA00000000000000000000A2 /* UserHistory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserHistory.swift; sourceTree = "<group>"; };
AA00000000000000000000A4 /* TrainingHistoryStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrainingHistoryStore.swift; sourceTree = "<group>"; };
AA00000000000000000000B1 /* SentenceExplanationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentenceExplanationStore.swift; sourceTree = "<group>"; };
AA0000000000000000000093 /* AuthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthView.swift; sourceTree = "<group>"; };
AA000000000000000000000A /* OralTrainer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OralTrainer.app; sourceTree = BUILT_PRODUCTS_DIR; };
AA000000000000000000001F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -131,6 +133,7 @@
AA000000000000000000002A /* SentenceBoundaryAPI.swift */,
AA000000000000000000002B /* ImitationAssessor.swift */,
AA00000000000000000000A4 /* TrainingHistoryStore.swift */,
AA00000000000000000000B1 /* SentenceExplanationStore.swift */,
AA00000000000000000000A2 /* UserHistory.swift */,
AA000000000000000000007E /* DubbingCompositor.swift */,
AA0000000000000000000081 /* AudioRecorderSession.swift */,
@@ -267,6 +270,7 @@
AA0000000000000000000090 /* AuthAPI.swift in Sources */,
AA00000000000000000000A1 /* UserHistory.swift in Sources */,
AA00000000000000000000A3 /* TrainingHistoryStore.swift in Sources */,
AA00000000000000000000B0 /* SentenceExplanationStore.swift in Sources */,
AA0000000000000000000092 /* AuthView.swift in Sources */,
AA0000000000000000000053 /* TestView.swift in Sources */,
AA0000000000000000000054 /* FullscreenPlayerView.swift in Sources */,

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>

View File

@@ -0,0 +1,47 @@
import Foundation
final class SentenceExplanationStore {
static let shared = SentenceExplanationStore()
private let root: URL
init() {
let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
root = documents.appendingPathComponent("SentenceExplanations", isDirectory: true)
try? FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
}
func load(mediaId: String, sentenceIndex: Int) -> String? {
let url = fileURL(mediaId: mediaId, sentenceIndex: sentenceIndex)
guard FileManager.default.fileExists(atPath: url.path) else { return nil }
let text = try? String(contentsOf: url, encoding: .utf8)
let trimmed = text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
return trimmed.isEmpty ? nil : trimmed
}
@discardableResult
func save(mediaId: String, sentenceIndex: Int, explanation: String) -> Bool {
let trimmed = explanation.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return false }
let url = fileURL(mediaId: mediaId, sentenceIndex: sentenceIndex)
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
do {
try trimmed.write(to: url, atomically: true, encoding: .utf8)
return true
} catch {
return false
}
}
private func fileURL(mediaId: String, sentenceIndex: Int) -> URL {
let directory = root.appendingPathComponent(Self.sanitize(mediaId), isDirectory: true)
return directory.appendingPathComponent("\(sentenceIndex).txt")
}
static func sanitize(_ mediaId: String) -> String {
let allowed = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "._-"))
let filtered = mediaId.lowercased().unicodeScalars.map { allowed.contains($0) ? Character($0) : "_" }
let value = String(filtered).trimmingCharacters(in: CharacterSet(charactersIn: "_"))
return value.isEmpty || value == "." || value == ".." ? "unknown" : value
}
}

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
)
}
}