add save function of explanation
This commit is contained in:
@@ -434,6 +434,7 @@ class MainActivity : Activity() {
|
||||
explanationOverlay.show(
|
||||
sentence,
|
||||
controller.currentTrainingItem()?.title.orEmpty(),
|
||||
controller.currentTrainingItem()?.id.orEmpty(),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,12 @@ internal class SentenceExplanationOverlay(
|
||||
private val activity: Activity,
|
||||
private val api: SentenceExplanationApi,
|
||||
) {
|
||||
private val store = SentenceExplanationStore(activity)
|
||||
private var currentOverlay: View? = null
|
||||
private var currentRequest: CancellableExplanationRequest? = null
|
||||
private var requestGeneration = 0
|
||||
|
||||
fun show(sentence: SentenceBoundary, courseTitle: String) {
|
||||
fun show(sentence: SentenceBoundary, courseTitle: String, mediaId: String) {
|
||||
close()
|
||||
val generation = ++requestGeneration
|
||||
val root = FrameLayout(activity).apply {
|
||||
@@ -51,6 +52,31 @@ internal class SentenceExplanationOverlay(
|
||||
setTextColor(SampleTheme.textDark)
|
||||
textSize = 19f
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
|
||||
}
|
||||
val saveHint = TextView(activity).apply {
|
||||
text = HINT_UNSAVED
|
||||
setTextColor(SampleTheme.textSubtle)
|
||||
textSize = 12f
|
||||
setPadding(6.dp, 0, 0, 0)
|
||||
}
|
||||
val saveButton = TextView(activity).apply {
|
||||
text = "保存"
|
||||
setTextColor(Color.WHITE)
|
||||
textSize = 13f
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
gravity = Gravity.CENTER
|
||||
background = rounded(SampleTheme.button, 8f)
|
||||
setPadding(12.dp, 6.dp, 12.dp, 6.dp)
|
||||
isEnabled = false
|
||||
alpha = 0.45f
|
||||
}
|
||||
val titleRow = LinearLayout(activity).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
addView(title)
|
||||
addView(saveButton)
|
||||
addView(saveHint)
|
||||
}
|
||||
val subtitle = TextView(activity).apply {
|
||||
text = sentence.text?.takeIf { it.isNotBlank() } ?: courseTitle
|
||||
@@ -86,7 +112,7 @@ internal class SentenceExplanationOverlay(
|
||||
).apply { topMargin = 16.dp }
|
||||
setOnClickListener { close() }
|
||||
}
|
||||
card.addView(title)
|
||||
card.addView(titleRow)
|
||||
card.addView(subtitle)
|
||||
card.addView(scroll)
|
||||
card.addView(close)
|
||||
@@ -94,6 +120,22 @@ internal class SentenceExplanationOverlay(
|
||||
|
||||
addToWindow(root)
|
||||
currentOverlay = root
|
||||
|
||||
val cached = store.load(mediaId, sentence.index)
|
||||
if (!cached.isNullOrBlank()) {
|
||||
message.text = cached
|
||||
message.setTextColor(SampleTheme.textDark)
|
||||
markSaved(saveButton, saveHint)
|
||||
return
|
||||
}
|
||||
|
||||
var loadedExplanation: String? = null
|
||||
saveButton.setOnClickListener {
|
||||
val explanation = loadedExplanation ?: return@setOnClickListener
|
||||
if (store.save(mediaId, sentence.index, explanation)) {
|
||||
markSaved(saveButton, saveHint)
|
||||
}
|
||||
}
|
||||
currentRequest = api.explain(
|
||||
sentence.text.orEmpty(),
|
||||
courseTitle,
|
||||
@@ -101,14 +143,20 @@ internal class SentenceExplanationOverlay(
|
||||
sentence.language,
|
||||
object : SentenceExplanationCallback {
|
||||
override fun onSuccess(result: SentenceExplanationResult) {
|
||||
if (generation == requestGeneration) message.text = result.explanation
|
||||
if (generation != requestGeneration) return
|
||||
loadedExplanation = result.explanation
|
||||
message.text = result.explanation
|
||||
message.setTextColor(SampleTheme.textDark)
|
||||
saveButton.isEnabled = true
|
||||
saveButton.alpha = 1f
|
||||
}
|
||||
|
||||
override fun onError(error: Throwable) {
|
||||
if (generation == requestGeneration) {
|
||||
message.text = "讲解失败:${error.message ?: "请稍后重试"}"
|
||||
message.setTextColor(SampleTheme.danger)
|
||||
}
|
||||
if (generation != requestGeneration) return
|
||||
message.text = "讲解失败:${error.message ?: "请稍后重试"}"
|
||||
message.setTextColor(SampleTheme.danger)
|
||||
saveButton.isEnabled = false
|
||||
saveButton.alpha = 0.45f
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -128,6 +176,13 @@ internal class SentenceExplanationOverlay(
|
||||
api.release()
|
||||
}
|
||||
|
||||
private fun markSaved(saveButton: TextView, saveHint: TextView) {
|
||||
saveButton.text = "已保存"
|
||||
saveButton.isEnabled = false
|
||||
saveButton.alpha = 0.7f
|
||||
saveHint.text = HINT_SAVED
|
||||
}
|
||||
|
||||
private fun addToWindow(view: View) {
|
||||
(activity.window?.decorView as? ViewGroup)?.addView(view)
|
||||
}
|
||||
@@ -147,4 +202,9 @@ internal class SentenceExplanationOverlay(
|
||||
cornerRadius = radiusDp.dp.toFloat()
|
||||
strokeColor?.let { setStroke(1.dp, it) }
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val HINT_UNSAVED = "(下次直接看本地)"
|
||||
const val HINT_SAVED = "(已保存本地)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.learningpad.oraltrainer.sample
|
||||
|
||||
import android.content.Context
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
||||
/** Persists sentence explanations under app filesDir for reuse without another API call. */
|
||||
internal class SentenceExplanationStore(context: Context) {
|
||||
private val rootDir = File(context.applicationContext.filesDir, "sentence-explanations")
|
||||
|
||||
fun load(mediaId: String, sentenceIndex: Int): String? {
|
||||
val file = fileFor(mediaId, sentenceIndex) ?: return null
|
||||
if (!file.isFile || file.length() <= 0L) return null
|
||||
return file.readText()
|
||||
}
|
||||
|
||||
fun save(mediaId: String, sentenceIndex: Int, explanation: String): Boolean {
|
||||
val text = explanation.trim()
|
||||
if (text.isEmpty()) return false
|
||||
val file = fileFor(mediaId, sentenceIndex) ?: return false
|
||||
file.parentFile?.mkdirs()
|
||||
file.writeText(text)
|
||||
return file.isFile && file.length() > 0L
|
||||
}
|
||||
|
||||
private fun fileFor(mediaId: String, sentenceIndex: Int): File? {
|
||||
val directoryName = sanitize(mediaId) ?: return null
|
||||
return File(File(rootDir, directoryName), "$sentenceIndex.txt")
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun sanitize(mediaId: String): String? {
|
||||
val filtered = mediaId.trim().lowercase(Locale.US)
|
||||
.replace(Regex("[^a-z0-9._-]"), "_")
|
||||
.trim('_')
|
||||
return filtered.takeIf { it.isNotBlank() && it != "." && it != ".." }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user