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 27, 2023
1 parent d7ced05 commit 40fb87f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package org.android.go.sopt.present.loginPage

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.view.MotionEvent
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import org.android.go.sopt.MainActivity
import org.android.go.sopt.RequestSignUpDto
import org.android.go.sopt.ResponseSignUpDto
import org.android.go.sopt.databinding.ActivitySignupBinding
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 retrofit2.Call
import retrofit2.Response
import org.android.go.sopt.util.makeToastMessage
import android.text.TextWatcher as TextWatcher


class SignUpActivity : AppCompatActivity() {

private val singUpService = ServicePool.loginPageService
private val viewModel: LoginPageViewModel by viewModels { ViewModelFactory() }

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

signUp()
observeIsSignUpSuccess()
}

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
Expand All @@ -45,58 +45,37 @@ class SignUpActivity : AppCompatActivity() {
canClickButton()
binding.btnSignup.setOnClickListener {
if (binding.etId.text.length in 6..10 && binding.etPassword.text.length in 8..12) {

singUpService.signUp(
with(binding) {
with(binding) {
viewModel.signUp(
RequestSignUpDto(
etId.text.toString(),
etPassword.text.toString(),
etName.text.toString(),
etSpeciality.text.toString()

)
}
).enqueue(object : retrofit2.Callback<ResponseSignUpDto> {
override fun onResponse(
call: Call<ResponseSignUpDto>,
response: Response<ResponseSignUpDto>
) {
if (response.isSuccessful) {
response.body()?.message?.let {
Toast.makeText(
this@SignUpActivity,
response.body()?.message ?: "회원가입 성공",
Toast.LENGTH_SHORT
).show()

}
val intent = Intent(this@SignUpActivity, LoginActivity::class.java)
setResult(RESULT_OK, intent)
finish()
} else {
Toast.makeText(this@SignUpActivity, "회원가입 실패", Toast.LENGTH_SHORT)
.show()
}
}

override fun onFailure(call: Call<ResponseSignUpDto>, t: Throwable) {
Toast.makeText(this@SignUpActivity, "서버 통신 실패", Toast.LENGTH_SHORT).show()
}
})

etSpeciality.text.toString(),
),
)
}
} 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() {

with(binding) {
btnSignup.isEnabled = false

Expand All @@ -118,11 +97,6 @@ class SignUpActivity : AppCompatActivity() {
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 40fb87f

Please sign in to comment.