mobile/android/base/home/PanelItemView.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/home/PanelItemView.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.home;
    1.10 +
    1.11 +import org.mozilla.gecko.R;
    1.12 +import org.mozilla.gecko.db.BrowserContract.HomeItems;
    1.13 +import org.mozilla.gecko.home.HomeConfig.ItemType;
    1.14 +
    1.15 +import android.content.Context;
    1.16 +import android.database.Cursor;
    1.17 +import android.text.TextUtils;
    1.18 +import android.view.LayoutInflater;
    1.19 +import android.view.View;
    1.20 +import android.widget.ImageView;
    1.21 +import android.widget.LinearLayout;
    1.22 +import android.widget.TextView;
    1.23 +
    1.24 +import com.squareup.picasso.Picasso;
    1.25 +
    1.26 +class PanelItemView extends LinearLayout {
    1.27 +    private final TextView title;
    1.28 +    private final TextView description;
    1.29 +    private final ImageView image;
    1.30 +    private final LinearLayout titleDescContainer;
    1.31 +
    1.32 +    private PanelItemView(Context context, int layoutId) {
    1.33 +        super(context);
    1.34 +
    1.35 +        LayoutInflater.from(context).inflate(layoutId, this);
    1.36 +        title = (TextView) findViewById(R.id.title);
    1.37 +        description = (TextView) findViewById(R.id.description);
    1.38 +        image = (ImageView) findViewById(R.id.image);
    1.39 +        titleDescContainer = (LinearLayout) findViewById(R.id.title_desc_container);
    1.40 +    }
    1.41 +
    1.42 +    public void updateFromCursor(Cursor cursor) {
    1.43 +        int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE);
    1.44 +        final String titleText = cursor.getString(titleIndex);
    1.45 +
    1.46 +        // Only show title if the item has one
    1.47 +        final boolean hasTitle = !TextUtils.isEmpty(titleText);
    1.48 +        title.setVisibility(hasTitle ? View.VISIBLE : View.GONE);
    1.49 +        titleDescContainer.setVisibility(hasTitle ? View.VISIBLE : View.GONE);
    1.50 +        if (hasTitle) {
    1.51 +            title.setText(titleText);
    1.52 +        }
    1.53 +
    1.54 +        int descriptionIndex = cursor.getColumnIndexOrThrow(HomeItems.DESCRIPTION);
    1.55 +        final String descriptionText = cursor.getString(descriptionIndex);
    1.56 +
    1.57 +        // Only show description if the item has one
    1.58 +        final boolean hasDescription = !TextUtils.isEmpty(descriptionText);
    1.59 +        description.setVisibility(hasDescription ? View.VISIBLE : View.GONE);
    1.60 +        if (hasDescription) {
    1.61 +            description.setText(descriptionText);
    1.62 +        }
    1.63 +
    1.64 +        titleDescContainer.setVisibility(hasTitle || hasDescription ? View.VISIBLE : View.GONE);
    1.65 +
    1.66 +        int imageIndex = cursor.getColumnIndexOrThrow(HomeItems.IMAGE_URL);
    1.67 +        final String imageUrl = cursor.getString(imageIndex);
    1.68 +
    1.69 +        // Only try to load the image if the item has define image URL
    1.70 +        final boolean hasImageUrl = !TextUtils.isEmpty(imageUrl);
    1.71 +        image.setVisibility(hasImageUrl ? View.VISIBLE : View.GONE);
    1.72 +
    1.73 +        if (hasImageUrl) {
    1.74 +            Picasso.with(getContext())
    1.75 +                   .load(imageUrl)
    1.76 +                   .into(image);
    1.77 +        }
    1.78 +    }
    1.79 +
    1.80 +    private static class ArticleItemView extends PanelItemView {
    1.81 +        private ArticleItemView(Context context) {
    1.82 +            super(context, R.layout.panel_article_item);
    1.83 +            setOrientation(LinearLayout.HORIZONTAL);
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    private static class ImageItemView extends PanelItemView {
    1.88 +        private ImageItemView(Context context) {
    1.89 +            super(context, R.layout.panel_image_item);
    1.90 +            setOrientation(LinearLayout.VERTICAL);
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    public static PanelItemView create(Context context, ItemType itemType) {
    1.95 +        switch(itemType) {
    1.96 +            case ARTICLE:
    1.97 +                return new ArticleItemView(context);
    1.98 +
    1.99 +            case IMAGE:
   1.100 +                return new ImageItemView(context);
   1.101 +
   1.102 +            default:
   1.103 +                throw new IllegalArgumentException("Could not create panel item view from " + itemType);
   1.104 +        }
   1.105 +    }
   1.106 +}

mercurial