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
17package androidx.room.integration.testapp;
18
19import android.view.ViewGroup;
20import android.widget.TextView;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.Nullable;
24import androidx.paging.PagedList;
25import androidx.paging.PagedListAdapter;
26import androidx.recyclerview.widget.RecyclerView;
27import androidx.room.integration.testapp.database.Customer;
28import androidx.room.integration.testapp.database.LastNameAscCustomerDataSource;
29
30/**
31 * Sample adapter which uses a AsyncPagedListDiffer.
32 */
33class 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
39    PagedListCustomerAdapter() {
40        super(Customer.DIFF_CALLBACK);
41    }
42
43    void setScrollToPosition(int position) {
44        mScrollToPosition = position;
45    }
46
47    void setScrollToKey(String key) {
48        mScrollToKey = key;
49    }
50
51    @Override
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
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
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
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
107    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
108        mRecyclerView = recyclerView;
109    }
110
111    @Override
112    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
113        mRecyclerView = null;
114    }
115}
116