diff options
3 files changed, 42 insertions, 19 deletions
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModelTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModelTest.kt index 4226a9d89ad1..0551bfb89865 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModelTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModelTest.kt @@ -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 diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaCarouselController.kt b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaCarouselController.kt index 229e592a6a0c..19e3e0715989 100644 --- a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaCarouselController.kt +++ b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaCarouselController.kt @@ -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,9 +606,7 @@ 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() - } + 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) { diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModel.kt b/packages/SystemUI/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModel.kt index fd5f44594ae8..4e9093642c6b 100644 --- a/packages/SystemUI/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModel.kt +++ b/packages/SystemUI/src/com/android/systemui/media/controls/ui/viewmodel/MediaCarouselViewModel.kt @@ -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() + } + if (allowReorder) { + if (modelsPendingRemoval.size > 0) { + updateHostVisibility() } - isAttached = false + 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) } |