Skip to content

Commit

Permalink
일부 if문을 when으로 바꾸고, early return 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
plgafhd committed Oct 10, 2024
1 parent 76c917c commit 47352f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.facebook.login.LoginManager
import com.wafflestudio.snutt2.R
import com.wafflestudio.snutt2.components.compose.CustomDialog
import com.wafflestudio.snutt2.components.compose.SimpleTopBar
import com.wafflestudio.snutt2.lib.android.toast
import com.wafflestudio.snutt2.lib.network.dto.core.UserDto
import com.wafflestudio.snutt2.ui.SNUTTColors
import com.wafflestudio.snutt2.ui.SNUTTTypography
Expand Down Expand Up @@ -137,7 +136,7 @@ fun SocialLinkPage() {
title = stringResource(R.string.social_link_facebook),
hasNextPage = false,
onClick = {
socialLinkViewModel.prepareFacebookSignin()
socialLinkViewModel.prepareFacebookSignIn()
socialLinkViewModel.loginManager.logInWithReadPermissions(
context as ActivityResultRegistryOwner,
socialLinkViewModel.callbackManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.user.UserApiClient
import com.wafflestudio.snutt2.data.user.UserRepository
import com.wafflestudio.snutt2.lib.network.dto.GetUserFacebookResults
import com.wafflestudio.snutt2.lib.network.dto.core.UserDto
import com.wafflestudio.snutt2.ui.state.SocialLoginState
import com.wafflestudio.snutt2.ui.state.SocialLoginType
Expand All @@ -40,7 +39,7 @@ class SocialLinkViewModel @Inject constructor(

val userInfo: StateFlow<UserDto?> = userRepository.user

val kakaolLoginState = MutableStateFlow<SocialLoginState>(SocialLoginState.Initial)
val kakaoLoginState = MutableStateFlow<SocialLoginState>(SocialLoginState.Initial)
val googleLoginState = MutableStateFlow<SocialLoginState>(SocialLoginState.Initial)
val facebookLoginState = MutableStateFlow<SocialLoginState>(SocialLoginState.Initial)

Expand Down Expand Up @@ -78,11 +77,11 @@ class SocialLinkViewModel @Inject constructor(
userRepository.fetchUserInfo()
}

suspend fun getAccessTokenByAuthCode(authCode: String, clientId: String, clientSecret: String): String? {
private suspend fun getAccessTokenByAuthCode(authCode: String, clientId: String, clientSecret: String): String? {
return userRepository.getAccessTokenByAuthCode(authCode = authCode, clientId = clientId, clientSecret = clientSecret)
}

fun prepareFacebookSignin() {
fun prepareFacebookSignIn() {
loginManager.registerCallback(callbackManager, getFacebookTokenCallback)
updateFacebookLoginState(SocialLoginState.InProgress)
}
Expand All @@ -97,7 +96,7 @@ class SocialLinkViewModel @Inject constructor(

fun updateKakaoLoginState(state: SocialLoginState) {
viewModelScope.launch {
kakaolLoginState.emit(state)
kakaoLoginState.emit(state)
}
}

Expand All @@ -121,19 +120,20 @@ class SocialLinkViewModel @Inject constructor(
}
}

fun triggerKakaoSignin(context: Context) {
fun triggerKakaoSignIn(context: Context) {
updateKakaoLoginState(SocialLoginState.InProgress)
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) {
UserApiClient.instance.loginWithKakaoTalk(context) { token, loginError ->
if (loginError != null) {
if (loginError is ClientError && loginError.reason == ClientErrorCause.Cancelled) {
updateKakaoLoginState(SocialLoginState.Cancelled)
} else if (loginError is AuthError && loginError.reason == AuthErrorCause.AccessDenied) {
return@loginWithKakaoTalk
}
if (loginError is AuthError && loginError.reason == AuthErrorCause.AccessDenied) {
updateKakaoLoginState(SocialLoginState.Cancelled)
} else {
// 카카오계정으로 로그인
UserApiClient.instance.loginWithKakaoAccount(context = context, callback = loginWithKakaoAccountCallback)
return@loginWithKakaoTalk
}
UserApiClient.instance.loginWithKakaoAccount(context = context, callback = loginWithKakaoAccountCallback)
} else if (token != null) {
updateKakaoLoginState(SocialLoginState.Success(token.accessToken))
} else {
Expand All @@ -151,35 +151,38 @@ class SocialLinkViewModel @Inject constructor(
clientId: String,
clientSecret: String,
) {
if (result.resultCode == Activity.RESULT_OK) {
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(result.data)
try {
val account = task.getResult(ApiException::class.java)
val authCode = account?.serverAuthCode
if (authCode != null) {
when (result.resultCode) {
Activity.RESULT_OK -> {
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(result.data)
try {
val account = task.getResult(ApiException::class.java)
val authCode = account?.serverAuthCode
if (authCode == null) {
updateGoogleLoginState(SocialLoginState.Failed)
return
}
viewModelScope.launch {
val googleAccessToken = getAccessTokenByAuthCode(
authCode = authCode,
clientId = clientId,
clientSecret = clientSecret,
)
if (googleAccessToken != null) {
updateGoogleLoginState(SocialLoginState.Success(googleAccessToken))
}
else {
if (googleAccessToken == null) {
updateGoogleLoginState(SocialLoginState.Failed)
return@launch
}
updateGoogleLoginState(SocialLoginState.Success(googleAccessToken))
}
} else {
} catch (e: ApiException) {
updateGoogleLoginState(SocialLoginState.Failed)
}
} catch (e: ApiException) {
}
Activity.RESULT_CANCELED -> {
updateGoogleLoginState(SocialLoginState.Cancelled)
}
else -> {
updateGoogleLoginState(SocialLoginState.Failed)
}
} else if (result.resultCode == Activity.RESULT_CANCELED) {
updateGoogleLoginState(SocialLoginState.Cancelled)
} else {
updateGoogleLoginState(SocialLoginState.Failed)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun TutorialPage() {
val homeViewModel = hiltViewModel<HomeViewModel>()
val socialLinkViewModel = hiltViewModel<SocialLinkViewModel>()

val kakaoLoginState by socialLinkViewModel.kakaolLoginState.collectAsStateWithLifecycle()
val kakaoLoginState by socialLinkViewModel.kakaoLoginState.collectAsStateWithLifecycle()
val googleLoginState by socialLinkViewModel.googleLoginState.collectAsStateWithLifecycle()
val facebookLoginState by socialLinkViewModel.facebookLoginState.collectAsStateWithLifecycle()

Expand Down Expand Up @@ -204,7 +204,7 @@ fun TutorialPage() {
) {
SocialLoginButton(
painter = painterResource(id = R.drawable.kakao_login),
onClick = { socialLinkViewModel.triggerKakaoSignin(context) },
onClick = { socialLinkViewModel.triggerKakaoSignIn(context) },
)

SocialLoginButton(
Expand All @@ -219,7 +219,7 @@ fun TutorialPage() {
SocialLoginButton(
painter = painterResource(id = R.drawable.facebook_login),
onClick = {
socialLinkViewModel.prepareFacebookSignin()
socialLinkViewModel.prepareFacebookSignIn()
socialLinkViewModel.loginManager.logInWithReadPermissions(
context as ActivityResultRegistryOwner,
socialLinkViewModel.callbackManager,
Expand Down

0 comments on commit 47352f2

Please sign in to comment.