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 package androidx.room.integration.testapp;
18 
19 import android.view.ViewGroup;
20 import android.widget.TextView;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import androidx.paging.PagedList;
25 import androidx.paging.PagedListAdapter;
26 import androidx.recyclerview.widget.RecyclerView;
27 import androidx.room.integration.testapp.database.Customer;
28 import androidx.room.integration.testapp.database.LastNameAscCustomerDataSource;
29 
30 /**
31  * Sample adapter which uses a AsyncPagedListDiffer.
32  */
33 class PagedListCustomerAdapter extends PagedListAdapter<Customer, RecyclerView.ViewHolder> {
34     private RecyclerView mRecyclerView;
35     private boolean mSetObserved;
36     private int mScrollToPosition = -1;
37     private String mScrollToKey = null;
38 
PagedListCustomerAdapter()39     PagedListCustomerAdapter() {
40         super(Customer.DIFF_CALLBACK);
41     }
42 
setScrollToPosition(int position)43     void setScrollToPosition(int position) {
44         mScrollToPosition = position;
45     }
46 
setScrollToKey(String key)47     void setScrollToKey(String key) {
48         mScrollToKey = key;
49     }
50 
51     @Override
onCreateViewHolder(ViewGroup parent, int viewType)52     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
53         RecyclerView.ViewHolder holder = new RecyclerView.ViewHolder(
54                 new TextView(parent.getContext())) {};
55         holder.itemView.setMinimumHeight(400);
56         return holder;
57     }
58 
59     @Override
onBindViewHolder(RecyclerView.ViewHolder holder, int position)60     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
61         Customer customer = getItem(position);
62 
63         if (customer != null) {
64             ((TextView) (holder.itemView)).setText(customer.getId() + " " + customer.getLastName());
65         } else {
66             ((TextView) (holder.itemView)).setText(R.string.loading);
67         }
68     }
69 
findKeyInPagedList(@onNull String key, @NonNull PagedList<Customer> list)70     private static int findKeyInPagedList(@NonNull String key, @NonNull PagedList<Customer> list) {
71         for (int i = 0; i < list.size(); i++) {
72             @Nullable Customer customer = list.get(i);
73             if (customer != null
74                     && LastNameAscCustomerDataSource.getKeyStatic(customer).equals(key)) {
75                 return i;
76             }
77         }
78         return 0; // couldn't find, fall back to 0 - could alternately search with comparator
79     }
80 
81     @Override
submitList(PagedList<Customer> pagedList)82     public void submitList(PagedList<Customer> pagedList) {
83         super.submitList(pagedList);
84 
85         if (pagedList != null) {
86             final boolean firstSet = !mSetObserved;
87             mSetObserved = true;
88 
89             if (firstSet
90                     && mRecyclerView != null
91                     && (mScrollToPosition >= 0 || mScrollToKey != null)) {
92                 int localScrollToPosition;
93                 if (mScrollToKey != null) {
94                     localScrollToPosition = findKeyInPagedList(mScrollToKey, pagedList);
95                     mScrollToKey = null;
96                 } else {
97                     // if there's 20 items unloaded items (without placeholders holding the spots)
98                     // at the beginning of list, we subtract 20 from saved position
99                     localScrollToPosition = mScrollToPosition - pagedList.getPositionOffset();
100                 }
101                 mRecyclerView.scrollToPosition(localScrollToPosition);
102             }
103         }
104     }
105 
106     @Override
onAttachedToRecyclerView(RecyclerView recyclerView)107     public void onAttachedToRecyclerView(RecyclerView recyclerView) {
108         mRecyclerView = recyclerView;
109     }
110 
111     @Override
onDetachedFromRecyclerView(RecyclerView recyclerView)112     public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
113         mRecyclerView = null;
114     }
115 }
116