133 lines
4.9 KiB
Kotlin
133 lines
4.9 KiB
Kotlin
package com.catshark.lpt.ui
|
|
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
import androidx.compose.foundation.layout.Column
|
|
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.text.KeyboardOptions
|
|
import androidx.compose.foundation.verticalScroll
|
|
import androidx.compose.material3.Button
|
|
import androidx.compose.material3.CircularProgressIndicator
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.OutlinedTextField
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.material3.TextButton
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
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.input.KeyboardType
|
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
@Composable
|
|
fun LoginScreen(
|
|
initialBaseUrl: String,
|
|
onLogin: (username: String, password: String, baseUrl: String, onResult: (String?) -> Unit) -> Unit,
|
|
) {
|
|
var username by remember { mutableStateOf("") }
|
|
var password by remember { mutableStateOf("") }
|
|
var baseUrl by remember { mutableStateOf(initialBaseUrl) }
|
|
var showAdvanced by remember { mutableStateOf(false) }
|
|
var loading by remember { mutableStateOf(false) }
|
|
var error by remember { mutableStateOf<String?>(null) }
|
|
|
|
Column(
|
|
modifier = Modifier
|
|
.fillMaxSize()
|
|
.verticalScroll(rememberScrollState())
|
|
.padding(24.dp),
|
|
verticalArrangement = Arrangement.Center,
|
|
) {
|
|
Text("LPT 学习", style = MaterialTheme.typography.headlineMedium)
|
|
Spacer(Modifier.height(4.dp))
|
|
Text(
|
|
"移动端学习会话 · 悬浮窗计时",
|
|
style = MaterialTheme.typography.bodyMedium,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
Spacer(Modifier.height(24.dp))
|
|
|
|
OutlinedTextField(
|
|
value = username,
|
|
onValueChange = { username = it },
|
|
label = { Text("账号") },
|
|
singleLine = true,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
)
|
|
Spacer(Modifier.height(12.dp))
|
|
OutlinedTextField(
|
|
value = password,
|
|
onValueChange = { password = it },
|
|
label = { Text("密码") },
|
|
singleLine = true,
|
|
visualTransformation = PasswordVisualTransformation(),
|
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
|
|
modifier = Modifier.fillMaxWidth(),
|
|
)
|
|
|
|
if (showAdvanced) {
|
|
Spacer(Modifier.height(12.dp))
|
|
OutlinedTextField(
|
|
value = baseUrl,
|
|
onValueChange = { baseUrl = it },
|
|
label = { Text("API 地址") },
|
|
singleLine = true,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
)
|
|
} else {
|
|
TextButton(onClick = { showAdvanced = true }) { Text("高级:修改 API 地址") }
|
|
}
|
|
|
|
if (error != null) {
|
|
Spacer(Modifier.height(8.dp))
|
|
Text(error!!, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
|
|
}
|
|
|
|
Spacer(Modifier.height(20.dp))
|
|
Button(
|
|
onClick = {
|
|
error = null
|
|
loading = true
|
|
onLogin(username.trim(), password, baseUrl.trim()) { message ->
|
|
loading = false
|
|
error = message
|
|
}
|
|
},
|
|
enabled = !loading && username.isNotBlank() && password.isNotBlank(),
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) {
|
|
if (loading) {
|
|
CircularProgressIndicator(
|
|
modifier = Modifier.height(18.dp),
|
|
strokeWidth = 2.dp,
|
|
color = MaterialTheme.colorScheme.onPrimary,
|
|
)
|
|
} else {
|
|
Text("登录")
|
|
}
|
|
}
|
|
Spacer(Modifier.height(8.dp))
|
|
Text(
|
|
"提示:dev 环境默认 https://lpt-dev.cat-shark.xyz/api",
|
|
style = MaterialTheme.typography.bodySmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
Spacer(Modifier.height(24.dp))
|
|
Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxWidth()) {
|
|
Text(
|
|
"LPT",
|
|
style = MaterialTheme.typography.labelSmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
}
|
|
}
|
|
}
|