From 963c0e5a50e4805b84a673d2e008356523a11768 Mon Sep 17 00:00:00 2001 From: Eric Decanini Date: Wed, 12 Oct 2022 23:11:27 -0400 Subject: [PATCH] Local Notification Settings Event (#7300) * Adds push notifications switch * Adds functionality to Push notification toggle * Adds DefaultPushersServiceTest for togglePusher * Adds DefaultTogglePusherTaskTest * Adds SessionOverviewViewModelTest for toggling pusher * Hides pusher toggle if there are no pushers of the device * Adds changelog file * Edits changelog file * Fixes copyrights * Unregisters checkedChangelistener in onDetachedFromWindow for switch view * Links notification settings toggle to pusher service * Adds changelog file * Adds error handling to VectorSettingsNotificationPreferenceFragment * Removes comment in FakePushersService * Adds parsing for LocalNotificationSettings event * Adds changelog file * Fixes post merge errors * Fixes imports and improves string name * Fixes legal copies * Fixes kdoc punctuation * Fixes string error * Removes unused imports * Moves LocalNotificationSettingsContent * Fixes lint errors * Fixes test errors * Fixes test errors * Fixes error * Fixes error * Fixes error * Fixes error * Fixes error * Fixes error * Fixes error * Adds lost tests * Adds PusherEntity migration * Fixes session overview layout overlap * Fixes switch being enabled by default * Adds device id and unstable prefix --- changelog.d/7300.wip | 1 + .../LocalNotificationSettingsContent.kt | 25 +++++++++++++++++++ .../accountdata/UserAccountDataTypes.kt | 1 + .../vector/app/features/home/HomeActivity.kt | 9 +++++++ .../features/home/HomeActivityViewModel.kt | 18 +++++++++++++ .../features/home/HomeActivityViewState.kt | 3 ++- 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 changelog.d/7300.wip create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt diff --git a/changelog.d/7300.wip b/changelog.d/7300.wip new file mode 100644 index 0000000000..0a1777e651 --- /dev/null +++ b/changelog.d/7300.wip @@ -0,0 +1 @@ +Implements client-side of local notification settings event diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt new file mode 100644 index 0000000000..2a95ccce7a --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2022 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.api.account + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class LocalNotificationSettingsContent( + @Json(name = "is_silenced") val isSilenced: Boolean = false +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/accountdata/UserAccountDataTypes.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/accountdata/UserAccountDataTypes.kt index 91167d896f..0edd824c26 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/accountdata/UserAccountDataTypes.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/accountdata/UserAccountDataTypes.kt @@ -28,4 +28,5 @@ object UserAccountDataTypes { const val TYPE_IDENTITY_SERVER = "m.identity_server" const val TYPE_ACCEPTED_TERMS = "m.accepted_terms" const val TYPE_OVERRIDE_COLORS = "im.vector.setting.override_colors" + const val TYPE_LOCAL_NOTIFICATION_SETTINGS = "org.matrix.msc3890.local_notification_settings." } diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index 10e8447a2b..4402b5a053 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -215,6 +215,7 @@ class HomeActivity : ) } } + sharedActionViewModel = viewModelProvider[HomeSharedActionViewModel::class.java] roomListSharedActionViewModel = viewModelProvider[RoomListSharedActionViewModel::class.java] views.drawerLayout.addDrawerListener(drawerListener) @@ -406,6 +407,14 @@ class HomeActivity : } private fun renderState(state: HomeActivityViewState) { + lifecycleScope.launch { + if (state.areNotificationsSilenced) { + unifiedPushHelper.unregister(pushersManager) + } else { + unifiedPushHelper.register(this@HomeActivity) + } + } + when (val status = state.syncRequestState) { is SyncRequestState.InitialSyncProgressing -> { val initSyncStepStr = initSyncStepFormatter.format(status.initialSyncStep) diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt index a08298e402..85a8fb0c7f 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt @@ -16,6 +16,7 @@ package im.vector.app.features.home +import androidx.lifecycle.asFlow import com.airbnb.mvrx.Mavericks import com.airbnb.mvrx.MavericksViewModelFactory import com.airbnb.mvrx.ViewModelContext @@ -45,10 +46,12 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent import org.matrix.android.sdk.api.auth.UIABaseAuth import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor import org.matrix.android.sdk.api.auth.UserPasswordAuth @@ -57,9 +60,11 @@ import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse import org.matrix.android.sdk.api.auth.registration.nextUncompletedStage import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.raw.RawService +import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes import org.matrix.android.sdk.api.session.crypto.crosssigning.CrossSigningService import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap +import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.session.pushrules.RuleIds import org.matrix.android.sdk.api.session.room.model.Membership @@ -115,6 +120,7 @@ class HomeActivityViewModel @AssistedInject constructor( observeCrossSigningReset() observeAnalytics() observeReleaseNotes() + observeLocalNotificationsSilenced() initThreadsMigration() } @@ -136,6 +142,18 @@ class HomeActivityViewModel @AssistedInject constructor( } } + fun observeLocalNotificationsSilenced() { + val currentSession = activeSessionHolder.getActiveSession() + val deviceId = currentSession.cryptoService().getMyDevice().deviceId + viewModelScope.launch { + currentSession.accountDataService() + .getLiveUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId) + .asFlow() + .map { it.getOrNull()?.content?.toModel()?.isSilenced ?: false } + .onEach { setState { copy(areNotificationsSilenced = it) } } + } + } + private fun observeAnalytics() { if (analyticsConfig.isEnabled) { analyticsStore.didAskUserConsentFlow diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivityViewState.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivityViewState.kt index 843f9bdfec..4df2957cbc 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivityViewState.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivityViewState.kt @@ -22,5 +22,6 @@ import org.matrix.android.sdk.api.session.sync.SyncRequestState data class HomeActivityViewState( val syncRequestState: SyncRequestState = SyncRequestState.Idle, - val authenticationDescription: AuthenticationDescription? = null + val authenticationDescription: AuthenticationDescription? = null, + val areNotificationsSilenced: Boolean = false, ) : MavericksState