Parcourir la source

feat 增加设置校验

RobinTan1024 il y a 4 ans
Parent
commit
801d1288c3

+ 15 - 0
app/src/main/java/com/doverfuelingsolutions/issp/utils/ValidateUtil.kt

@@ -0,0 +1,15 @@
+package com.doverfuelingsolutions.issp.utils
+
+class ValidateUtil {
+
+    companion object {
+
+        private val regexIP: Regex by lazyOf(Regex("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\$"))
+        private val regexPositiveInt: Regex by lazyOf(Regex("^[1-9]\\d*$"))
+        private val regexUrl: Regex by lazyOf(Regex("^(https?|ftp|file)://([\\da-z.-]+)\\.([a-z.]{2,6})([/\\w .-]*)*/?\$"))
+
+        fun isIP(ip: String): Boolean = regexIP.matches(ip)
+        fun isPositiveInt(num: String): Boolean = regexPositiveInt.matches(num)
+        fun isUrl(url: String): Boolean = regexUrl.matches(url)
+    }
+}

+ 2 - 8
app/src/main/java/com/doverfuelingsolutions/issp/view/LoginActivity.kt

@@ -1,9 +1,9 @@
 package com.doverfuelingsolutions.issp.view
 
 import android.content.pm.PackageManager
-import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import android.view.View
+import androidx.appcompat.app.AppCompatActivity
 import androidx.databinding.DataBindingUtil
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
@@ -13,7 +13,6 @@ import com.doverfuelingsolutions.issp.data.DataStore
 import com.doverfuelingsolutions.issp.databinding.ActivityLoginBinding
 import com.doverfuelingsolutions.issp.utils.PermissionUtil
 import com.doverfuelingsolutions.issp.utils.StringUtil
-import com.doverfuelingsolutions.issp.utils.log.DFSLog
 import com.google.android.material.snackbar.Snackbar
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
@@ -64,16 +63,11 @@ class LoginActivity : AppCompatActivity(), View.OnClickListener {
     }
 
     private fun autoLogin() {
-//        if (DataStore.accountName.isNotEmpty() && DataStore.password.isNotEmpty()) {
-//            // submit() // FIXME 调试代码
-//            MainActivity.start(this)
-//            finish()
-//        }
+        if (DataStore.accountName.isNotEmpty() && DataStore.password.isNotEmpty()) submit()
     }
 
     private fun submit() {
         if (mainViewModel.submitting.value == true) {
-            DFSLog.w("submitting")
             return
         }
 

+ 32 - 23
app/src/main/java/com/doverfuelingsolutions/issp/view/fragment/SettingsFragment.kt

@@ -3,18 +3,19 @@ package com.doverfuelingsolutions.issp.view.fragment
 import android.os.Bundle
 import android.text.InputType
 import android.widget.EditText
-import androidx.lifecycle.Lifecycle
-import androidx.lifecycle.LifecycleObserver
-import androidx.lifecycle.OnLifecycleEvent
 import androidx.preference.EditTextPreference
 import androidx.preference.Preference
 import androidx.preference.PreferenceFragmentCompat
 import com.doverfuelingsolutions.issp.R
+import com.doverfuelingsolutions.issp.utils.ValidateUtil
 import com.doverfuelingsolutions.issp.utils.log.DFSLog
 import com.doverfuelingsolutions.issp.utils.sp.SPKeys
+import com.google.android.material.snackbar.Snackbar
 
 class SettingsFragment : PreferenceFragmentCompat(), EditTextPreference.OnBindEditTextListener, Preference.OnPreferenceChangeListener {
 
+    private val snackbar: Snackbar by lazy { Snackbar.make(requireView(), R.string.blank, 4000) }
+
     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
         setPreferencesFromResource(R.xml.root_preferences, rootKey)
 
@@ -24,39 +25,47 @@ class SettingsFragment : PreferenceFragmentCompat(), EditTextPreference.OnBindEd
         setNumberInput(SPKeys.MIDDLE_PORT)
         setNumberInput(SPKeys.MIDDLE_WORKSTATION_ID)
         setNumberInput(SPKeys.FUEL_PORT)
+        setValidate(SPKeys.FUEL_IP)
+        setValidate(SPKeys.MIDDLE_IP)
+        setValidate(SPKeys.SERVER_DOMAIN)
+
+        DFSLog.i("view = $view", "listView = $listView")
     }
 
     override fun onBindEditText(editText: EditText) {
         editText.inputType = InputType.TYPE_CLASS_NUMBER
     }
 
-    override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean {
-        val inputValue = newValue.toString()
-        DFSLog.i("${preference.key} = $inputValue")
-        return true
+    override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
+        val value = newValue.toString()
+        val result = when (preference?.key) {
+            SPKeys.SERVER_PORT_BASE,
+            SPKeys.SERVER_PORT_TRX,
+            SPKeys.SERVER_PORT_CONFIG,
+            SPKeys.MIDDLE_PORT,
+            SPKeys.MIDDLE_WORKSTATION_ID,
+            SPKeys.FUEL_PORT -> ValidateUtil.isPositiveInt(value)
+            SPKeys.FUEL_IP,
+            SPKeys.MIDDLE_IP -> ValidateUtil.isIP(value)
+            SPKeys.SERVER_DOMAIN -> ValidateUtil.isUrl(value)
+            else -> false
+        }
+        if (!result) snackbar.setText(R.string.input_not_right).show()
+        return result
     }
 
     private fun setNumberInput(key: String): EditTextPreference? {
+        val fragment = this@SettingsFragment
         return findPreference<EditTextPreference>(key)?.apply {
-            setOnBindEditTextListener(this@SettingsFragment)
-            onPreferenceChangeListener = this@SettingsFragment
+            setOnBindEditTextListener(fragment)
+            onPreferenceChangeListener = fragment
         }
     }
 
-    class PortValidator(private val preference: Preference) : LifecycleObserver, Preference.OnPreferenceChangeListener {
-
-        @OnLifecycleEvent(Lifecycle.Event.ON_START)
-        fun listen() {
-            preference.onPreferenceChangeListener = this
-        }
-
-        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
-        fun remove() {
-            preference.onPreferenceChangeListener = null
-        }
-
-        override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
-            return true
+    private fun setValidate(key: String) {
+        val fragment = this@SettingsFragment
+        findPreference<EditTextPreference>(key)?.apply {
+            onPreferenceChangeListener = fragment
         }
     }
 }

+ 0 - 2
app/src/main/res/layout/activity_login.xml

@@ -20,8 +20,6 @@
             style="@style/fullWidth"
             android:layout_marginStart="200dp"
             android:layout_marginEnd="200dp"
-            android:focusable="true"
-            android:focusableInTouchMode="true"
             android:gravity="center"
             android:orientation="vertical"
             app:layout_constraintBottom_toBottomOf="parent"