Skip to content

Commit

Permalink
feat: 이미 등록된 FCM 토큰인 경우 갱신 호출 방지
Browse files Browse the repository at this point in the history
  • Loading branch information
eshc123 committed Dec 22, 2024
1 parent df7101d commit 368116a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ class UserRepositoryImpl @Inject constructor(

override fun getMemberId(): Flow<Long?> = defaultDataSource.getMemberId()

override fun setCachedFcmToken(data: String): Flow<Unit> = defaultDataSource.setFcmToken(data)

override fun getCachedFcmToken(): Flow<String?> = defaultDataSource.getFcmToken()

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface DefaultDataSource {
fun setViewedTooltip() : Flow<Unit>
fun setMemberId(data: Long) : Flow<Unit>
fun getMemberId() : Flow<Long?>
}
fun setFcmToken(data: String) : Flow<Unit>
fun getFcmToken() : Flow<String?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class DefaultDataSourceImpl @Inject constructor(
val USER_CHARACTER = stringPreferencesKey("USER_CHARACTER")
val VIEWED_TOOLTIP = booleanPreferencesKey("VIEWED_TOOLTIP")
val MEMBER_ID = longPreferencesKey("MEMBER_ID")
val FCM_TOKEN = stringPreferencesKey("FCM_TOKEN")
}

override fun clearUserData(): Flow<Unit> = flow {
Expand Down Expand Up @@ -73,4 +74,15 @@ class DefaultDataSourceImpl @Inject constructor(
override fun getMemberId(): Flow<Long?> = dataStore.data.map { preferences ->
preferences[PreferencesKey.MEMBER_ID]
}
}

override fun setFcmToken(data: String): Flow<Unit> = flow {
dataStore.edit { preferences ->
preferences[PreferencesKey.FCM_TOKEN] = data
}
emit(Unit)
}

override fun getFcmToken(): Flow<String?> = dataStore.data.map { preferences ->
preferences[PreferencesKey.FCM_TOKEN]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.goalpanzi.mission_mate.core.domain.common.DomainResult
import com.goalpanzi.mission_mate.core.domain.common.model.user.UserProfile
import com.goalpanzi.mission_mate.core.domain.user.repository.UserRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
Expand All @@ -15,6 +16,7 @@ class LoginUseCase @Inject constructor(
private val userRepository: UserRepository
) {
suspend fun requestGoogleLogin(email: String): GoogleLogin? {
userRepository.clearUserData().collect()
return when (val response = authRepository.requestGoogleLogin(email)) {
is DomainResult.Success -> {
response.data.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface UserRepository {
fun getUserProfile() : Flow<UserProfile?>
fun setMemberId(data: Long) : Flow<Unit>
fun getMemberId() : Flow<Long?>
fun setCachedFcmToken(data: String) : Flow<Unit>
fun getCachedFcmToken() : Flow<String?>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.goalpanzi.mission_mate.core.domain.user.usecase

import com.goalpanzi.mission_mate.core.domain.user.repository.UserRepository
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.firstOrNull
import javax.inject.Inject

class UpdateFcmTokenUseCase @Inject constructor(
private val userRepository: UserRepository
) {
suspend operator fun invoke(fcmToken: String) = userRepository.updateFcmToken(fcmToken)
suspend operator fun invoke(fcmToken: String) {
val cachedFcmToken = userRepository.getCachedFcmToken().firstOrNull()
if(fcmToken != cachedFcmToken){
userRepository.setCachedFcmToken(fcmToken).collect()
userRepository.updateFcmToken(fcmToken)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.timeout
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds

@HiltViewModel
class OnboardingViewModel @Inject constructor(
Expand Down Expand Up @@ -57,11 +55,9 @@ class OnboardingViewModel @Inject constructor(
}
}

@OptIn(FlowPreview::class)
fun updateTokenAndGetJoinedMissions() {
viewModelScope.launch {
getFcmTokenUseCase()
.timeout(3.seconds)
.catch {
getJoinedMissions()
}.collect { token ->
Expand Down

0 comments on commit 368116a

Please sign in to comment.