-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WEEK7 : 필수과제 완료 #5
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.sopt.dosopttemplate | ||
|
||
import android.provider.ContactsContract.CommonDataKinds.Nickname | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import org.sopt.dosopttemplate.ServicePool.authService | ||
import org.sopt.dosopttemplate.data.RequestLoginDto | ||
import org.sopt.dosopttemplate.data.RequestSignUpDto | ||
|
||
class AuthViewModel : ViewModel() { | ||
|
||
// UI State 사용 | ||
private val _loginState = MutableStateFlow<LoginState>(LoginState.Loading) | ||
val loginState: StateFlow<LoginState> = _loginState.asStateFlow() | ||
|
||
private val _signUpState = MutableStateFlow<SignUpState>(SignUpState.Loading) | ||
val signUpState: StateFlow<SignUpState> = _signUpState.asStateFlow() | ||
|
||
fun login(id: String, password: String) { | ||
viewModelScope.launch { | ||
kotlin.runCatching { | ||
val response = authService.login(RequestLoginDto(id, password)) | ||
response.body() | ||
}.onSuccess { | ||
if(it != null){ | ||
_loginState.value = LoginState.Success(it) | ||
} else{ | ||
_loginState.value = LoginState.Error | ||
} | ||
}.onFailure { | ||
_loginState.value = LoginState.Error | ||
} | ||
} | ||
} | ||
|
||
fun signUp(id: String, password: String, nickname: String){ | ||
viewModelScope.launch { | ||
kotlin.runCatching { | ||
val response = authService.signup(RequestSignUpDto(id, password, nickname)) | ||
response.body() | ||
}.onSuccess { | ||
if(it!=null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드 정렬을 생활화 합시다 |
||
_signUpState.value = SignUpState.Success(it) | ||
}else{ | ||
_signUpState. value = SignUpState.Error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value 띄어쓰기 부분! 붙여야 할 것 같아요~ |
||
} | ||
}.onFailure { | ||
_signUpState.value = SignUpState.Error | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 하나의 UIState를 만들어서 적용할 수도 있답니다! LoginState와 SignUpState를 따로 나누지 않고요 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.sopt.dosopttemplate | ||
|
||
import org.sopt.dosopttemplate.data.ResponseLoginDto | ||
|
||
sealed class LoginState{ | ||
object Loading : LoginState() | ||
data class Success(val data: ResponseLoginDto) : LoginState() | ||
object Error : LoginState() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,13 @@ package org.sopt.dosopttemplate | |
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.datepicker.MaterialDatePicker | ||
import com.google.android.material.snackbar.Snackbar | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
import org.sopt.dosopttemplate.ServicePool.authService | ||
import org.sopt.dosopttemplate.Utils.showToast | ||
import org.sopt.dosopttemplate.data.RequestSignUpDto | ||
|
@@ -22,6 +26,7 @@ import kotlin.properties.Delegates | |
class SignUpActivity : AppCompatActivity() { | ||
lateinit var binding: ActivitySignUpBinding | ||
private var available by Delegates.notNull<Boolean>() | ||
private val authViewModel by viewModels<AuthViewModel>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
@@ -30,6 +35,7 @@ class SignUpActivity : AppCompatActivity() { | |
|
||
birth() | ||
signup() | ||
observeSignUpResult() | ||
} | ||
|
||
// 생일 정보 입력 | ||
|
@@ -116,39 +122,32 @@ class SignUpActivity : AppCompatActivity() { | |
if (available) { | ||
showToast("중복된 아이디입니다.") | ||
} else if (testResult && !available) { // 조건이 적절하고 중복된 아이디가 아닌 경우 | ||
authService.signup(RequestSignUpDto(id, pw, nickname)) | ||
.enqueue(object : retrofit2.Callback<ResponseSignUpDto> { | ||
override fun onResponse( | ||
call: Call<ResponseSignUpDto>, | ||
response: Response<ResponseSignUpDto> | ||
) { | ||
when (response.code()) { | ||
201 -> { | ||
// 회원가입 성공 | ||
showToast("회원가입 성공!") | ||
val intent = | ||
Intent(this@SignUpActivity, LoginActivity::class.java) | ||
startActivity(intent) | ||
} | ||
|
||
400 -> { | ||
// 회원가입 실패 | ||
showToast("회원가입에 실패했습니다.") | ||
} | ||
|
||
else -> { | ||
showToast("서버 에러 발생") | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseSignUpDto>, t: Throwable) { | ||
showToast("네트워크 에러 발생") | ||
} | ||
}) | ||
authViewModel.signUp( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. authViewModel의 signup과 버튼을 누르는 signup함수 이름이 같아서 조금 헷갈렸어요. |
||
id = id, | ||
password = pw, | ||
nickname = nickname | ||
) | ||
} else { | ||
showToast("올바른 정보를 입력해주세요.") | ||
} | ||
} | ||
} | ||
|
||
private fun observeSignUpResult(){ | ||
lifecycleScope.launch { | ||
authViewModel.signUpState.collect{ SignUpState -> | ||
when(SignUpState){ | ||
is SignUpState.Success -> { | ||
showToast("회원가입 성공") | ||
} | ||
is SignUpState.Error -> { | ||
showToast("회원가입 실패") | ||
} | ||
is SignUpState.Loading -> { | ||
showToast("회원가입 중") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.sopt.dosopttemplate | ||
|
||
import org.sopt.dosopttemplate.data.ResponseSignUpDto | ||
|
||
sealed class SignUpState { | ||
object Loading : SignUpState() | ||
data class Success(val data: ResponseSignUpDto) : SignUpState() | ||
object Error : SignUpState() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지워줘도 됩니둥