Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68a457d6 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[New pipeline] Fix visibility issues" into main

parents 6981b524 cd1472ea
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ class MediaCarouselViewModelTest : SysuiTestCase() {
            assertThat(mediaControl2.instanceId).isEqualTo(instanceId2)
            assertThat(mediaControl1.instanceId).isEqualTo(instanceId1)

            underTest.onAttached()
            underTest.onReorderingAllowed()

            mediaControl1 = sortedMedia?.get(0) as MediaCommonViewModel.MediaControl
            mediaControl2 = sortedMedia?.get(1) as MediaCommonViewModel.MediaControl
+21 −9
Original line number Diff line number Diff line
@@ -308,7 +308,11 @@ constructor(
     * It will be called when the container is out of view.
     */
    lateinit var updateUserVisibility: () -> Unit
    lateinit var updateHostVisibility: () -> Unit
    var updateHostVisibility: () -> Unit = {}
        set(value) {
            field = value
            mediaCarouselViewModel.updateHostVisibility = value
        }

    private val isReorderingAllowed: Boolean
        get() = visualStabilityProvider.isReorderingAllowed
@@ -345,6 +349,20 @@ constructor(
        configurationController.addCallback(configListener)
        if (!mediaFlags.isMediaControlsRefactorEnabled()) {
            setUpListeners()
        } else {
            val visualStabilityCallback = OnReorderingAllowedListener {
                mediaCarouselViewModel.onReorderingAllowed()

                // Update user visibility so that no extra impression will be logged when
                // activeMediaIndex resets to 0
                if (this::updateUserVisibility.isInitialized) {
                    updateUserVisibility()
                }

                // Let's reset our scroll position
                mediaCarouselScrollHandler.scrollToStart()
            }
            visualStabilityProvider.addPersistentReorderingAllowedListener(visualStabilityCallback)
        }
        mediaFrame.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
            // The pageIndicator is not laid out yet when we get the current state update,
@@ -366,10 +384,6 @@ constructor(
        )
        keyguardUpdateMonitor.registerCallback(keyguardUpdateMonitorCallback)
        mediaCarousel.repeatWhenAttached {
            if (mediaFlags.isMediaControlsRefactorEnabled()) {
                mediaCarouselViewModel.onAttached()
                mediaCarouselScrollHandler.scrollToStart()
            }
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                listenForAnyStateToGoneKeyguardTransition(this)
                listenForAnyStateToLockscreenTransition(this)
@@ -592,10 +606,8 @@ constructor(
                        if (!immediately) {
                            // Although it wasn't requested, we were able to process the removal
                            // immediately since reordering is allowed. So, notify hosts to update
                            if (this@MediaCarouselController::updateHostVisibility.isInitialized) {
                            updateHostVisibility()
                        }
                        }
                    } else {
                        keysNeedRemoval.add(key)
                    }
@@ -751,6 +763,7 @@ constructor(
            }
        }
        viewController.setListening(mediaCarouselScrollHandler.visibleToUser && currentlyExpanded)
        controllerByViewModel[commonViewModel] = viewController
        updateViewControllerToState(viewController, noAnimation = true)
        updatePageIndicator()
        if (
@@ -764,7 +777,6 @@ constructor(
        mediaCarouselScrollHandler.onPlayersChanged()
        mediaFrame.requiresRemeasuring = true
        commonViewModel.onAdded(commonViewModel)
        controllerByViewModel[commonViewModel] = viewController
    }

    private fun onUpdated(commonViewModel: MediaCommonViewModel) {
+20 −9
Original line number Diff line number Diff line
@@ -57,12 +57,12 @@ constructor(
    val mediaItems: StateFlow<List<MediaCommonViewModel>> =
        interactor.currentMedia
            .map { sortedItems ->
                buildList {
                val mediaList = buildList {
                    sortedItems.forEach { commonModel ->
                        // When view is started we should make sure to clean models that are pending
                        // removal.
                        // This action should only be triggered once.
                        if (!isAttached || !modelsPendingRemoval.contains(commonModel)) {
                        if (!allowReorder || !modelsPendingRemoval.contains(commonModel)) {
                            when (commonModel) {
                                is MediaCommonModel.MediaControl -> add(toViewModel(commonModel))
                                is MediaCommonModel.MediaRecommendations ->
@@ -70,11 +70,16 @@ constructor(
                            }
                        }
                    }
                    if (isAttached) {
                        modelsPendingRemoval.clear()
                }
                    isAttached = false
                if (allowReorder) {
                    if (modelsPendingRemoval.size > 0) {
                        updateHostVisibility()
                    }
                    modelsPendingRemoval.clear()
                }
                allowReorder = false

                mediaList
            }
            .stateIn(
                scope = applicationScope,
@@ -82,6 +87,8 @@ constructor(
                initialValue = emptyList(),
            )

    var updateHostVisibility: () -> Unit = {}

    private val mediaControlByInstanceId =
        mutableMapOf<InstanceId, MediaCommonViewModel.MediaControl>()

@@ -89,15 +96,15 @@ constructor(

    private var modelsPendingRemoval: MutableSet<MediaCommonModel> = mutableSetOf()

    private var isAttached = false
    private var allowReorder = false

    fun onSwipeToDismiss() {
        logger.logSwipeDismiss()
        interactor.onSwipeToDismiss()
    }

    fun onAttached() {
        isAttached = true
    fun onReorderingAllowed() {
        allowReorder = true
        interactor.reorderMedia()
    }

@@ -194,7 +201,11 @@ constructor(
    ) {
        if (immediatelyRemove || isReorderingAllowed()) {
            interactor.dismissSmartspaceRecommendation(commonModel.recsLoadingModel.key, 0L)
            // TODO if not immediate remove update host visibility
            if (!immediatelyRemove) {
                // Although it wasn't requested, we were able to process the removal
                // immediately since reordering is allowed. So, notify hosts to update
                updateHostVisibility()
            }
        } else {
            modelsPendingRemoval.add(commonModel)
        }