diff --git a/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt b/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt deleted file mode 100644 index 8f767a0..0000000 --- a/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt +++ /dev/null @@ -1,66 +0,0 @@ -package com.sopt.now.test.presentation - -import android.content.Intent -import android.os.Bundle -import androidx.activity.viewModels -import androidx.appcompat.app.AppCompatActivity -import com.sopt.now.databinding.ActivityLoginBinding -import com.sopt.now.test.core.util.context.showToast -import com.sopt.now.test.data.dto.request.RequestLoginDto - -class LoginActivity : AppCompatActivity() { - - private val binding by lazy { ActivityLoginBinding.inflate(layoutInflater) } - private val loginViewModel by viewModels() - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(binding.root) - - loginViewModel.initializeApiFactory(this) - - initLoginBtnClickListener() - initPostLoginObserver() - initSignUpBtnClickListener() - } - - // 로그인 - private fun initLoginBtnClickListener() { - with(binding){ - btnLogin.setOnClickListener { - loginViewModel.postLogin(RequestLoginDto( - authenticationId = etLoginId.text.toString(), - password = etLoginPw.text.toString() - )) - } - } - } - - private fun initPostLoginObserver() { - loginViewModel.postLoginLiveData.observe(this) { response -> - if(response.isSuccess){ - navigateToMain(response.message) - } - showToast(response.message) - } - } - - // 메인 페이지로 이동 - private fun navigateToMain(content: String) { - Intent(this, MainActivity::class.java).apply { - startActivity(this) - finish() - } - showToast(content) - } - - // 회원가입 페이지로 이동 - private fun initSignUpBtnClickListener() { - binding.tvSignUp.setOnClickListener { - Intent(this, SignUpActivity::class.java).apply { - startActivity(this) - finish() - } - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/test/presentation/login/LoginActivity.kt b/app/src/main/java/com/sopt/now/test/presentation/login/LoginActivity.kt new file mode 100644 index 0000000..17ee1a2 --- /dev/null +++ b/app/src/main/java/com/sopt/now/test/presentation/login/LoginActivity.kt @@ -0,0 +1,92 @@ +package com.sopt.now.test.presentation.login + +import android.content.Intent +import android.os.Bundle +import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatActivity +import com.sopt.now.R +import com.sopt.now.databinding.ActivityLoginBinding +import com.sopt.now.test.core.util.context.showToast +import com.sopt.now.test.core.view.UiState +import com.sopt.now.test.data.ApiFactory +import com.sopt.now.test.data.UserPreference +import com.sopt.now.test.data.dto.request.RequestLoginDto +import com.sopt.now.test.presentation.MainActivity +import com.sopt.now.test.presentation.signup.SignUpActivity +import timber.log.Timber + +class LoginActivity : AppCompatActivity() { + + private val binding by lazy { ActivityLoginBinding.inflate(layoutInflater) } + private val loginViewModel by viewModels() + private lateinit var userPreference: UserPreference + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(binding.root) + + initializeUserPreference() + initLoginBtnClickListener() + initPostLoginObserver() + initSignUpBtnClickListener() + } + + // UserPreference 초기화 + private fun initializeUserPreference() { + userPreference = UserPreference(this) + ApiFactory.initializeUserPreference(userPreference) + } + + // 로그인 + private fun initLoginBtnClickListener() { + with(binding) { + btnLogin.setOnClickListener { + loginViewModel.postLogin(getLoginRequestDto()) + } + } + } + + private fun getLoginRequestDto(): RequestLoginDto { + with(binding) { + val id = etLoginId.text.toString() + val password = etLoginPw.text.toString() + return RequestLoginDto( + authenticationId = id, + password = password + ) + } + } + + private fun initPostLoginObserver() { + loginViewModel.postLoginLiveData.observe(this) { + when (it) { + is UiState.Success -> { + userPreference.getUserId()?.let { + navigateToMain(it) + } + } + is UiState.Failure -> showToast(it.errorMessage) + is UiState.Loading -> Timber.d(getString(R.string.message_loading)) + } + } + } + + // 메인 페이지로 이동 + private fun navigateToMain(userId: String) { + Intent(this, MainActivity::class.java).apply { + startActivity(this) + finish() + } + showToast(getString(R.string.message_login_success, userId)) + } + + // 회원가입 페이지로 이동 + private fun initSignUpBtnClickListener() { + binding.tvSignUp.setOnClickListener { + Intent(this, SignUpActivity::class.java).apply { + startActivity(this) + finish() + } + } + } +} \ No newline at end of file