-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from Team-HMH/refact/kakao_login
[refact/kakao login]: 카카오 로그인 코드 리팩토링
- Loading branch information
Showing
27 changed files
with
212 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ dependencies { | |
|
||
// dot indicator | ||
implementation(libs.dot.indicator) | ||
implementation(projects.core.designsystem) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 31 additions & 63 deletions
94
feature/login/src/main/java/com/hmh/hamyeonham/feature/login/LoginActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,66 @@ | ||
package com.hmh.hamyeonham.feature.login | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.hmh.hamyeonham.common.context.toast | ||
import androidx.lifecycle.flowWithLifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import com.hmh.hamyeonham.common.navigation.NavigationProvider | ||
import com.hmh.hamyeonham.feature.login.data.DummyImage | ||
import com.hmh.hamyeonham.common.view.viewBinding | ||
import com.hmh.hamyeonham.feature.login.databinding.ActivityLoginBinding | ||
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.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class LoginActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivityLoginBinding | ||
|
||
private val loginViewModel: LoginViewModel by viewModels() | ||
private val binding by viewBinding(ActivityLoginBinding::inflate) | ||
private val viewModel by viewModels<LoginViewModel>() | ||
private lateinit var loginViewPagerAdapter: LoginViewPagerAdapter | ||
|
||
@Inject | ||
lateinit var navigationProvider: NavigationProvider | ||
|
||
// 삭제 예정 | ||
private val dummyImageList = listOf( | ||
DummyImage( | ||
Image = R.drawable.login_sample_rectagle_viewpager, | ||
), | ||
DummyImage( | ||
Image = R.drawable.login_sample_rectagle_viewpager, | ||
), | ||
DummyImage( | ||
Image = R.drawable.login_sample_rectagle_viewpager, | ||
), | ||
) | ||
|
||
private val callback: (OAuthToken?, Throwable?) -> Unit = { token, error -> | ||
when { | ||
error != null -> { | ||
} | ||
|
||
token != null -> { | ||
moveToMainActivity() | ||
} | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
binding = ActivityLoginBinding.inflate(layoutInflater) | ||
super.onCreate(savedInstanceState) | ||
setContentView(binding.root) | ||
|
||
binding.btnLogin.setOnClickListener { | ||
loginWithKakaoApp() | ||
binding.ivKakaoLogin.setOnClickListener { | ||
viewModel.loginWithKakaoApp(this) | ||
} | ||
setLoginViewPager() | ||
handleKakaoLoginSuccess() | ||
} | ||
|
||
private fun handleKakaoLoginSuccess() { | ||
viewModel.kakaoLoginState.flowWithLifecycle(lifecycle).onEach { state -> | ||
if (state.isSuccessResult) { | ||
viewModel.getKakaoUserNickname() | ||
// 닉네임 출력 | ||
Log.d("LoginActivity", "닉네임: ${state.kakaoNickname}") | ||
moveToOnBoardingActivity() | ||
} | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
private fun setLoginViewPager() { | ||
loginViewPagerAdapter = LoginViewPagerAdapter(dummyImageList) | ||
val loginViewImageList = listOf( | ||
R.drawable.login_sample_rectagle_viewpager, | ||
R.drawable.login_sample_rectagle_viewpager, | ||
R.drawable.login_sample_rectagle_viewpager, | ||
) | ||
|
||
loginViewPagerAdapter = LoginViewPagerAdapter(loginViewImageList) | ||
binding.run { | ||
vpLogin.adapter = loginViewPagerAdapter | ||
indicatorLoginDots.attachTo(binding.vpLogin) | ||
} | ||
} | ||
|
||
private fun loginWithKakaoApp() { | ||
if (UserApiClient.instance.isKakaoTalkLoginAvailable(this)) { | ||
UserApiClient.instance.loginWithKakaoTalk(this) { token, error -> | ||
if (error != null) { | ||
toast("카카오 로그인 실패") | ||
if (error is ClientError && error.reason == ClientErrorCause.Cancelled) { | ||
toast("다시 로그인 해주세요.") | ||
return@loginWithKakaoTalk | ||
} | ||
loginWithKakaoAccount() | ||
} else if (token != null) { | ||
toast("카카오 로그인 성공") | ||
moveToMainActivity() | ||
} | ||
} | ||
} else { | ||
loginWithKakaoAccount() | ||
} | ||
} | ||
|
||
private fun loginWithKakaoAccount() { | ||
UserApiClient.instance.loginWithKakaoAccount(this, callback = callback) | ||
} | ||
|
||
private fun moveToMainActivity() { | ||
startActivity(navigationProvider.toMain()) | ||
private fun moveToOnBoardingActivity() { | ||
startActivity(navigationProvider.toOnBoarding()) | ||
finish() | ||
} | ||
} |
88 changes: 83 additions & 5 deletions
88
feature/login/src/main/java/com/hmh/hamyeonham/feature/login/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,90 @@ | ||
package com.hmh.hamyeonham.feature.login | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import android.content.Context | ||
import androidx.lifecycle.ViewModel | ||
import com.kakao.sdk.common.model.ClientError | ||
import com.kakao.sdk.common.model.ClientErrorCause | ||
import com.kakao.sdk.user.UserApiClient | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import javax.inject.Inject | ||
|
||
class LoginViewModel : ViewModel(){ | ||
@HiltViewModel | ||
class LoginViewModel @Inject constructor() : ViewModel() { | ||
data class KakaoLoginState( | ||
val isSuccessResult: Boolean = false, | ||
val accessToken: String? = null, | ||
val refreshToken: String? = null, | ||
val kakaoNickname: String? = null, | ||
) | ||
|
||
private val _kakaoLoginResult = MutableLiveData<Boolean>() | ||
val kakaoLoginResult: LiveData<Boolean> get() = _kakaoLoginResult | ||
private val _kakaoLoginState = MutableStateFlow(KakaoLoginState()) | ||
val kakaoLoginState = _kakaoLoginState.asStateFlow() | ||
|
||
private fun updateState(transform: KakaoLoginState.() -> KakaoLoginState) { | ||
val currentState = kakaoLoginState.value | ||
val newState = currentState.transform() | ||
_kakaoLoginState.value = newState | ||
} | ||
|
||
fun loginWithKakaoApp(context: Context) { | ||
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) { | ||
UserApiClient.instance.loginWithKakaoTalk(context) { token, error -> | ||
if (error != null) { | ||
if (error is ClientError && error.reason == ClientErrorCause.Cancelled) { | ||
return@loginWithKakaoTalk | ||
} | ||
loginWithKakaoAccount(context) | ||
} else if (token != null) { | ||
updateState { | ||
copy( | ||
isSuccessResult = true, | ||
accessToken = token.accessToken, | ||
refreshToken = token.refreshToken, | ||
) | ||
} | ||
} | ||
} | ||
} else { | ||
loginWithKakaoAccount(context) | ||
} | ||
} | ||
|
||
private fun loginWithKakaoAccount(context: Context) { | ||
UserApiClient.instance.loginWithKakaoAccount(context) { token, error -> | ||
if (error != null) { | ||
updateState { | ||
copy( | ||
isSuccessResult = false, | ||
accessToken = "", | ||
refreshToken = "", | ||
) | ||
} | ||
} else if (token != null) { | ||
updateState { | ||
copy( | ||
isSuccessResult = true, | ||
accessToken = token.accessToken, | ||
refreshToken = token.refreshToken, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun getKakaoUserNickname() { | ||
UserApiClient.instance.me { user, error -> | ||
if (error != null) { | ||
// 닉네임 정보 얻기 실패 시 | ||
} else if (user != null) { | ||
val kakaoNickname = user.kakaoAccount?.profile?.nickname | ||
updateState { | ||
copy( | ||
kakaoNickname = kakaoNickname, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.