Skip to content

Commit

Permalink
#34 Feat : 로그인 (유저상태 ACTIVE), 회원가입 시 FCM 토큰 등록
Browse files Browse the repository at this point in the history
  • Loading branch information
DongChyeon committed Jan 22, 2024
1 parent 18d3b0f commit a934eb9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class LoginViewModel @Inject constructor(
updateState(currentState.copy(isLoading = false))
when (result) {
is ApiResult.Success -> {
runBlocking {
dataStoreRepository.setStringValue(ACCESS_TOKEN, result.data.result.accessToken)
dataStoreRepository.setStringValue(REFRESH_TOKEN, result.data.result.refreshToken)
dataStoreRepository.setIntValue(Constants.USER_ID, result.data.result.userId)
}

Log.i(
"[ACCESS_TOKEN]",
"accessToken: ${result.data.result.accessToken}"
Expand All @@ -124,6 +130,8 @@ class LoginViewModel @Inject constructor(
if (userStatus == UserStatus.ACTIVE) {
dataStoreRepository.setStringValue(LOGIN_TYPE, socialType.name)

registerFcmToken()

postEffect(LoginContract.Effect.NavigateTo(
destination = HomeDestinations.ROUTE,
navOptions = navOptions {
Expand All @@ -135,12 +143,6 @@ class LoginViewModel @Inject constructor(
} else {
postEffect(LoginContract.Effect.NavigateTo("${AuthDestinations.SignUp.ROUTE}?userId=${result.data.result.userId}"))
}

runBlocking {
dataStoreRepository.setStringValue(ACCESS_TOKEN, result.data.result.accessToken)
dataStoreRepository.setStringValue(REFRESH_TOKEN, result.data.result.refreshToken)
dataStoreRepository.setIntValue(Constants.USER_ID, result.data.result.userId)
}
}

is ApiResult.ApiError -> {
Expand All @@ -155,4 +157,23 @@ class LoginViewModel @Inject constructor(
}
}

private fun registerFcmToken() = viewModelScope.launch {
val fcmToken = dataStoreRepository.getStringValue(Constants.FCM_TOKEN).first()
val deviceId = dataStoreRepository.getStringValue(Constants.DEVICE_ID).first()

authRepository.registerFcmToken(fcmToken, deviceId).collectLatest {
when (it) {
is ApiResult.ApiError -> {
postEffect(LoginContract.Effect.ShowSnackBar(it.message))
}

is ApiResult.NetworkError -> {
postEffect(LoginContract.Effect.ShowSnackBar("네트워크 오류가 발생했습니다."))
}

else -> { }
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import androidx.lifecycle.viewModelScope
import com.teamwiney.auth.signup.component.state.SignUpFavoriteCategoryUiState
import com.teamwiney.core.common.base.BaseViewModel
import com.teamwiney.core.common.navigation.AuthDestinations
import com.teamwiney.core.common.util.Constants
import com.teamwiney.data.network.adapter.ApiResult
import com.teamwiney.data.network.model.request.PhoneNumberRequest
import com.teamwiney.data.network.model.request.PhoneNumberWithVerificationCodeRequest
import com.teamwiney.data.network.model.request.SetPreferencesRequest
import com.teamwiney.data.repository.auth.AuthRepository
import com.teamwiney.data.repository.persistence.DataStoreRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SignUpViewModel @Inject constructor(
private val authRepository: AuthRepository
private val authRepository: AuthRepository,
private val dataStoreRepository: DataStoreRepository
) : BaseViewModel<SignUpContract.State, SignUpContract.Event, SignUpContract.Effect>(
initialState = SignUpContract.State()
) {
Expand Down Expand Up @@ -57,6 +61,7 @@ class SignUpViewModel @Inject constructor(
).collectLatest {
when (it) {
is ApiResult.Success -> {
registerFcmToken() // 회원가입 완료 후 FCM 토큰 등록
postEffect(SignUpContract.Effect.NavigateTo(AuthDestinations.SignUp.COMPLETE))
}

Expand Down Expand Up @@ -132,6 +137,25 @@ class SignUpViewModel @Inject constructor(
}
}

private fun registerFcmToken() = viewModelScope.launch {
val fcmToken = dataStoreRepository.getStringValue(Constants.FCM_TOKEN).first()
val deviceId = dataStoreRepository.getStringValue(Constants.DEVICE_ID).first()

authRepository.registerFcmToken(fcmToken, deviceId).collectLatest {
when (it) {
is ApiResult.ApiError -> {
postEffect(SignUpContract.Effect.ShowSnackBar(it.message))
}

is ApiResult.NetworkError -> {
postEffect(SignUpContract.Effect.ShowSnackBar("네트워크 오류가 발생했습니다."))
}

else -> { }
}
}
}

fun updateUserId(userId: String) = viewModelScope.launch {
updateState(currentState.copy(userId = userId))
}
Expand Down

0 comments on commit a934eb9

Please sign in to comment.