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 static com.android.settings.intelligence.ContextualCardProto.ContextualCard.Category.IMPORTANT_VALUE;
20 import static com.android.settings.intelligence.ContextualCardProto.ContextualCard.Category.STICKY_VALUE;
21 
22 import androidx.recyclerview.widget.DiffUtil;
23 
24 import java.util.List;
25 
26 /**
27  * A DiffCallback to calculate the difference between old and new {@link ContextualCard} List.
28  */
29 public class ContextualCardsDiffCallback extends DiffUtil.Callback {
30 
31     private final List<ContextualCard> mOldCards;
32     private final List<ContextualCard> mNewCards;
33 
ContextualCardsDiffCallback(List<ContextualCard> oldCards, List<ContextualCard> newCards)34     public ContextualCardsDiffCallback(List<ContextualCard> oldCards,
35             List<ContextualCard> newCards) {
36         mOldCards = oldCards;
37         mNewCards = newCards;
38     }
39 
40     @Override
getOldListSize()41     public int getOldListSize() {
42         return mOldCards.size();
43     }
44 
45     @Override
getNewListSize()46     public int getNewListSize() {
47         return mNewCards.size();
48     }
49 
50     @Override
areItemsTheSame(int oldCardPosition, int newCardPosition)51     public boolean areItemsTheSame(int oldCardPosition, int newCardPosition) {
52         return mOldCards.get(oldCardPosition).getName().equals(
53                 mNewCards.get(newCardPosition).getName());
54     }
55 
56     @Override
areContentsTheSame(int oldCardPosition, int newCardPosition)57     public boolean areContentsTheSame(int oldCardPosition, int newCardPosition) {
58         final ContextualCard newCard = mNewCards.get(newCardPosition);
59         // Sticky, important, or toggleable slices need to be updated continuously, which means
60         // their contents may change. So here we assume the content will always be different to
61         // force view rebinding.
62         if (newCard.getCategory() == STICKY_VALUE || newCard.getCategory() == IMPORTANT_VALUE
63                 || newCard.hasInlineAction()) {
64             return false;
65         }
66         return mOldCards.get(oldCardPosition).equals(newCard);
67     }
68 }