1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.media.controls.ui.util 18 19 import androidx.recyclerview.widget.ListUpdateCallback 20 import com.android.systemui.media.controls.ui.viewmodel.MediaControlViewModel 21 import kotlin.math.min 22 23 /** A [ListUpdateCallback] to apply media events needed to reach the new state. */ 24 class MediaViewModelListUpdateCallback( 25 private val old: List<MediaControlViewModel>, 26 private val new: List<MediaControlViewModel>, 27 private val onAdded: (MediaControlViewModel, Int) -> Unit, 28 private val onUpdated: (MediaControlViewModel, Int) -> Unit, 29 private val onRemoved: (MediaControlViewModel) -> Unit, 30 private val onMoved: (MediaControlViewModel, Int, Int) -> Unit, 31 ) : ListUpdateCallback { 32 onInsertednull33 override fun onInserted(position: Int, count: Int) { 34 for (i in position until position + count) { 35 onAdded(new[i], i) 36 } 37 } 38 onRemovednull39 override fun onRemoved(position: Int, count: Int) { 40 for (i in position until position + count) { 41 onRemoved(old[i]) 42 } 43 } 44 onMovednull45 override fun onMoved(fromPosition: Int, toPosition: Int) { 46 onMoved(old[fromPosition], fromPosition, toPosition) 47 } 48 onChangednull49 override fun onChanged(position: Int, count: Int, payload: Any?) { 50 for (i in position until min(position + count, new.size)) { 51 onUpdated(new[i], position) 52 } 53 } 54 } 55