1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.supportleanbackshowcase.cards.presenters;
15
16import android.content.Context;
17import android.support.v17.leanback.supportleanbackshowcase.models.Card;
18import android.support.v17.leanback.widget.BaseCardView;
19import android.support.v17.leanback.widget.Presenter;
20import android.view.ViewGroup;
21
22/**
23 * This abstract, generic class will create and manage the
24 * ViewHolder and will provide typed Presenter callbacks such that you do not have to perform casts
25 * on your own.
26 *
27 * @param <T> View type for the card.
28 */
29public abstract class AbstractCardPresenter<T extends BaseCardView> extends Presenter {
30
31    private static final String TAG = "AbstractCardPresenter";
32    private final Context mContext;
33
34    /**
35     * @param context The current context.
36     */
37    public AbstractCardPresenter(Context context) {
38        mContext = context;
39    }
40
41    public Context getContext() {
42        return mContext;
43    }
44
45    @Override public final ViewHolder onCreateViewHolder(ViewGroup parent) {
46        T cardView = onCreateView();
47        return new ViewHolder(cardView);
48    }
49
50    @Override public final void onBindViewHolder(ViewHolder viewHolder, Object item) {
51        Card card = (Card) item;
52        onBindViewHolder(card, (T) viewHolder.view);
53    }
54
55    @Override public final void onUnbindViewHolder(ViewHolder viewHolder) {
56        onUnbindViewHolder((T) viewHolder.view);
57    }
58
59    public void onUnbindViewHolder(T cardView) {
60        // Nothing to clean up. Override if necessary.
61    }
62
63    /**
64     * Invoked when a new view is created.
65     *
66     * @return Returns the newly created view.
67     */
68    protected abstract T onCreateView();
69
70    /**
71     * Implement this method to update your card's view with the data bound to it.
72     *
73     * @param card The model containing the data for the card.
74     * @param cardView The view the card is bound to.
75     * @see Presenter#onBindViewHolder(Presenter.ViewHolder, Object)
76     */
77    public abstract void onBindViewHolder(Card card, T cardView);
78
79}
80