Skip to content

Commit 0b5e477

Browse files
authored
Merge pull request #226 from YAPP-Github/BOOK-437-refactor/#225
refactor: FCM 토큰 및 알림 설정 동기화 로직 개선
2 parents 7b3443e + f54d6e0 commit 0b5e477

File tree

6 files changed

+16
-40
lines changed

6 files changed

+16
-40
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.ninecraft.booket.core.common.utils
2+
3+
fun shouldSyncNotification(effectiveEnabled: Boolean, lastSynced: Boolean?): Boolean =
4+
lastSynced == null || lastSynced != effectiveEnabled

core/data/impl/src/main/kotlin/com/ninecraft/booket/core/data/impl/repository/DefaultUserRepository.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,11 @@ internal class DefaultUserRepository @Inject constructor(
4040

4141
override suspend fun syncFcmToken() = runSuspendCatching {
4242
val newToken = getRemoteFcmToken()
43-
val localToken = getLocalFcmToken()
44-
45-
if (newToken == localToken) {
46-
Logger.d("Skip FCM token sync (already up-to-date)")
47-
return@runSuspendCatching
48-
}
49-
5043
registerDevice(newToken)
51-
setFcmToken(newToken)
5244
}
5345

5446
override suspend fun syncFcmToken(fcmToken: String): Result<Unit> = runSuspendCatching {
5547
registerDevice(fcmToken)
56-
setFcmToken(fcmToken)
5748
}
5849

5950
override val isUserNotificationEnabled = notificationDataSource.isUserNotificationEnabled
@@ -93,12 +84,6 @@ internal class DefaultUserRepository @Inject constructor(
9384
}
9485
}
9586

