Skip to content

Commit

Permalink
Merge branch 'hotfix/signup'
Browse files Browse the repository at this point in the history
  • Loading branch information
ny2060 committed Apr 24, 2023
2 parents 9b2d02c + b6b2dac commit 69672a5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 61 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
applicationId "org.sopt.havit"
minSdk 23
targetSdk 31
versionCode 104
versionName "1.0.4"
versionCode 105
versionName "1.0.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "HAVIT_BASE_URL_DEV", properties["HAVIT_BASE_URL_DEV"])
buildConfigField("String", "HAVIT_BASE_URL_PROD", properties["HAVIT_BASE_URL_PROD"])
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/sopt/havit/di/ServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ object ServiceModule {
@Singleton
fun provideHavitService(retrofit: Retrofit): HavitApi =
retrofit.create(HavitApi::class.java)

}
58 changes: 30 additions & 28 deletions app/src/main/java/org/sopt/havit/ui/sign/KakaoLoginService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import com.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.auth.model.Prompt
import com.kakao.sdk.user.UserApiClient
import com.kakao.sdk.user.model.User
import dagger.hilt.android.qualifiers.ActivityContext
import org.sopt.havit.data.local.HavitAuthLocalPreferences
import javax.inject.Inject
Expand All @@ -13,8 +14,6 @@ class KakaoLoginService @Inject constructor(
@ActivityContext private val context: Context,
private val preferences: HavitAuthLocalPreferences
) {


fun setLoginWithAccount(kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit) {
UserApiClient.instance.loginWithKakaoAccount(
context,
Expand All @@ -23,23 +22,6 @@ class KakaoLoginService @Inject constructor(
)
}

private fun getKakaoUserInfo() {
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.d("TAG", "사용자 정보 요청 실패", error)
} else if (user != null) {
Log.d("TAG", "사용자 정보 요청 성공")
val age = user.kakaoAccount?.ageRange.toString().split("_")

preferences.userEmail = user.kakaoAccount?.email.toString()
preferences.userGender = user.kakaoAccount?.gender.toString()
preferences.userAge = (age[1].toInt() + age[2].toInt()) / 2
preferences.userNickName = user.kakaoAccount?.profile?.nickname ?: ""

}
}
}

