Skip to content

Commit

Permalink
[ADD] : init id & password regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokwanhee committed Nov 28, 2023
1 parent 6bb14a6 commit 1fc9d4d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/dosoptkwanheejo/DoSoptApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.sopt.dosoptkwanheejo.db.remote.AuthApiHelper
import org.sopt.dosoptkwanheejo.db.remote.NaverApiHelper
import org.sopt.dosoptkwanheejo.db.remote.RetrofitServicePool
import org.sopt.dosoptkwanheejo.db.remote.UserApiHelper
import java.util.regex.Pattern

class DoSoptApp : Application() {
override fun onCreate() {
Expand All @@ -19,6 +20,10 @@ class DoSoptApp : Application() {
private lateinit var authApiHelper: AuthApiHelper
private lateinit var userApiHelper: UserApiHelper
private lateinit var naverApiHelper: NaverApiHelper
private const val ID_PATTERN = "^[a-zA-Z0-9]{6,10}$"
private const val PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{6,12}$"
val ID_REGEX: Pattern = Pattern.compile(ID_PATTERN)
val PASSWORD_REGEX: Pattern = Pattern.compile(PASSWORD_PATTERN)

@Synchronized
private fun getSharedPreferencesInstance(context: Context): SharedPreferences {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package org.sopt.dosoptkwanheejo.presentation.login

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.ViewModelProvider
import org.sopt.dosoptkwanheejo.DoSoptApp
import org.sopt.dosoptkwanheejo.DoSoptApp.Companion.ID_REGEX
import org.sopt.dosoptkwanheejo.DoSoptApp.Companion.PASSWORD_REGEX
import org.sopt.dosoptkwanheejo.DoSoptApp.Companion.sharedPreferencesInstance
import org.sopt.dosoptkwanheejo.R
import org.sopt.dosoptkwanheejo.base.BaseActivity
Expand All @@ -25,6 +30,8 @@ import org.sopt.dosoptkwanheejo.util.AuthViewModelFactory
import org.sopt.dosoptkwanheejo.util.hideKeyboard
import org.sopt.dosoptkwanheejo.util.showShortSnackBar
import org.sopt.dosoptkwanheejo.util.showShortToastMessage
import org.w3c.dom.Text
import java.util.regex.Pattern

class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::inflate) {
private lateinit var loginViewModel: LoginViewModel
Expand All @@ -45,9 +52,35 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::i
initHideKeyboard()
initLogin()
initSignUp()
addIdTextChangedListener()
addPasswordTextChangedListener()
observeData()
}

private fun addIdTextChangedListener() {
binding.soptEvId.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
loginViewModel.idFlag.value = ID_REGEX.matcher(s).matches()
}

override fun afterTextChanged(s: Editable?) {}
})
}

private fun addPasswordTextChangedListener() {
binding.soptEvPwd.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
loginViewModel.passwordFlag.value = PASSWORD_REGEX.matcher(s).matches()
}

override fun afterTextChanged(s: Editable?) {}
})
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putString(ID, binding.soptEvId.getEditText())
outState.putString(PWD, binding.soptEvPwd.getEditText())
Expand Down Expand Up @@ -124,11 +157,18 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::i
is LoginResp -> {
successLogin()
}

is RespResult -> {
binding.root.showShortSnackBar(it.message)
hideKeyboard(binding.root)
}
}
}
loginViewModel.idFlag.observe(this) {
binding.btLogin.isEnabled = it && loginViewModel.passwordFlag.value == true
}
loginViewModel.passwordFlag.observe(this) {
binding.btLogin.isEnabled = it && loginViewModel.idFlag.value == true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package org.sopt.dosoptkwanheejo.presentation.login.viewmodel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.sopt.dosoptkwanheejo.model.dto.RespResult
import org.sopt.dosoptkwanheejo.repository.AuthRepository

class LoginViewModel(
private val authRepository: AuthRepository
) : ViewModel() {
val idFlag = MutableLiveData<Boolean>()
val passwordFlag = MutableLiveData<Boolean>()

private val _loginResp = MutableLiveData<RespResult>()
val loginResp: LiveData<RespResult> = _loginResp

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.dosoptkwanheejo.view

import android.content.Context
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.TypedValue
import androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -64,4 +65,8 @@ class SoptEditView @JvmOverloads constructor(
fun setInputType(inputType: Int) {
binding.etContent.inputType = inputType
}

fun addTextChangedListener(textWatcher: TextWatcher) {
binding.etContent.addTextChangedListener(textWatcher)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
android:text="@string/do_login"
android:textColor="@color/white"
android:textSize="18sp"
android:enabled="false"
app:layout_constraintBottom_toTopOf="@id/bt_sign_up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down

0 comments on commit 1fc9d4d

Please sign in to comment.