Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix animation frozen after app went background #1263

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 The Android Open Source Project
*
* 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 androidx.compose.mpp.demo.bugs

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Switch
import androidx.compose.mpp.demo.Screen
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import platform.UIKit.UIApplication
import platform.Foundation.NSURL

val AnimationFreezeBug = Screen.Example("AnimationFreezeBug") {
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
Column(modifier = Modifier.fillMaxSize()) {
var state by remember { mutableStateOf(true) }
Switch(checked = state, onCheckedChange = {
state = it
UIApplication.sharedApplication.openURL(NSURL.URLWithString("app-settings:")!!)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ val IosBugs = Screen.Selection(
KeyboardIMEActionPopup,
ProperContainmentDisposal,
ComposeAndNativeScroll,
MeasureAndLayoutCrash
MeasureAndLayoutCrash,
AnimationFreezeBug,
)
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import androidx.compose.ui.unit.roundToIntRect
import androidx.compose.ui.unit.toDpRect
import androidx.compose.ui.unit.toOffset
import androidx.compose.ui.window.ComposeSceneKeyboardOffsetManager
import androidx.compose.ui.window.ApplicationStateListener
import androidx.compose.ui.window.FocusStack
import androidx.compose.ui.window.InteractionUIView
import androidx.compose.ui.window.KeyboardEventHandler
Expand Down Expand Up @@ -253,6 +254,15 @@ internal class ComposeSceneMediator(
renderingUIViewFactory(interopContext, renderDelegate)
}

private val applicationStateListener = ApplicationStateListener { isForeground ->
// Sometimes the application can trigger animation and go background before the animation is
// finished. The scheduled GPU work is performed, but no presentation can be done, causing
// mismatch between visual state and application state. This can be fixed by forcing
// a redraw when app returns to foreground, which will ensure that the visual state is in
// sync with the application state even if such sequence of events took a place.
renderingView.needRedraw()
}

/**
* view, that contains [interopViewContainer] and [interactionView] and is added to [container]
*/
Expand Down Expand Up @@ -490,6 +500,7 @@ internal class ComposeSceneMediator(

fun dispose() {
uiKitTextInputService.stopInput()
applicationStateListener.dispose()
focusStack?.popUntilNext(renderingView)
keyboardManager.stop()
renderingView.dispose()
Expand All @@ -501,7 +512,7 @@ internal class ComposeSceneMediator(
interopContext.retrieve().actions.forEach { it.invoke() }
}

fun onComposeSceneInvalidate() = renderingView.needRedraw()
private fun onComposeSceneInvalidate() = renderingView.needRedraw()

fun setLayout(value: SceneLayout) {
_layout = value
Expand Down
Loading