Skip to content

Commit

Permalink
#9 [Refactor] : 수정 로그인, 회원가입 UI 변경 및 예외처리
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaejugkim committed Nov 15, 2024
1 parent cbdf2ef commit 2f97125
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/sopt/and/data/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.sopt.and.BuildConfig
import org.sopt.and.data.service.MyService
import org.sopt.and.data.service.UserService
import retrofit2.Retrofit

Expand Down Expand Up @@ -35,4 +36,5 @@ object ApiFactory {

object ServicePool {
val userService = ApiFactory.create<UserService>()
val myService = ApiFactory.create<MyService>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,19 @@ fun SignUpScreen(
modifier = Modifier.padding(top = 8.dp)
)

Spacer(modifier = Modifier.weight(0.5f))
SignUpInfoRow(
iconResId = R.drawable.ic_info,
text = stringResource(R.string.sign_up_passwd)
)

Spacer(modifier = Modifier.weight(1f))
IdHobbyTextField(
valueState = hobbyState,
holderText = R.string.sign_up_hobby,
modifier = Modifier.padding(bottom = 8.dp)
)

Spacer(modifier = Modifier.weight(0.5f))
SignUpInfoRow(
iconResId = R.drawable.ic_info,
text = stringResource(R.string.sign_up_passwd)
)

Spacer(modifier = Modifier.weight(2f))
SocialServiceLogIn()
Spacer(modifier = Modifier.weight(8f))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.sopt.and.R
Expand All @@ -17,13 +15,10 @@ import org.sopt.and.data.dto.RequestUserLoginDto
import org.sopt.and.data.dto.RequestUserRegisterDto
import org.sopt.and.data.dto.ResponseUserLoginDto
import org.sopt.and.data.dto.ResponseUserRegisterDto
import org.sopt.and.domain.SharedPreferenceManager
import org.sopt.and.domain.SharedPreferenceManager.saveToken
import org.sopt.and.domain.SharedPreferenceManager.saveUserName
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.math.log

interface AuthState {
class Success(val type: AuthType) : AuthState
Expand All @@ -41,14 +36,10 @@ class UserViewModel : ViewModel() {
private val _authState = MutableStateFlow<AuthState?>(null) // 초기 상태로 null 설정
val authState: StateFlow<AuthState?> = _authState.asStateFlow()

// 유저 정보를 관리
private val _userState = mutableStateOf<ResponseUserLoginDto?>(null)
val userState: State<ResponseUserLoginDto?> get() = _userState


// 회원가입 로직
fun signUp(username: String, password: String, hobby: String) {
viewModelScope.launch {
// 호출 전 8자리 검증 진행
when (checkSignUpValue(username, password, hobby)) {
"idError" -> _authState.value = AuthState.Error(R.string.sign_up_error, AuthType.SIGNUP)
"passwdError" -> _authState.value = AuthState.Error(R.string.sign_up_paswd, AuthType.SIGNUP)
Expand All @@ -67,8 +58,7 @@ class UserViewModel : ViewModel() {
if (response.isSuccessful) {
_authState.value = AuthState.Success(AuthType.SIGNUP)
} else {
val error = response.message()
Log.e("error", error.toString())
_authState.value = AuthState.Error(R.string.sign_up_duplicate_name, AuthType.SIGNUP)
}
}

Expand All @@ -87,18 +77,13 @@ class UserViewModel : ViewModel() {
override fun onResponse(call: Call<ResponseUserLoginDto>, response: Response<ResponseUserLoginDto>) {
if(response.isSuccessful) {
val loginResponse = response.body()
_userState.value = loginResponse
loginResponse?.result?.token?.let {
// SharedPreference에 토큰 및 유저네임 저장
// SharedPreference에 토큰 저장
saveToken(it)
saveUserName(username)
}

Log.d("token?", _userState.value.toString())
_authState.value = AuthState.Success(AuthType.LOGIN)
} else {
val error = response.message()
Log.e("error", error.toString())
_authState.value = AuthState.Error(R.string.log_in_error, AuthType.LOGIN)
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="sign_up_error">유효하지 않은 아이디 입니다. 다시 한번 확인해주세요</string>
<string name="sign_up_paswd">유효하지 않은 비밀번호 입니다. 다시 한번 확인해주세요</string>
<string name="sign_up_error_hobby">취미는 8자 이내로 입력해주세요</string>
<string name="sign_up_duplicate_name">중복된 유저입니다. 다시 한번 확인해주세요</string>
<string name="sign_up_passwd">비밀번호는 8~20자 이내로 영문 대소문자, 숫자, 특수문자 중 3가지 이상 혼용하여 입력해주세요</string>
<string name="sign_up_id">로그인, 비밀번호 찾기, 알림에 사용되니 정확한 이메일을 입력해주세요.</string>
<string name="sign_up_hobby">취미</string>
Expand Down Expand Up @@ -57,4 +58,5 @@
<string name="home_cateogry_foreign">해외시리즈</string>
<string name="home_category_normal">시사교양</string>
<string name="home_category_kids">키즈</string>
<string name="my_hobby">취미</string>
</resources>

0 comments on commit 2f97125

Please sign in to comment.