From 9fab13c50be261f7da406188a4950b6a245381f5 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 26 Aug 2024 17:11:41 +0200 Subject: [PATCH] Avoid keeping a reference to the eventSink in a separate value --- .../call/impl/ui/ElementCallActivity.kt | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt index 8c758c2ee5..abeafad4d3 100644 --- a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt +++ b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt @@ -33,14 +33,21 @@ import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.rememberUpdatedState +import androidx.core.app.PictureInPictureModeChangedInfo import androidx.core.content.IntentCompat +import androidx.core.util.Consumer import androidx.lifecycle.Lifecycle import io.element.android.features.call.api.CallType import io.element.android.features.call.impl.DefaultElementCallEntryPoint import io.element.android.features.call.impl.di.CallBindings import io.element.android.features.call.impl.pip.PictureInPictureEvents import io.element.android.features.call.impl.pip.PictureInPicturePresenter +import io.element.android.features.call.impl.pip.PictureInPictureState import io.element.android.features.call.impl.pip.PipActivity import io.element.android.features.call.impl.services.CallForegroundService import io.element.android.features.call.impl.utils.CallIntentDataParser @@ -74,7 +81,6 @@ class ElementCallActivity : private val webViewTarget = mutableStateOf(null) private var eventSink: ((CallScreenEvents) -> Unit)? = null - private var pipEventSink: ((PictureInPictureEvents) -> Unit)? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -102,7 +108,7 @@ class ElementCallActivity : setContent { val pipState = pictureInPicturePresenter.present() - pipEventSink = pipState.eventSink + ListenToAndroidEvents(pipState) ElementThemeApp(appPreferencesStore) { val state = presenter.present() eventSink = state.eventSink @@ -118,21 +124,38 @@ class ElementCallActivity : } } + @Composable + private fun ListenToAndroidEvents(pipState: PictureInPictureState) { + val pipEventSink by rememberUpdatedState(pipState.eventSink) + DisposableEffect(Unit) { + val onUserLeaveHintListener = Runnable { + pipEventSink(PictureInPictureEvents.EnterPictureInPicture) + } + addOnUserLeaveHintListener(onUserLeaveHintListener) + onDispose { + removeOnUserLeaveHintListener(onUserLeaveHintListener) + } + } + DisposableEffect(Unit) { + val onPictureInPictureModeChangedListener = Consumer { _: PictureInPictureModeChangedInfo -> + pipEventSink(PictureInPictureEvents.OnPictureInPictureModeChanged(isInPictureInPictureMode)) + if (!isInPictureInPictureMode && !lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) { + Timber.d("Exiting PiP mode: Hangup the call") + eventSink?.invoke(CallScreenEvents.Hangup) + } + } + addOnPictureInPictureModeChangedListener(onPictureInPictureModeChangedListener) + onDispose { + removeOnPictureInPictureModeChangedListener(onPictureInPictureModeChangedListener) + } + } + } + override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) updateUiMode(newConfig) } - override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) { - super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig) - pipEventSink?.invoke(PictureInPictureEvents.OnPictureInPictureModeChanged(isInPictureInPictureMode)) - - if (!isInPictureInPictureMode && !lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) { - Timber.d("Exiting PiP mode: Hangup the call") - eventSink?.invoke(CallScreenEvents.Hangup) - } - } - override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) setCallType(intent) @@ -150,11 +173,6 @@ class ElementCallActivity : } } - override fun onUserLeaveHint() { - super.onUserLeaveHint() - pipEventSink?.invoke(PictureInPictureEvents.EnterPictureInPicture) - } - override fun onDestroy() { super.onDestroy() releaseAudioFocus()