Skip to content

Commit

Permalink
#8 [feat] : 유저 로그인 api 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsuhyun committed Nov 11, 2024
1 parent ef08fbd commit 1445650
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/org/sopt/and/feature/login/LoginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import org.sopt.and.R
import org.sopt.and.core.designsystem.component.AuthTextField
import org.sopt.and.core.designsystem.component.SocialLoginButtonGroup
import org.sopt.and.core.designsystem.component.WavveLoginButton
import org.sopt.and.data.model.request.UserLoginRequest
import org.sopt.and.feature.main.Routes
import org.sopt.and.ui.theme.WavveTheme
import org.sopt.and.utils.noRippleClickable
Expand Down Expand Up @@ -187,6 +188,12 @@ fun LoginScreen(
localEmail = localEmail,
localPassword = localPassword,
onSuccess = { email, password ->
viewModel.postUserLogin(
body = UserLoginRequest(
username = "email",
password = "password"
)
)
onLoginSuccess(email, password)
},
onFailure = {
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/org/sopt/and/feature/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
package org.sopt.and.feature.login

import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.sopt.and.data.ServicePool
import org.sopt.and.data.model.dto.ResponseUserTokenDto
import org.sopt.and.data.model.request.UserLoginRequest
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginViewModel : ViewModel() {
private val userService by lazy { ServicePool.userService }

private val _userState = mutableStateOf<ResponseUserTokenDto?>(null)
val userState: State<ResponseUserTokenDto?> get() = _userState

fun postUserLogin(body: UserLoginRequest) {
userService.postUserLogin(
body = body
).enqueue(object : Callback<ResponseUserTokenDto> {
override fun onResponse(
call: Call<ResponseUserTokenDto>,
response: Response<ResponseUserTokenDto>
) {
if (response.isSuccessful) {
_userState.value = response.body()
Log.d("postUserLogin", response.body().toString())

} else {
val error = response.message()
Log.e("error", error.toString())
}
}

override fun onFailure(call: Call<ResponseUserTokenDto>, t: Throwable) {
Log.e("failure", t.message.toString())
}
})
}

private val _email = MutableLiveData("")
val email: LiveData<String> get() = _email

Expand Down

0 comments on commit 1445650

Please sign in to comment.