fix: 所有发请求的按钮加防连点
- SessionController 新增在途标记 expectationSaving / fragmentAdding,入口早退 + 按钮禁用(文案改「保存中…」「记录中…」) - 记碎片改为「成功才清空输入、失败保留」;浮窗拉起的碎片输入页同样防连点,且写入成功才关弹窗 - 设置页保存 API 地址期间禁用按钮;登录按钮补显式重入保护 - 任务列表开始会话在途时禁用卡片(文案改「开始中…」),不再能连点重复开始
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
- **悬浮窗只做三件事:展示倒计时、记一条碎片、暂停/继续**。结束会话、写报告、改预期、看历史一律在 App 页面内完成;不要往浮窗里加按钮或面板(它常驻在其他 App 之上,每多一个控件都是常态干扰)。
|
||||
- **开始新任务前必须先处理未结束会话**。`startOrContinue` 先查 `GET /study-sessions/active?excludeTaskNum=`,有冲突就只设置 `State.conflict`(UI 只给「前往继续 / 取消」);不得绕过这一步直接开始,也不得在列表页代替用户结束或放弃那个会话——结束与误操作结束必须在会话页里由用户自己做。
|
||||
- **写操作必须有可见反馈**。保存预期、保存 API 地址、开始/暂停/结束、记碎片等动作都要给 snackbar 提示(失败文案要写清楚为什么);不要只靠页面底部一行静态文案。
|
||||
- **发请求的按钮必须防连点**。在中间层(`SessionController` / `AppContainer`)加“在途”标记并在入口早退,同时把按钮 `enabled` 绑到该标记(文案可改「保存中…」);涉及写入的动作要“成功才清输入、失败保留输入”,不能让一次连点变成两条碎片。
|
||||
- **学习预期不可为空**。`State.expectationRequired` 为真时用不可关闭的弹窗挡住(只能填预期或误操作结束)。
|
||||
- **前台服务停之前要确认“确实没有会话”**。对账是异步的,`State.resolved` 为 false 时看到的空状态是“还不知道”,不能据此 `stopSelf()`。
|
||||
- **计时不得用 tick 累加**。显示值一律走 `State.effectiveSeconds(nowMs)`,基准取服务端 `effectiveTime`,锚点取 `SystemClock.elapsedRealtime()`。
|
||||
|
||||
@@ -20,7 +20,7 @@ LPT 的学习计时原本只能在电脑浏览器里进行;而真实的学习
|
||||
| 登录 | `POST /login` 拿 token,DataStore 持久化,OkHttp 拦截器注入 `satoken` 请求头;服务端返回 401 时自动退出登录并在登录页说明原因 |
|
||||
| 权限引导 | 悬浮窗权限 / 通知权限 / 电池优化白名单 / MIUI 自启动与后台弹出 |
|
||||
| 任务列表 | `GET /tasks`,每个任务带「报告」入口;有进行中的会话时顶部带「回到会话」入口 |
|
||||
| 操作反馈 | 保存预期、保存 API 地址、开始/暂停/结束、记碎片等动作都有 snackbar 提示(成功短、失败长) |
|
||||
| 操作反馈 | 保存预期、保存 API 地址、开始/暂停/结束、记碎片等动作都有 snackbar 提示(成功短、失败长);所有发请求的按钮在途时禁用,防连点重复提交 |
|
||||
| 学习会话 | 开始/继续、暂停、结束、误操作结束(零数据)、学习预期(必填)、知识碎片、结束报告三段 |
|
||||
| 会话冲突出口 | 开始新任务前先查其他任务上的未结束会话;有冲突时只给「前往继续 / 取消」,结束与误操作结束必须回到那个会话的页面里自己做 |
|
||||
| 悬浮窗 | 前台服务(`specialUse`)托管,跨应用常驻;**只做三件事**:展示倒计时 / 记一条碎片 / 暂停继续 |
|
||||
|
||||
@@ -205,6 +205,7 @@ private fun AppRoot(openEndRequest: androidx.compose.runtime.MutableState<Boolea
|
||||
screen = Screen.Reports
|
||||
},
|
||||
hasActiveSession = sessionState.hasSession,
|
||||
starting = sessionState.busy,
|
||||
onOpenSession = { screen = Screen.Session },
|
||||
onOpenSettings = { screen = Screen.Settings },
|
||||
onOpenPermissions = { screen = Screen.Permissions },
|
||||
@@ -237,8 +238,8 @@ private fun AppRoot(openEndRequest: androidx.compose.runtime.MutableState<Boolea
|
||||
refreshTasks()
|
||||
}
|
||||
},
|
||||
onSaveExpectation = { container.session.saveExpectation(it) },
|
||||
onAddFragment = { container.session.addFragment(it) },
|
||||
onSaveExpectation = { text, onResult -> container.session.saveExpectation(text, onResult) },
|
||||
onAddFragment = { text, onResult -> container.session.addFragment(text, onResult) },
|
||||
onShowOverlay = { StudySessionService.showOverlay(context) },
|
||||
onGenerateDraft = { container.session.generateReportDraft() },
|
||||
onBack = { screen = Screen.Tasks },
|
||||
@@ -262,9 +263,10 @@ private fun AppRoot(openEndRequest: androidx.compose.runtime.MutableState<Boolea
|
||||
|
||||
Screen.Settings -> SettingsScreen(
|
||||
baseUrl = baseUrl,
|
||||
onSave = { url ->
|
||||
onSave = { url, done ->
|
||||
container.updateBaseUrl(url) { error ->
|
||||
toast(error ?: "API 地址已保存", long = error != null)
|
||||
done(error)
|
||||
}
|
||||
},
|
||||
onLogout = {
|
||||
|
||||
@@ -52,7 +52,11 @@ class SessionController(
|
||||
val expectation: String = "",
|
||||
/** 学习预期是否已经从服务端取回过(没取回前不要弹必填弹窗) */
|
||||
val expectationLoaded: Boolean = false,
|
||||
/** 预期保存请求在途:按钮据此禁用,防连点重复提交 */
|
||||
val expectationSaving: Boolean = false,
|
||||
val fragments: List<FragmentItem> = emptyList(),
|
||||
/** 记碎片请求在途 */
|
||||
val fragmentAdding: Boolean = false,
|
||||
val busy: Boolean = false,
|
||||
/** 是否已经与服务端对过账:前台服务据此判断「真没有会话」还是「还不知道」 */
|
||||
val resolved: Boolean = false,
|
||||
@@ -179,34 +183,59 @@ class SessionController(
|
||||
}
|
||||
}
|
||||
|
||||
fun saveExpectation(text: String, onSaved: () -> Unit = {}) {
|
||||
/**
|
||||
* 保存学习预期。在途时再点直接返回,并在成功时回调;失败文案走 State.error(由页面弹提示)。
|
||||
*/
|
||||
fun saveExpectation(text: String, onResult: (String?) -> Unit = {}) {
|
||||
val num = _state.value.sessionNum ?: return
|
||||
if (_state.value.expectationSaving) return
|
||||
val value = text.trim()
|
||||
if (value.isBlank()) {
|
||||
_state.update { it.copy(error = "学习预期不可为空") }
|
||||
return
|
||||
}
|
||||
_state.update { it.copy(expectationSaving = true) }
|
||||
scope.launch {
|
||||
repo.putExpectation(num, value)
|
||||
.onSuccess {
|
||||
_state.update { it.copy(expectation = value, expectationLoaded = true, notice = "学习预期已保存") }
|
||||
onSaved()
|
||||
_state.update {
|
||||
it.copy(expectation = value, expectationLoaded = true, notice = "学习预期已保存")
|
||||
}
|
||||
onResult(null)
|
||||
}
|
||||
.onFailure { e -> _state.update { it.copy(error = e.message ?: "保存学习预期失败") } }
|
||||
.onFailure { e ->
|
||||
val message = e.message ?: "保存学习预期失败"
|
||||
_state.update { it.copy(error = message) }
|
||||
onResult(message)
|
||||
}
|
||||
_state.update { it.copy(expectationSaving = false) }
|
||||
}
|
||||
}
|
||||
|
||||
fun addFragment(content: String) {
|
||||
/**
|
||||
* 记一条知识碎片。在途时再点直接返回,避免一次输入变成两条碎片;
|
||||
* onResult 给 null 表示已写入,UI 可以据此清空输入框(失败时保留输入)。
|
||||
*/
|
||||
fun addFragment(content: String, onResult: (String?) -> Unit = {}) {
|
||||
val num = _state.value.sessionNum ?: return
|
||||
if (content.isBlank()) return
|
||||
val value = content.trim()
|
||||
if (value.isBlank()) return
|
||||
if (_state.value.fragmentAdding) return
|
||||
_state.update { it.copy(fragmentAdding = true) }
|
||||
scope.launch {
|
||||
repo.createFragment(num, content)
|
||||
repo.createFragment(num, value)
|
||||
.onSuccess {
|
||||
repo.fragments(num).onSuccess { list ->
|
||||
_state.update { it.copy(fragments = list, notice = "已记录一条碎片") }
|
||||
}
|
||||
onResult(null)
|
||||
}
|
||||
.onFailure { e -> _state.update { it.copy(error = e.message ?: "记录碎片失败") } }
|
||||
.onFailure { e ->
|
||||
val message = e.message ?: "记录碎片失败"
|
||||
_state.update { it.copy(error = message) }
|
||||
onResult(message)
|
||||
}
|
||||
_state.update { it.copy(fragmentAdding = false) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,9 +47,8 @@ class FragmentInputActivity : ComponentActivity() {
|
||||
LptTheme {
|
||||
FragmentInputDialog(
|
||||
sessionNum = (application as LptApp).container.session.state.value.sessionNum,
|
||||
onSubmit = { text ->
|
||||
(application as LptApp).container.session.addFragment(text)
|
||||
finish()
|
||||
onSubmit = { text, onResult ->
|
||||
(application as LptApp).container.session.addFragment(text, onResult)
|
||||
},
|
||||
onDismiss = { finish() },
|
||||
)
|
||||
@@ -61,10 +60,13 @@ class FragmentInputActivity : ComponentActivity() {
|
||||
@Composable
|
||||
private fun FragmentInputDialog(
|
||||
sessionNum: String?,
|
||||
onSubmit: (String) -> Unit,
|
||||
onSubmit: (String, (String?) -> Unit) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
var text by remember { mutableStateOf("") }
|
||||
// 提交期间禁用按钮:这里多发一次就是两条重复碎片
|
||||
var submitting by remember { mutableStateOf(false) }
|
||||
var error by remember { mutableStateOf<String?>(null) }
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
val context = LocalContext.current
|
||||
|
||||
@@ -99,18 +101,35 @@ private fun FragmentInputDialog(
|
||||
.focusRequester(focusRequester),
|
||||
)
|
||||
Spacer(Modifier.height(14.dp))
|
||||
if (error != null) {
|
||||
Text(error!!, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
}
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
TextButton(onClick = onDismiss) { Text("取消") }
|
||||
TextButton(onClick = onDismiss, enabled = !submitting) { Text("取消") }
|
||||
Spacer(Modifier.height(0.dp))
|
||||
Button(
|
||||
onClick = { onSubmit(text.trim()) },
|
||||
enabled = text.isNotBlank() && sessionNum != null,
|
||||
onClick = {
|
||||
if (submitting) return@Button
|
||||
submitting = true
|
||||
error = null
|
||||
// 写入成功才关弹窗;失败保留输入并提示,避免碎片静默丢失
|
||||
onSubmit(text.trim()) { message ->
|
||||
if (message == null) {
|
||||
onDismiss()
|
||||
} else {
|
||||
submitting = false
|
||||
error = message
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = !submitting && text.isNotBlank() && sessionNum != null,
|
||||
modifier = Modifier.padding(start = 8.dp),
|
||||
) { Text("保存") }
|
||||
) { Text(if (submitting) "保存中…" else "保存") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +106,7 @@ fun LoginScreen(
|
||||
Spacer(Modifier.height(20.dp))
|
||||
Button(
|
||||
onClick = {
|
||||
if (loading) return@Button
|
||||
error = null
|
||||
loading = true
|
||||
onLogin(username.trim(), password, baseUrl.trim()) { message ->
|
||||
|
||||
@@ -49,8 +49,8 @@ fun SessionScreen(
|
||||
onTogglePause: () -> Unit,
|
||||
onEnd: (ReportSections) -> Unit,
|
||||
onAbort: (String) -> Unit,
|
||||
onSaveExpectation: (String) -> Unit,
|
||||
onAddFragment: (String) -> Unit,
|
||||
onSaveExpectation: (String, (String?) -> Unit) -> Unit,
|
||||
onAddFragment: (String, (String?) -> Unit) -> Unit,
|
||||
onShowOverlay: () -> Unit,
|
||||
onGenerateDraft: () -> Unit,
|
||||
onBack: () -> Unit,
|
||||
@@ -185,7 +185,14 @@ fun SessionScreen(
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
|
||||
Button(onClick = { onSaveExpectation(expectationDraft) }) { Text("保存预期") }
|
||||
if (state.expectationSaving) {
|
||||
CircularProgressIndicator(modifier = Modifier.size(18.dp), strokeWidth = 2.dp)
|
||||
Spacer(Modifier.height(0.dp))
|
||||
}
|
||||
Button(
|
||||
onClick = { onSaveExpectation(expectationDraft) {} },
|
||||
enabled = !state.expectationSaving && expectationDraft.isNotBlank(),
|
||||
) { Text(if (state.expectationSaving) "保存中…" else "保存预期") }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,11 +213,11 @@ fun SessionScreen(
|
||||
Spacer(Modifier.height(0.dp))
|
||||
TextButton(
|
||||
onClick = {
|
||||
onAddFragment(fragmentDraft)
|
||||
fragmentDraft = ""
|
||||
// 成功后清空输入;失败保留,避免手写的碎片白丢
|
||||
onAddFragment(fragmentDraft) { error -> if (error == null) fragmentDraft = "" }
|
||||
},
|
||||
enabled = fragmentDraft.isNotBlank(),
|
||||
) { Text("记录") }
|
||||
enabled = fragmentDraft.isNotBlank() && !state.fragmentAdding,
|
||||
) { Text(if (state.fragmentAdding) "记录中…" else "记录") }
|
||||
}
|
||||
state.fragments.forEachIndexed { index, fragment ->
|
||||
Spacer(Modifier.height(8.dp))
|
||||
@@ -242,8 +249,9 @@ fun SessionScreen(
|
||||
if (state.expectationRequired) {
|
||||
ExpectationGateDialog(
|
||||
busy = state.busy,
|
||||
saving = state.expectationSaving,
|
||||
error = state.error,
|
||||
onSave = onSaveExpectation,
|
||||
onSave = { text -> onSaveExpectation(text) {} },
|
||||
onAbort = onAbort,
|
||||
)
|
||||
}
|
||||
@@ -279,6 +287,7 @@ fun SessionScreen(
|
||||
@Composable
|
||||
private fun ExpectationGateDialog(
|
||||
busy: Boolean,
|
||||
saving: Boolean,
|
||||
error: String?,
|
||||
onSave: (String) -> Unit,
|
||||
onAbort: (String) -> Unit,
|
||||
@@ -338,8 +347,8 @@ private fun ExpectationGateDialog(
|
||||
} else {
|
||||
Button(
|
||||
onClick = { onSave(text) },
|
||||
enabled = !busy && text.isNotBlank(),
|
||||
) { Text("保存并开始") }
|
||||
enabled = !busy && !saving && text.isNotBlank(),
|
||||
) { Text(if (saving) "保存中…" else "保存并开始") }
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
|
||||
@@ -25,11 +25,13 @@ import androidx.compose.ui.unit.dp
|
||||
@Composable
|
||||
fun SettingsScreen(
|
||||
baseUrl: String,
|
||||
onSave: (String) -> Unit,
|
||||
onSave: (String, (String?) -> Unit) -> Unit,
|
||||
onLogout: () -> Unit,
|
||||
onBack: () -> Unit,
|
||||
) {
|
||||
var draft by remember { mutableStateOf(baseUrl) }
|
||||
// 保存期间禁用按钮:地址写入是异步的,不加标记会被连点重复提交
|
||||
var saving by remember { mutableStateOf(false) }
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@@ -55,9 +57,15 @@ fun SettingsScreen(
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Button(
|
||||
onClick = { onSave(draft.trim()) },
|
||||
onClick = {
|
||||
if (!saving) {
|
||||
saving = true
|
||||
onSave(draft.trim()) { saving = false }
|
||||
}
|
||||
},
|
||||
enabled = !saving,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) { Text("保存") }
|
||||
) { Text(if (saving) "保存中…" else "保存") }
|
||||
|
||||
Spacer(Modifier.height(8.dp))
|
||||
TextButton(onClick = onLogout, modifier = Modifier.fillMaxWidth()) {
|
||||
|
||||
@@ -34,6 +34,7 @@ fun TaskListScreen(
|
||||
onStart: (TaskItem) -> Unit,
|
||||
onOpenReports: (TaskItem) -> Unit,
|
||||
hasActiveSession: Boolean,
|
||||
starting: Boolean,
|
||||
onOpenSession: () -> Unit,
|
||||
onOpenSettings: () -> Unit,
|
||||
onOpenPermissions: () -> Unit,
|
||||
@@ -129,7 +130,8 @@ fun TaskListScreen(
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onStart(task) },
|
||||
// 开始会话是网络请求:在途时禁用,防连点重复提交
|
||||
.clickable(enabled = !starting) { onStart(task) },
|
||||
) {
|
||||
Column(modifier = Modifier.padding(14.dp)) {
|
||||
Text(
|
||||
@@ -154,9 +156,9 @@ fun TaskListScreen(
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
Spacer(Modifier.weight(1f))
|
||||
TextButton(onClick = { onOpenReports(task) }) { Text("报告") }
|
||||
TextButton(onClick = { onOpenReports(task) }, enabled = !starting) { Text("报告") }
|
||||
Text(
|
||||
"开始学习 ›",
|
||||
if (starting) "开始中…" else "开始学习 ›",
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user