add delete function

This commit is contained in:
2026-09-08 11:25:03 +08:00
parent d0d43794f3
commit fb64343a9f
2 changed files with 113 additions and 6 deletions

View File

@@ -165,6 +165,22 @@ class UserApi internal constructor(
onComplete: (Result<List<UserDubShare>>) -> Unit, onComplete: (Result<List<UserDubShare>>) -> Unit,
): CancellableRequest = submitObject(onComplete, token) { parseDubShares(getBlocking(it, "api/v1/me/dub-shares")) } ): CancellableRequest = submitObject(onComplete, token) { parseDubShares(getBlocking(it, "api/v1/me/dub-shares")) }
fun deleteResult(
token: String,
attemptId: String,
onComplete: (Result<Unit>) -> Unit,
): CancellableRequest = submitUnit(onComplete, token) {
request("api/v1/me/results/$attemptId", token, method = "DELETE")
}
fun deleteDubShare(
token: String,
shareId: String,
onComplete: (Result<Unit>) -> Unit,
): CancellableRequest = submitUnit(onComplete, token) {
request("api/v1/me/dub-shares/$shareId", token, method = "DELETE")
}
fun dubShareDetail( fun dubShareDetail(
shareId: String, shareId: String,
onComplete: (Result<DubShareDetail>) -> Unit, onComplete: (Result<DubShareDetail>) -> Unit,

View File

@@ -3,12 +3,14 @@ package cn.learningpad.oraltrainer.sample.ui
import android.app.Activity import android.app.Activity
import android.graphics.Color import android.graphics.Color
import android.graphics.Typeface import android.graphics.Typeface
import android.view.Gravity
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.ScrollView import android.widget.ScrollView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast
import cn.learningpad.oraltrainer.sample.MineScreenActions import cn.learningpad.oraltrainer.sample.MineScreenActions
import cn.learningpad.oraltrainer.sample.SampleBindings import cn.learningpad.oraltrainer.sample.SampleBindings
import cn.learningpad.oraltrainer.sample.SampleTheme import cn.learningpad.oraltrainer.sample.SampleTheme
@@ -25,6 +27,8 @@ internal class MineScreen(
) : UiStyled { ) : UiStyled {
override val uiContext get() = activity override val uiContext get() = activity
private val deletingHistoryIds = mutableSetOf<String>()
fun createSection(): View { fun createSection(): View {
bindings.myContentList = LinearLayout(activity).apply { bindings.myContentList = LinearLayout(activity).apply {
orientation = LinearLayout.VERTICAL orientation = LinearLayout.VERTICAL
@@ -79,10 +83,13 @@ internal class MineScreen(
filtered.forEach { row -> filtered.forEach { row ->
val passedLabel = if (row.passed) "通过" else "未通过" val passedLabel = if (row.passed) "通过" else "未通过"
list.addView( list.addView(
widgets.infoCard( historyCard(
"第 ${row.sentenceIndex + 1} 句" + "\n" + text = "第 ${row.sentenceIndex + 1} 句" + "\n" +
"得分 ${row.overallScore.toInt()} · $passedLabel", "得分 ${row.overallScore.toInt()} · $passedLabel",
), id = row.attemptId,
) {
deleteResult(userApi, token, row.attemptId)
},
) )
} }
}, },
@@ -99,10 +106,13 @@ internal class MineScreen(
} }
filtered.forEach { row -> filtered.forEach { row ->
list.addView( list.addView(
widgets.infoCard( historyCard(
"${row.title}" + "\n" + text = "${row.title}" + "\n" +
"${row.segmentCount} 句 · 平均 ${row.averageScore.toInt()} 分", "${row.segmentCount} 句 · 平均 ${row.averageScore.toInt()} 分",
), id = row.shareId,
) {
deleteDubShare(userApi, token, row.shareId)
},
) )
} }
}, },
@@ -115,6 +125,87 @@ internal class MineScreen(
} }
} }
private fun deleteResult(userApi: UserApi, token: String, attemptId: String) {
userApi.deleteResult(token, attemptId) { result ->
result.fold(
onSuccess = {
Toast.makeText(activity, "测试记录已删除", Toast.LENGTH_SHORT).show()
refreshContent(userApi, token)
},
onFailure = {
deletingHistoryIds.remove(attemptId)
Toast.makeText(activity, "删除失败:${it.message}", Toast.LENGTH_SHORT).show()
refreshContent(userApi, token)
},
)
}
}
private fun deleteDubShare(userApi: UserApi, token: String, shareId: String) {
userApi.deleteDubShare(token, shareId) { result ->
result.fold(
onSuccess = {
Toast.makeText(activity, "配音分享已删除", Toast.LENGTH_SHORT).show()
refreshContent(userApi, token)
},
onFailure = {
deletingHistoryIds.remove(shareId)
Toast.makeText(activity, "删除失败:${it.message}", Toast.LENGTH_SHORT).show()
refreshContent(userApi, token)
},
)
}
}
private fun historyCard(
text: String,
id: String,
onDelete: () -> Unit,
): View {
val isDeleting = id in deletingHistoryIds
return LinearLayout(activity).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
background = rounded(SampleTheme.selected, 8f)
setPadding(12.dp, 10.dp, 10.dp, 10.dp)
layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
).withMargins(0, 0, 0, 8.dp)
addView(TextView(activity).apply {
this.text = text
setTextColor(SampleTheme.textDark)
textSize = 13f
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
})
addView(Button(activity).apply {
setText(if (isDeleting) "删除中..." else "删除")
isAllCaps = false
isEnabled = !isDeleting
setTextColor(Color.WHITE)
textSize = 12f
typeface = Typeface.DEFAULT_BOLD
minWidth = 0
minHeight = 0
minimumWidth = 0
minimumHeight = 0
setPadding(10.dp, 0, 10.dp, 0)
background = rounded(SampleTheme.danger, 8f)
layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
32.dp,
).withMargins(8.dp, 0, 0, 0)
setOnClickListener {
if (!deletingHistoryIds.add(id)) return@setOnClickListener
setText("删除中...")
isEnabled = false
onDelete()
}
})
}
}
private fun courseHeading(title: String): TextView = private fun courseHeading(title: String): TextView =
TextView(activity).apply { TextView(activity).apply {
text = "当前课程:$title" text = "当前课程:$title"