From 2b83951a4b48d259ef3e1feca1d2d2d1eb107f7d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 1 Dec 2023 15:02:42 +0100 Subject: [PATCH] Let RustNotificationSettingsService handle the lifecycle of `notificationSettings`, and call `setDelegate(null)` --- .../libraries/matrix/impl/RustMatrixClient.kt | 8 +++----- .../RustNotificationSettingsService.kt | 13 +++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt index 9c03bd7a0a..30229fbe8a 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt @@ -119,10 +119,9 @@ class RustMatrixClient constructor( .filterByPushRules() .finish() } - private val notificationSettings = client.getNotificationSettings() - private val notificationService = RustNotificationService(sessionId, notificationClient, dispatchers, clock) - private val notificationSettingsService = RustNotificationSettingsService(notificationSettings, dispatchers) + private val notificationSettingsService = RustNotificationSettingsService(client, dispatchers) + .also { it.start() } private val roomSyncSubscriber = RoomSyncSubscriber(innerRoomListService, dispatchers) private val encryptionService = RustEncryptionService( client = client, @@ -346,8 +345,7 @@ class RustMatrixClient constructor( override fun close() { sessionCoroutineScope.cancel() clientDelegateTaskHandle?.cancelAndDestroy() - notificationSettings.setDelegate(null) - notificationSettings.destroy() + notificationSettingsService.destroy() verificationService.destroy() syncService.destroy() innerRoomListService.destroy() diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt index 42b2662a6d..c857de56dd 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt @@ -26,16 +26,16 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.withContext -import org.matrix.rustcomponents.sdk.NotificationSettings +import org.matrix.rustcomponents.sdk.Client import org.matrix.rustcomponents.sdk.NotificationSettingsDelegate import org.matrix.rustcomponents.sdk.NotificationSettingsException import timber.log.Timber class RustNotificationSettingsService( - private val notificationSettings: NotificationSettings, + client: Client, private val dispatchers: CoroutineDispatchers, ) : NotificationSettingsService { - + private val notificationSettings = client.getNotificationSettings() private val _notificationSettingsChangeFlow = MutableSharedFlow(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) override val notificationSettingsChangeFlow: SharedFlow = _notificationSettingsChangeFlow.asSharedFlow() @@ -45,10 +45,15 @@ class RustNotificationSettingsService( } } - init { + fun start() { notificationSettings.setDelegate(notificationSettingsDelegate) } + fun destroy() { + notificationSettings.setDelegate(null) + notificationSettings.destroy() + } + override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result = runCatching { notificationSettings.getRoomNotificationSettings(roomId.value, isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::map)