内容与 dev 完全一致,只有 ApiClient.DEFAULT_BASE_URL 指向生产环境(流水线第一步会逐字校验这一行)。 包含:会话冲突出口(只留前往继续/取消)、学习预期必填、服务端收敛提示、报告草稿与报告查看、 悬浮窗只做计时/记碎片/暂停继续、前台服务自恢复竞态修复、加载动画变形修复、动作反馈与防连点。
410 lines
16 KiB
Kotlin
410 lines
16 KiB
Kotlin
package com.catshark.lpt
|
|
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.provider.Settings
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
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.fillMaxSize
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.material3.AlertDialog
|
|
import androidx.compose.material3.Button
|
|
import androidx.compose.material3.CircularProgressIndicator
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.OutlinedButton
|
|
import androidx.compose.material3.SnackbarDuration
|
|
import androidx.compose.material3.SnackbarHost
|
|
import androidx.compose.material3.SnackbarHostState
|
|
import androidx.compose.material3.Surface
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.material3.TextButton
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
import androidx.compose.runtime.collectAsState
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.rememberCoroutineScope
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import androidx.compose.ui.unit.dp
|
|
import com.catshark.lpt.data.TaskItem
|
|
import com.catshark.lpt.service.StudySessionService
|
|
import com.catshark.lpt.session.SessionController
|
|
import com.catshark.lpt.ui.LoginScreen
|
|
import com.catshark.lpt.ui.PermissionScreen
|
|
import com.catshark.lpt.ui.ReportsScreen
|
|
import com.catshark.lpt.ui.SessionScreen
|
|
import com.catshark.lpt.ui.SettingsScreen
|
|
import com.catshark.lpt.ui.TaskListScreen
|
|
import com.catshark.lpt.ui.theme.LptTheme
|
|
import kotlinx.coroutines.launch
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
|
|
private val openEndRequest = mutableStateOf(false)
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
handleIntent(intent)
|
|
setContent {
|
|
LptTheme {
|
|
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
|
|
AppRoot(openEndRequest)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onNewIntent(intent: Intent) {
|
|
super.onNewIntent(intent)
|
|
handleIntent(intent)
|
|
}
|
|
|
|
private fun handleIntent(intent: Intent?) {
|
|
if (intent?.getBooleanExtra(EXTRA_OPEN_END, false) == true) {
|
|
openEndRequest.value = true
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
const val EXTRA_OPEN_END = "open_end"
|
|
}
|
|
}
|
|
|
|
private enum class Screen { Tasks, Session, Reports, Permissions, Settings }
|
|
|
|
@Composable
|
|
private fun AppRoot(openEndRequest: androidx.compose.runtime.MutableState<Boolean>) {
|
|
val container = (LocalContext.current.applicationContext as LptApp).container
|
|
val context = LocalContext.current
|
|
val scope = rememberCoroutineScope()
|
|
|
|
val ready by container.ready.collectAsState()
|
|
val loggedIn by container.loggedIn.collectAsState()
|
|
val username by container.username.collectAsState()
|
|
val baseUrl by container.baseUrl.collectAsState()
|
|
val sessionState by container.session.state.collectAsState()
|
|
val authNotice by container.authNotice.collectAsState()
|
|
val openEnd by openEndRequest
|
|
|
|
var screen by remember { mutableStateOf(Screen.Tasks) }
|
|
var tasks by remember { mutableStateOf<List<TaskItem>>(emptyList()) }
|
|
var tasksLoading by remember { mutableStateOf(false) }
|
|
var tasksError by remember { mutableStateOf<String?>(null) }
|
|
var reportTask by remember { mutableStateOf<TaskItem?>(null) }
|
|
|
|
// 保存/开始/结束这类动作必须给出即时反馈,不能只靠页面底部的静态文案
|
|
val snackbarHostState = remember { SnackbarHostState() }
|
|
// 同一句话在 1.5 秒内重复出现就丢掉:连点不应该看到两条一模一样的提示
|
|
val lastToast = remember { java.util.concurrent.atomic.AtomicReference<Pair<String, Long>?>(null) }
|
|
fun toast(message: String, long: Boolean = false) {
|
|
val now = System.currentTimeMillis()
|
|
val previous = lastToast.get()
|
|
if (previous != null && previous.first == message && now - previous.second < 1_500) return
|
|
lastToast.set(message to now)
|
|
scope.launch {
|
|
snackbarHostState.showSnackbar(
|
|
message = message,
|
|
withDismissAction = false,
|
|
duration = if (long) SnackbarDuration.Long else SnackbarDuration.Short,
|
|
)
|
|
}
|
|
}
|
|
|
|
fun refreshTasks() {
|
|
tasksLoading = true
|
|
tasksError = null
|
|
scope.launch {
|
|
container.repository.tasks()
|
|
.onSuccess {
|
|
tasks = it
|
|
tasksLoading = false
|
|
}
|
|
.onFailure {
|
|
tasksError = it.message ?: "加载任务失败"
|
|
tasksLoading = false
|
|
}
|
|
}
|
|
}
|
|
|
|
LaunchedEffect(loggedIn) {
|
|
if (loggedIn) {
|
|
refreshTasks()
|
|
if (!Settings.canDrawOverlays(context)) screen = Screen.Permissions
|
|
} else {
|
|
tasks = emptyList()
|
|
screen = Screen.Tasks
|
|
}
|
|
}
|
|
|
|
LaunchedEffect(sessionState.hasSession) {
|
|
if (sessionState.hasSession) {
|
|
screen = Screen.Session
|
|
} else if (screen == Screen.Session) {
|
|
screen = Screen.Tasks
|
|
}
|
|
}
|
|
|
|
// 会话存在就把前台服务拉起来(重装/重启/被系统杀掉后恢复浮窗)
|
|
LaunchedEffect(ready, loggedIn, sessionState.hasSession) {
|
|
if (ready && loggedIn && sessionState.hasSession) {
|
|
StudySessionService.start(context)
|
|
}
|
|
}
|
|
|
|
// 会话层的提示与失败统一弹 snackbar(错误停留久一点)
|
|
LaunchedEffect(sessionState.notice) {
|
|
sessionState.notice?.let {
|
|
toast(it)
|
|
container.session.consumeNotice()
|
|
}
|
|
}
|
|
LaunchedEffect(sessionState.error) {
|
|
sessionState.error?.let {
|
|
toast(it, long = true)
|
|
container.session.consumeError()
|
|
}
|
|
}
|
|
|
|
Box(modifier = Modifier.fillMaxSize()) {
|
|
Column(modifier = Modifier.fillMaxSize()) {
|
|
// 不在会话页时,会话层的失败也要让用户看到(用正常布局占位,不遮住下面的标题)
|
|
if (screen != Screen.Session) {
|
|
sessionState.error?.let { message ->
|
|
ErrorBar(message = message, onDismiss = { container.session.consumeError() })
|
|
}
|
|
}
|
|
|
|
Box(modifier = Modifier.weight(1f)) {
|
|
when {
|
|
!ready -> LoadingScreen()
|
|
|
|
!loggedIn -> LoginScreen(
|
|
initialBaseUrl = baseUrl,
|
|
notice = authNotice,
|
|
) { user, password, url, onResult ->
|
|
container.login(user, password, url, onResult)
|
|
}
|
|
|
|
else -> when (screen) {
|
|
Screen.Tasks -> TaskListScreen(
|
|
tasks = tasks,
|
|
loading = tasksLoading,
|
|
error = tasksError,
|
|
username = username,
|
|
onRefresh = { refreshTasks() },
|
|
onStart = { task ->
|
|
container.session.startOrContinue(task.taskNum) {
|
|
StudySessionService.start(context)
|
|
}
|
|
},
|
|
onOpenReports = { task ->
|
|
reportTask = task
|
|
screen = Screen.Reports
|
|
},
|
|
hasActiveSession = sessionState.hasSession,
|
|
starting = sessionState.busy,
|
|
onOpenSession = { screen = Screen.Session },
|
|
onOpenSettings = { screen = Screen.Settings },
|
|
onOpenPermissions = { screen = Screen.Permissions },
|
|
onLogout = {
|
|
StudySessionService.stop(context)
|
|
container.logout()
|
|
},
|
|
)
|
|
|
|
Screen.Session -> SessionScreen(
|
|
state = sessionState,
|
|
openEndDialog = openEnd,
|
|
onConsumeOpenEnd = { openEndRequest.value = false },
|
|
onTogglePause = {
|
|
if (sessionState.status == SessionController.Status.ONGOING) {
|
|
container.session.pause()
|
|
} else {
|
|
container.session.resume()
|
|
}
|
|
},
|
|
onEnd = { sections ->
|
|
container.session.end(sections) {
|
|
StudySessionService.stop(context)
|
|
refreshTasks()
|
|
}
|
|
},
|
|
onAbort = { phrase ->
|
|
container.session.abort(phrase) {
|
|
StudySessionService.stop(context)
|
|
refreshTasks()
|
|
}
|
|
},
|
|
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 },
|
|
)
|
|
|
|
Screen.Reports -> {
|
|
val task = reportTask
|
|
if (task == null) {
|
|
LaunchedEffect(Unit) { screen = Screen.Tasks }
|
|
} else {
|
|
ReportsScreen(
|
|
taskName = task.taskName,
|
|
onLoadReports = { page -> container.repository.reports(task.taskNum, page) },
|
|
onLoadFragments = { sessionNum -> container.repository.allFragments(sessionNum) },
|
|
onBack = { screen = Screen.Tasks },
|
|
)
|
|
}
|
|
}
|
|
|
|
Screen.Permissions -> PermissionScreen(onBack = { screen = Screen.Tasks })
|
|
|
|
Screen.Settings -> SettingsScreen(
|
|
baseUrl = baseUrl,
|
|
onSave = { url, done ->
|
|
container.updateBaseUrl(url) { error ->
|
|
toast(error ?: "API 地址已保存", long = error != null)
|
|
done(error)
|
|
}
|
|
},
|
|
onLogout = {
|
|
StudySessionService.stop(context)
|
|
container.logout()
|
|
},
|
|
onBack = { screen = Screen.Tasks },
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 会话-05:别的任务上还有未结束会话时,只能先去把它处理完
|
|
sessionState.conflict?.let {
|
|
SessionConflictDialog(
|
|
state = sessionState,
|
|
onContinue = {
|
|
// hasSession 一直是 true,靠状态变化不会触发导航,这里必须显式回会话页
|
|
container.session.continueConflict {
|
|
StudySessionService.start(context)
|
|
screen = Screen.Session
|
|
}
|
|
},
|
|
onDismiss = { container.session.dismissConflict() },
|
|
)
|
|
}
|
|
|
|
SnackbarHost(
|
|
hostState = snackbarHostState,
|
|
modifier = Modifier.align(Alignment.BottomCenter),
|
|
)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 会话-05:别的任务上还有未结束会话时的提示。
|
|
*
|
|
* 这里故意只给「前往继续」与「取消」:结束与误操作结束都必须在那个会话自己的页面里由用户完成,
|
|
* 列表页不允许一句话就把未完成的会话结掉或丢掉。
|
|
*/
|
|
@Composable
|
|
private fun SessionConflictDialog(
|
|
state: SessionController.State,
|
|
onContinue: () -> Unit,
|
|
onDismiss: () -> Unit,
|
|
) {
|
|
val conflict = state.conflict ?: return
|
|
val busy = state.conflictBusy
|
|
AlertDialog(
|
|
onDismissRequest = { if (!busy) onDismiss() },
|
|
title = { Text("还有未结束的学习会话") },
|
|
text = {
|
|
Column {
|
|
Text(
|
|
"你已经有一个未结束的学习会话:「${conflict.taskName ?: conflict.taskNum}」",
|
|
style = MaterialTheme.typography.bodyMedium,
|
|
)
|
|
Text(
|
|
conflict.sessionNum,
|
|
style = MaterialTheme.typography.labelSmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
if (conflict.autoConverged) {
|
|
Text(
|
|
conflict.systemMessage ?: "这个会话看起来是忘记结束了,服务端已按一个番茄钟自动截止。",
|
|
style = MaterialTheme.typography.bodySmall,
|
|
color = MaterialTheme.colorScheme.secondary,
|
|
)
|
|
}
|
|
Text(
|
|
"需要先把它处理完才能开始新任务。结束或误操作结束都请在那个会话的页面里操作," +
|
|
"这里不提供「放弃」这个入口。",
|
|
style = MaterialTheme.typography.bodySmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
if (busy) {
|
|
Row(verticalAlignment = Alignment.CenterVertically) {
|
|
CircularProgressIndicator(modifier = Modifier.padding(end = 8.dp), strokeWidth = 2.dp)
|
|
Text("正在打开那个会话…", style = MaterialTheme.typography.bodySmall)
|
|
}
|
|
}
|
|
state.conflictError?.let {
|
|
Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
|
|
}
|
|
}
|
|
},
|
|
confirmButton = {
|
|
Column(modifier = Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
|
Button(
|
|
onClick = onContinue,
|
|
enabled = !busy,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) { Text("前往继续") }
|
|
TextButton(
|
|
onClick = onDismiss,
|
|
enabled = !busy,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) { Text("取消") }
|
|
}
|
|
},
|
|
)
|
|
}
|
|
|
|
@Composable
|
|
private fun ErrorBar(message: String, onDismiss: () -> Unit) {
|
|
Surface(
|
|
color = MaterialTheme.colorScheme.errorContainer,
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(12.dp),
|
|
shape = MaterialTheme.shapes.medium,
|
|
) {
|
|
Row(
|
|
modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp),
|
|
verticalAlignment = Alignment.CenterVertically,
|
|
) {
|
|
Text(
|
|
message,
|
|
style = MaterialTheme.typography.bodySmall,
|
|
color = MaterialTheme.colorScheme.onErrorContainer,
|
|
modifier = Modifier.weight(1f),
|
|
)
|
|
TextButton(onClick = onDismiss) { Text("关闭") }
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
private fun LoadingScreen() {
|
|
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
|
CircularProgressIndicator()
|
|
}
|
|
}
|