迁移compose fun到AccountActivity类体

master
pan 4 years ago
parent 169a6973f2
commit dc58940561
  1. 464
      foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt

@ -19,7 +19,6 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.withStyle import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
@ -117,282 +116,263 @@ class AccountActivity : ComponentActivity() {
} }
} }
} }
}
/** /**
* 帐号表单 * 帐号表单
* *
* @param accountViewModel * @param accountViewModel
* @param Action 表单操作区域 * @param Action 表单操作区域
*/ */
@Composable @Composable
private fun Account( private fun Account(
accountViewModel: AccountViewModel = viewModel(), accountViewModel: AccountViewModel = viewModel(),
scaffoldState: ScaffoldState, scaffoldState: ScaffoldState,
route: AccountRoute, route: AccountRoute,
Action: @Composable (isValidForm: Boolean, accountViewModel: AccountViewModel) -> Unit Action: @Composable (isValidForm: Boolean, accountViewModel: AccountViewModel) -> Unit
) {
accountViewModel.route = route
Row(
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxSize()
) { ) {
Column(modifier = Modifier.width(IntrinsicSize.Min)) { accountViewModel.route = route
Text(buildAnnotatedString { Row(
withStyle( horizontalArrangement = Arrangement.Center,
style = MaterialTheme.typography.subtitle1.toSpanStyle() verticalAlignment = Alignment.CenterVertically,
.copy(color = MaterialTheme.colors.primary) modifier = Modifier.fillMaxSize()
) { ) {
append(accountViewModel.name.formValue.value ?: "") Column(modifier = Modifier.width(IntrinsicSize.Min)) {
} Text(buildAnnotatedString {
withStyle(style = MaterialTheme.typography.subtitle1.toSpanStyle()) { withStyle(
append(accountViewModel.welcomeStart) style = MaterialTheme.typography.subtitle1.toSpanStyle()
} .copy(color = MaterialTheme.colors.primary)
withStyle(style = MaterialTheme.typography.subtitle2.toSpanStyle()) { ) {
append(accountViewModel.welcomeEnd) append(accountViewModel.name.formValue.value ?: "")
} }
withStyle( withStyle(style = MaterialTheme.typography.subtitle1.toSpanStyle()) {
style = MaterialTheme.typography.subtitle2.toSpanStyle() append(accountViewModel.welcomeStart)
.copy(color = MaterialTheme.colors.secondary) }
) { withStyle(style = MaterialTheme.typography.subtitle2.toSpanStyle()) {
append(BuildConfig.foreground_app_name) append(accountViewModel.welcomeEnd)
} }
}) withStyle(
style = MaterialTheme.typography.subtitle2.toSpanStyle()
StudentId(checkRepeat = route == AccountRoute.register) .copy(color = MaterialTheme.colors.secondary)
Spacer(modifier = Modifier.height(10.dp)) ) {
append(BuildConfig.foreground_app_name)
}
})
if (route == AccountRoute.register) Name() else Password() StudentId(checkRepeat = route == AccountRoute.register)
Spacer(modifier = Modifier.height(10.dp))
Spacer(modifier = Modifier.height(10.dp))
PasswordTip()
val isValidForm: Boolean by accountViewModel.isValidForm.observeAsState(false)
Action(isValidForm = isValidForm, accountViewModel = accountViewModel)
val snackBarMsg: String by accountViewModel.snackBarMsg.observeAsState("") if (route == AccountRoute.register) Name() else Password()
if (snackBarMsg != "") { Spacer(modifier = Modifier.height(10.dp))
LaunchedEffect(scaffoldState) { PasswordTip()
scaffoldState.snackbarHostState.showSnackbar( val isValidForm: Boolean by accountViewModel.isValidForm.observeAsState(false)
message = snackBarMsg Action(isValidForm = isValidForm, accountViewModel = accountViewModel)
)
accountViewModel.resetRegisterResMsg() val snackBarMsg: String by accountViewModel.snackBarMsg.observeAsState("")
if (snackBarMsg != "") {
LaunchedEffect(scaffoldState) {
scaffoldState.snackbarHostState.showSnackbar(
message = snackBarMsg
)
accountViewModel.resetRegisterResMsg()
}
} }
}
RegisterDialog() RegisterDialog()
}
} }
} }
}
/** /**
* 学号 * 学号
* @param accountViewModel * @param accountViewModel
*/ */
@Composable @Composable
private fun StudentId(accountViewModel: AccountViewModel = viewModel(), checkRepeat: Boolean) { private fun StudentId(accountViewModel: AccountViewModel = viewModel(), checkRepeat: Boolean) {
Column { Column {
val isValidStudentId: Boolean by accountViewModel.isValidStudentId.observeAsState(false) val isValidStudentId: Boolean by accountViewModel.isValidStudentId.observeAsState(false)
BaseTextField( BaseTextField(
form = accountViewModel.studentId, keyboardOptions = KeyboardOptions.Default.copy( form = accountViewModel.studentId, keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number, keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done imeAction = ImeAction.Done
), isError = !isValidStudentId ), isError = !isValidStudentId
) )
if (isValidStudentId) { if (isValidStudentId) {
if (checkRepeat) { if (checkRepeat) {
val isRepeat: Boolean? by accountViewModel.isRepeat.observeAsState(null) val isRepeat: Boolean? by accountViewModel.isRepeat.observeAsState(null)
Logger.i("isRepeat=$isRepeat") Logger.i("isRepeat=$isRepeat")
when (isRepeat) { when (isRepeat) {
null -> AnimationText(text = accountViewModel.checkRegTip) null -> AnimationText(text = accountViewModel.checkRegTip)
true -> true ->
Text(buildAnnotatedString { Text(buildAnnotatedString {
append(accountViewModel.studentId.formDesc) append(accountViewModel.studentId.formDesc)
withStyle( withStyle(
style = MaterialTheme.typography.body1.toSpanStyle().copy( style = MaterialTheme.typography.body1.toSpanStyle().copy(
color = MaterialTheme.colors.error color = MaterialTheme.colors.error
) )
) { ) {
append(accountViewModel.studentId.formValue.value ?: "") append(accountViewModel.studentId.formValue.value ?: "")
} }
append(accountViewModel.registered) append(accountViewModel.registered)
}) })
false -> false ->
Text(buildAnnotatedString { Text(buildAnnotatedString {
append(accountViewModel.studentId.formDesc) append(accountViewModel.studentId.formDesc)
withStyle( withStyle(
style = MaterialTheme.typography.body1.toSpanStyle().copy( style = MaterialTheme.typography.body1.toSpanStyle().copy(
color = MaterialTheme.colors.primary color = MaterialTheme.colors.primary
) )
) { ) {
append(accountViewModel.studentId.formValue.value ?: "") append(accountViewModel.studentId.formValue.value ?: "")
} }
append(accountViewModel.canRegister) append(accountViewModel.canRegister)
}) })
}
} }
} else {
Text(
text = accountViewModel.studentIdFormat,
color = MaterialTheme.colors.error,
style = MaterialTheme.typography.body1
)
} }
} else {
Text(
text = accountViewModel.studentIdFormat,
color = MaterialTheme.colors.error,
style = MaterialTheme.typography.body1
)
}
}
} }
}
/** /**
* 注册弹窗 * 注册弹窗
* *
* @param accountViewModel * @param accountViewModel
*/ */
@Composable @Composable
private fun RegisterDialog(accountViewModel: AccountViewModel = viewModel()) { private fun RegisterDialog(accountViewModel: AccountViewModel = viewModel()) {
val dialogMsg: DialogMessage? by accountViewModel.dialogMsg.observeAsState(null) val dialogMsg: DialogMessage? by accountViewModel.dialogMsg.observeAsState(null)
val message = dialogMsg?.userResDto?.password val message = dialogMsg?.userResDto?.password
if (message?.isNotEmpty() == true) { if (message?.isNotEmpty() == true) {
PasswordDialog(message = message) PasswordDialog(message = message)
}
} }
}
/** /**
* 密码弹窗 * 密码弹窗
* *
* @param accountViewModel * @param accountViewModel
* @param message * @param message
*/ */
@Composable @Composable
private fun PasswordDialog(accountViewModel: AccountViewModel = viewModel(), message: String) { private fun PasswordDialog(accountViewModel: AccountViewModel = viewModel(), message: String) {
val context = LocalContext.current val context = LocalContext.current
val button: @Composable () -> Unit = { val button: @Composable () -> Unit = {
Row( Row(
horizontalArrangement = Arrangement.Center, modifier = Modifier horizontalArrangement = Arrangement.Center, modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(bottom = 10.dp) .padding(bottom = 10.dp)
) {
OutlinedButton(
onClick = { accountViewModel.resetDialogMsg() },
modifier = Modifier.padding(end = 10.dp)
) {
Text(text = accountViewModel.confirmDesc)
}
OutlinedButton(
onClick = {
context.startActivity(Intent(context, AccountActivity::class.java))
},
colors = ButtonDefaults.outlinedButtonColors(
contentColor = MaterialTheme.colors.onBackground
)
) { ) {
Text(text = accountViewModel.backLogin) OutlinedButton(
} onClick = { accountViewModel.resetDialogMsg() },
} modifier = Modifier.padding(end = 10.dp)
} ) {
AlertDialog(onDismissRequest = { accountViewModel.resetDialogMsg() }, Text(text = accountViewModel.confirmDesc)
buttons = button, }
title = { Text(text = accountViewModel.title) }, OutlinedButton(
text = { onClick = {
Text(buildAnnotatedString { context.startActivity(Intent(context, AccountActivity::class.java))
append(accountViewModel.passwordDialogStart) },
withStyle( colors = ButtonDefaults.outlinedButtonColors(
style = MaterialTheme.typography.body1.toSpanStyle() contentColor = MaterialTheme.colors.onBackground
.copy(color = MaterialTheme.colors.secondary) )
) { ) {
append(message) Text(text = accountViewModel.backLogin)
} }
append(accountViewModel.passwordDialogEnd) }
}
AlertDialog(onDismissRequest = { accountViewModel.resetDialogMsg() },
buttons = button,
title = { Text(text = accountViewModel.title) },
text = {
Text(buildAnnotatedString {
append(accountViewModel.passwordDialogStart)
withStyle(
style = MaterialTheme.typography.body1.toSpanStyle()
.copy(color = MaterialTheme.colors.secondary)
) {
append(message)
}
append(accountViewModel.passwordDialogEnd)
})
}) })
}) }
}
/** /**
* 姓名文本框 * 姓名文本框
* @param accountViewModel * @param accountViewModel
*/ */
@Composable @Composable
private fun Name(accountViewModel: AccountViewModel = viewModel()) { private fun Name(accountViewModel: AccountViewModel = viewModel()) {
Column { Column {
val isValidName: Boolean by accountViewModel.isValidName.observeAsState(false) val isValidName: Boolean by accountViewModel.isValidName.observeAsState(false)
BaseTextField(form = accountViewModel.name, isError = !isValidName) BaseTextField(form = accountViewModel.name, isError = !isValidName)
if (!isValidName) { if (!isValidName) {
Text( Text(
text = accountViewModel.nameFormat, text = accountViewModel.nameFormat,
color = MaterialTheme.colors.error color = MaterialTheme.colors.error
) )
}
} }
}
} }
/** /**
* 密码框 * 密码框
* @param accountViewModel * @param accountViewModel
*/ */
@Composable @Composable
private fun Password(accountViewModel: AccountViewModel = viewModel()) { private fun Password(accountViewModel: AccountViewModel = viewModel()) {
Column { Column {
val isValidPwd: Boolean by accountViewModel.isValidPwd.observeAsState(false) val isValidPwd: Boolean by accountViewModel.isValidPwd.observeAsState(false)
BaseTextField( BaseTextField(
form = accountViewModel.password, isError = !isValidPwd, form = accountViewModel.password, isError = !isValidPwd,
keyboardOptions = KeyboardOptions.Default.copy( keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done, imeAction = ImeAction.Done,
keyboardType = KeyboardType.Number, keyboardType = KeyboardType.Number,
), visualTransformation = PasswordVisualTransformation() ), visualTransformation = PasswordVisualTransformation()
)
if (!isValidPwd) {
Text(
text = accountViewModel.passwordFormat,
color = MaterialTheme.colors.error
) )
if (!isValidPwd) {
Text(
text = accountViewModel.passwordFormat,
color = MaterialTheme.colors.error
)
}
} }
} }
}
/** /**
* 提示自动生成密码 * 提示自动生成密码
* *
* @param accountViewModel * @param accountViewModel
*/ */
@Composable @Composable
private fun PasswordTip(accountViewModel: AccountViewModel = viewModel()) { private fun PasswordTip(accountViewModel: AccountViewModel = viewModel()) {
if (accountViewModel.isValidForm.value == true) { if (accountViewModel.isValidForm.value == true) {
Text( Text(
text = accountViewModel.passwordTip, color = MaterialTheme.colors.primary, text = accountViewModel.passwordTip, color = MaterialTheme.colors.primary,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) )
}
} }
} }
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
CSAMSTheme {
Row(
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxSize()
) {
Column(modifier = Modifier.width(IntrinsicSize.Min)) {
val model: AccountViewModel = viewModel()
StudentId(model, false)
Spacer(modifier = Modifier.height(10.dp))
}
}
}
}

Loading…
Cancel
Save