96-
private suspend fun getLocalFcmToken(): String = notificationDataSource.fcmToken.first()
97-
98-
private suspend fun setFcmToken(fcmToken: String) {
99-
notificationDataSource.setFcmToken(fcmToken)
100-
}
101-
10287
private suspend fun getDeviceId(): String {
10388
return try {
10489
firebaseInstallations.id.await()

core/datastore/api/src/main/kotlin/com/ninecraft/booket/core/datastore/api/datasource/NotificationDataSource.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package com.ninecraft.booket.core.datastore.api.datasource
33
import kotlinx.coroutines.flow.Flow
44

55
interface NotificationDataSource {
6-
val fcmToken: Flow<String>
7-
suspend fun setFcmToken(fcmToken: String)
8-
96
val isUserNotificationEnabled: Flow<Boolean>
107
suspend fun setUserNotificationEnabled(isEnabled: Boolean)
118

core/datastore/impl/src/main/kotlin/com/ninecraft/booket/core/datastore/impl/datasource/DefaultNotificationDataSource.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import androidx.datastore.core.DataStore
44
import androidx.datastore.preferences.core.Preferences
55
import androidx.datastore.preferences.core.booleanPreferencesKey
66
import androidx.datastore.preferences.core.edit
7-
import androidx.datastore.preferences.core.stringPreferencesKey
87
import com.ninecraft.booket.core.datastore.api.datasource.NotificationDataSource
98
import com.ninecraft.booket.core.datastore.impl.di.NotificationDataStore
109
import com.ninecraft.booket.core.datastore.impl.util.handleIOException
@@ -15,18 +14,6 @@ import javax.inject.Inject
1514
class DefaultNotificationDataSource @Inject constructor(
1615
@NotificationDataStore private val dataStore: DataStore<Preferences>,
1716
) : NotificationDataSource {
18-
override val fcmToken: Flow<String> = dataStore.data
19-
.handleIOException()
20-
.map { prefs ->
21-
prefs[FCM_TOKEN] ?: ""
22-
}
23-
24-
override suspend fun setFcmToken(fcmToken: String) {
25-
dataStore.edit { prefs ->
26-
prefs[FCM_TOKEN] = fcmToken
27-
}
28-
}
29-
3017
override val isUserNotificationEnabled: Flow<Boolean> = dataStore.data
3118
.handleIOException()
3219
.map { prefs ->
@@ -58,7 +45,6 @@ class DefaultNotificationDataSource @Inject constructor(
5845
}
5946

6047
companion object Companion {
61-
private val FCM_TOKEN = stringPreferencesKey("FCM_TOKEN")
6248
private val USER_NOTIFICATION_ENABLED = booleanPreferencesKey("USER_NOTIFICATION_ENABLED")
6349
private val LAST_SYNCED_NOTIFICATION_ENABLED = booleanPreferencesKey("LAST_SYNCED_NOTIFICATION_ENABLED")
6450
}

feature/home/src/main/kotlin/com/ninecraft/booket/feature/home/HomePresenter.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.runtime.rememberCoroutineScope
77
import androidx.compose.runtime.setValue
88
import com.ninecraft.booket.core.common.analytics.AnalyticsHelper
99
import com.ninecraft.booket.core.common.utils.handleException
10+
import com.ninecraft.booket.core.common.utils.shouldSyncNotification
1011
import com.ninecraft.booket.core.data.api.repository.AuthRepository
1112
import com.ninecraft.booket.core.data.api.repository.BookRepository
1213
import com.ninecraft.booket.core.data.api.repository.UserRepository
@@ -115,14 +116,15 @@ class HomePresenter @AssistedInject constructor(
115116
is HomeUiEvent.OnNotificationPermissionResult -> {
116117
scope.launch {
117118
val isPermissionGranted = event.granted
118-
val userEnabled = userRepository.getUserNotificationEnabled()
119+
val userSettingEnabled = userRepository.getUserNotificationEnabled()
119120
val lastSyncedServerEnabled = userRepository.getLastSyncedNotificationEnabled()
120121

121-
val shouldSync = (!isPermissionGranted && lastSyncedServerEnabled != false) ||
122-
(userEnabled && (lastSyncedServerEnabled == null || lastSyncedServerEnabled != isPermissionGranted))
122+
val effectiveNotificationEnabled = userSettingEnabled && isPermissionGranted
123+
124+
val shouldSync = shouldSyncNotification(effectiveNotificationEnabled, lastSyncedServerEnabled)
123125

124126
if (shouldSync) {
125-
syncNotificationSettings(isPermissionGranted)
127+
syncNotificationSettings(effectiveNotificationEnabled)
126128
}
127129
}
128130
}

feature/settings/src/main/kotlin/com/ninecraft/booket/feature/settings/notification/NotificationPresenter.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.runtime.mutableStateOf
66
import androidx.compose.runtime.rememberCoroutineScope
77
import androidx.compose.runtime.setValue
88
import com.ninecraft.booket.core.common.utils.handleException
9+
import com.ninecraft.booket.core.common.utils.shouldSyncNotification
910
import com.ninecraft.booket.core.data.api.repository.UserRepository
1011
import com.ninecraft.booket.feature.screens.LoginScreen
1112
import com.ninecraft.booket.feature.screens.NotificationScreen
@@ -92,14 +93,15 @@ class NotificationPresenter @AssistedInject constructor(
9293
is NotificationUiEvent.OnNotificationPermissionResult -> {
9394
scope.launch {
9495
val isPermissionGranted = event.granted
95-
val userEnabled = userRepository.getUserNotificationEnabled()
96+
val userSettingEnabled = userRepository.getUserNotificationEnabled()
9697
val lastSyncedServerEnabled = userRepository.getLastSyncedNotificationEnabled()
9798

98-
val shouldSync = (!isPermissionGranted && lastSyncedServerEnabled != false) ||
99-
(userEnabled && (lastSyncedServerEnabled == null || lastSyncedServerEnabled != isPermissionGranted))
99+
val effectiveNotificationEnabled = userSettingEnabled && isPermissionGranted
100+
101+
val shouldSync = shouldSyncNotification(effectiveNotificationEnabled, lastSyncedServerEnabled)
100102

101103
if (shouldSync) {
102-
syncNotificationSettings(isPermissionGranted)
104+
syncNotificationSettings(effectiveNotificationEnabled)
103105
}
104106
}
105107
}

0 commit comments

Comments
 (0)