Skip to content

Commit

Permalink
[FEAT/#8] 로그인 서버 통신 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed May 12, 2023
1 parent 95b1324 commit 38648d3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -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?,
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +19,11 @@ class AuthRepositoryImpl @Inject constructor(
authDataSource.postSignup(requestPostSignUpDto).data
}

override suspend fun postSignin(requestPostSignInDto: RequestPostSignInDto): Result<ResponsePostSignInDto?> =
runCatching {
authDataSource.postSignIn(requestPostSignInDto).data
}

override fun setAutoLogin(isAutoLogin: Boolean) {
sharedPrefDataSource.isAutoLogin = isAutoLogin
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,4 +13,9 @@ interface AuthService {
suspend fun postSignUp(
@Body requestPostSignUpDto: RequestPostSignUpDto,
): BaseResponse<ResponsePostSignUpDto>

@POST("sign-in")
suspend fun postSignIn(
@Body requestPostSignInDto: RequestPostSignInDto,
): BaseResponse<ResponsePostSignInDto>
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,4 +15,9 @@ class AuthDataSource @Inject constructor(
requestPostSignUpDto: RequestPostSignUpDto,
): BaseResponse<ResponsePostSignUpDto> =
authService.postSignUp(requestPostSignUpDto)

suspend fun postSignIn(
requestPostSignInDto: RequestPostSignInDto,
): BaseResponse<ResponsePostSignInDto> =
authService.postSignIn(requestPostSignInDto)
}
2 changes: 1 addition & 1 deletion app/src/main/java/org/android/go/sopt/domain/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ data class User(
val id: String = "",
val pwd: String = "",
val name: String = "익명",
val specialty: String = "",
val specialty: String? = "",
) : Parcelable
Original file line number Diff line number Diff line change
@@ -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<ResponsePostSignUpDto?>

suspend fun postSignin(requestPostSignInDto: RequestPostSignInDto): Result<ResponsePostSignInDto?>

fun setAutoLogin(isAutoLogin: Boolean)

fun getAutoLogin(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,8 +24,13 @@ class LoginViewModel @Inject constructor(
val loginState: LiveData<LocalUiState>
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()
Expand All @@ -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()}")
}
}
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 38648d3

Please sign in to comment.