fun setKakaoLogin(kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit) {
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) {
UserApiClient.instance.loginWithKakaoTalk(
Expand All @@ -54,7 +36,7 @@ class KakaoLoginService @Inject constructor(
}
}

fun getUserNeedNewScopes() {
fun getUserNeedNewScopes(isGetUserInfo: (Boolean) -> Unit) {
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.d("TAG", "사용자 정보 요청 실패", error)
Expand All @@ -72,22 +54,42 @@ class KakaoLoginService @Inject constructor(
if (scopes.isNotEmpty()) {
Log.d("TAG", "사용자에게 추가 동의를 받아야 합니다.")
// scope 목록을 전달하여 카카오 로그인 요청
UserApiClient.instance.loginWithNewScopes(
context,
scopes
) { _, error ->
UserApiClient.instance.loginWithNewScopes(context, scopes) { token, error ->
if (error != null) {
Log.d("TAG", "사용자 추가 동의 실패", error)
Log.e("TAG", "사용자 추가 동의 실패", error)
} else {
Log.d("TAG", "allowed scopes: ${token!!.scopes}")

// 사용자 정보 재요청
getKakaoUserInfo()
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.e("TAG", "사용자 정보 요청 실패", error)
isGetUserInfo(false)
} else if (user != null) {
Log.i("TAG", "사용자 정보 요청 성공")
getUserInfo(user)
isGetUserInfo(true)
}
}
}
}

} else {
getKakaoUserInfo()
getUserInfo(user)
isGetUserInfo(true)
}
}
}
}

}
private fun getUserInfo(user: User) {
val age = user.kakaoAccount?.ageRange.toString().split("_")

preferences.userEmail = user.kakaoAccount?.email.toString()
preferences.userGender = user.kakaoAccount?.gender.toString()
preferences.userAge = (age[1].toInt() + age[2].toInt()) / 2
preferences.userNickName = user.kakaoAccount?.profile?.nickname ?: ""
}


}
11 changes: 5 additions & 6 deletions app/src/main/java/org/sopt/havit/ui/sign/SignInViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class SignInViewModel @Inject constructor(
private var _isAlreadyUser = MutableLiveData<Event<SignInResponse>>()
var isAlreadyUser: LiveData<Event<SignInResponse>> = _isAlreadyUser

private var _isNeedScopes = MutableLiveData<Event<Boolean>>()
var isNeedScopes: LiveData<Event<Boolean>> = _isNeedScopes

private var _accessToken = MutableLiveData<String>()
var accessToken: LiveData<String> = _accessToken

Expand All @@ -48,12 +45,14 @@ class SignInViewModel @Inject constructor(

val kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit = { token, error ->
if (error != null) {
_isSuccessKakaoLogin.value = Event(false)
if (error.toString().contains("statusCode=302")) {
_isSuccessKakaoLogin.value = Event(false)
}
Log.d("TAG", "카카오계정으로 로그인 실패 ${error}")
} else if (token != null) {
Log.d("TAG", "카카오계정으로 로그인 성공 ${token.accessToken}")
setKakaoToken(token.accessToken)
_isNeedScopes.value = Event(true)
_isSuccessKakaoLogin.value = Event(true)
setKakaoToken(token.accessToken)
}
}

Expand Down
39 changes: 14 additions & 25 deletions app/src/main/java/org/sopt/havit/ui/sign/SplashWithSignActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.AndroidEntryPoint
import org.sopt.havit.MainActivity
import org.sopt.havit.R
Expand All @@ -28,12 +27,11 @@ import kotlin.properties.Delegates
class SplashWithSignActivity :
BaseBindingActivity<ActivitySplashWithSignBinding>(R.layout.activity_splash_with_sign) {


@Inject
lateinit var kakaoLoginService: KakaoLoginService
lateinit var preference: HavitSharedPreference

@Inject
lateinit var preference: HavitSharedPreference
lateinit var kakaoLoginService: KakaoLoginService

private val signInViewModel: SignInViewModel by viewModels()
private var isFromShare by Delegates.notNull<Boolean>()
Expand All @@ -57,26 +55,27 @@ class SplashWithSignActivity :
setContentView(binding.root)
binding.main = signInViewModel
initFcmToken()
initSuccessKakaoLoginOserver()
initSuccessKakaoLoginObserver()
initWhereSplashComesFrom()
setLoginGuideIfFromShare()
setSplashView()
setListeners()
isAlreadyUserObserver()
isNeedScopesObserver()
}

private fun initFcmToken() {
signInViewModel.initFcmToken()
}

private fun initSuccessKakaoLoginOserver() {
signInViewModel.isSuccessKakaoLogin.observe(this, EventObserver {
if (it) signInViewModel.getSignIn()
else UserApiClient.instance.loginWithKakaoAccount(
this,
callback = signInViewModel.kakaoLoginCallback
)
private fun initSuccessKakaoLoginObserver() {
signInViewModel.isSuccessKakaoLogin.observe(this, EventObserver { isSuccess ->
if (isSuccess) {
kakaoLoginService.getUserNeedNewScopes { isGetUserInfo ->
if (isGetUserInfo) signInViewModel.getSignIn()
}
} else {
kakaoLoginService.setLoginWithAccount(signInViewModel.kakaoLoginCallback)
}
})
}

Expand Down Expand Up @@ -142,14 +141,10 @@ class SplashWithSignActivity :
setAutoLogin()
}
binding.btnKakaoLogin.setOnSinglePostClickListener {
kakaoLoginService.setKakaoLogin(
signInViewModel.kakaoLoginCallback
)
kakaoLoginService.setKakaoLogin(signInViewModel.kakaoLoginCallback)
}
binding.tvAnotherLogin.setOnClickListener {
kakaoLoginService.setLoginWithAccount(
signInViewModel.kakaoLoginCallback
)
kakaoLoginService.setLoginWithAccount(signInViewModel.kakaoLoginCallback)
}
}

Expand All @@ -165,12 +160,6 @@ class SplashWithSignActivity :
)
}

private fun isNeedScopesObserver() {
signInViewModel.isNeedScopes.observe(this) {
kakaoLoginService.getUserNeedNewScopes()
}
}

private fun isAlreadyUserObserver() {
signInViewModel.isAlreadyUser.observe(
this,
Expand Down

0 comments on commit 69672a5

Please sign in to comment.