summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutRecyclerAdapter.java
blob: 090d74f9db44bf2b405330c1c9ae2833aeb3f2ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.tabs;

import org.mozilla.gecko.Tab;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;

public class TabsLayoutRecyclerAdapter
        extends RecyclerView.Adapter<TabsLayoutRecyclerAdapter.TabsListViewHolder> {

    private static final String LOGTAG = "Gecko" + TabsLayoutRecyclerAdapter.class.getSimpleName();

    private final int tabLayoutId;
    private @NonNull ArrayList<Tab> tabs;
    private final LayoutInflater inflater;
    private final boolean isPrivate;
    // Click listener for the close button on itemViews.
    private final Button.OnClickListener closeOnClickListener;

    // The TabsLayoutItemView takes care of caching its own Views, so we don't need to do anything
    // here except not be abstract.
    public static class TabsListViewHolder extends RecyclerView.ViewHolder {
        public TabsListViewHolder(View itemView) {
            super(itemView);
        }
    }

    public TabsLayoutRecyclerAdapter(Context context, int tabLayoutId, boolean isPrivate,
                                     Button.OnClickListener closeOnClickListener) {
        inflater = LayoutInflater.from(context);
        this.tabLayoutId = tabLayoutId;
        this.isPrivate = isPrivate;
        this.closeOnClickListener = closeOnClickListener;
        tabs = new ArrayList<>(0);
    }

    /* package */ final void setTabs(@NonNull ArrayList<Tab> tabs) {
        this.tabs = tabs;
        notifyDataSetChanged();
    }

    /* package */ final void clear() {
        tabs = new ArrayList<>(0);
        notifyDataSetChanged();
    }

    /* package */ final boolean removeTab(Tab tab) {
        final int position = getPositionForTab(tab);
        if (position == -1) {
            return false;
        }
        tabs.remove(position);
        notifyItemRemoved(position);
        return true;
    }

    /* package */ final int getPositionForTab(Tab tab) {
        if (tab == null) {
            return -1;
        }

        return tabs.indexOf(tab);
    }

    /* package */ void notifyTabChanged(Tab tab) {
        notifyItemChanged(getPositionForTab(tab));
    }

    /* package */ void notifyTabInserted(Tab tab, int index) {
        if (index >= 0 && index <= tabs.size()) {
            tabs.add(index, tab);
            notifyItemInserted(index);
        } else {
            // Add to the end.
            tabs.add(tab);
            notifyItemInserted(tabs.size() - 1);
            // index == -1 is a valid way to add to the end, the other cases are errors.
            if (index != -1) {
                Log.e(LOGTAG, "Tab was inserted at an invalid position: " + Integer.toString(index));
            }
        }
    }

    @Override
    public int getItemCount() {
        return tabs.size();
    }

    private Tab getItem(int position) {
        return tabs.get(position);
    }

    @Override
    public void onBindViewHolder(TabsListViewHolder viewHolder, int position) {
        final Tab tab = getItem(position);
        final TabsLayoutItemView itemView = (TabsLayoutItemView) viewHolder.itemView;
        itemView.assignValues(tab);
        // Be careful (re)setting position values here: bind is called on each notifyItemChanged,
        // so you could be stomping on values that have been set in support of other animations
        // that are already underway.
    }

    @Override
    public TabsListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final TabsLayoutItemView viewItem = (TabsLayoutItemView) inflater.inflate(tabLayoutId, parent, false);
        viewItem.setPrivateMode(isPrivate);
        viewItem.setCloseOnClickListener(closeOnClickListener);

        return new TabsListViewHolder(viewItem);
    }
}