优化登录

This commit is contained in:
Cold Mint 2022-04-23 20:41:25 +08:00
parent 6c3676e57e
commit 5d4f078664
17 changed files with 288 additions and 86 deletions

View File

@ -6,9 +6,6 @@ plugins {
} }
android { android {
lintOptions {
baseline file("lint-baseline.xml")
}
signingConfigs { signingConfigs {
debug { debug {
storeFile file('keystore\\coldmint_debug.keystore') storeFile file('keystore\\coldmint_debug.keystore')
@ -63,6 +60,9 @@ android {
buildFeatures { buildFeatures {
viewBinding true viewBinding true
} }
lint {
baseline file('lint-baseline.xml')
}
} }

View File

@ -63,6 +63,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>() {
} else { } else {
viewBinding.root.setBackgroundResource(0) viewBinding.root.setBackgroundResource(0)
} }
setLoginButtonEnable()
} }
}) })
@ -79,6 +80,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>() {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
val passWord = s.toString() val passWord = s.toString()
checkPassword(passWord) checkPassword(passWord)
setLoginButtonEnable()
} }
}) })
@ -357,6 +359,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>() {
val agree = appSettings.getValue(AppSettings.Setting.AgreePolicy, false) val agree = appSettings.getValue(AppSettings.Setting.AgreePolicy, false)
viewBinding.checkbox.isChecked = agree viewBinding.checkbox.isChecked = agree
viewBinding.checkbox.setOnCheckedChangeListener { p0, p1 -> viewBinding.checkbox.setOnCheckedChangeListener { p0, p1 ->
setLoginButtonEnable()
appSettings.setValue(AppSettings.Setting.AgreePolicy, p1) appSettings.setValue(AppSettings.Setting.AgreePolicy, p1)
} }
@ -407,38 +410,72 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>() {
} }
} }
fun checkAccount(account: String): Boolean { /**
* 检查账号
* @param account String
* @param updateView Boolean
* @return Boolean
*/
fun checkAccount(account: String, updateView: Boolean = true): Boolean {
return if (account.isBlank()) { return if (account.isBlank()) {
setErrorAndInput( if (updateView) {
viewBinding.accountView, setErrorAndInput(
getString(R.string.please_enter_your_account_or_email), viewBinding.accountView,
viewBinding.accountInputLayout getString(R.string.please_enter_your_account_or_email),
) viewBinding.accountInputLayout
)
}
false false
} else { } else {
viewBinding.accountInputLayout.isErrorEnabled = false if (updateView) {
viewBinding.accountInputLayout.isErrorEnabled = false
}
true true
} }
} }
fun checkPassword(passWord: String): Boolean { /**
* 设置登录按钮
*/
fun setLoginButtonEnable() {
viewBinding.button.isEnabled =
checkAccount(viewBinding.accountView.text.toString(), false) && checkPassword(
viewBinding.passwordView.text.toString(),
false
) && viewBinding.checkbox.isChecked
}
/**
* 检查密码
* @param passWord String
* @param updateView Boolean
* @return Boolean
*/
fun checkPassword(passWord: String, updateView: Boolean = true): Boolean {
return if (passWord.isBlank()) { return if (passWord.isBlank()) {
setErrorAndInput( if (updateView) {
viewBinding.passwordView, setErrorAndInput(
getString(R.string.please_enter_your_password), viewBinding.passwordView,
viewBinding.passwordInputLayout getString(R.string.please_enter_your_password),
) viewBinding.passwordInputLayout
)
}
false false
} else { } else {
if (passWord.matches(Regex("^[a-zA-Z0-9_]{6,20}\$"))) { if (passWord.matches(Regex("^[a-zA-Z0-9_]{6,20}\$"))) {
viewBinding.passwordInputLayout.isErrorEnabled = false if (updateView) {
viewBinding.passwordInputLayout.isErrorEnabled = false
}
true true
} else { } else {
setErrorAndInput( if (updateView) {
viewBinding.passwordView, setErrorAndInput(
getString(R.string.password_error), viewBinding.passwordView,
viewBinding.passwordInputLayout, false getString(R.string.password_error),
) viewBinding.passwordInputLayout, false
)
}
false false
} }
} }

View File

@ -6,6 +6,7 @@ import android.text.InputType
import android.text.TextWatcher import android.text.TextWatcher
import com.coldmint.rust.pro.base.BaseActivity import com.coldmint.rust.pro.base.BaseActivity
import android.view.View import android.view.View
import androidx.core.view.isVisible
import com.coldmint.rust.pro.tool.AppSettings import com.coldmint.rust.pro.tool.AppSettings
import com.coldmint.rust.pro.tool.GlobalMethod import com.coldmint.rust.pro.tool.GlobalMethod
import com.coldmint.rust.core.interfaces.ApiCallBack import com.coldmint.rust.core.interfaces.ApiCallBack
@ -34,7 +35,7 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
val userName = s.toString() val userName = s.toString()
checkUserName(userName) checkUserName(userName)
setRegisterButtonEnable()
} }
}) })
@ -50,6 +51,7 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
val account = s.toString() val account = s.toString()
checkAccount(account) checkAccount(account)
setRegisterButtonEnable()
} }
}) })
@ -66,6 +68,24 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
val passWord = s.toString() val passWord = s.toString()
checkPassword(passWord) checkPassword(passWord)
setRegisterButtonEnable()
}
})
viewBinding.confirmPasswordView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(p0: Editable?) {
val confirmPassword = p0.toString()
checkConfirmPassword(confirmPassword)
setRegisterButtonEnable()
} }
}) })
@ -82,6 +102,7 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
val email = s.toString() val email = s.toString()
checkEmail(email) checkEmail(email)
setRegisterButtonEnable()
} }
}) })
@ -125,6 +146,11 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
val passWord = viewBinding.passwordView.text.toString() val passWord = viewBinding.passwordView.text.toString()
val userName = viewBinding.userNameView.text.toString() val userName = viewBinding.userNameView.text.toString()
val email = viewBinding.emailView.text.toString() val email = viewBinding.emailView.text.toString()
val confirmPassword = viewBinding.confirmPasswordView.text.toString()
if (!checkConfirmPassword(confirmPassword)) {
return@OnClickListener
}
if (!checkAccount(account)) { if (!checkAccount(account)) {
return@OnClickListener return@OnClickListener
@ -231,49 +257,111 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
} }
fun checkEmail(email: String): Boolean { /**
* 检查邮箱
* @param email String
* @return Boolean
*/
fun checkEmail(email: String, updateView: Boolean = true): Boolean {
return if (email.isBlank()) { return if (email.isBlank()) {
setErrorAndInput( if (updateView) {
viewBinding.emailView, viewBinding.mailHelpTextView.isVisible = true
String.format( setErrorAndInput(
getString(R.string.please_input_value), viewBinding.emailView,
viewBinding.emailInputLayout.hint.toString() String.format(
), viewBinding.emailInputLayout getString(R.string.please_input_value),
) viewBinding.emailInputLayout.hint.toString()
), viewBinding.emailInputLayout
)
}
false false
} else { } else {
if (email.matches(Regex("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$"))) { if (email.matches(Regex("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$"))) {
viewBinding.emailInputLayout.isErrorEnabled = false if (updateView) {
viewBinding.emailInputLayout.isErrorEnabled = false
viewBinding.mailHelpTextView.isVisible = false
}
true true
} else { } else {
setErrorAndInput( if (updateView) {
viewBinding.emailView, viewBinding.mailHelpTextView.isVisible = true
getString(R.string.email_error), viewBinding.emailInputLayout, false setErrorAndInput(
) viewBinding.emailView,
getString(R.string.email_error), viewBinding.emailInputLayout, false
)
}
false false
} }
} }
} }
/**
* 检查确认密码
* @param confirmPassword String
* @return Boolean
*/
fun checkConfirmPassword(confirmPassword: String, updateView: Boolean = true): Boolean {
return if (confirmPassword.isBlank()) {
if (updateView) {
setErrorAndInput(
viewBinding.confirmPasswordView, String.format(
getString(R.string.please_input_value),
viewBinding.confirmPasswordInputLayout.hint.toString()
), viewBinding.confirmPasswordInputLayout
)
}
false
} else {
val passWord = viewBinding.passwordView.text.toString()
if (passWord == confirmPassword) {
if (updateView) {
viewBinding.confirmPasswordInputLayout.isErrorEnabled = false
}
true
} else {
if (updateView) {
setErrorAndInput(
viewBinding.confirmPasswordView,
getString(R.string.confirm_password_error),
viewBinding.confirmPasswordInputLayout,
false
)
}
false
}
fun checkPassword(passWord: String): Boolean { }
}
/**
* 检查密码
* @param passWord String
* @return Boolean
*/
fun checkPassword(passWord: String, updateView: Boolean = true): Boolean {
return if (passWord.isBlank()) { return if (passWord.isBlank()) {
setErrorAndInput( if (updateView) {
viewBinding.passwordView, setErrorAndInput(
getString(R.string.please_enter_your_password), viewBinding.passwordView,
viewBinding.passwordInputLayout getString(R.string.please_enter_your_password),
) viewBinding.passwordInputLayout
)
}
false false
} else { } else {
if (passWord.matches(Regex("^[a-zA-Z0-9_]{6,20}\$"))) { if (passWord.matches(Regex("^[a-zA-Z0-9_]{6,20}\$"))) {
viewBinding.passwordInputLayout.isErrorEnabled = false if (updateView) {
viewBinding.passwordInputLayout.isErrorEnabled = false
}
true true
} else { } else {
setErrorAndInput( if (updateView) {
viewBinding.passwordView, setErrorAndInput(
getString(R.string.password_error), viewBinding.passwordView,
viewBinding.passwordInputLayout, false getString(R.string.password_error),
) viewBinding.passwordInputLayout, false
)
}
false false
} }
} }
@ -284,19 +372,23 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
* @param userName String * @param userName String
* @return Boolean * @return Boolean
*/ */
fun checkUserName(userName: String): Boolean { fun checkUserName(userName: String, updateView: Boolean = true): Boolean {
return if (userName.isBlank()) { return if (userName.isBlank()) {
setErrorAndInput( if (updateView) {
viewBinding.userNameView, setErrorAndInput(
String.format( viewBinding.userNameView,
getString(R.string.please_input_value), String.format(
viewBinding.userNameInputLayout.hint.toString() getString(R.string.please_input_value),
), viewBinding.userNameInputLayout.hint.toString()
viewBinding.userNameInputLayout ),
) viewBinding.userNameInputLayout
)
}
false false
} else { } else {
viewBinding.userNameInputLayout.isErrorEnabled = false if (updateView) {
viewBinding.userNameInputLayout.isErrorEnabled = false
}
true true
} }
} }
@ -306,28 +398,52 @@ class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
* @param account String * @param account String
* @return Boolean * @return Boolean
*/ */
fun checkAccount(account: String): Boolean { fun checkAccount(account: String, updateView: Boolean = true): Boolean {
if (account.isBlank()) { if (account.isBlank()) {
setErrorAndInput( if (updateView) {
viewBinding.accountView, setErrorAndInput(
getString(R.string.please_enter_your_account), viewBinding.accountInputLayout viewBinding.accountView,
) getString(R.string.please_enter_your_account), viewBinding.accountInputLayout
)
}
return false return false
} else { } else {
return if (account.matches(Regex("^[A-Za-z0-9_]+\$"))) { return if (account.matches(Regex("^[A-Za-z0-9_]+\$"))) {
viewBinding.accountInputLayout.isErrorEnabled = false if (updateView) {
viewBinding.accountInputLayout.isErrorEnabled = false
}
true true
} else { } else {
setErrorAndInput( if (updateView) {
viewBinding.accountView, setErrorAndInput(
getString(R.string.account_error), viewBinding.accountInputLayout, false viewBinding.accountView,
) getString(R.string.account_error), viewBinding.accountInputLayout, false
)
}
false false
} }
} }
} }
/**
* 设置注册按钮启用状态
*/
fun setRegisterButtonEnable() {
val account = viewBinding.accountView.text.toString()
val passWord = viewBinding.passwordView.text.toString()
val userName = viewBinding.userNameView.text.toString()
val email = viewBinding.emailView.text.toString()
val confirmPassword = viewBinding.confirmPasswordView.text.toString()
viewBinding.registerButton.isEnabled =
checkConfirmPassword(confirmPassword, false) && checkAccount(
account,
false
) && checkUserName(userName, false) && checkPassword(
passWord
) && checkEmail(email, false)
}
override fun getViewBindingObject(): ActivityRegisterBinding { override fun getViewBindingObject(): ActivityRegisterBinding {
return ActivityRegisterBinding.inflate(layoutInflater) return ActivityRegisterBinding.inflate(layoutInflater)
} }

View File

@ -63,6 +63,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/passwordInputLayout" android:layout_below="@id/passwordInputLayout"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:enabled="false"
android:text="@string/login" /> android:text="@string/login" />
@ -75,7 +76,6 @@
android:text="@string/agreement_agreed" /> android:text="@string/agreement_agreed" />
<RelativeLayout <RelativeLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -103,6 +103,26 @@
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/confirmPasswordInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
app:counterEnabled="true"
app:counterMaxLength="20"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/confirmPasswordView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/confirm_password"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:maxLength="20" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailInputLayout" android:id="@+id/emailInputLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -124,9 +144,10 @@
android:id="@+id/mailHelpTextView" android:id="@+id/mailHelpTextView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/mail_helper" android:text="@string/mail_helper"
android:textColor="?android:colorPrimary" android:textSize="13sp"
android:textSize="13sp" /> android:visibility="gone" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
@ -135,6 +156,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:enabled="false"
android:text="@string/register" /> android:text="@string/register" />
</LinearLayout> </LinearLayout>

View File

@ -825,4 +825,6 @@
<string name="qq_number">QQ号</string> <string name="qq_number">QQ号</string>
<string name="mail_helper_tip">建议您使用QQ邮箱注册请输入QQ号我们会自动填充您的QQ邮箱。</string> <string name="mail_helper_tip">建议您使用QQ邮箱注册请输入QQ号我们会自动填充您的QQ邮箱。</string>
<string name="email_fill_complete">邮箱地址填充完成。</string> <string name="email_fill_complete">邮箱地址填充完成。</string>
<string name="confirm_password">确认密码</string>
<string name="confirm_password_error">两次密码不一致。</string>
</resources> </resources>

View File

@ -156,7 +156,6 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
translatorListener.beforeTranslate() translatorListener.beforeTranslate()
translatorListener.onTranslateComplete(code) translatorListener.onTranslateComplete(code)
} }
} else { } else {
val tokenizer = StringTokenizer(code, split, true) val tokenizer = StringTokenizer(code, split, true)
//缓存翻译数据,以便加速重复数据的翻译 //缓存翻译数据,以便加速重复数据的翻译
@ -166,6 +165,8 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
//保存完整的翻译结果 //保存完整的翻译结果
val translationResult = StringBuilder() val translationResult = StringBuilder()
var codeBlockType = CompileConfiguration.CodeBlockType.Key var codeBlockType = CompileConfiguration.CodeBlockType.Key
//保存资源引用值(应该看做整体处理)
val referenceResult = StringBuilder()
handler.post { handler.post {
translatorListener.beforeTranslate() translatorListener.beforeTranslate()
} }
@ -177,23 +178,46 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
codeResult.delete(0, codeResult.length) codeResult.delete(0, codeResult.length)
when (code) { when (code) {
"\n" -> { "\n" -> {
if (codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
val referenceValue = referenceResult.toString()
val codeInfo =
codeDataBase.getCodeDao().findCodeByCode(referenceValue)
if (codeInfo == null) {
translationResult.append(referenceResult)
}else{
// tr
}
}
codeBlockType = CompileConfiguration.CodeBlockType.Key codeBlockType = CompileConfiguration.CodeBlockType.Key
codeResult.append(code) codeResult.append(code)
} }
"\r" -> { "\r" -> {
} }
" ", ",", "(", ")", "=", "%", "{", "}", "+", "*", "/" -> codeResult.append( " ", ",", "(", ")", "=", "%", "{", "}", "+", "*", "/" -> {
code if (codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
) referenceResult.append(code)
":" -> { } else {
if (codeBlockType == CompileConfiguration.CodeBlockType.Key) { codeResult.append(
codeBlockType = code
CompileConfiguration.CodeBlockType.Value )
}
}
":" -> {
if (codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
referenceResult.append(code)
} else {
if (codeBlockType == CompileConfiguration.CodeBlockType.Key) {
codeBlockType =
CompileConfiguration.CodeBlockType.Value
}
codeResult.append(code)
} }
codeResult.append(code)
} }
else -> if (codeBlockType == CompileConfiguration.CodeBlockType.Note) { else -> if (codeBlockType == CompileConfiguration.CodeBlockType.Note) {
codeResult.append(code) codeResult.append(code)
} else if (codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
//资源引用值应该被整体处理
referenceResult.append(code)
} else { } else {
if (code.startsWith("#")) { if (code.startsWith("#")) {
codeBlockType = CompileConfiguration.CodeBlockType.Note codeBlockType = CompileConfiguration.CodeBlockType.Note
@ -258,7 +282,7 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
if (!tag.isNullOrBlank()) { if (!tag.isNullOrBlank()) {
//如果此类型为特殊标注,那么设置为注释 //如果此类型为特殊标注,那么设置为注释
codeBlockType = codeBlockType =
CompileConfiguration.CodeBlockType.Note CompileConfiguration.CodeBlockType.Reference
} }
} }
codeResult.append(codeInfo.translate) codeResult.append(codeInfo.translate)

View File

@ -118,10 +118,11 @@ data class CompileConfiguration(
/** /**
* 代码块类 * 代码块类
* 注释变量名 * 注释变量名,引用
* Reference 引用是一种特殊的数据类型编译器会尝试编译此值若无法编译则使用原始值
*/ */
enum class CodeBlockType { enum class CodeBlockType {
Key, Value, Section, Note, VariableName Key, Value, Section, Note, VariableName, Reference
} }
/** /**

View File

@ -7,7 +7,7 @@ buildscript {
maven { url 'https://repo1.maven.org/maven2/' } maven { url 'https://repo1.maven.org/maven2/' }
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.1.2' classpath 'com.android.tools.build:gradle:7.1.3'
// classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5" // classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'

View File

@ -4,5 +4,5 @@
# Location of the SDK. This is only used by Gradle. # Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the # For customization when using a Version Control System, please read the
# header note. # header note.
#Sat Mar 26 20:50:56 CST 2022 #Sat Apr 23 18:41:07 CST 2022
sdk.dir=D\:\\Document\\AndroidSdk sdk.dir=D\:\\Android_SDK