From fbe219c3f7bf4378a5a805a7e9ecedb7f120443e Mon Sep 17 00:00:00 2001 From: easyhz Date: Mon, 12 Aug 2024 01:37:52 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=86=8C=EC=85=9C=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EC=A0=84=EB=9E=B5=20=ED=8C=A8=ED=84=B4=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/di/strategy/LoginStrategyModule.kt | 17 +++++++------- .../auth/repository/login/LoginRepository.kt | 2 +- .../repository/login/LoginRepositoryIml.kt | 11 +++++---- .../data/auth/strategy/AuthStrategyContext.kt | 23 +++++++++++++++++++ .../easyhz/noffice/data/auth/util/Provider.kt | 4 +++- .../domain/sign/usecase/LoginUseCase.kt | 8 +++---- .../sign/component/login/SocialLoginButton.kt | 8 +++---- .../sign/contract/login/LoginIntent.kt | 3 ++- .../feature/sign/screen/login/LoginScreen.kt | 7 ++---- .../sign/screen/login/LoginViewModel.kt | 8 ++++--- .../sign/util/login/SocialLoginType.kt | 5 ---- 11 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 data/auth/src/main/java/com/easyhz/noffice/data/auth/strategy/AuthStrategyContext.kt diff --git a/data/auth/src/main/java/com/easyhz/noffice/data/auth/di/strategy/LoginStrategyModule.kt b/data/auth/src/main/java/com/easyhz/noffice/data/auth/di/strategy/LoginStrategyModule.kt index 5f67d2c6..991f55fa 100644 --- a/data/auth/src/main/java/com/easyhz/noffice/data/auth/di/strategy/LoginStrategyModule.kt +++ b/data/auth/src/main/java/com/easyhz/noffice/data/auth/di/strategy/LoginStrategyModule.kt @@ -2,19 +2,20 @@ package com.easyhz.noffice.data.auth.di.strategy import com.easyhz.noffice.data.auth.strategy.BaseStrategy import com.easyhz.noffice.data.auth.strategy.GoogleStrategy -import dagger.Binds +import com.easyhz.noffice.data.auth.util.google import dagger.Module +import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent -import javax.inject.Singleton +import dagger.multibindings.IntoMap +import dagger.multibindings.StringKey @Module @InstallIn(SingletonComponent::class) -abstract class LoginStrategyModule { +object LoginStrategyModule { - @Binds - @Singleton - abstract fun bindGoogleStrategy( - googleStrategy: GoogleStrategy - ): BaseStrategy + @Provides + @IntoMap + @StringKey(google) + fun provideGoogleStrategy(googleStrategy: GoogleStrategy): BaseStrategy = googleStrategy } \ No newline at end of file diff --git a/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepository.kt b/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepository.kt index 7848271f..a1157749 100644 --- a/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepository.kt +++ b/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepository.kt @@ -3,5 +3,5 @@ package com.easyhz.noffice.data.auth.repository.login import android.content.Context interface LoginRepository { - suspend fun loginWithGoogle(context: Context): Result + suspend fun login(context: Context, provider: String): Result } \ No newline at end of file diff --git a/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepositoryIml.kt b/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepositoryIml.kt index f68ab8f5..9824a9cb 100644 --- a/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepositoryIml.kt +++ b/data/auth/src/main/java/com/easyhz/noffice/data/auth/repository/login/LoginRepositoryIml.kt @@ -6,7 +6,7 @@ import com.easyhz.noffice.core.common.di.NofficeDispatchers.IO import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSource import com.easyhz.noffice.core.network.api.auth.AuthService import com.easyhz.noffice.core.network.util.toResult -import com.easyhz.noffice.data.auth.strategy.GoogleStrategy +import com.easyhz.noffice.data.auth.strategy.AuthStrategyContext import com.easyhz.noffice.data.auth.util.Provider import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.withContext @@ -14,14 +14,15 @@ import javax.inject.Inject class LoginRepositoryIml @Inject constructor( @Dispatcher(IO) private val dispatcher: CoroutineDispatcher, - private val googleStrategy: GoogleStrategy, + private val authStrategyContext: AuthStrategyContext, private val authService: AuthService, private val authLocalDataSource: AuthLocalDataSource, ) : LoginRepository { - override suspend fun loginWithGoogle(context: Context): Result = withContext(dispatcher) { + override suspend fun login(context: Context, provider: String): Result = withContext(dispatcher) { runCatching { - val authCode = googleStrategy.login(context).getOrThrow() - val request = Provider.GOOGLE.getLoginRequest(authCode) + authStrategyContext.setStrategy(provider) + val authCode = authStrategyContext.login(context).getOrThrow() + val request = Provider.valueOf(provider).getLoginRequest(authCode) val user = authService.login(request).toResult().getOrThrow() val token = user.token authLocalDataSource.updateTokens(token.accessToken, refresh = token.refreshToken) diff --git a/data/auth/src/main/java/com/easyhz/noffice/data/auth/strategy/AuthStrategyContext.kt b/data/auth/src/main/java/com/easyhz/noffice/data/auth/strategy/AuthStrategyContext.kt new file mode 100644 index 00000000..5af70ab4 --- /dev/null +++ b/data/auth/src/main/java/com/easyhz/noffice/data/auth/strategy/AuthStrategyContext.kt @@ -0,0 +1,23 @@ +package com.easyhz.noffice.data.auth.strategy + +import android.content.Context +import com.easyhz.noffice.data.auth.util.Provider +import javax.inject.Inject + +class AuthStrategyContext @Inject constructor( + private val strategies: Map +) { + private var currentStrategy: BaseStrategy = strategies[Provider.GOOGLE.name]!! + + fun setStrategy(provider: String) { + currentStrategy = strategies[provider] ?: throw IllegalArgumentException("Invalid provider") + } + + suspend fun login(context: Context): Result { + return currentStrategy.login(context) + } + + suspend fun logout(context: Context): Result { + return currentStrategy.logout(context) + } +} \ No newline at end of file diff --git a/data/auth/src/main/java/com/easyhz/noffice/data/auth/util/Provider.kt b/data/auth/src/main/java/com/easyhz/noffice/data/auth/util/Provider.kt index d8d5f78e..16fc2bee 100644 --- a/data/auth/src/main/java/com/easyhz/noffice/data/auth/util/Provider.kt +++ b/data/auth/src/main/java/com/easyhz/noffice/data/auth/util/Provider.kt @@ -2,10 +2,12 @@ package com.easyhz.noffice.data.auth.util import com.easyhz.noffice.core.network.model.request.sign.LoginRequest +const val google = "GOOGLE" + enum class Provider( val type: String ) { - GOOGLE(type = "GOOGLE"); + GOOGLE(type = google); fun getLoginRequest(code: String) = LoginRequest( code = code, diff --git a/domain/sign/src/main/java/com/easyhz/noffice/domain/sign/usecase/LoginUseCase.kt b/domain/sign/src/main/java/com/easyhz/noffice/domain/sign/usecase/LoginUseCase.kt index 9654ce84..fe90f57e 100644 --- a/domain/sign/src/main/java/com/easyhz/noffice/domain/sign/usecase/LoginUseCase.kt +++ b/domain/sign/src/main/java/com/easyhz/noffice/domain/sign/usecase/LoginUseCase.kt @@ -1,13 +1,13 @@ package com.easyhz.noffice.domain.sign.usecase -import android.content.Context import com.easyhz.noffice.core.common.base.BaseUseCase +import com.easyhz.noffice.core.model.auth.param.AuthParam import com.easyhz.noffice.data.auth.repository.login.LoginRepository import javax.inject.Inject class LoginUseCase @Inject constructor( private val loginRepository: LoginRepository -): BaseUseCase() { - override suspend fun invoke(param: Context): Result = - loginRepository.loginWithGoogle(param) +): BaseUseCase() { + override suspend fun invoke(param: AuthParam): Result = + loginRepository.login(param.context, param.providerName) } \ No newline at end of file diff --git a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/component/login/SocialLoginButton.kt b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/component/login/SocialLoginButton.kt index 13c9f91e..f7be82a3 100644 --- a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/component/login/SocialLoginButton.kt +++ b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/component/login/SocialLoginButton.kt @@ -28,7 +28,7 @@ import com.easyhz.noffice.feature.sign.util.login.SocialLoginType @Composable internal fun LoginView( modifier: Modifier = Modifier, - onClickSocial: SocialLoginType.OnItemClickListener, + onClick: (SocialLoginType) -> Unit ) { Column( modifier = modifier, @@ -36,7 +36,7 @@ internal fun LoginView( ) { SocialLoginButton( socialLoginType = SocialLoginType.GOOGLE, - onClick = onClickSocial::onClickGoogle + onClick = onClick ) } } @@ -45,7 +45,7 @@ internal fun LoginView( private fun SocialLoginButton( modifier: Modifier = Modifier, socialLoginType: SocialLoginType, - onClick: () -> Unit + onClick: (SocialLoginType) -> Unit ) { Row( modifier = modifier @@ -59,7 +59,7 @@ private fun SocialLoginButton( .fillMaxWidth() .clip(RoundedCornerShape(12.dp)) .background(socialLoginType.containerColor) - .clickable { onClick() }, + .clickable { onClick(socialLoginType) }, verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center ) { diff --git a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/contract/login/LoginIntent.kt b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/contract/login/LoginIntent.kt index d91a39f8..f469fec1 100644 --- a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/contract/login/LoginIntent.kt +++ b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/contract/login/LoginIntent.kt @@ -2,7 +2,8 @@ package com.easyhz.noffice.feature.sign.contract.login import android.content.Context import com.easyhz.noffice.core.common.base.UiIntent +import com.easyhz.noffice.feature.sign.util.login.SocialLoginType sealed class LoginIntent : UiIntent() { - data class ClickToLogInWithGoogle(val context: Context): LoginIntent() + data class ClickToSocialLogin(val loginType: SocialLoginType, val context: Context): LoginIntent() } \ No newline at end of file diff --git a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginScreen.kt b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginScreen.kt index 13ecd37b..1a5d1988 100644 --- a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginScreen.kt +++ b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginScreen.kt @@ -22,7 +22,6 @@ import com.easyhz.noffice.core.design_system.extension.screenHorizonPadding import com.easyhz.noffice.feature.sign.component.login.LoginView import com.easyhz.noffice.feature.sign.contract.login.LoginIntent import com.easyhz.noffice.feature.sign.contract.login.LoginSideEffect -import com.easyhz.noffice.feature.sign.util.login.SocialLoginType @Composable fun LoginScreen( @@ -59,10 +58,8 @@ fun LoginScreen( modifier = Modifier .screenHorizonPadding() .weight(0.2f), - onClickSocial = object : SocialLoginType.OnItemClickListener { - override fun onClickGoogle() { - viewModel.postIntent(LoginIntent.ClickToLogInWithGoogle(context)) - } + onClick = { + viewModel.postIntent(LoginIntent.ClickToSocialLogin(it, context)) } ) } diff --git a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt index 8e7ee0fb..0da1e100 100644 --- a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt +++ b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt @@ -3,10 +3,12 @@ package com.easyhz.noffice.feature.sign.screen.login import android.content.Context import androidx.lifecycle.viewModelScope import com.easyhz.noffice.core.common.base.BaseViewModel +import com.easyhz.noffice.core.model.auth.param.AuthParam import com.easyhz.noffice.domain.sign.usecase.LoginUseCase import com.easyhz.noffice.feature.sign.contract.login.LoginIntent import com.easyhz.noffice.feature.sign.contract.login.LoginSideEffect import com.easyhz.noffice.feature.sign.contract.login.LoginState +import com.easyhz.noffice.feature.sign.util.login.SocialLoginType import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import javax.inject.Inject @@ -19,12 +21,12 @@ class LoginViewModel @Inject constructor( ) { override fun handleIntent(intent: LoginIntent) { when(intent) { - is LoginIntent.ClickToLogInWithGoogle -> { onClickToLogInWithGoogle(intent.context) } + is LoginIntent.ClickToSocialLogin -> { onClickToSocialLogin(intent.loginType, intent.context) } } } - private fun onClickToLogInWithGoogle(context: Context) = viewModelScope.launch { - loginUseCase.invoke(context).onSuccess { + private fun onClickToSocialLogin(type: SocialLoginType, context: Context) = viewModelScope.launch { + loginUseCase.invoke(AuthParam(context, type.name)).onSuccess { // FIXME 가입 된 유저 네비게이션 처리 navigateToHome() }.onFailure { diff --git a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/util/login/SocialLoginType.kt b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/util/login/SocialLoginType.kt index a7617d5a..07e9b62c 100644 --- a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/util/login/SocialLoginType.kt +++ b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/util/login/SocialLoginType.kt @@ -19,9 +19,4 @@ enum class SocialLoginType( containerColor = Grey50, contentColor = Grey800, ); - - interface OnItemClickListener { - fun onClickGoogle() - } - } \ No newline at end of file