真机(Redmi K30 5G / Android 12 / MIUI)验证后修: - 登录失效不再卡死:任意请求返回 401 时自动退出登录、停掉前台服务,并在登录页说明「登录已失效,请重新登录」;网络/HTTP 层错误统一转成可读文案,不再直接抛「HTTP 401」 - 会话错误条不再盖住任务页标题:从叠在最上层改成正常布局占位(叠层只留给会话冲突弹窗) - 会话不再回不去:冲突出口处理后显式跳回会话页(hasSession 一直是 true,靠状态变化不会触发导航,原本会导致新会话的预期门禁被绕过);任务页在有进行中会话时新增「有一次学习正在进行 / 回到会话」入口 - 结束弹窗复用已缓存的报告草稿,重开不再白跑一次 AI
372 lines
14 KiB
Kotlin
372 lines
14 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.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) }
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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,
|
|
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 = { container.session.saveExpectation(it) },
|
|
onAddFragment = { container.session.addFragment(it) },
|
|
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 = { container.updateBaseUrl(it) },
|
|
onLogout = {
|
|
StudySessionService.stop(context)
|
|
container.logout()
|
|
},
|
|
onBack = { screen = Screen.Tasks },
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 会话-05:别的任务上还有未结束会话时,先让用户选出口
|
|
sessionState.conflict?.let {
|
|
SessionConflictDialog(
|
|
state = sessionState,
|
|
onAction = { action ->
|
|
// 处理完必须显式回到会话页:hasSession 一直是 true,靠状态变化不会触发导航
|
|
container.session.resolveConflict(action) {
|
|
StudySessionService.start(context)
|
|
screen = Screen.Session
|
|
}
|
|
},
|
|
onDismiss = { container.session.dismissConflict() },
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 会话-05 的出口选择弹窗:不能直接开始新任务,必须先说清旧会话怎么办。
|
|
* 三个出口对应 lpt-docs「离开一个未结束的会话」:前往继续 / 结束它并开始 / 放弃它。
|
|
*/
|
|
@Composable
|
|
private fun SessionConflictDialog(
|
|
state: SessionController.State,
|
|
onAction: (SessionController.ConflictAction) -> Unit,
|
|
onDismiss: () -> Unit,
|
|
) {
|
|
val conflict = state.conflict ?: return
|
|
val busy = state.conflictBusy
|
|
AlertDialog(
|
|
onDismissRequest = { if (busy == null) 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 != null) {
|
|
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 = { onAction(SessionController.ConflictAction.CONTINUE) },
|
|
enabled = busy == null,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) { Text("前往继续") }
|
|
OutlinedButton(
|
|
onClick = { onAction(SessionController.ConflictAction.END_AND_START) },
|
|
enabled = busy == null,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) { Text("结束它并开始新任务") }
|
|
OutlinedButton(
|
|
onClick = { onAction(SessionController.ConflictAction.ABORT) },
|
|
enabled = busy == null,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) { Text("放弃该会话(零数据)") }
|
|
TextButton(
|
|
onClick = onDismiss,
|
|
enabled = busy == null,
|
|
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()
|
|
}
|
|
}
|