From 80c12faa791874fae45c43368ae6e4bbe97af813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Wed, 1 Mar 2023 18:20:54 +0900 Subject: [PATCH] =?UTF-8?q?Issues=20#287=20feat:=20GoogleAuthViewModel=20?= =?UTF-8?q?=EB=A1=9C=20=EB=AA=A8=EB=93=88=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth-google/build.gradle.kts | 3 + .../auth/google/model/GoogleAuthEvent.kt | 8 ++ .../auth/google/ui/GoogleAuthViewModel.kt | 99 +++++++++++++++++++ auth-google/src/main/res/values/strings.xml | 13 +++ .../auth/repository/AuthRepositoryImpl.kt | 4 + .../repository/user/UserRepositoryImpl.kt | 8 +- .../domain/repository/auth/AuthRepository.kt | 2 + .../domain/usecase/user/GetUserIdUseCase.kt | 13 +++ .../domain/usecase/user/SignOutUseCase.kt | 3 + .../usecase/user/TransferDataUseCase.kt | 16 +++ .../domain/usecase/user/WithdrawalUseCase.kt | 1 + .../features/intro/model/SignInState.kt | 12 --- .../features/intro/ui/IntroFragment.kt | 72 ++++++-------- .../features/intro/ui/IntroViewModel.kt | 26 ----- .../ui-intro/src/main/res/values/strings.xml | 8 -- .../features/setting/model/SettingEvent.kt | 8 -- .../features/setting/ui/SettingFragment.kt | 59 +++++++++-- .../features/setting/ui/SettingViewModel.kt | 38 +------ .../src/main/res/values/strings.xml | 3 +- .../auth}/FailedSaveLoginUserException.kt | 2 +- 20 files changed, 248 insertions(+), 150 deletions(-) create mode 100644 auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt create mode 100644 auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt create mode 100644 auth-google/src/main/res/values/strings.xml create mode 100644 domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt create mode 100644 domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt delete mode 100644 features/ui-intro/src/main/java/com/lighthouse/features/intro/model/SignInState.kt delete mode 100644 features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroViewModel.kt delete mode 100644 features/ui-setting/src/main/java/com/lighthouse/features/setting/model/SettingEvent.kt rename {features/ui-intro/src/main/java/com/lighthouse/features/intro/exception => model/src/main/java/com/lighthouse/beep/model/exception/auth}/FailedSaveLoginUserException.kt (72%) diff --git a/auth-google/build.gradle.kts b/auth-google/build.gradle.kts index fed0b53df..88ba0d8c5 100644 --- a/auth-google/build.gradle.kts +++ b/auth-google/build.gradle.kts @@ -14,6 +14,9 @@ dependencies { implementation(projects.model) implementation(projects.common) implementation(projects.commonAndroid) + implementation(projects.domain) + + implementation(libs.androidX.fragment.ktx) implementation(libs.gms.play.services.auth) diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt b/auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt new file mode 100644 index 000000000..118a25e5a --- /dev/null +++ b/auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt @@ -0,0 +1,8 @@ +package com.lighthouse.auth.google.model + +import com.lighthouse.core.android.utils.resource.UIText + +sealed class GoogleAuthEvent { + + data class SnackBar(val text: UIText) : GoogleAuthEvent() +} diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt b/auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt new file mode 100644 index 000000000..53c9dac97 --- /dev/null +++ b/auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt @@ -0,0 +1,99 @@ +package com.lighthouse.auth.google.ui + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.lighthouse.auth.google.R +import com.lighthouse.auth.google.exception.FailedConnectException +import com.lighthouse.auth.google.model.GoogleAuthEvent +import com.lighthouse.beep.model.exception.auth.FailedSaveLoginUserException +import com.lighthouse.core.android.utils.resource.UIText +import com.lighthouse.core.utils.flow.MutableEventFlow +import com.lighthouse.core.utils.flow.asEventFlow +import com.lighthouse.domain.usecase.user.GetUserIdUseCase +import com.lighthouse.domain.usecase.user.LoginUseCase +import com.lighthouse.domain.usecase.user.SignOutUseCase +import com.lighthouse.domain.usecase.user.TransferDataUseCase +import com.lighthouse.domain.usecase.user.WithdrawalUseCase +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch +import java.lang.Exception +import javax.inject.Inject + +@HiltViewModel +class GoogleAuthViewModel @Inject constructor( + private val getUserIdUseCase: GetUserIdUseCase, + private val loginUseCase: LoginUseCase, + private val transferDataUseCase: TransferDataUseCase, + private val signOutUseCase: SignOutUseCase, + private val withdrawalUseCase: WithdrawalUseCase +) : ViewModel() { + + private val _eventFlow = MutableEventFlow() + val eventFlow = _eventFlow.asEventFlow() + + private val _signInLoading = MutableStateFlow(false) + val signInLoading = _signInLoading.asStateFlow() + + fun getUserId() = getUserIdUseCase() + + fun login() { + viewModelScope.launch { + finishSignIn(loginUseCase().exceptionOrNull()) + } + } + + fun transferData(oldUserId: String) { + viewModelScope.launch { + finishSignIn(transferDataUseCase(oldUserId).exceptionOrNull()) + } + } + + fun startSignIn() { + _signInLoading.value = true + } + + fun finishSignIn(throwable: Throwable? = null) { + finishSignIn(throwable as? Exception) + } + + private fun finishSignIn(exception: Exception? = null) { + _signInLoading.value = false + viewModelScope.launch { + val stringRes = when (exception) { + null -> R.string.login_success + is FailedSaveLoginUserException -> R.string.error_save_login_user + is FailedConnectException -> R.string.google_connect_fail + else -> R.string.error_unknown + } + _eventFlow.emit(GoogleAuthEvent.SnackBar(UIText.StringResource(stringRes))) + } + } + + fun signOut() { + viewModelScope.launch { + val exception = signOutUseCase().exceptionOrNull() + if (exception != null) { + _eventFlow.emit( + GoogleAuthEvent.SnackBar( + UIText.StringResource(R.string.error_sign_out) + ) + ) + } + } + } + + fun withdrawal() { + viewModelScope.launch { + val exception = withdrawalUseCase().exceptionOrNull() + if (exception != null) { + _eventFlow.emit( + GoogleAuthEvent.SnackBar( + UIText.StringResource(R.string.error_withdrawal) + ) + ) + } + } + } +} diff --git a/auth-google/src/main/res/values/strings.xml b/auth-google/src/main/res/values/strings.xml new file mode 100644 index 000000000..8c96dc222 --- /dev/null +++ b/auth-google/src/main/res/values/strings.xml @@ -0,0 +1,13 @@ + + + 구글 로그인 실패 + 구글 연결 실패 + 로그인 성공 + 로그인 실패 + + 로그인 정보를 저장하는데 실패 했습니다. + 알 수 없는 오류 입니다. + + 로그아웃에 실패 했습니다. + 회원 탈퇴에 실패 했습니다. + \ No newline at end of file diff --git a/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt b/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt index 595a4402b..02d5ac18b 100644 --- a/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt +++ b/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt @@ -26,6 +26,10 @@ internal class AuthRepositoryImpl @Inject constructor() : AuthRepository { return Firebase.auth.currentUser?.uid ?: GUEST_ID } + override fun signOut() { + Firebase.auth.signOut() + } + override suspend fun withdrawal(): Result { val user = Firebase.auth.currentUser ?: return Result.success(Unit) return runCatching { diff --git a/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt b/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt index 13ebde742..05e7f03b4 100644 --- a/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt +++ b/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt @@ -80,9 +80,11 @@ internal class UserRepositoryImpl @Inject constructor( return userPreferenceRepository.getFilterExpired(userId) } - override suspend fun transferData(userId: String, newUserId: String): Result { - return userPreferenceRepository.transferData(userId, newUserId) - } + override suspend fun transferData(userId: String, newUserId: String): Result = + runCatching { + userPreferenceRepository.transferData(userId, newUserId).getOrThrow() + userPreferenceRepository.setLoginUserUid(newUserId).getOrThrow() + } override suspend fun withdrawal(userId: String): Result = runCatching { userPreferenceRepository.withdrawal(userId).getOrThrow() diff --git a/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt b/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt index 391647102..96f5c706b 100644 --- a/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt +++ b/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt @@ -8,5 +8,7 @@ interface AuthRepository { fun getCurrentUserId(): String + fun signOut() + suspend fun withdrawal(): Result } diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt new file mode 100644 index 000000000..729998ab9 --- /dev/null +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt @@ -0,0 +1,13 @@ +package com.lighthouse.domain.usecase.user + +import com.lighthouse.domain.repository.auth.AuthRepository +import javax.inject.Inject + +class GetUserIdUseCase @Inject constructor( + private val authRepository: AuthRepository +) { + + operator fun invoke(): String { + return authRepository.getCurrentUserId() + } +} diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt index 55cb665c9..186d72796 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt @@ -1,13 +1,16 @@ package com.lighthouse.domain.usecase.user +import com.lighthouse.domain.repository.auth.AuthRepository import com.lighthouse.domain.repository.user.UserRepository import javax.inject.Inject class SignOutUseCase @Inject constructor( + private val authRepository: AuthRepository, private val userRepository: UserRepository ) { suspend operator fun invoke(): Result = runCatching { + authRepository.signOut() userRepository.signOut().getOrThrow() } } diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt new file mode 100644 index 000000000..0f03b2d3e --- /dev/null +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt @@ -0,0 +1,16 @@ +package com.lighthouse.domain.usecase.user + +import com.lighthouse.domain.repository.auth.AuthRepository +import com.lighthouse.domain.repository.user.UserRepository +import javax.inject.Inject + +class TransferDataUseCase @Inject constructor( + private val authRepository: AuthRepository, + private val userRepository: UserRepository +) { + + suspend operator fun invoke(oldUserId: String): Result { + val newUserId = authRepository.getCurrentUserId() + return userRepository.transferData(oldUserId, newUserId) + } +} diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt index 98cb7f0da..91a07d0fe 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt @@ -12,6 +12,7 @@ class WithdrawalUseCase @Inject constructor( suspend operator fun invoke(): Result = runCatching { val userId = authRepository.getCurrentUserId() authRepository.withdrawal().getOrThrow() + authRepository.signOut() userRepository.withdrawal(userId).getOrThrow() } } diff --git a/features/ui-intro/src/main/java/com/lighthouse/features/intro/model/SignInState.kt b/features/ui-intro/src/main/java/com/lighthouse/features/intro/model/SignInState.kt deleted file mode 100644 index 3b8757f18..000000000 --- a/features/ui-intro/src/main/java/com/lighthouse/features/intro/model/SignInState.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.lighthouse.features.intro.model - -internal sealed class SignInState { - - object None : SignInState() - object Loading : SignInState() - object Success : SignInState() - data class Failed(val e: Exception) : SignInState() { - - constructor(throwable: Throwable) : this(throwable as Exception) - } -} diff --git a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt index bc0fe5a89..697fdbd7b 100644 --- a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt +++ b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt @@ -8,8 +8,10 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import com.google.android.material.snackbar.Snackbar -import com.lighthouse.auth.google.exception.FailedConnectException +import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient +import com.lighthouse.auth.google.ui.GoogleAuthViewModel +import com.lighthouse.core.android.utils.resource.UIText import com.lighthouse.features.common.binding.viewBindings import com.lighthouse.features.common.dialog.progress.ProgressDialog import com.lighthouse.features.common.ext.repeatOnStarted @@ -17,8 +19,6 @@ import com.lighthouse.features.common.ext.show import com.lighthouse.features.common.utils.throttle.onThrottleClick import com.lighthouse.features.intro.R import com.lighthouse.features.intro.databinding.FragmentIntroBinding -import com.lighthouse.features.intro.exception.FailedSaveLoginUserException -import com.lighthouse.features.intro.model.SignInState import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch import javax.inject.Inject @@ -28,7 +28,7 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { private val binding by viewBindings() - private val viewModel: IntroViewModel by viewModels() + private val googleAuthViewModel: GoogleAuthViewModel by viewModels() @Inject lateinit var googleClient: GoogleClient @@ -40,9 +40,9 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { lifecycleScope.launch { val exception = googleClient.signIn(result).exceptionOrNull() if (exception != null) { - viewModel.setState(SignInState.Failed(exception)) + googleAuthViewModel.finishSignIn(exception) } else { - signIn() + googleAuthViewModel.login() } } } @@ -51,7 +51,8 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { super.onViewCreated(view, savedInstanceState) animateLogo() - setUpSignInStateFlow() + setUpGoogleAuthEvent() + setUpSignInLoading() setUpBtnGoogleLogin() setUpTvGuestSignIn() } @@ -61,10 +62,20 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { drawable.start() } - private fun setUpSignInStateFlow() { + private fun setUpGoogleAuthEvent() { repeatOnStarted { - viewModel.signInState.collect { state -> - if (state == SignInState.Loading) { + googleAuthViewModel.eventFlow.collect { event -> + when (event) { + is GoogleAuthEvent.SnackBar -> showSnackBar(event.text) + } + } + } + } + + private fun setUpSignInLoading() { + repeatOnStarted { + googleAuthViewModel.signInLoading.collect { loading -> + if (loading) { if (progressDialog?.isAdded == true) { progressDialog?.dismiss() } @@ -73,56 +84,29 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { } else { progressDialog?.dismiss() } - - when (state) { - is SignInState.Success -> signInSuccess() - is SignInState.Failed -> signInFailed(state.e) - else -> {} - } } } } private fun setUpBtnGoogleLogin() { binding.btnGoogleLogin.onThrottleClick { - viewModel.setState(SignInState.Loading) + googleAuthViewModel.startSignIn() loginLauncher.launch(googleClient.signInIntent()) } } private fun setUpTvGuestSignIn() { binding.tvGuestSignin.onThrottleClick { - viewModel.setState(SignInState.Loading) - signIn() + googleAuthViewModel.startSignIn() + googleAuthViewModel.login() } } - private fun signIn() { - lifecycleScope.launch { - val state = if (viewModel.login().isSuccess) { - SignInState.Success - } else { - SignInState.Failed(FailedSaveLoginUserException()) - } - viewModel.setState(state) - } - } - - private fun signInSuccess() { - val message = getString(R.string.login_success) - showSnackBar(message) - } - - private fun signInFailed(e: Exception) { - val message = when (e) { - is FailedSaveLoginUserException -> getString(R.string.error_save_login_user) - is FailedConnectException -> getString(R.string.google_connect_fail) - else -> getString(R.string.error_unknown) - } - showSnackBar(message) - } - private fun showSnackBar(string: String) { Snackbar.make(binding.root, string, Snackbar.LENGTH_SHORT).show() } + + private fun showSnackBar(text: UIText) { + showSnackBar(text.asString(requireContext()).toString()) + } } diff --git a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroViewModel.kt b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroViewModel.kt deleted file mode 100644 index 1590cb93e..000000000 --- a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroViewModel.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.lighthouse.features.intro.ui - -import androidx.lifecycle.ViewModel -import com.lighthouse.domain.usecase.user.LoginUseCase -import com.lighthouse.features.intro.model.SignInState -import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.asStateFlow -import javax.inject.Inject - -@HiltViewModel -internal class IntroViewModel @Inject constructor( - private val loginUseCase: LoginUseCase -) : ViewModel() { - - private val _signInState = MutableStateFlow(SignInState.None) - val signInState = _signInState.asStateFlow() - - suspend fun login(): Result { - return loginUseCase() - } - - fun setState(state: SignInState) { - _signInState.value = state - } -} diff --git a/features/ui-intro/src/main/res/values/strings.xml b/features/ui-intro/src/main/res/values/strings.xml index 597230820..6392dfb37 100644 --- a/features/ui-intro/src/main/res/values/strings.xml +++ b/features/ui-intro/src/main/res/values/strings.xml @@ -2,12 +2,4 @@ Google 계정으로 로그인 게스트로 시작하기 - - 구글 로그인 실패 - 구글 연결 실패 - 로그인 성공 - 로그인 실패 - - 로그인 정보를 저장하는데 실패 했습니다. - 알 수 없는 오류 입니다. \ No newline at end of file diff --git a/features/ui-setting/src/main/java/com/lighthouse/features/setting/model/SettingEvent.kt b/features/ui-setting/src/main/java/com/lighthouse/features/setting/model/SettingEvent.kt deleted file mode 100644 index 8f6b271c9..000000000 --- a/features/ui-setting/src/main/java/com/lighthouse/features/setting/model/SettingEvent.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.lighthouse.features.setting.model - -import com.lighthouse.core.android.utils.resource.UIText - -internal sealed class SettingEvent { - - data class SnackBar(val text: UIText) : SettingEvent() -} diff --git a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt index 104e5b1ca..d5c8665b0 100644 --- a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt +++ b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt @@ -4,20 +4,24 @@ import android.content.Context import android.os.Bundle import android.view.View import androidx.activity.addCallback +import androidx.activity.result.contract.ActivityResultContracts import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.NavHostFragment import com.google.android.material.snackbar.Snackbar +import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient +import com.lighthouse.auth.google.ui.GoogleAuthViewModel import com.lighthouse.core.android.utils.resource.UIText import com.lighthouse.features.common.binding.viewBindings +import com.lighthouse.features.common.dialog.progress.ProgressDialog import com.lighthouse.features.common.ext.repeatOnStarted +import com.lighthouse.features.common.ext.show import com.lighthouse.features.setting.R import com.lighthouse.features.setting.adapter.SettingAdapter import com.lighthouse.features.setting.databinding.FragmentSettingBinding -import com.lighthouse.features.setting.model.SettingEvent import com.lighthouse.features.setting.model.SettingMenu import com.lighthouse.navs.app.model.AppNavigationItem import com.lighthouse.navs.app.navigator.AppNavigationViewModel @@ -55,9 +59,25 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { } ) + private val googleAuthViewModel: GoogleAuthViewModel by viewModels() + @Inject lateinit var googleClient: GoogleClient + private var progressDialog: ProgressDialog? = null + + private val loginLauncher = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + lifecycleScope.launch { + val exception = googleClient.signIn(result).exceptionOrNull() + if (exception != null) { + googleAuthViewModel.finishSignIn(exception) + } else { + googleAuthViewModel.login() + } + } + } + override fun onAttach(context: Context) { super.onAttach(context) @@ -73,8 +93,9 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + setUpGoogleAuthEvent() + setUpSignInLoading() setUpSettingMenu() - setUpEvent() } private fun setUpSettingMenu() { @@ -86,11 +107,27 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { } } - private fun setUpEvent() { + private fun setUpGoogleAuthEvent() { viewLifecycleOwner.repeatOnStarted { - viewModel.eventFlow.collect { event -> + googleAuthViewModel.eventFlow.collect { event -> when (event) { - is SettingEvent.SnackBar -> showSnackBar(event.text) + is GoogleAuthEvent.SnackBar -> showSnackBar(event.text) + } + } + } + } + + private fun setUpSignInLoading() { + repeatOnStarted { + googleAuthViewModel.signInLoading.collect { loading -> + if (loading) { + if (progressDialog?.isAdded == true) { + progressDialog?.dismiss() + } + progressDialog = ProgressDialog() + progressDialog?.show(childFragmentManager) + } else { + progressDialog?.dismiss() } } } @@ -128,15 +165,18 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { } private fun signIn() { + googleAuthViewModel.startSignIn() + loginLauncher.launch(googleClient.signInIntent()) } private fun signOut() { lifecycleScope.launch { val exception = googleClient.signOut().exceptionOrNull() if (exception != null) { - showSnackBar(getString(R.string.user_sign_out_exception)) + showSnackBar(getString(R.string.error_sign_out_google_client)) + } else { + googleAuthViewModel.signOut() } - viewModel.signOut() } } @@ -144,9 +184,10 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { lifecycleScope.launch { val exception = googleClient.signOut().exceptionOrNull() if (exception != null) { - showSnackBar(getString(R.string.user_withdrawal_exception)) + showSnackBar(getString(R.string.error_sign_out_google_client)) + } else { + googleAuthViewModel.withdrawal() } - viewModel.withdrawal() } } diff --git a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingViewModel.kt b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingViewModel.kt index b6d586af8..ba6969c62 100644 --- a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingViewModel.kt +++ b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingViewModel.kt @@ -4,18 +4,13 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.lighthouse.beep.model.user.SecurityOption import com.lighthouse.core.android.utils.resource.UIText -import com.lighthouse.core.utils.flow.MutableEventFlow -import com.lighthouse.core.utils.flow.asEventFlow import com.lighthouse.domain.usecase.setting.GetNotificationOptionUseCase import com.lighthouse.domain.usecase.setting.GetSecurityOptionUseCase import com.lighthouse.domain.usecase.setting.SetNotificationOptionUseCase import com.lighthouse.domain.usecase.user.IsGuestUseCase -import com.lighthouse.domain.usecase.user.SignOutUseCase -import com.lighthouse.domain.usecase.user.WithdrawalUseCase import com.lighthouse.features.setting.R import com.lighthouse.features.setting.ext.settingGroup import com.lighthouse.features.setting.ext.settingItems -import com.lighthouse.features.setting.model.SettingEvent import com.lighthouse.features.setting.model.SettingMenu import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow @@ -31,14 +26,9 @@ internal class SettingViewModel @Inject constructor( isGuestUseCase: IsGuestUseCase, getNotificationOptionUseCase: GetNotificationOptionUseCase, getSecurityOptionUseCase: GetSecurityOptionUseCase, - private val setNotificationOptionUseCase: SetNotificationOptionUseCase, - private val signOutUseCase: SignOutUseCase, - private val withdrawalUseCase: WithdrawalUseCase + private val setNotificationOptionUseCase: SetNotificationOptionUseCase ) : ViewModel() { - private val _eventFlow = MutableEventFlow() - val eventFlow = _eventFlow.asEventFlow() - private val notificationEnable = getNotificationOptionUseCase().map { it.getOrDefault(false) } @@ -94,32 +84,6 @@ internal class SettingViewModel @Inject constructor( } } - fun signOut() { - viewModelScope.launch { - val exception = signOutUseCase().exceptionOrNull() - if (exception != null) { - _eventFlow.emit( - SettingEvent.SnackBar( - UIText.StringResource(R.string.user_sign_out_exception) - ) - ) - } - } - } - - fun withdrawal() { - viewModelScope.launch { - val exception = withdrawalUseCase().exceptionOrNull() - if (exception != null) { - _eventFlow.emit( - SettingEvent.SnackBar( - UIText.StringResource(R.string.user_withdrawal_exception) - ) - ) - } - } - } - val settingMenus = combine( configGroup, userGroup diff --git a/features/ui-setting/src/main/res/values/strings.xml b/features/ui-setting/src/main/res/values/strings.xml index 14831fbdb..cd213c6fa 100644 --- a/features/ui-setting/src/main/res/values/strings.xml +++ b/features/ui-setting/src/main/res/values/strings.xml @@ -15,10 +15,9 @@ 사용자 로그아웃 - 로그아웃에 실패 했습니다. + 구글 클라이언트 로그아웃에 실패했습니다. 구글 계정 연동 회원 탈퇴 - 회원 탈퇴에 실패 했습니다. 기타 개발자에게 커피 사주기 diff --git a/features/ui-intro/src/main/java/com/lighthouse/features/intro/exception/FailedSaveLoginUserException.kt b/model/src/main/java/com/lighthouse/beep/model/exception/auth/FailedSaveLoginUserException.kt similarity index 72% rename from features/ui-intro/src/main/java/com/lighthouse/features/intro/exception/FailedSaveLoginUserException.kt rename to model/src/main/java/com/lighthouse/beep/model/exception/auth/FailedSaveLoginUserException.kt index f7fd39931..b2fc73e80 100644 --- a/features/ui-intro/src/main/java/com/lighthouse/features/intro/exception/FailedSaveLoginUserException.kt +++ b/model/src/main/java/com/lighthouse/beep/model/exception/auth/FailedSaveLoginUserException.kt @@ -1,4 +1,4 @@ -package com.lighthouse.features.intro.exception +package com.lighthouse.beep.model.exception.auth class FailedSaveLoginUserException( message: String = "로그인 정보 저장에 실패 했습니다."