diff --git a/app/src/main/java/org/android/go/sopt/data/entity/remote/request/RequestPostSignInDto.kt b/app/src/main/java/org/android/go/sopt/data/entity/remote/request/RequestPostSignInDto.kt new file mode 100644 index 0000000..bc7a894 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/data/entity/remote/request/RequestPostSignInDto.kt @@ -0,0 +1,12 @@ +package org.android.go.sopt.data.entity.remote.request + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class RequestPostSignInDto( + @SerialName("id") + val id: String, + @SerialName("password") + val password: String, +) diff --git a/app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponsePostSignInDto.kt b/app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponsePostSignInDto.kt new file mode 100644 index 0000000..01e6ed2 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponsePostSignInDto.kt @@ -0,0 +1,14 @@ +package org.android.go.sopt.data.entity.remote.response + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ResponsePostSignInDto( + @SerialName("id") + val id: String, + @SerialName("name") + val name: String, + @SerialName("skill") + val skill: String?, +) diff --git a/app/src/main/java/org/android/go/sopt/data/repository/AuthRepositoryImpl.kt b/app/src/main/java/org/android/go/sopt/data/repository/AuthRepositoryImpl.kt index b0524d0..5ccc43d 100644 --- a/app/src/main/java/org/android/go/sopt/data/repository/AuthRepositoryImpl.kt +++ b/app/src/main/java/org/android/go/sopt/data/repository/AuthRepositoryImpl.kt @@ -1,6 +1,8 @@ package org.android.go.sopt.data.repository +import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto import org.android.go.sopt.data.entity.remote.request.RequestPostSignUpDto +import org.android.go.sopt.data.entity.remote.response.ResponsePostSignInDto import org.android.go.sopt.data.entity.remote.response.ResponsePostSignUpDto import org.android.go.sopt.data.source.local.SharedPrefDataSource import org.android.go.sopt.data.source.remote.AuthDataSource @@ -17,6 +19,11 @@ class AuthRepositoryImpl @Inject constructor( authDataSource.postSignup(requestPostSignUpDto).data } + override suspend fun postSignin(requestPostSignInDto: RequestPostSignInDto): Result = + runCatching { + authDataSource.postSignIn(requestPostSignInDto).data + } + override fun setAutoLogin(isAutoLogin: Boolean) { sharedPrefDataSource.isAutoLogin = isAutoLogin } diff --git a/app/src/main/java/org/android/go/sopt/data/service/AuthService.kt b/app/src/main/java/org/android/go/sopt/data/service/AuthService.kt index 9dd01c1..233e16a 100644 --- a/app/src/main/java/org/android/go/sopt/data/service/AuthService.kt +++ b/app/src/main/java/org/android/go/sopt/data/service/AuthService.kt @@ -1,6 +1,8 @@ package org.android.go.sopt.data.service +import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto import org.android.go.sopt.data.entity.remote.request.RequestPostSignUpDto +import org.android.go.sopt.data.entity.remote.response.ResponsePostSignInDto import org.android.go.sopt.data.entity.remote.response.ResponsePostSignUpDto import org.android.go.sopt.data.entity.remote.response.wrapper.BaseResponse import retrofit2.http.Body @@ -11,4 +13,9 @@ interface AuthService { suspend fun postSignUp( @Body requestPostSignUpDto: RequestPostSignUpDto, ): BaseResponse + + @POST("sign-in") + suspend fun postSignIn( + @Body requestPostSignInDto: RequestPostSignInDto, + ): BaseResponse } diff --git a/app/src/main/java/org/android/go/sopt/data/source/remote/AuthDataSource.kt b/app/src/main/java/org/android/go/sopt/data/source/remote/AuthDataSource.kt index 0c448a9..1f993cd 100644 --- a/app/src/main/java/org/android/go/sopt/data/source/remote/AuthDataSource.kt +++ b/app/src/main/java/org/android/go/sopt/data/source/remote/AuthDataSource.kt @@ -1,6 +1,8 @@ package org.android.go.sopt.data.source.remote +import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto import org.android.go.sopt.data.entity.remote.request.RequestPostSignUpDto +import org.android.go.sopt.data.entity.remote.response.ResponsePostSignInDto import org.android.go.sopt.data.entity.remote.response.ResponsePostSignUpDto import org.android.go.sopt.data.entity.remote.response.wrapper.BaseResponse import org.android.go.sopt.data.service.AuthService @@ -13,4 +15,9 @@ class AuthDataSource @Inject constructor( requestPostSignUpDto: RequestPostSignUpDto, ): BaseResponse = authService.postSignUp(requestPostSignUpDto) + + suspend fun postSignIn( + requestPostSignInDto: RequestPostSignInDto, + ): BaseResponse = + authService.postSignIn(requestPostSignInDto) } diff --git a/app/src/main/java/org/android/go/sopt/domain/model/User.kt b/app/src/main/java/org/android/go/sopt/domain/model/User.kt index 6fa189b..358915a 100644 --- a/app/src/main/java/org/android/go/sopt/domain/model/User.kt +++ b/app/src/main/java/org/android/go/sopt/domain/model/User.kt @@ -8,5 +8,5 @@ data class User( val id: String = "", val pwd: String = "", val name: String = "익명", - val specialty: String = "", + val specialty: String? = "", ) : Parcelable diff --git a/app/src/main/java/org/android/go/sopt/domain/repository/AuthRepository.kt b/app/src/main/java/org/android/go/sopt/domain/repository/AuthRepository.kt index f19542b..319a4da 100644 --- a/app/src/main/java/org/android/go/sopt/domain/repository/AuthRepository.kt +++ b/app/src/main/java/org/android/go/sopt/domain/repository/AuthRepository.kt @@ -1,12 +1,16 @@ package org.android.go.sopt.domain.repository +import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto import org.android.go.sopt.data.entity.remote.request.RequestPostSignUpDto +import org.android.go.sopt.data.entity.remote.response.ResponsePostSignInDto import org.android.go.sopt.data.entity.remote.response.ResponsePostSignUpDto import org.android.go.sopt.domain.model.User interface AuthRepository { suspend fun postSignup(requestPostSignUpDto: RequestPostSignUpDto): Result + suspend fun postSignin(requestPostSignInDto: RequestPostSignInDto): Result + fun setAutoLogin(isAutoLogin: Boolean) fun getAutoLogin(): Boolean diff --git a/app/src/main/java/org/android/go/sopt/presentation/login/LoginViewModel.kt b/app/src/main/java/org/android/go/sopt/presentation/login/LoginViewModel.kt index 7f77ed1..e49b601 100644 --- a/app/src/main/java/org/android/go/sopt/presentation/login/LoginViewModel.kt +++ b/app/src/main/java/org/android/go/sopt/presentation/login/LoginViewModel.kt @@ -3,11 +3,17 @@ package org.android.go.sopt.presentation.login import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto +import org.android.go.sopt.domain.model.User import org.android.go.sopt.domain.repository.AuthRepository import org.android.go.sopt.util.state.LocalUiState import org.android.go.sopt.util.state.LocalUiState.Failure import org.android.go.sopt.util.state.LocalUiState.Success +import retrofit2.HttpException +import timber.log.Timber import javax.inject.Inject @HiltViewModel @@ -18,8 +24,13 @@ class LoginViewModel @Inject constructor( val loginState: LiveData get() = _loginState - val id = MutableLiveData("") - val pwd = MutableLiveData("") + val _id = MutableLiveData("") + private val id: String + get() = _id.value?.trim() ?: "" + + val _pwd = MutableLiveData("") + private val pwd: String + get() = _pwd.value?.trim() ?: "" init { setupAutoLogin() @@ -31,15 +42,38 @@ class LoginViewModel @Inject constructor( } } - private fun isValidInput() = !id.value.isNullOrBlank() && !pwd.value.isNullOrBlank() + private fun isValidInput() = id.isNotBlank() && pwd.isNotBlank() fun login() { if (!isValidInput()) { _loginState.value = Failure(null) return } - // TODO : 로그인 정보 저장 - authRepository.setAutoLogin(true) - _loginState.value = Success + + val requestPostSignInDto = RequestPostSignInDto( + id = id, + password = pwd, + ) + viewModelScope.launch { + authRepository.postSignin(requestPostSignInDto) + .onSuccess { response -> + authRepository.setSignedUpUser( + User( + id = requestPostSignInDto.id, + pwd = requestPostSignInDto.password, + name = requireNotNull(response).name, + specialty = response.skill, + ), + ) + authRepository.setAutoLogin(true) + _loginState.value = Success + Timber.d("POST SIGNIN SUCCESS : $response") + } + .onFailure { t -> + if (t is HttpException) { + Timber.e("POST SIGNIN FAIL ${t.code()} : ${t.message()}") + } + } + } } } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index e722c17..7fc435a 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -50,7 +50,7 @@ android:imeOptions="actionNext" android:inputType="text" android:paddingVertical="16dp" - android:text="@={vm.id}" + android:text="@={vm._id}" android:textAppearance="?textAppearanceBodyLarge" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -78,7 +78,7 @@ android:imeOptions="actionDone" android:inputType="textPassword" android:paddingVertical="16dp" - android:text="@={vm.pwd}" + android:text="@={vm._pwd}" android:textAppearance="?textAppearanceBodyLarge" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"