Skip to content

Commit

Permalink
[Refactor/#9] dataBinding에 Stateflow 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeun5744 committed Jun 29, 2023
1 parent 4c61e0f commit 380a614
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package org.android.go.sopt.present.loginPage

import android.content.Intent
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.viewModels
import org.android.go.sopt.R
import org.android.go.sopt.RequestSignUpDto
import org.android.go.sopt.databinding.ActivitySignupBinding
import org.android.go.sopt.present.viewModel.LoginPageViewModel
import org.android.go.sopt.util.ViewModelFactory
import org.android.go.sopt.util.hideKeyboard
import org.android.go.sopt.util.makeToastMessage
import java.util.regex.Pattern

class SignUpActivity : AppCompatActivity() {

Expand All @@ -31,8 +26,6 @@ class SignUpActivity : AppCompatActivity() {
with(binding) {
vmSignUp = viewModel
lifecycleOwner = this@SignUpActivity
etId.doAfterTextChanged { checkValidSignUpId() }
etPassword.doAfterTextChanged { checkValidSignUpPwd() }
}

signUp()
Expand Down Expand Up @@ -78,48 +71,4 @@ class SignUpActivity : AppCompatActivity() {
}
}
}

private fun checkValidSignUpId(): Boolean {
var correctId = false

val idPattern = "^(?=.*\\d)(?=.*[a-zA-Z]).{6,10}\$"

viewModel.signUpId.observe(this) { id ->
correctId = Pattern.matches(idPattern, id)
if (!correctId) {
binding.tvIdWarn.visibility = View.VISIBLE
correctId = false
binding.etId.backgroundTintList = ColorStateList.valueOf(getColor(R.color.red_500))
} else {
binding.tvIdWarn.visibility = View.GONE
correctId = true
binding.etId.backgroundTintList = ColorStateList.valueOf(getColor(R.color.black))
}
}

return correctId
}

private fun checkValidSignUpPwd(): Boolean {
var correctPw = false

val pwPattern = "^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[\$@\$!%*#?&]).{6,12}\$"

viewModel.signUpPwd.observe(this) { pwd ->
correctPw = Pattern.matches(pwPattern, pwd)
if (!correctPw) {
binding.tvPwWarn.visibility = View.VISIBLE
correctPw = false
binding.etPassword.backgroundTintList =
ColorStateList.valueOf(getColor(R.color.red_500))
} else {
binding.tvPwWarn.visibility = View.GONE
correctPw = true
binding.etPassword.backgroundTintList =
ColorStateList.valueOf(getColor(R.color.black))
}
}

return correctPw
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import org.android.go.sopt.RequestSignUpDto
import org.android.go.sopt.remote.remoteData.model.MyProfileDto
Expand All @@ -23,8 +24,24 @@ class LoginPageViewModel(private val loginPageRepoImpl: LoginPageRepoImpl) : Vie
private val _getMyProfile = MutableLiveData<MyProfileDto>()
val getMyProfile: LiveData<MyProfileDto> get() = _getMyProfile

val signUpId: MutableLiveData<String> = MutableLiveData("")
val signUpPwd: MutableLiveData<String> = MutableLiveData("")
val signUpId = MutableStateFlow("")
val signUpPwd = MutableStateFlow("")

val isValidId: StateFlow<Boolean> = signUpId.map { inputId ->
inputId.matches(Regex(idPattern))
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000L), true)

val isValidPwd: StateFlow<Boolean> = signUpPwd.map { inputPwd ->
inputPwd.matches(Regex(pwPattern))
}.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(),
false,
)
val canClickSignUpBtn: StateFlow<Boolean> =
combine(isValidId, isValidPwd) { isValidId, isValidPwd ->
isValidId && isValidPwd
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), true)

fun login(request: RequestLogInDto) = viewModelScope.launch {
kotlin.runCatching {
Expand Down Expand Up @@ -56,4 +73,9 @@ class LoginPageViewModel(private val loginPageRepoImpl: LoginPageRepoImpl) : Vie
Log.d("error", "서버 통신 실패")
}
}

companion object {
const val idPattern = "^(?=.*\\d)(?=.*[a-zA-Z]).{6,10}\$"
const val pwPattern = "^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[\$@\$!%*#?&]).{6,12}\$"
}
}
8 changes: 6 additions & 2 deletions app/src/main/res/layout/activity_signup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools">

<data>
<import type="android.view.View"/>

<variable
name="vmSignUp"
Expand Down Expand Up @@ -46,6 +47,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:backgroundTint="@{!vmSignUp.signUpId.empty &amp; !vmSignUp.isValidId()? @color/red_500 : @color/black}"
android:hint="아이디를 입력하세요"
android:inputType="text"
android:text="@={vmSignUp.signUpId}"
Expand All @@ -60,7 +62,7 @@
android:text="ID 조건: 영문, 숫자가 포함되어야 하고 6~10글자 이내"
android:textColor="@color/red_500"
android:textSize="8sp"
android:visibility="invisible"
visibility="@{ !vmSignUp.signUpId.empty &amp; !vmSignUp.isValidId() ? View.VISIBLE : View.GONE}"
app:layout_constraintStart_toStartOf="@id/et_id"
app:layout_constraintTop_toBottomOf="@id/et_id" />

Expand All @@ -85,6 +87,7 @@
android:hint="비밀번호를 입력하세요"
android:inputType="textPassword"
android:text="@={vmSignUp.signUpPwd}"
android:backgroundTint="@{!vmSignUp.signUpPwd.empty &amp; !vmSignUp.isValidPwd()? @color/red_500 : @color/black}"
app:layout_constraintEnd_toEndOf="@id/et_id"
app:layout_constraintStart_toStartOf="@id/et_id"
app:layout_constraintTop_toBottomOf="@id/tv_password" />
Expand All @@ -96,7 +99,7 @@
android:text="Password 조건: 영문, 숫자, 특수문자가 포함되어야 하고 6~12글자 이내"
android:textColor="@color/red_500"
android:textSize="8sp"
android:visibility="invisible"
android:visibility="@{ !vmSignUp.signUpPwd.empty &amp; !vmSignUp.isValidPwd() ? View.VISIBLE : View.GONE}"
app:layout_constraintStart_toStartOf="@id/et_password"
app:layout_constraintTop_toBottomOf="@id/et_password" />

Expand Down Expand Up @@ -150,6 +153,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:enabled="@{vmSignUp.canClickSignUpBtn}"
android:padding="10dp"
android:text="회원가입 완료"
app:layout_constraintEnd_toEndOf="@id/et_id"
Expand Down

0 comments on commit 380a614

Please sign in to comment.