1 /*
2  * Copyright (C) 2018 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.settings.homepage.contextualcards;
18 
19 import androidx.recyclerview.widget.DiffUtil;
20 
21 import java.util.List;
22 
23 /**
24  * A DiffCallback to calculate the difference between old and new {@link ContextualCard} List.
25  */
26 public class ContextualCardsDiffCallback extends DiffUtil.Callback {
27 
28     private final List<ContextualCard> mOldCards;
29     private final List<ContextualCard> mNewCards;
30 
ContextualCardsDiffCallback(List<ContextualCard> oldCards, List<ContextualCard> newCards)31     public ContextualCardsDiffCallback(List<ContextualCard> oldCards,
32             List<ContextualCard> newCards) {
33         mOldCards = oldCards;
34         mNewCards = newCards;
35     }
36 
37     @Override
getOldListSize()38     public int getOldListSize() {
39         return mOldCards.size();
40     }
41 
42     @Override
getNewListSize()43     public int getNewListSize() {
44         return mNewCards.size();
45     }
46 
47     @Override
areItemsTheSame(int oldCardPosition, int newCardPosition)48     public boolean areItemsTheSame(int oldCardPosition, int newCardPosition) {
49         return mOldCards.get(oldCardPosition).getName().equals(
50                 mNewCards.get(newCardPosition).getName());
51     }
52 
53     @Override
areContentsTheSame(int oldCardPosition, int newCardPosition)54     public boolean areContentsTheSame(int oldCardPosition, int newCardPosition) {
55         // Slices with toggles needs to be updated continuously, which means their contents may
56         // change. So here we assume the content will always be different to force view rebinding.
57         if (mNewCards.get(newCardPosition).hasInlineAction()) {
58             return false;
59         }
60         return mOldCards.get(oldCardPosition).equals(mNewCards.get(newCardPosition));
61     }
62 }