diff --git a/feature/auth/src/main/java/com/teamwiney/auth/login/LoginViewModel.kt b/feature/auth/src/main/java/com/teamwiney/auth/login/LoginViewModel.kt index 465228ab..30a3dcd8 100644 --- a/feature/auth/src/main/java/com/teamwiney/auth/login/LoginViewModel.kt +++ b/feature/auth/src/main/java/com/teamwiney/auth/login/LoginViewModel.kt @@ -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}" @@ -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 { @@ -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 -> { @@ -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 -> { } + } + } + } + } \ No newline at end of file diff --git a/feature/auth/src/main/java/com/teamwiney/auth/signup/SignUpViewModel.kt b/feature/auth/src/main/java/com/teamwiney/auth/signup/SignUpViewModel.kt index 6a2aa8c0..f524f8de 100644 --- a/feature/auth/src/main/java/com/teamwiney/auth/signup/SignUpViewModel.kt +++ b/feature/auth/src/main/java/com/teamwiney/auth/signup/SignUpViewModel.kt @@ -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( initialState = SignUpContract.State() ) { @@ -57,6 +61,7 @@ class SignUpViewModel @Inject constructor( ).collectLatest { when (it) { is ApiResult.Success -> { + registerFcmToken() // 회원가입 완료 후 FCM 토큰 등록 postEffect(SignUpContract.Effect.NavigateTo(AuthDestinations.SignUp.COMPLETE)) } @@ -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)) }