feat: LPT Android 客户端首版(学习会话 + 悬浮窗)
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
package com.catshark.lpt.ui
|
||||
|
||||
import android.os.SystemClock
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableLongStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.catshark.lpt.data.ReportSections
|
||||
import com.catshark.lpt.session.SessionController
|
||||
import com.catshark.lpt.util.formatDuration
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
private const val MISOPERATION_PHRASE = "我误操作导致开启了本次学习"
|
||||
|
||||
@Composable
|
||||
fun SessionScreen(
|
||||
state: SessionController.State,
|
||||
openEndDialog: Boolean,
|
||||
onConsumeOpenEnd: () -> Unit,
|
||||
onTogglePause: () -> Unit,
|
||||
onEnd: (ReportSections) -> Unit,
|
||||
onAbort: (String) -> Unit,
|
||||
onSaveExpectation: (String) -> Unit,
|
||||
onAddFragment: (String) -> Unit,
|
||||
onShowOverlay: () -> Unit,
|
||||
onBack: () -> Unit,
|
||||
) {
|
||||
var nowMs by remember { mutableLongStateOf(SystemClock.elapsedRealtime()) }
|
||||
LaunchedEffect(state.status) {
|
||||
while (true) {
|
||||
delay(1000)
|
||||
nowMs = SystemClock.elapsedRealtime()
|
||||
}
|
||||
}
|
||||
val seconds = state.effectiveSeconds(nowMs)
|
||||
val ongoing = state.status == SessionController.Status.ONGOING
|
||||
|
||||
var expectationDraft by remember { mutableStateOf(state.expectation) }
|
||||
LaunchedEffect(state.expectation) { expectationDraft = state.expectation }
|
||||
|
||||
var fragmentDraft by remember { mutableStateOf("") }
|
||||
var showEnd by remember { mutableStateOf(false) }
|
||||
var showAbort by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(openEndDialog) {
|
||||
if (openEndDialog) {
|
||||
showEnd = true
|
||||
onConsumeOpenEnd()
|
||||
}
|
||||
}
|
||||
|
||||
Column(modifier = Modifier.fillMaxSize()) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
TextButton(onClick = onBack) { Text("‹ 任务") }
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
state.taskName ?: "学习会话",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
Text(
|
||||
state.sessionNum ?: "",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
if (state.busy) CircularProgressIndicator(modifier = Modifier.height(18.dp), strokeWidth = 2.dp)
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(horizontal = 16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
// 计时卡片
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
Text(
|
||||
if (ongoing) "有效学习时间" else "已暂停 · 有效学习时间",
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Spacer(Modifier.height(6.dp))
|
||||
Text(
|
||||
formatDuration(seconds),
|
||||
fontSize = 44.sp,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = if (ongoing) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Spacer(Modifier.height(4.dp))
|
||||
Text(
|
||||
"状态:${statusLabel(state.status)}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 控制按钮
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(10.dp), modifier = Modifier.fillMaxWidth()) {
|
||||
Button(
|
||||
onClick = onTogglePause,
|
||||
enabled = !state.busy,
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
Text(if (ongoing) "暂停" else "继续")
|
||||
}
|
||||
Button(
|
||||
onClick = { showEnd = true },
|
||||
enabled = !state.busy,
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
Text("结束")
|
||||
}
|
||||
}
|
||||
OutlinedButton(
|
||||
onClick = { showAbort = true },
|
||||
enabled = !state.busy && state.fragments.isEmpty(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text(if (state.fragments.isEmpty()) "误操作结束(零数据)" else "误操作结束(已产生碎片,不可用)")
|
||||
}
|
||||
OutlinedButton(
|
||||
onClick = onShowOverlay,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text("显示 / 恢复悬浮窗")
|
||||
}
|
||||
|
||||
// 学习预期
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(14.dp)) {
|
||||
Text("本次学习预期", style = MaterialTheme.typography.titleSmall)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
OutlinedTextField(
|
||||
value = expectationDraft,
|
||||
onValueChange = { expectationDraft = it },
|
||||
label = { Text("开始前写下这次想学会什么") },
|
||||
minLines = 2,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
|
||||
Button(onClick = { onSaveExpectation(expectationDraft) }) { Text("保存预期") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 碎片
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(14.dp)) {
|
||||
Text("知识碎片(${state.fragments.size})", style = MaterialTheme.typography.titleSmall)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
OutlinedTextField(
|
||||
value = fragmentDraft,
|
||||
onValueChange = { fragmentDraft = it },
|
||||
label = { Text("记一条…") },
|
||||
singleLine = true,
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
Spacer(Modifier.height(0.dp))
|
||||
TextButton(
|
||||
onClick = {
|
||||
onAddFragment(fragmentDraft)
|
||||
fragmentDraft = ""
|
||||
},
|
||||
enabled = fragmentDraft.isNotBlank(),
|
||||
) { Text("记录") }
|
||||
}
|
||||
state.fragments.forEachIndexed { index, fragment ->
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Row {
|
||||
Text(
|
||||
"${index + 1}.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Spacer(Modifier.height(0.dp))
|
||||
Text(
|
||||
fragment.content,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
modifier = Modifier.padding(start = 6.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.error != null) {
|
||||
Text(state.error, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
if (state.notice != null) {
|
||||
Text(state.notice, color = MaterialTheme.colorScheme.secondary, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
}
|
||||
|
||||
if (showEnd) {
|
||||
EndSessionDialog(
|
||||
defaultSummary = "",
|
||||
busy = state.busy,
|
||||
onDismiss = { showEnd = false },
|
||||
onConfirm = { sections ->
|
||||
showEnd = false
|
||||
onEnd(sections)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if (showAbort) {
|
||||
AbortSessionDialog(
|
||||
busy = state.busy,
|
||||
onDismiss = { showAbort = false },
|
||||
onConfirm = { phrase ->
|
||||
showAbort = false
|
||||
onAbort(phrase)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EndSessionDialog(
|
||||
defaultSummary: String,
|
||||
busy: Boolean,
|
||||
onDismiss: () -> Unit,
|
||||
onConfirm: (ReportSections) -> Unit,
|
||||
) {
|
||||
var summary by remember { mutableStateOf(defaultSummary) }
|
||||
var verdict by remember { mutableStateOf("") }
|
||||
var free by remember { mutableStateOf("") }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text("结束本次学习") },
|
||||
text = {
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
Text(
|
||||
"三段都可留空,但至少要写一段。全空时会记为「未填写总结」。",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Spacer(Modifier.height(10.dp))
|
||||
OutlinedTextField(
|
||||
value = summary,
|
||||
onValueChange = { summary = it },
|
||||
label = { Text("会话摘要(这次实际学了什么)") },
|
||||
minLines = 2,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.height(10.dp))
|
||||
OutlinedTextField(
|
||||
value = verdict,
|
||||
onValueChange = { verdict = it },
|
||||
label = { Text("预期达成判断") },
|
||||
minLines = 2,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.height(10.dp))
|
||||
OutlinedTextField(
|
||||
value = free,
|
||||
onValueChange = { free = it },
|
||||
label = { Text("自由总结") },
|
||||
minLines = 2,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
Button(
|
||||
onClick = {
|
||||
val sections = if (summary.isBlank() && verdict.isBlank() && free.isBlank()) {
|
||||
ReportSections("", "", "(在手机上结束了本次学习,未填写总结)")
|
||||
} else {
|
||||
ReportSections(summary, verdict, free)
|
||||
}
|
||||
onConfirm(sections)
|
||||
},
|
||||
enabled = !busy,
|
||||
) { Text("确认结束") }
|
||||
},
|
||||
dismissButton = { TextButton(onClick = onDismiss) { Text("取消") } },
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AbortSessionDialog(
|
||||
busy: Boolean,
|
||||
onDismiss: () -> Unit,
|
||||
onConfirm: (String) -> Unit,
|
||||
) {
|
||||
var phrase by remember { mutableStateOf("") }
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text("误操作结束") },
|
||||
text = {
|
||||
Column {
|
||||
Text(
|
||||
"此操作会删除本次会话与学习预期,不产生碎片和报告,且不可恢复。",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
)
|
||||
Spacer(Modifier.height(10.dp))
|
||||
Text("请输入确认语:$MISOPERATION_PHRASE", style = MaterialTheme.typography.bodySmall)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
OutlinedTextField(
|
||||
value = phrase,
|
||||
onValueChange = { phrase = it },
|
||||
label = { Text("确认语") },
|
||||
singleLine = true,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
Button(
|
||||
onClick = { onConfirm(phrase) },
|
||||
enabled = !busy && phrase.trim() == MISOPERATION_PHRASE,
|
||||
) { Text("确认放弃") }
|
||||
},
|
||||
dismissButton = { TextButton(onClick = onDismiss) { Text("取消") } },
|
||||
)
|
||||
}
|
||||
|
||||
private fun statusLabel(status: SessionController.Status): String = when (status) {
|
||||
SessionController.Status.ONGOING -> "进行中"
|
||||
SessionController.Status.PAUSED -> "已暂停"
|
||||
SessionController.Status.ENDED -> "已结束"
|
||||
SessionController.Status.NONE -> "无"
|
||||
}
|
||||
Reference in New Issue
Block a user