forked from boostcampwm-2022/android04-BEEP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issues boostcampwm-2022#287 feat: GoogleAuthViewModel 로 모듈 분리
- Loading branch information
Showing
20 changed files
with
248 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
99 changes: 99 additions & 0 deletions
99
auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GoogleAuthEvent>() | ||
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) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="google_login_failed">구글 로그인 실패</string> | ||
<string name="google_connect_fail">구글 연결 실패</string> | ||
<string name="login_success">로그인 성공</string> | ||
<string name="login_failed">로그인 실패</string> | ||
|
||
<string name="error_save_login_user">로그인 정보를 저장하는데 실패 했습니다.</string> | ||
<string name="error_unknown">알 수 없는 오류 입니다.</string> | ||
|
||
<string name="error_sign_out">로그아웃에 실패 했습니다.</string> | ||
<string name="error_withdrawal">회원 탈퇴에 실패 했습니다.</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,7 @@ interface AuthRepository { | |
|
||
fun getCurrentUserId(): String | ||
|
||
fun signOut() | ||
|
||
suspend fun withdrawal(): Result<Unit> | ||
} |
13 changes: 13 additions & 0 deletions
13
domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Unit> = runCatching { | ||
authRepository.signOut() | ||
userRepository.signOut().getOrThrow() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Unit> { | ||
val newUserId = authRepository.getCurrentUserId() | ||
return userRepository.transferData(oldUserId, newUserId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
features/ui-intro/src/main/java/com/lighthouse/features/intro/model/SignInState.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.