Skip to content

Commit

Permalink
refact: KaKao Login 기능 ViewModel과 연결 - 모듈화 필요, 서버 연결 필요 / #ARCH-22
Browse files Browse the repository at this point in the history
  • Loading branch information
comst19 committed Jan 11, 2024
1 parent 0658e0f commit c0fa937
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import kotlinx.coroutines.flow.StateFlow
interface AuthViewModel {
val doneEvent: SharedFlow<AuthFlowEvent>
val phoneNumber: MutableStateFlow<String>

val singInState : StateFlow<SingInState>
val singInEvent : SharedFlow<SignInResult>

val rawPhoneNumber: StateFlow<String>
val remainTime: StateFlow<Int>
val certificationNumber: StateFlow<String>
Expand All @@ -22,9 +26,22 @@ interface AuthViewModel {
fun signUpToCertification()
fun certificationToSignUpSuccess()

fun SignInSuccess()
fun SignInFail()

enum class AuthFlowEvent {
SIGNIN_TO_SIGNUP,
SIGNUP_TO_CERTIFICATION,
CERTIFICATION_TO_SIGNUPSUCCESS,
}

enum class SingInState {
SIGININ,
SIGNOUT
}

enum class SignInResult{
SUCCESS,
FAIL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class AuthViewModelImpl @Inject constructor() : BaseViewModel(), AuthViewModel {
private val _doneEvent = MutableSharedFlow<AuthViewModel.AuthFlowEvent>()
override val doneEvent: SharedFlow<AuthViewModel.AuthFlowEvent> = _doneEvent.asSharedFlow()

// SignInFragment
private val _singInState = MutableStateFlow(AuthViewModel.SingInState.SIGNOUT)
override val singInState: StateFlow<AuthViewModel.SingInState> = _singInState

private val _singInEvent = MutableSharedFlow<AuthViewModel.SignInResult>()
override val singInEvent: SharedFlow<AuthViewModel.SignInResult> get() = _singInEvent.asSharedFlow()


// SignUpFragment
private val _phoneNumber = MutableStateFlow("")
override val phoneNumber: MutableStateFlow<String> = _phoneNumber
Expand Down Expand Up @@ -84,4 +92,18 @@ class AuthViewModelImpl @Inject constructor() : BaseViewModel(), AuthViewModel {
_doneEvent.emit(AuthViewModel.AuthFlowEvent.CERTIFICATION_TO_SIGNUPSUCCESS)
}
}

override fun SignInSuccess() {
_singInState.value = AuthViewModel.SingInState.SIGININ
viewModelScope.launch {
_singInEvent.emit(AuthViewModel.SignInResult.SUCCESS)
}
}

override fun SignInFail() {
_singInState.value = AuthViewModel.SingInState.SIGNOUT
viewModelScope.launch {
_singInEvent.emit(AuthViewModel.SignInResult.FAIL)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import androidx.navigation.Navigation
import com.droidblossom.archive.R
import com.droidblossom.archive.databinding.FragmentSignInBinding
import com.droidblossom.archive.presentation.base.BaseFragment
import com.droidblossom.archive.presentation.ui.MainActivity
import com.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

Expand All @@ -42,8 +44,7 @@ class SignInFragment : BaseFragment<AuthViewModelImpl,FragmentSignInBinding>(R.l

navController = Navigation.findNavController(view)
binding.kakaoLoginBtn.setOnClickListener {
//viewModel.signInToSignUp()
//kakaoLogin()
kakaoLogin()
}

binding.googleLoginBtn.setOnClickListener {
Expand All @@ -63,6 +64,18 @@ class SignInFragment : BaseFragment<AuthViewModelImpl,FragmentSignInBinding>(R.l
}
}
}

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED){
viewModel.singInEvent.collect() { event ->
if (event == AuthViewModel.SignInResult.SUCCESS){
// 회원이 아닐 때의 로직이 추가되어야함
//viewModel.signInToSignUp()
MainActivity.goMain(requireContext())
}
}
}
}
}

private fun kakaoLogin(){
Expand All @@ -81,9 +94,11 @@ class SignInFragment : BaseFragment<AuthViewModelImpl,FragmentSignInBinding>(R.l
else {
UserApiClient.instance.loginWithKakaoAccount(requireContext(), callback = mCallback) // 카카오 이메일 로그인
}
viewModel.SignInSuccess()
}
// 로그인 성공 부분
else if (token != null) {
viewModel.SignInSuccess()
Log.e("카카오", "로그인 성공 ${token.accessToken}")
}
}
Expand Down

0 comments on commit c0fa937

Please sign in to comment.