Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.home; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.home.HomeConfig.ItemType; |
michael@0 | 9 | import org.mozilla.gecko.home.HomeConfig.ViewConfig; |
michael@0 | 10 | import org.mozilla.gecko.home.PanelLayout.FilterManager; |
michael@0 | 11 | |
michael@0 | 12 | import org.mozilla.gecko.R; |
michael@0 | 13 | |
michael@0 | 14 | import android.content.Context; |
michael@0 | 15 | import android.database.Cursor; |
michael@0 | 16 | import android.support.v4.widget.CursorAdapter; |
michael@0 | 17 | import android.view.View; |
michael@0 | 18 | import android.view.ViewGroup; |
michael@0 | 19 | |
michael@0 | 20 | class PanelViewAdapter extends CursorAdapter { |
michael@0 | 21 | private static final int VIEW_TYPE_ITEM = 0; |
michael@0 | 22 | private static final int VIEW_TYPE_BACK = 1; |
michael@0 | 23 | |
michael@0 | 24 | private final ViewConfig viewConfig; |
michael@0 | 25 | private FilterManager filterManager; |
michael@0 | 26 | private final Context context; |
michael@0 | 27 | |
michael@0 | 28 | public PanelViewAdapter(Context context, ViewConfig viewConfig) { |
michael@0 | 29 | super(context, null, 0); |
michael@0 | 30 | this.context = context; |
michael@0 | 31 | this.viewConfig = viewConfig; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | public void setFilterManager(FilterManager manager) { |
michael@0 | 35 | this.filterManager = manager; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | @Override |
michael@0 | 39 | public final int getViewTypeCount() { |
michael@0 | 40 | return 2; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | @Override |
michael@0 | 44 | public int getCount() { |
michael@0 | 45 | return super.getCount() + (isShowingBack() ? 1 : 0); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | @Override |
michael@0 | 49 | public int getItemViewType(int position) { |
michael@0 | 50 | if (isShowingBack() && position == 0) { |
michael@0 | 51 | return VIEW_TYPE_BACK; |
michael@0 | 52 | } else { |
michael@0 | 53 | return VIEW_TYPE_ITEM; |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | @Override |
michael@0 | 58 | public final View getView(int position, View convertView, ViewGroup parent) { |
michael@0 | 59 | if (convertView == null) { |
michael@0 | 60 | convertView = newView(parent.getContext(), position, parent); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | bindView(convertView, position); |
michael@0 | 64 | return convertView; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | private View newView(Context context, int position, ViewGroup parent) { |
michael@0 | 68 | if (getItemViewType(position) == VIEW_TYPE_BACK) { |
michael@0 | 69 | return new PanelBackItemView(context, viewConfig.getBackImageUrl()); |
michael@0 | 70 | } else { |
michael@0 | 71 | return PanelItemView.create(context, viewConfig.getItemType()); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | private void bindView(View view, int position) { |
michael@0 | 76 | if (isShowingBack()) { |
michael@0 | 77 | if (position == 0) { |
michael@0 | 78 | final PanelBackItemView item = (PanelBackItemView) view; |
michael@0 | 79 | item.updateFromFilter(filterManager.getPreviousFilter()); |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | position--; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | final Cursor cursor = getCursor(position); |
michael@0 | 87 | final PanelItemView item = (PanelItemView) view; |
michael@0 | 88 | item.updateFromCursor(cursor); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | private boolean isShowingBack() { |
michael@0 | 92 | return (filterManager != null ? filterManager.canGoBack() : false); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | private final Cursor getCursor(int position) { |
michael@0 | 96 | final Cursor cursor = getCursor(); |
michael@0 | 97 | if (cursor == null || !cursor.moveToPosition(position)) { |
michael@0 | 98 | throw new IllegalStateException("Couldn't move cursor to position " + position); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | return cursor; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | @Override |
michael@0 | 105 | public final void bindView(View view, Context context, Cursor cursor) { |
michael@0 | 106 | // Do nothing. |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | @Override |
michael@0 | 110 | public final View newView(Context context, Cursor cursor, ViewGroup parent) { |
michael@0 | 111 | return null; |
michael@0 | 112 | } |
michael@0 | 113 | } |