1/*
2 * Copyright 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 com.example.android.supportv7.widget.selection.fancy;
18
19import static androidx.core.util.Preconditions.checkArgument;
20
21import android.content.Context;
22import android.net.Uri;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.LinearLayout;
27
28import androidx.recyclerview.selection.ItemKeyProvider;
29import androidx.recyclerview.selection.SelectionTracker;
30import androidx.recyclerview.widget.RecyclerView;
31
32import com.example.android.supportv7.Cheeses;
33import com.example.android.supportv7.R;
34
35final class FancySelectionDemoAdapter extends RecyclerView.Adapter<FancyHolder> {
36
37    private final ContentUriKeyProvider mKeyProvider;
38    private final Context mContext;
39
40    // This should be replaced at "bind" time with a real test that
41    // asks SelectionTracker.
42    private SelectionTest mSelTest;
43
44    FancySelectionDemoAdapter(Context context) {
45        mContext = context;
46        mKeyProvider = new ContentUriKeyProvider("cheeses", Cheeses.sCheeseStrings);
47        mSelTest = new SelectionTest() {
48            @Override
49            public boolean isSelected(Uri id) {
50                throw new IllegalStateException(
51                        "Adapter must be initialized with SelectionTracker.");
52            }
53        };
54
55        // In the fancy edition of selection support we supply access to stable
56        // ids using content URI. Since we can map between position and selection key
57        // at will we get fancy dependent functionality like band selection and range support.
58        setHasStableIds(false);
59    }
60
61    ItemKeyProvider<Uri> getItemKeyProvider() {
62        return mKeyProvider;
63    }
64
65    // Glue together SelectionTracker and the adapter.
66    public void bindSelectionHelper(final SelectionTracker<Uri> selectionTracker) {
67        checkArgument(selectionTracker != null);
68        mSelTest = new SelectionTest() {
69            @Override
70            public boolean isSelected(Uri id) {
71                return selectionTracker.isSelected(id);
72            }
73        };
74    }
75
76    void loadData() {
77        onDataReady();
78    }
79
80    private void onDataReady() {
81        notifyDataSetChanged();
82    }
83
84    @Override
85    public int getItemCount() {
86        return Cheeses.sCheeseStrings.length;
87    }
88
89    @Override
90    public long getItemId(int position) {
91        return position;
92    }
93
94    @Override
95    public void onBindViewHolder(FancyHolder holder, int position) {
96        Uri uri = mKeyProvider.getKey(position);
97        holder.update(uri, uri.getLastPathSegment(), mSelTest.isSelected(uri));
98    }
99
100    @Override
101    public FancyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
102        LinearLayout layout = inflateLayout(mContext, parent, R.layout.selection_demo_list_item);
103        return new FancyHolder(layout);
104    }
105
106    @SuppressWarnings("TypeParameterUnusedInFormals")  // Convenience to avoid clumsy cast.
107    private static <V extends View> V inflateLayout(
108            Context context, ViewGroup parent, int layout) {
109
110        return (V) LayoutInflater.from(context).inflate(layout, parent, false);
111    }
112
113    private interface SelectionTest {
114        boolean isSelected(Uri id);
115    }
116}
117