Skip to content

Commit

Permalink
[mod] #18 Login 레포지토리로 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
cacaocoffee committed Jun 5, 2024
1 parent f1ad914 commit 8339904
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
14 changes: 8 additions & 6 deletions app/src/main/java/com/sopt/now/ui/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import com.sopt.now.R
import com.sopt.now.data.model.User
import com.sopt.now.data.dto.request.RequestLoginDto
import com.sopt.now.databinding.ActivityLoginBinding
import com.sopt.now.domain.entity.request.AuthRequestModel
import com.sopt.now.ui.main.MainActivity
import com.sopt.now.ui.signup.SignUpActivity
import com.sopt.now.util.BindingActivity
Expand All @@ -18,7 +18,7 @@ import com.sopt.now.util.toast

class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_login) {
private lateinit var resultLauncher: ActivityResultLauncher<Intent>
private val viewModel by viewModels<LoginViewModel>()
private val viewModel: LoginViewModel by viewModels { LoginViewModelFactory() }
private lateinit var user: User

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -79,14 +79,16 @@ class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_lo

private fun initLoginBtnClickListener() {
binding.btnLogin.setOnClickListener {
viewModel.login(getLoginRequestDto())
viewModel.login(getLoginRequest())
}
}

private fun getLoginRequestDto() =
RequestLoginDto(
private fun getLoginRequest() =
AuthRequestModel(
authenticationId = binding.etLoginId.text.toString(),
password = binding.etLoginPassword.text.toString()
password = binding.etLoginPassword.text.toString(),
nickname = "",
phone = ""
)

companion object {
Expand Down
58 changes: 29 additions & 29 deletions app/src/main/java/com/sopt/now/ui/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@ package com.sopt.now.ui.login
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.data.api.ServicePool
import com.sopt.now.data.dto.request.RequestLoginDto
import com.sopt.now.data.model.User
import com.sopt.now.domain.entity.request.AuthRequestModel
import com.sopt.now.domain.repository.AuthRepository
import com.sopt.now.util.StringNetworkError.FAIL_ERROR
import com.sopt.now.util.StringNetworkError.LOGIN
import com.sopt.now.util.UiState
import kotlinx.coroutines.launch
import org.json.JSONObject
import retrofit2.HttpException

class LoginViewModel : ViewModel() {
private val loginService by lazy { ServicePool.authService }
class LoginViewModel(private val authRepository: AuthRepository) : ViewModel() {
private val _loginState = MutableLiveData<UiState<User>>()
val loginState = _loginState

fun login(request: RequestLoginDto) {
fun login(request: AuthRequestModel) {
_loginState.value = UiState.Loading
viewModelScope.launch {
runCatching {
loginService.login(request)
}.onSuccess { response ->
val userId = response.headers()["Location"].toString()
if (response.isSuccessful)
_loginState.value =
UiState.Success( User(
request.authenticationId,
request.password,
"",
"",
userId = userId
))
else {
val errorMessage =
JSONObject(response.errorBody()?.string()).getString("message")
_loginState.value = UiState.Error(errorMessage.toString())
authRepository.logIn(request)
.onSuccess { response ->
val userId = response.headers()["Location"].toString()
if (response.isSuccessful)
_loginState.value =
UiState.Success(
User(
request.authenticationId,
request.password,
"",
"",
userId = userId
)
)
else {
val errorMessage =
JSONObject(response.errorBody()?.string()).getString("message")
_loginState.value = UiState.Error(errorMessage.toString())
}
}.onFailure { e ->
if (e is HttpException) {
_loginState.value = UiState.Error(e.message())
} else {
_loginState.value = UiState.Error(FAIL_ERROR.format(LOGIN))
}
}
}.onFailure { e ->
if (e is HttpException) {
_loginState.value = UiState.Error(e.message())
} else {
_loginState.value = UiState.Error(FAIL_ERROR.format(LOGIN))
}
}
}
}
}

0 comments on commit 8339904

Please sign in to comment.