From 18b9ef396ff6d7d72b65c64b342749550cfef6f5 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Mon, 24 May 2021 12:07:25 +0800 Subject: [PATCH] backup --- foreground/src/main/java/com/gyf/csams/Api.kt | 6 +++ .../main/java/com/gyf/csams/InitActivity.kt | 31 ++++++++----- .../main/java/com/gyf/csams/InitViewModel.kt | 29 ++++++++++--- .../csams/account/model/AccountViewModel.kt | 14 ++++-- .../gyf/csams/account/ui/AccountActivity.kt | 35 ++++++++------- .../csams/activity/model/ApplyActViewModel.kt | 43 +++++++++++-------- .../java/com/gyf/lib/uikit/BaseTextField.kt | 8 +--- .../main/java/com/gyf/lib/uikit/MainFrame.kt | 14 ++++++ .../main/java/com/gyf/lib/uikit/Snackbar.kt | 5 ++- .../main/java/com/gyf/lib/util/HttpUtil.kt | 5 ++- 10 files changed, 126 insertions(+), 64 deletions(-) diff --git a/foreground/src/main/java/com/gyf/csams/Api.kt b/foreground/src/main/java/com/gyf/csams/Api.kt index 5c82c01..97447d8 100644 --- a/foreground/src/main/java/com/gyf/csams/Api.kt +++ b/foreground/src/main/java/com/gyf/csams/Api.kt @@ -5,6 +5,12 @@ interface UrlPath { fun build(): String } +enum class TestApi(val path: String) : UrlPath { + Test("test"); + + override fun build(): String = "/api/${this.path}" +} + /** * 帐号接口 * diff --git a/foreground/src/main/java/com/gyf/csams/InitActivity.kt b/foreground/src/main/java/com/gyf/csams/InitActivity.kt index 890ced6..0b51267 100644 --- a/foreground/src/main/java/com/gyf/csams/InitActivity.kt +++ b/foreground/src/main/java/com/gyf/csams/InitActivity.kt @@ -7,11 +7,14 @@ import androidx.activity.compose.setContent import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.Alignment import androidx.compose.ui.platform.LocalContext import androidx.lifecycle.viewmodel.compose.viewModel import com.gyf.csams.account.ui.AccountActivity import com.gyf.csams.main.ui.MainActivity import com.gyf.csams.uikit.AnimationText +import com.gyf.lib.uikit.Body +import com.gyf.lib.uikit.MainBoxFrame import com.orhanobut.logger.Logger class InitActivity : ComponentActivity() { @@ -22,19 +25,25 @@ class InitActivity : ComponentActivity() { // 检查网络 setContent { - val initViewModel: InitViewModel = viewModel() - initViewModel.checkServer() - val isNetWorkWorking: Boolean? by initViewModel.isNetWorkWorking.observeAsState(null) - when (isNetWorkWorking) { - null -> AnimationText(text = "检查服务器网络状态中!!!") - true -> { - Init() - finish() - } - false -> { - TODO("无法连接到服务器,请检查本地网络或联系管理员") + Body { + MainBoxFrame(background = { /*TODO*/ }, contentAlignment = Alignment.Center) { + val initViewModel: InitViewModel = viewModel() + val isNetWorkWorking: Boolean? by initViewModel.isNetWorkWorking.observeAsState( + null + ) + when (isNetWorkWorking) { + null -> AnimationText(text = "测试服务端运行状态中。。。") + true -> { + Init() + finish() + } + false -> { + AnimationText(text = "无法连接到服务端,请检查服务端地址${BuildConfig.SERVER_ADDRESS}是否配置正确") + } + } } } + } } diff --git a/foreground/src/main/java/com/gyf/csams/InitViewModel.kt b/foreground/src/main/java/com/gyf/csams/InitViewModel.kt index 7b6e724..e5f1d62 100644 --- a/foreground/src/main/java/com/gyf/csams/InitViewModel.kt +++ b/foreground/src/main/java/com/gyf/csams/InitViewModel.kt @@ -13,6 +13,7 @@ import com.gyf.csams.util.Token import com.gyf.lib.util.ApiResponse import com.gyf.lib.util.HttpClient import com.orhanobut.logger.Logger +import kotlinx.coroutines.delay import kotlinx.coroutines.launch data class TokenVo(val token: String, val studentId: String) @@ -30,10 +31,25 @@ class InitViewModel : ViewModel() { private val _token = MutableLiveData() val token: LiveData = _token + init { + checkServer() + } - fun checkServer() { + private fun checkServer() { Logger.i("测试连接到服务端") - _isNetWorkWorking.postValue(true) + viewModelScope.launch { + delay(5000) + HttpClient.get( + Api.buildUrl(TestApi.Test), + SimpleCallback(action = "测试", onSuccess = { + _isNetWorkWorking.postValue(true) + }, onFail = { + Logger.e(it) + _isNetWorkWorking.postValue(false) + }, type = object : TypeToken>() {}.type) + ) + } + } /** @@ -52,9 +68,12 @@ class InitViewModel : ViewModel() { url, SimpleCallback( action = action, - onSuccess = { - _token.postValue(it.body) - Logger.i("token校验结果:${it.body}") + onSuccess = { it -> + it.body?.let { + _token.postValue(it) + Logger.i("token校验结果:${it}") + } + }, onFail = { TODO("token校验失败") }, type = object : TypeToken>() {}.type diff --git a/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt b/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt index 1700e18..a2151e7 100644 --- a/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt +++ b/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt @@ -65,13 +65,17 @@ typealias Token = TokenResDto * 注册表单 */ class AccountViewModel(application: Application) : AndroidViewModel(application) { + val welcomeEnd = application.getString(R.string.welcome_end) + val welcomeStart = application.getString(R.string.welcome_start) + //学号 val studentId = object : ValidStringForm(formDesc = "学号", textLength = 8) { override fun check() { _formValue.value?.let { when { !it.matches(Regex("\\d{8}")) -> _statusForm.value = FormStatus.FormatError - it.matches(Regex("\\d{8}")) -> checkRepeat(_statusForm) + route == AccountRoute.Login -> _statusForm.value = FormStatus.Valid + route == AccountRoute.Register -> checkRepeat(_statusForm) } } } @@ -252,6 +256,10 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) if (checkForm()) { val url = Api.buildUrl(AccountApi.Login) Logger.i("开始$loginDesc,请求接口:$url") + val studentId = "${studentId.formValue.value}" + val password = "${password.formValue.value}" + this.studentId.formValue + Logger.i("使用学号:$studentId,密码:$password 进行登录") HttpClient.post( url, SimpleCallback( @@ -275,8 +283,8 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) ), jsonBody = Gson().toJson( UserLoginVo( - studentId = "${studentId.formValue.value}", - password = "${password.formValue.value}", + studentId = studentId, + password = password, device = "${Build.MANUFACTURER} ${Build.MODEL}" ) ) diff --git a/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt b/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt index 2243290..fe8b731 100644 --- a/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt +++ b/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt @@ -27,10 +27,7 @@ import com.gyf.csams.BuildConfig import com.gyf.csams.R import com.gyf.csams.account.model.AccountViewModel import com.gyf.csams.account.model.DialogMessage -import com.gyf.lib.uikit.BaseTextField -import com.gyf.lib.uikit.Body -import com.gyf.lib.uikit.FormStatus -import com.gyf.lib.uikit.ScaffoldModel +import com.gyf.lib.uikit.* enum class AccountRoute { @@ -43,12 +40,13 @@ class AccountActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { - Body { nav -> + TestBody { nav, scaffoldState -> NavHost(navController = nav, startDestination = AccountRoute.Login.name) { composable(AccountRoute.Login.name) { Account( route = AccountRoute.Login ) { isValidForm: Boolean, accountViewModel: AccountViewModel, scaffoldModel: ScaffoldModel -> + Spacer(modifier = Modifier.height(10.dp)) OutlinedButton( onClick = { @@ -70,15 +68,16 @@ class AccountActivity : ComponentActivity() { finish() } - OutlinedButton( - onClick = { nav.navigate(AccountRoute.Register.name) }, - modifier = Modifier.fillMaxWidth(), - colors = ButtonDefaults.outlinedButtonColors( - contentColor = MaterialTheme.colors.onBackground - ) - ) { - Text(text = accountViewModel.goRegister) - } + OutlinedButton( + onClick = { nav.navigate(AccountRoute.Register.name) }, + modifier = Modifier.fillMaxWidth(), + colors = ButtonDefaults.outlinedButtonColors( + contentColor = MaterialTheme.colors.onBackground + ) + ) { + Text(text = accountViewModel.goRegister) + } + ShowSnackbar(scaffoldState = scaffoldState) } } @@ -147,10 +146,10 @@ class AccountActivity : ComponentActivity() { append(accountViewModel.name.formValue.value ?: "") } withStyle(style = MaterialTheme.typography.subtitle1.toSpanStyle()) { - append(stringResource(id = R.string.welcome_start)) + append(accountViewModel.welcomeStart) } withStyle(style = MaterialTheme.typography.subtitle2.toSpanStyle()) { - append(stringResource(id = R.string.welcome_end)) + append(accountViewModel.welcomeEnd) } withStyle( style = MaterialTheme.typography.subtitle2.toSpanStyle() @@ -160,7 +159,7 @@ class AccountActivity : ComponentActivity() { } }) - StudentId(checkRepeat = route == AccountRoute.Register) + StudentId() Spacer(modifier = Modifier.height(10.dp)) @@ -184,7 +183,7 @@ class AccountActivity : ComponentActivity() { * @param accountViewModel */ @Composable - private fun StudentId(accountViewModel: AccountViewModel = viewModel(), checkRepeat: Boolean) { + private fun StudentId(accountViewModel: AccountViewModel = viewModel()) { Column { diff --git a/foreground/src/main/java/com/gyf/csams/activity/model/ApplyActViewModel.kt b/foreground/src/main/java/com/gyf/csams/activity/model/ApplyActViewModel.kt index b5be74f..186f0da 100644 --- a/foreground/src/main/java/com/gyf/csams/activity/model/ApplyActViewModel.kt +++ b/foreground/src/main/java/com/gyf/csams/activity/model/ApplyActViewModel.kt @@ -30,6 +30,30 @@ import kotlinx.coroutines.launch import java.util.* +class SizeForm(val application: Application) : + StringForm(formDesc = application.getString(R.string.activity_size), textLength = 2) { + + val _formError = MutableLiveData() + val formError: LiveData = _formError + val maxActivitySize = application.resources.getInteger(R.integer.activity_size) + val rangeError = application.getString(R.string.activity_size_error, 1, maxActivitySize) + + + override fun onChange(value: String) { + if (value.length > textLength) { + _formError.value = "${formDesc}不能超过最大长度$textLength" + _formValue.value = value.slice(IntRange(0, textLength - 1)) + } else if (value.matches(Regex("\\d+")) && value.toInt() !in 1..maxActivitySize) { + Logger.i("活动人数:${value}不合法") + _formError.value = rangeError + } else { + _formValue.value = value + _formError.value = "" + } + + } +} + /** * 申请活动数据状态管理 * @@ -49,24 +73,9 @@ class ApplyActViewModel(application: Application) : AndroidViewModel(application StringForm(formDesc = application.getString(R.string.activity_address), textLength = 30) val activityDesc = StringForm(formDesc = application.getString(R.string.activity_desc), textLength = 50) - val maxActivitySize = application.resources.getInteger(R.integer.activity_size) - val activitySize = object : - StringForm(formDesc = application.getString(R.string.activity_size), textLength = 2) { - override fun onChange(value: String) { - if (value.length > textLength) { -// formError.value = "${formDesc}不能超过最大长度$textLength" - _formValue.value = value.slice(IntRange(0, textLength - 1)) - } else if (value.matches(Regex("\\d+")) && value.toInt() !in 1..maxActivitySize) { - Logger.i("活动人数:${value}不合法") -// formError.value = -// application.getString(R.string.activity_size_error, 1, maxActivitySize) - } else { - _formValue.value = value -// formError.value = "" - } - } - } + val activitySize = SizeForm(application = application) + val city = object : StringForm(formDesc = application.getString(R.string.city), textLength = 4) { override val formPlaceholder = "" diff --git a/lib/src/main/java/com/gyf/lib/uikit/BaseTextField.kt b/lib/src/main/java/com/gyf/lib/uikit/BaseTextField.kt index afcf996..0f8af8b 100644 --- a/lib/src/main/java/com/gyf/lib/uikit/BaseTextField.kt +++ b/lib/src/main/java/com/gyf/lib/uikit/BaseTextField.kt @@ -32,9 +32,6 @@ enum class FormStatus { Valid } -interface FormLength { - val textLength: Int -} abstract class FormName(val formDesc: String) { protected val _formValue = MutableLiveData() @@ -52,9 +49,8 @@ abstract class FormName(val formDesc: String) { * * @param formDesc */ -open class StringForm(formDesc: String, override val textLength: Int) : - FormName(formDesc = formDesc), - FormLength { +open class StringForm(formDesc: String, val textLength: Int) : + FormName(formDesc = formDesc) { constructor(formDesc: String, textLength: Int, value: String) : this( formDesc = formDesc, diff --git a/lib/src/main/java/com/gyf/lib/uikit/MainFrame.kt b/lib/src/main/java/com/gyf/lib/uikit/MainFrame.kt index b8a2eac..7d20e31 100644 --- a/lib/src/main/java/com/gyf/lib/uikit/MainFrame.kt +++ b/lib/src/main/java/com/gyf/lib/uikit/MainFrame.kt @@ -177,4 +177,18 @@ fun Body(content: @Composable (nav: NavHostController) -> Unit) { } } } +} + +@Composable +fun TestBody(content: @Composable (nav: NavHostController, scaffoldState: ScaffoldState) -> Unit) { + CSAMSTheme { + Surface(color = MaterialTheme.colors.background) { + val navController = rememberNavController() + val scaffoldState = rememberScaffoldState() + + Scaffold(scaffoldState = scaffoldState) { + content(nav = navController, scaffoldState = scaffoldState) + } + } + } } \ No newline at end of file diff --git a/lib/src/main/java/com/gyf/lib/uikit/Snackbar.kt b/lib/src/main/java/com/gyf/lib/uikit/Snackbar.kt index 29a83ea..717a5a3 100644 --- a/lib/src/main/java/com/gyf/lib/uikit/Snackbar.kt +++ b/lib/src/main/java/com/gyf/lib/uikit/Snackbar.kt @@ -32,10 +32,11 @@ class ScaffoldModel : ViewModel() { fun update(message: String? = null, actionLabel: String? = null, callback: () -> Unit? = {}) { if (message == null) { - _data.value = null + _data.postValue(null) } else { - _data.value = + _data.postValue( SnackBar(message = message, actionLabel = actionLabel, callback = callback) + ) } } } diff --git a/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt b/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt index 45b1d77..43da770 100644 --- a/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt +++ b/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt @@ -140,9 +140,10 @@ abstract class HttpCallback( val body = response.body if (body != null && body.contentType()?.subtype == "json") { val jsonRes = body.string() - val res: ApiResponse = gson.fromJson(jsonRes, type) - Logger.i("${action}请求响应成功:") Logger.json(jsonRes) + val res: ApiResponse = gson.fromJson(jsonRes, type) + Logger.i("json解析成功:$res") + onSuccess(res) } else { onFail("${action}失败,请联系管理员")