-
Notifications
You must be signed in to change notification settings - Fork 0
7주차 세미나 필수과제 #12
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
base: develop
Are you sure you want to change the base?
7주차 세미나 필수과제 #12
Changes from all commits
715e359
85edff0
ff9886a
e4bf9e2
ee4c516
4dde14a
abfa597
08dd917
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,9 @@ | ||
| package org.sopt.dosopttemplate.presentation.auth.login | ||
|
|
||
| import org.sopt.dosopttemplate.server.auth.response.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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.sopt.dosopttemplate.presentation.auth.signUp | ||
|
|
||
| sealed class SignUpState { | ||
| object Loading : SignUpState() | ||
| data class Success(val data: Unit) : SignUpState() | ||
| object Error : SignUpState() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,27 @@ | ||
| package org.sopt.dosopttemplate.presentation.auth.signUp | ||
|
|
||
| import androidx.lifecycle.MutableLiveData | ||
| 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.server.ServicePool.authService | ||
| import org.sopt.dosopttemplate.server.auth.request.RequestSignUpDto | ||
| import java.util.regex.Pattern | ||
|
|
||
| class SignUpViewModel : ViewModel() { | ||
| private val _signUpSuccess: MutableLiveData<Boolean> = MutableLiveData() | ||
| val signUpSuccess: MutableLiveData<Boolean> = _signUpSuccess | ||
| private val _signUpState = MutableStateFlow<SignUpState>(SignUpState.Loading) | ||
| val signUpState: StateFlow<SignUpState> = _signUpState.asStateFlow() | ||
|
|
||
| fun signUp(id: String, name: String, password: String) { | ||
| viewModelScope.launch { | ||
| kotlin.runCatching { | ||
| authService.signUp(RequestSignUpDto(id, name, password)) | ||
| }.onSuccess { | ||
| signUpSuccess.value = it.isSuccessful | ||
| _signUpState.value = SignUpState.Success(it.body()!!) | ||
|
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. !!(Non-null assertion operator)를 사용하신 이유는 무엇인가요?! |
||
| }.onFailure { | ||
| // 에러 처리 로직 | ||
| _signUpState.value = SignUpState.Error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import android.view.View | |
| import android.view.ViewGroup | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.viewModels | ||
| import androidx.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.launch | ||
| import org.sopt.dosopttemplate.data.adaptor.UserAdaptor | ||
| import org.sopt.dosopttemplate.databinding.FragmentHomeBinding | ||
| import org.sopt.dosopttemplate.util.makeToast | ||
|
|
@@ -49,16 +51,21 @@ class HomeFragment : Fragment() { | |
|
|
||
| private fun getUserList() { | ||
| userViewModel.getUserList(2) | ||
| userViewModel.userSuccess.observe(viewLifecycleOwner) { | ||
| if (it) { | ||
| val userList = userViewModel.userResult.value?.data | ||
| if (userList != null) { | ||
| Log.e("서버 통신", "성공") | ||
|
|
||
| userAdapter.setUserList(userList) | ||
| lifecycleScope.launch { | ||
| userViewModel.userState.collect { userState -> | ||
|
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. flowWithLifecycle에 대해서 공부해보면 좋을 것 같아요! |
||
| when (userState) { | ||
| is UserState.Success -> { | ||
| val userList = userState.data.data | ||
| userAdapter.setUserList(userList) | ||
| binding.root.makeToast("유저 서버 통신 성공") | ||
| } | ||
| is UserState.Error -> { | ||
| binding.root.makeToast("유저 받아오기 실패") | ||
| } | ||
| is UserState.Loading -> { | ||
| binding.root.makeToast("유저 받아오는 중") | ||
| } | ||
| } | ||
| } else { | ||
| binding.root.makeToast("유저 받아오기 실패") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.sopt.dosopttemplate.presentation.main.home | ||
|
|
||
| import org.sopt.dosopttemplate.server.auth.response.ResponseUserListDto | ||
|
|
||
| sealed class UserState { | ||
| object Loading : UserState() | ||
| data class Success(val data: ResponseUserListDto) : UserState() | ||
| object Error : UserState() | ||
| } | ||
|
Comment on lines
+5
to
+9
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. 현재 만들어진 코드만 보았을 때는 LoginState, SignUpState, UserState에서 처리하는 상태들이 동일합니다! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,27 @@ | ||
| package org.sopt.dosopttemplate.presentation.main.home | ||
|
|
||
| import androidx.lifecycle.MutableLiveData | ||
| 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.server.ServicePool.userService | ||
| import org.sopt.dosopttemplate.server.auth.response.ResponseUserListDto | ||
|
|
||
| class UserViewModel : ViewModel() { | ||
| private val _userResult: MutableLiveData<ResponseUserListDto> = MutableLiveData() | ||
| val userResult: MutableLiveData<ResponseUserListDto> = _userResult | ||
|
|
||
| private val _userSuccess: MutableLiveData<Boolean> = MutableLiveData() | ||
| val userSuccess: MutableLiveData<Boolean> = _userSuccess | ||
| private val _userState = MutableStateFlow<UserState>(UserState.Loading) | ||
| val userState: StateFlow<UserState> = _userState.asStateFlow() | ||
|
|
||
| fun getUserList(page: Int) { | ||
| viewModelScope.launch { | ||
| kotlin.runCatching { | ||
| userService.getUserList(page) | ||
| }.onSuccess { | ||
| if (it.isSuccessful) { | ||
| userResult.value = it.body() | ||
| userSuccess.value = true | ||
| } else { | ||
| userSuccess.value = false | ||
| if (it.body() != null) { | ||
| _userState.value = UserState.Success(it.body()!!) | ||
| } | ||
|
Comment on lines
+20
to
22
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. null일 때의 분기 처리는 어딨죠?!
위에서 설명했듯이 !!(Non-null assertion operator)를 사용하지 않고 null 타입 체크를 해보면 좋을 것 같습니다! |
||
| }.onFailure { | ||
| // 에러 처리 로직 | ||
| _userState.value = UserState.Error | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
sharedFlow가 아닌 stateFlow를 사용한 이유는 무엇일까요~?