File tree Expand file tree Collapse file tree 7 files changed +44
-2
lines changed
api/src/main/kotlin/com/ninecraft/booket/core/data/api/repository
impl/src/main/kotlin/com/ninecraft/booket/core/data/impl/repository
home/src/main/kotlin/com/ninecraft/booket/feature/home
settings/src/main/kotlin/com/ninecraft/booket/feature/settings/notification Expand file tree Collapse file tree 7 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ interface UserRepository {
2020
2121 val isUserNotificationEnabled: Flow <Boolean >
2222
23+ suspend fun getUserNotificationEnabled (): Boolean
24+
2325 suspend fun setUserNotificationEnabled (isEnabled : Boolean )
2426
2527 suspend fun getLastSyncedNotificationEnabled (): Boolean?
Original file line number Diff line number Diff line change @@ -58,6 +58,8 @@ internal class DefaultUserRepository @Inject constructor(
5858
5959 override val isUserNotificationEnabled = notificationDataSource.isUserNotificationEnabled
6060
61+ override suspend fun getUserNotificationEnabled (): Boolean = isUserNotificationEnabled.first()
62+
6163 override suspend fun setUserNotificationEnabled (isEnabled : Boolean ) {
6264 notificationDataSource.setUserNotificationEnabled(isEnabled)
6365 }
Original file line number Diff line number Diff line change @@ -104,7 +104,7 @@ class HomePresenter @AssistedInject constructor(
104104
105105 is HomeUiEvent .OnNotificationPermissionResult -> {
106106 scope.launch {
107- val isPermissionGranted = event.isGranted
107+ val isPermissionGranted = event.granted
108108 val lastSyncedServerEnabled = userRepository.getLastSyncedNotificationEnabled()
109109
110110 if (lastSyncedServerEnabled == null || isPermissionGranted != lastSyncedServerEnabled) {
Original file line number Diff line number Diff line change @@ -43,5 +43,5 @@ sealed interface HomeUiEvent : CircuitUiEvent {
4343 ) : HomeUiEvent
4444 data object OnRetryClick : HomeUiEvent
4545 data class OnTabSelected (val tab : MainTab ) : HomeUiEvent
46- data class OnNotificationPermissionResult (val isGranted : Boolean ) : HomeUiEvent
46+ data class OnNotificationPermissionResult (val granted : Boolean ) : HomeUiEvent
4747}
Original file line number Diff line number Diff line change @@ -57,6 +57,27 @@ class NotificationPresenter @AssistedInject constructor(
5757 }
5858 }
5959
60+ suspend fun syncNotificationSettings (enabled : Boolean ) {
61+ userRepository.updateNotificationSettings(enabled)
62+ .onSuccess {
63+ userRepository.setLastNotificationSyncedEnabled(enabled)
64+ }
65+ .onFailure { exception ->
66+ val handleErrorMessage = { message: String ->
67+ Logger .e(message)
68+ sideEffect = NotificationSideEffect .ShowToast (message)
69+ }
70+
71+ handleException(
72+ exception = exception,
73+ onError = handleErrorMessage,
74+ onLoginRequired = {
75+ navigator.resetRoot(LoginScreen ())
76+ },
77+ )
78+ }
79+ }
80+
6081 fun handleEvent (event : NotificationUiEvent ) {
6182 when (event) {
6283 is NotificationUiEvent .InitSideEffect -> {
@@ -67,6 +88,21 @@ class NotificationPresenter @AssistedInject constructor(
6788 navigator.pop()
6889 }
6990
91+ is NotificationUiEvent .OnNotificationPermissionResult -> {
92+ scope.launch {
93+ val isPermissionGranted = event.granted
94+ val userEnabled = userRepository.getUserNotificationEnabled()
95+ val lastSyncedServerEnabled = userRepository.getLastSyncedNotificationEnabled()
96+
97+ val shouldSync = (! isPermissionGranted && lastSyncedServerEnabled != false )
98+ || (userEnabled && (lastSyncedServerEnabled == null || lastSyncedServerEnabled != isPermissionGranted))
99+
100+ if (shouldSync) {
101+ syncNotificationSettings(isPermissionGranted)
102+ }
103+ }
104+ }
105+
70106 is NotificationUiEvent .OnNotificationToggle -> {
71107 updateNotificationSettings(event.enabled)
72108 }
Original file line number Diff line number Diff line change @@ -70,6 +70,7 @@ internal fun NotificationUi(
7070 val observer = LifecycleEventObserver { _, event ->
7171 if (event == Lifecycle .Event .ON_RESUME ) {
7272 value = checkSystemNotificationEnabled(context)
73+ state.eventSink(NotificationUiEvent .OnNotificationPermissionResult (value))
7374 }
7475 }
7576 lifecycleOwner.lifecycle.addObserver(observer)
Original file line number Diff line number Diff line change @@ -23,5 +23,6 @@ sealed interface NotificationSideEffect {
2323sealed interface NotificationUiEvent : CircuitUiEvent {
2424 data object InitSideEffect : NotificationUiEvent
2525 data object OnBackClick : NotificationUiEvent
26+ data class OnNotificationPermissionResult (val granted : Boolean ) : NotificationUiEvent
2627 data class OnNotificationToggle (val enabled : Boolean ) : NotificationUiEvent
2728}
You can’t perform that action at this time.
0 commit comments