generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat/#10] SignIn, SignUp 스크린의 서버통신을 Coroutine 방식으로 변경합니다.
- Loading branch information
Showing
4 changed files
with
85 additions
and
122 deletions.
There are no files selected for viewing
11 changes: 6 additions & 5 deletions
11
app/src/main/java/org/sopt/and/presentation/ui/auth/screen/AuthState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
package org.sopt.and.presentation.ui.auth.screen | ||
|
||
import org.sopt.and.data.remote.model.response.UserRegistrationResponseDto | ||
import org.sopt.and.domain.model.Token | ||
import org.sopt.and.domain.model.UserNo | ||
|
||
sealed class SignInState { | ||
data object Idle : SignInState() | ||
data object EmailEmpty : SignInState() | ||
data object PasswordEmpty : SignInState() | ||
data object Success : SignInState() | ||
data object Loading : SignInState() | ||
data class Success(val result: Token) : SignInState() | ||
data class Failure(val errorMessage: String) : SignInState() | ||
} | ||
|
||
sealed class SignUpState { | ||
data object Idle : SignUpState() | ||
data class Success(val response: UserRegistrationResponseDto?) : SignUpState() | ||
data object Loading : SignUpState() | ||
data class Success(val result: UserNo) : SignUpState() | ||
data class Failure(val errorMessage: String) : SignUpState() | ||
} |
102 changes: 30 additions & 72 deletions
102
app/src/main/java/org/sopt/and/presentation/ui/auth/screen/AuthViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,64 @@ | ||
package org.sopt.and.presentation.ui.auth.screen | ||
|
||
import android.util.Log | ||
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.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import org.sopt.and.data.remote.model.request.LoginRequestDto | ||
import org.sopt.and.data.remote.model.request.UserRegistrationRequestDto | ||
import org.sopt.and.data.remote.model.response.LoginResponseDto | ||
import org.sopt.and.data.remote.model.response.UserRegistrationResponseDto | ||
import org.sopt.and.domain.model.User | ||
import org.sopt.and.domain.repository.AuthRepository | ||
import org.sopt.and.domain.repository.TokenRepository | ||
import org.sopt.and.di.ServicePool | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class AuthViewModel @Inject constructor( | ||
private val tokenRepository: TokenRepository, | ||
private val authRepository: AuthRepository, | ||
) : ViewModel() { | ||
private val authService by lazy { ServicePool.authService } | ||
|
||
private val _signInState = MutableStateFlow<SignInState>(SignInState.Idle) | ||
val signInState: StateFlow<SignInState> = _signInState | ||
|
||
private val _signInUserName = MutableLiveData("") | ||
val signInUserName: LiveData<String> = _signInUserName | ||
|
||
private val _signInPassword = MutableLiveData("") | ||
val signInPassword: LiveData<String> = _signInPassword | ||
private val _token = MutableStateFlow("") | ||
val token: StateFlow<String> = _token | ||
|
||
private val _signUpState = MutableStateFlow<SignUpState>(SignUpState.Idle) | ||
val signUpState: StateFlow<SignUpState> = _signUpState | ||
|
||
private val _signUpUserName = MutableLiveData("") | ||
val signUpUserName: LiveData<String> = _signUpUserName | ||
|
||
private val _signUpPassword = MutableLiveData("") | ||
val signUpPassword: LiveData<String> = _signUpPassword | ||
|
||
private val _signUpHobby = MutableLiveData("") | ||
val signUpHobby: LiveData<String> = _signUpHobby | ||
|
||
|
||
|
||
fun validateSignIn(inputEmail: String, inputPassword: String) { | ||
fun validateSignIn(username: String, password: String) { | ||
_signInState.value = SignInState.Loading | ||
viewModelScope.launch { | ||
authService.login(LoginRequestDto(inputEmail, inputPassword)).enqueue(object : | ||
Callback<LoginResponseDto> { | ||
override fun onResponse( | ||
call: Call<LoginResponseDto>, | ||
response: Response<LoginResponseDto> | ||
) { | ||
if (response.isSuccessful) { | ||
_signInState.value = SignInState.Success | ||
tokenRepository.setToken(token = response.body()!!.token) | ||
Log.d("token", tokenRepository.getToken()) | ||
} else { | ||
_signInState.value = SignInState.Failure(response.message()) | ||
} | ||
} | ||
|
||
override fun onFailure( | ||
call: Call<LoginResponseDto>, | ||
t: Throwable | ||
) { | ||
_signInState.value = SignInState.Failure(t.message.toString()) | ||
val result = authRepository.login(username = username, password = password) | ||
_signInState.value = result.fold( | ||
onSuccess = { token -> | ||
tokenRepository.setToken(token.token) | ||
Log.d("token","토큰 저장 완료! 저장값은 ${token.token} 입니다.") | ||
SignInState.Success(token) | ||
}, | ||
onFailure = { | ||
SignInState.Failure(it.localizedMessage ?: "에러 발생") | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
fun validateSignUp(email: String, password: String, hobby: String) { | ||
fun validateSignUp(username: String, password: String, hobby: String) { | ||
_signUpState.value = SignUpState.Loading | ||
viewModelScope.launch { | ||
authService.registerUser(UserRegistrationRequestDto(email, password, hobby)) | ||
.enqueue(object : | ||
Callback<UserRegistrationResponseDto> { | ||
override fun onResponse( | ||
call: Call<UserRegistrationResponseDto>, | ||
response: Response<UserRegistrationResponseDto> | ||
) { | ||
if (response.isSuccessful) { | ||
_signUpState.value = SignUpState.Success(response.body()) | ||
} else { | ||
_signUpState.value = SignUpState.Failure(response.message()) | ||
} | ||
} | ||
|
||
override fun onFailure( | ||
call: Call<UserRegistrationResponseDto>, | ||
t: Throwable | ||
) { | ||
_signUpState.value = SignUpState.Failure(t.message.toString()) | ||
} | ||
val result = authRepository.registerUser(user = User( | ||
username = username, | ||
password = password, | ||
hobby = hobby | ||
)) | ||
_signUpState.value = result.fold( | ||
onSuccess = { | ||
SignUpState.Success(it) | ||
}, | ||
onFailure = { | ||
SignUpState.Failure(it.localizedMessage ?: "에러 발생") | ||
} | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters