Skip to content

Commit

Permalink
TutorialPage의 loginWithAccessToken 함수들 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
plgafhd committed Oct 10, 2024
1 parent 1c63f78 commit 7e0a5b4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.wafflestudio.snutt2.data.user.UserRepository
import com.wafflestudio.snutt2.lib.network.dto.core.UserDto
import com.wafflestudio.snutt2.model.TableTrimParam
import com.wafflestudio.snutt2.ui.ThemeMode
import com.wafflestudio.snutt2.ui.state.SocialLoginType
import com.wafflestudio.snutt2.views.logged_in.home.popups.PopupState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
Expand Down Expand Up @@ -60,6 +61,14 @@ class UserViewModel @Inject constructor(
userRepository.postLoginKakao(kakaoAccessToken)
}

suspend fun loginSocial(type: SocialLoginType, token: String) {
when (type) {
SocialLoginType.FACEBOOK -> loginFacebook(token)
SocialLoginType.KAKAO -> loginKakao(token)
SocialLoginType.GOOGLE -> loginGoogle(token)
}
}

suspend fun addNewLocalId(id: String, password: String) {
userRepository.postUserPassword(id, password)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.wafflestudio.snutt2.lib.android.toast
import com.wafflestudio.snutt2.ui.SNUTTColors
import com.wafflestudio.snutt2.ui.SNUTTTypography
import com.wafflestudio.snutt2.ui.state.SocialLoginState
import com.wafflestudio.snutt2.ui.state.SocialLoginType
import com.wafflestudio.snutt2.views.*
import com.wafflestudio.snutt2.views.logged_in.home.HomeViewModel
import com.wafflestudio.snutt2.views.logged_in.home.settings.SocialLinkViewModel
Expand All @@ -59,36 +60,6 @@ fun TutorialPage() {
val clientId = context.getString(R.string.web_client_id)
val clientSecret = context.getString(R.string.web_client_secret)

val loginWithFacebookAccessToken: (String) -> Unit = { facebookAccessToken ->
coroutineScope.launch {
launchSuspendApi(
apiOnProgress = apiOnProgress,
apiOnError = apiOnError,
loadingIndicatorTitle = context.getString(R.string.sign_in_sign_in_button),
) {
userViewModel.loginFacebook(
facebookAccessToken,
)
homeViewModel.refreshData()
navController.navigateAsOrigin(NavigationDestination.Home)
}
}
}

val loginWithGoogleAccessToken: (String) -> Unit = { googleAccessToken: String ->
coroutineScope.launch {
launchSuspendApi(
apiOnProgress = apiOnProgress,
apiOnError = apiOnError,
loadingIndicatorTitle = context.getString(R.string.sign_in_sign_in_button),
) {
userViewModel.loginGoogle(googleAccessToken)
homeViewModel.refreshData()
navController.navigateAsOrigin(NavigationDestination.Home)
}
}
}

val googleLoginActivityResultLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(),
) { result ->
Expand All @@ -109,20 +80,18 @@ fun TutorialPage() {
googleLoginActivityResultLauncher.launch(signInIntent)
}

val loginWithKaKaoAccessToken: (String) -> Unit = { kakaoAccessToken ->
val loginWithSocialAccessToken: (SocialLoginType, String) -> Unit = { type, token ->
coroutineScope.launch {
launchSuspendApi(
apiOnProgress = apiOnProgress,
apiOnError = apiOnError,
loadingIndicatorTitle = context.getString(R.string.sign_in_sign_in_button),
) {
if (kakaoAccessToken.isNotEmpty()) {
userViewModel.loginKakao(kakaoAccessToken)
if (token.isNotEmpty()) {
userViewModel.loginSocial(type, token)
homeViewModel.refreshData()
socialLinkViewModel.updateSocialLoginState(type, SocialLoginState.Initial)
navController.navigateAsOrigin(NavigationDestination.Home)
socialLinkViewModel.updateKakaoLoginState(SocialLoginState.Initial)
} else {
socialLinkViewModel.updateKakaoLoginState(SocialLoginState.Failed)
}
}
}
Expand All @@ -141,7 +110,10 @@ fun TutorialPage() {
socialLinkViewModel.updateKakaoLoginState(SocialLoginState.Initial)
}
is SocialLoginState.Success -> {
loginWithKaKaoAccessToken((kakaoLoginState as SocialLoginState.Success).token)
loginWithSocialAccessToken(
SocialLoginType.KAKAO,
(kakaoLoginState as SocialLoginState.Success).token
)
}
}
}
Expand All @@ -159,7 +131,10 @@ fun TutorialPage() {
socialLinkViewModel.updateGoogleLoginState(SocialLoginState.Initial)
}
is SocialLoginState.Success -> {
loginWithGoogleAccessToken((googleLoginState as SocialLoginState.Success).token)
loginWithSocialAccessToken(
SocialLoginType.GOOGLE,
(googleLoginState as SocialLoginState.Success).token
)
}
}
}
Expand All @@ -177,7 +152,10 @@ fun TutorialPage() {
socialLinkViewModel.updateFacebookLoginState(SocialLoginState.Initial)
}
is SocialLoginState.Success -> {
loginWithFacebookAccessToken((facebookLoginState as SocialLoginState.Success).token)
loginWithSocialAccessToken(
SocialLoginType.FACEBOOK,
(facebookLoginState as SocialLoginState.Success).token
)
}
}
}
Expand Down

0 comments on commit 7e0a5b4

Please sign in to comment.