1 /*
2  * Copyright (C) 2017 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 
18 package com.android.settings.intelligence.search;
19 
20 import androidx.recyclerview.widget.DiffUtil;
21 
22 import java.util.List;
23 
24 /**
25  * Callback for DiffUtil to elegantly update search data when the query changes.
26  */
27 public class SearchResultDiffCallback extends DiffUtil.Callback {
28 
29     private List<? extends SearchResult> mOldList;
30     private List<? extends SearchResult> mNewList;
31 
SearchResultDiffCallback(List<? extends SearchResult> oldList, List<? extends SearchResult> newList)32     public SearchResultDiffCallback(List<? extends SearchResult> oldList,
33             List<? extends SearchResult> newList) {
34         mOldList = oldList;
35         mNewList = newList;
36     }
37 
38     @Override
getOldListSize()39     public int getOldListSize() {
40         return mOldList.size();
41     }
42 
43     @Override
getNewListSize()44     public int getNewListSize() {
45         return mNewList.size();
46     }
47 
48     @Override
areItemsTheSame(int oldItemPosition, int newItemPosition)49     public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
50         return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
51     }
52 
53     @Override
areContentsTheSame(int oldItemPosition, int newItemPosition)54     public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
55         return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
56     }
57 }
58