Skip to content

Commit

Permalink
[Feature/#9] 회원가입 기능에 viewModel 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeun5744 committed Jun 28, 2023
1 parent b5ad89d commit c14d32f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
package org.android.go.sopt.present.loginPage

import android.content.Intent
import android.content.res.ColorStateList
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import org.android.go.sopt.MainActivity
import org.android.go.sopt.R
import org.android.go.sopt.RequestSignUpDto
import org.android.go.sopt.ResponseSignUpDto
import org.android.go.sopt.databinding.ActivitySignupBinding
import org.android.go.sopt.present.viewModel.LoginViewModel
import org.android.go.sopt.remote.ServicePool
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 retrofit2.Call
import retrofit2.Response
import java.util.regex.Pattern
import android.text.TextWatcher as TextWatcher


class SignUpActivity : AppCompatActivity() {

private val viewModel by viewModels<LoginViewModel>()
private val viewModel: LoginPageViewModel by viewModels { ViewModelFactory() }

lateinit var binding: ActivitySignupBinding
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -36,6 +27,7 @@ class SignUpActivity : AppCompatActivity() {
setContentView(binding.root)

signUp()
observeIsSignUpSuccess()
}

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
Expand All @@ -55,109 +47,56 @@ class SignUpActivity : AppCompatActivity() {
if (binding.etId.text.length in 6..10 && binding.etPassword.text.length in 8..12) {
with(binding) {
viewModel.signUp(
etId.text.toString(),
etPassword.text.toString(),
etName.text.toString(),
etSpeciality.text.toString()
RequestSignUpDto(
etId.text.toString(),
etPassword.text.toString(),
etName.text.toString(),
etSpeciality.text.toString(),
),
)
}

viewModel.signUpResult.observe(this) {
makeToastMessage("회원가입 성공")
val intent = Intent(this@SignUpActivity, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}

} else {
Snackbar.make(
binding.root,
"회원가입이 실패했다.",
Snackbar.LENGTH_SHORT
).show()
makeToastMessage("회원가입 조건 미충족")
}
}
}

private fun observeIsSignUpSuccess() {
viewModel.signUpResult.observe(this) { signUpResult ->
if (signUpResult) {
makeToastMessage("회원가입 성공")
val intent = Intent(this, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
} else {
makeToastMessage("회원가입 실패")
}
}
}

private fun canClickButton() {

var correctId: Boolean = false
var correctPw: Boolean = false

with(binding) {
btnSignup.isEnabled = false

etId.addTextChangedListener(object : TextWatcher {

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


val textWatcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {


}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

correctId = Pattern.matches(idPattern, etId.text)
if (!correctId) {
tvIdWarn.visibility = View.VISIBLE
correctId = false
etId.backgroundTintList = ColorStateList.valueOf(getColor(R.color.red_500))
} else {
tvIdWarn.visibility = View.GONE
correctId = true
etId.backgroundTintList = ColorStateList.valueOf(getColor(R.color.black))
}

}

override fun afterTextChanged(p0: Editable?) {
if (correctId && correctPw) {
if (etId.text.length in 6..10 && etPassword.text.length in 8..12) {
btnSignup.isEnabled = true
}
}

})
etPassword.addTextChangedListener(object : TextWatcher {

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



override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
correctPw = Pattern.matches(pwPattern, etPassword.text)
if (!correctPw) {
tvPwWarn.visibility = View.VISIBLE
correctPw = false
etPassword.backgroundTintList =
ColorStateList.valueOf(getColor(R.color.red_500))
} else {
tvPwWarn.visibility = View.GONE
correctPw = true
etPassword.backgroundTintList =
ColorStateList.valueOf(getColor(R.color.black))
}
}

override fun afterTextChanged(p0: Editable?) {
if (correctId && correctPw) {
btnSignup.isEnabled = true
}
}
}

})


etId.addTextChangedListener(textWatcher)
etPassword.addTextChangedListener(textWatcher)
etName.addTextChangedListener(textWatcher)
etSpeciality.addTextChangedListener(textWatcher)
}

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.android.go.sopt.RequestSignUpDto
import org.android.go.sopt.remote.remoteData.model.RequestLogInDto
import org.android.go.sopt.remote.remoteData.repoImpl.LoginPageRepoImpl
import org.android.go.sopt.util.Event
import org.android.go.sopt.util.MyApplication

class LoginPageViewModel(private val loginPageRepoImpl: LoginPageRepoImpl) : ViewModel() {
private val _loginResult = MutableLiveData(Event(false))
private val _loginResult = MutableLiveData<Event<Boolean>>()
val loginResult: LiveData<Event<Boolean>> get() = _loginResult

private val _signUpResult = MutableLiveData<Boolean>()
val signUpResult: LiveData<Boolean> get() = _signUpResult

fun login(request: RequestLogInDto) = viewModelScope.launch {
kotlin.runCatching {
loginPageRepoImpl.login(request)
Expand All @@ -24,4 +28,14 @@ class LoginPageViewModel(private val loginPageRepoImpl: LoginPageRepoImpl) : Vie
_loginResult.value = Event(false)
}
}

fun signUp(request: RequestSignUpDto) = viewModelScope.launch {
kotlin.runCatching {
loginPageRepoImpl.signUp(request)
}.onSuccess {
_signUpResult.value = true
}.onFailure {
_signUpResult.value = false
}
}
}

0 comments on commit c14d32f

Please sign in to comment.