Skip to content

Commit

Permalink
refactor: 소셜 로그인 전략 패턴 적용 #52
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhz committed Aug 11, 2024
1 parent de79230 commit fbe219c
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.easyhz.noffice.data.auth.repository.login
import android.content.Context

interface LoginRepository {
suspend fun loginWithGoogle(context: Context): Result<Unit>
suspend fun login(context: Context, provider: String): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ 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
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<Unit> = withContext(dispatcher) {
override suspend fun login(context: Context, provider: String): Result<Unit> = 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, @JvmSuppressWildcards BaseStrategy>
) {
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<String> {
return currentStrategy.login(context)
}

suspend fun logout(context: Context): Result<Unit> {
return currentStrategy.logout(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Context, Unit>() {
override suspend fun invoke(param: Context): Result<Unit> =
loginRepository.loginWithGoogle(param)
): BaseUseCase<AuthParam , Unit>() {
override suspend fun invoke(param: AuthParam): Result<Unit> =
loginRepository.login(param.context, param.providerName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
SocialLoginButton(
socialLoginType = SocialLoginType.GOOGLE,
onClick = onClickSocial::onClickGoogle
onClick = onClick
)
}
}
Expand All @@ -45,7 +45,7 @@ internal fun LoginView(
private fun SocialLoginButton(
modifier: Modifier = Modifier,
socialLoginType: SocialLoginType,
onClick: () -> Unit
onClick: (SocialLoginType) -> Unit
) {
Row(
modifier = modifier
Expand All @@ -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
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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))
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@ enum class SocialLoginType(
containerColor = Grey50,
contentColor = Grey800,
);

interface OnItemClickListener {
fun onClickGoogle()
}

}

0 comments on commit fbe219c

Please sign in to comment.