1/*
2 * Copyright 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
17package androidx.car.widget;
18
19import android.content.Context;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.widget.TextView;
23
24import java.util.Collection;
25
26import androidx.annotation.NonNull;
27import androidx.car.R;
28import androidx.gridlayout.widget.GridLayout;
29
30/**
31 * This view shows a grid of alphabetic letters that you can tap on to advance a list to the
32 * beginning of that list.
33 */
34public class AlphaJumpOverlayView extends GridLayout {
35    private IAlphaJumpAdapter mAdapter;
36    private PagedListView mPagedListView;
37    private Collection<IAlphaJumpAdapter.Bucket> mBuckets;
38
39    public AlphaJumpOverlayView(@NonNull Context context) {
40        super(context);
41        setBackgroundResource(R.color.car_card);
42        setColumnCount(context.getResources().getInteger(R.integer.alpha_jump_button_columns));
43        setUseDefaultMargins(false);
44    }
45
46    void init(PagedListView plv, IAlphaJumpAdapter adapter) {
47        mPagedListView = plv;
48        mAdapter = adapter;
49        mBuckets = adapter.getAlphaJumpBuckets();
50
51        createButtons();
52    }
53
54    @Override
55    protected void onVisibilityChanged(View changedView, int visibility) {
56        // TODO: change the hamburger button into a back button...
57        if (visibility == VISIBLE && changedView == this) {
58            mAdapter.onAlphaJumpEnter();
59        }
60    }
61
62    private void createButtons() {
63        LayoutInflater inflater = LayoutInflater.from(getContext());
64        removeAllViews();
65        for (IAlphaJumpAdapter.Bucket bucket : mBuckets) {
66            View container = inflater.inflate(R.layout.car_alpha_jump_button, this, false);
67            TextView btn = container.findViewById(R.id.button);
68            btn.setText(bucket.getLabel());
69            btn.setOnClickListener(this::onButtonClick);
70            btn.setTag(bucket);
71            if (bucket.isEmpty()) {
72                btn.setEnabled(false);
73            }
74            addView(container);
75        }
76    }
77
78    private void onButtonClick(View v) {
79        setVisibility(View.GONE);
80        IAlphaJumpAdapter.Bucket bucket = (IAlphaJumpAdapter.Bucket) v.getTag();
81        if (bucket != null) {
82            mAdapter.onAlphaJumpLeave(bucket);
83
84            mPagedListView.snapToPosition(bucket.getIndex());
85        }
86    }
87}
88