From 01eada0f527af8d5d66fcd333550ad8658d1266b Mon Sep 17 00:00:00 2001 From: Jokwanhee Date: Wed, 31 Jan 2024 17:59:07 +0900 Subject: [PATCH] [add] #163 kakao auto login & mod info logic --- .../mainpage/home/viewmodel/HomeViewModel.kt | 25 +++------------- data/build.gradle.kts | 1 + .../data/repository/UserRepositoryImpl.kt | 29 ++++++++++++++----- .../domain/repository/UserRepository.kt | 1 - .../domain/usecase/user/UserUseCase.kt | 4 --- 5 files changed, 26 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/jjbaksa/jjbaksa/ui/mainpage/home/viewmodel/HomeViewModel.kt b/app/src/main/java/com/jjbaksa/jjbaksa/ui/mainpage/home/viewmodel/HomeViewModel.kt index 2a918b33..64c75b02 100644 --- a/app/src/main/java/com/jjbaksa/jjbaksa/ui/mainpage/home/viewmodel/HomeViewModel.kt +++ b/app/src/main/java/com/jjbaksa/jjbaksa/ui/mainpage/home/viewmodel/HomeViewModel.kt @@ -10,14 +10,12 @@ import com.jjbaksa.jjbaksa.base.BaseViewModel import com.jjbaksa.jjbaksa.model.ShopContent import com.jjbaksa.jjbaksa.util.MyInfo import com.jjbaksa.jjbaksa.util.toShopContent -import com.kakao.sdk.user.UserApiClient import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import timber.log.Timber import javax.inject.Inject @HiltViewModel @@ -76,31 +74,16 @@ class HomeViewModel @Inject constructor( MyInfo.reviews = homeRepository.getMyInfoReviews() MyInfo.profileImage = homeRepository.getMyInfoProfileImage() MyInfo.token = homeRepository.getMyInfoToken() - getKakaoInfo() - } - } - } - } - - fun getKakaoInfo() { - UserApiClient.instance.me { user, error -> - if (error != null) { - Timber.tag("kakao_user_error").e(error) - } else if (user != null) { - if (homeRepository.getMyInfoAccount().isEmpty()) MyInfo.account = - user.kakaoAccount?.email.toString() - if (homeRepository.getMyInfoProfileImage().isEmpty()) MyInfo.profileImage = - user.kakaoAccount?.profile?.thumbnailImageUrl.toString() - if (homeRepository.getMyInfoNickname().isEmpty()) { - MyInfo.nickname = user.kakaoAccount?.profile?.nickname.toString() } } } } fun clearDataStoreNoneAutoLogin() { - runBlocking { - userUseCase.clearDataStoreNoneAutoLogin() + if (!homeRepository.getMyInfoAutoLogin()) { + runBlocking { + userUseCase.logout() + } } } } diff --git a/data/build.gradle.kts b/data/build.gradle.kts index 39868f03..807f09cf 100644 --- a/data/build.gradle.kts +++ b/data/build.gradle.kts @@ -28,6 +28,7 @@ android { dependencies { implementation(project(":domain")) + implementation("com.kakao.sdk:v2-user:2.11.0") KotlinDependencies.run { implementation(kotlin) } diff --git a/data/src/main/java/com/jjbaksa/data/repository/UserRepositoryImpl.kt b/data/src/main/java/com/jjbaksa/data/repository/UserRepositoryImpl.kt index bd06048f..8881c551 100644 --- a/data/src/main/java/com/jjbaksa/data/repository/UserRepositoryImpl.kt +++ b/data/src/main/java/com/jjbaksa/data/repository/UserRepositoryImpl.kt @@ -1,6 +1,7 @@ package com.jjbaksa.data.repository import android.net.Uri +import android.util.Log import com.jjbaksa.data.datasource.local.UserLocalDataSource import com.jjbaksa.data.datasource.remote.UserRemoteDataSource import com.jjbaksa.data.mapper.FormDataUtil @@ -21,7 +22,9 @@ import com.jjbaksa.domain.model.user.User import com.jjbaksa.domain.model.user.UserList import com.jjbaksa.domain.model.user.WithdrawalReasonReq import com.jjbaksa.domain.repository.UserRepository +import com.kakao.sdk.user.UserApiClient import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.runBlocking import javax.inject.Inject class UserRepositoryImpl @Inject constructor( @@ -32,8 +35,20 @@ class UserRepositoryImpl @Inject constructor( runCatching { userRemoteDataSource.postLoginSNS(token, snsType).toLoginResult() }.onSuccess { + userLocalDataSource.saveAutoLogin(true) userLocalDataSource.saveAccessToken(it.accessToken) userLocalDataSource.saveRefreshToken(it.refreshToken) + UserApiClient.instance.me { user, error -> + if (error != null) { + Log.e("로그", "error $error") + } else if (user != null) { + runBlocking { + userLocalDataSource.saveAccount(user.kakaoAccount?.email.toString()) + userLocalDataSource.saveProfileImage(user.kakaoAccount?.profile?.thumbnailImageUrl.toString()) + userLocalDataSource.saveNickname(user.kakaoAccount?.profile?.nickname.toString()) + } + } + } } override suspend fun getUserMe(): Flow> { @@ -42,10 +57,14 @@ class UserRepositoryImpl @Inject constructor( remoteData = { if (it.isSuccessful) { val user = it.body()?.toUser() ?: User() - userLocalDataSource.saveNickname(user.nickname) + if (userLocalDataSource.getNickname().isEmpty()) { + userLocalDataSource.saveNickname(user.nickname) + } + if (userLocalDataSource.getProfileImage().isEmpty()) { + userLocalDataSource.saveProfileImage(user.profileImage.url.toString()) + } userLocalDataSource.saveFollowers(user.userCountResponse.friendCount) userLocalDataSource.saveReviews(user.userCountResponse.reviewCount) - userLocalDataSource.saveProfileImage(user.profileImage.url.toString()) } }, mapper = { @@ -322,12 +341,6 @@ class UserRepositoryImpl @Inject constructor( userLocalDataSource.clearDataStore() } - override suspend fun clearDataStoreNoneAutoLogin() { - if (!userLocalDataSource.getAutoLoginFlag()) { - userLocalDataSource.clearDataStore() - } - } - override fun getAutoLoginFlag(): Boolean { return userLocalDataSource.getAutoLoginFlag() } diff --git a/domain/src/main/java/com/jjbaksa/domain/repository/UserRepository.kt b/domain/src/main/java/com/jjbaksa/domain/repository/UserRepository.kt index b82d7426..7e82439f 100644 --- a/domain/src/main/java/com/jjbaksa/domain/repository/UserRepository.kt +++ b/domain/src/main/java/com/jjbaksa/domain/repository/UserRepository.kt @@ -56,7 +56,6 @@ interface UserRepository { suspend fun postUserEmailCheck(email: String): Flow> suspend fun logout() - suspend fun clearDataStoreNoneAutoLogin() fun getAutoLoginFlag(): Boolean fun getAccount(): String fun getNickname(): String diff --git a/domain/src/main/java/com/jjbaksa/domain/usecase/user/UserUseCase.kt b/domain/src/main/java/com/jjbaksa/domain/usecase/user/UserUseCase.kt index a8df1dcf..0eeeb463 100644 --- a/domain/src/main/java/com/jjbaksa/domain/usecase/user/UserUseCase.kt +++ b/domain/src/main/java/com/jjbaksa/domain/usecase/user/UserUseCase.kt @@ -88,10 +88,6 @@ class UserUseCase @Inject constructor( return userRepository.logout() } - suspend fun clearDataStoreNoneAutoLogin() { - userRepository.clearDataStoreNoneAutoLogin() - } - suspend fun postUserWithdrawalReason( withdrawalReason: WithdrawalReasonReq, onError: (String) -> Unit