Android Open Source - Amphitheatre Tv Shows Card Presenter






From Project

Back to project page Amphitheatre.

License

The source code is released under:

Apache License

If you think the Android project Amphitheatre listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Jerrell Mardis//w  w  w  . j  a v a2  s .  c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jerrellmardis.amphitheatre.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.Presenter;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jerrellmardis.amphitheatre.R;
import com.jerrellmardis.amphitheatre.model.Video;
import com.jerrellmardis.amphitheatre.model.VideoGroup;
import com.jerrellmardis.amphitheatre.util.Constants;
import com.jerrellmardis.amphitheatre.util.Utils;
import com.squareup.picasso.Picasso;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TvShowsCardPresenter extends CardPresenter {

    public TvShowsCardPresenter(Context context) {
        super(context);
        mCardHeight = 338;
        mCardWidth = 600;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        final ImageCardView cardView = new ImageCardView(mContext);

        cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, final boolean isFocused) {
                setFocusState(cardView, isFocused, view);
            }
        });

        cardView.setFocusable(true);
        cardView.setFocusableInTouchMode(true);
        cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
        return new ViewHolder(cardView);
    }

    private void setFocusState(final ImageCardView cardView, final boolean isFocused, View view) {
        final Drawable mainImage = ((ImageView)view.findViewById(R.id.main_image)).getDrawable();

        if (isFocused) {
            ((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(4);
        }
        else {
            ((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(1);
        }

        if (mainImage != null) {
            Palette.generateAsync(((BitmapDrawable) mainImage).getBitmap(), new Palette.PaletteAsyncListener() {

                @Override
                public void onGenerated(Palette palette) {

                    if (isFocused) {
                        Utils.animateColorChange(
                                cardView.findViewById(R.id.info_field),
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_BACKGROUND_UNSELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_info_bg_color)),
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_BACKGROUND_SELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_info_bg_color))
                        );

                        ((TextView) cardView.findViewById(R.id.title_text)).setTextColor(
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_TITLE_SELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_title_text_color))
                        );

                        ((TextView) cardView.findViewById(R.id.content_text)).setTextColor(
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_CONTENT_SELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_content_text_color))
                        );
                    } else {
                        Utils.animateColorChange(
                                cardView.findViewById(R.id.info_field),
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_BACKGROUND_SELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_info_bg_color)),
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_BACKGROUND_UNSELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_info_bg_color))
                        );

                        ((TextView) cardView.findViewById(R.id.title_text)).setTextColor(
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_TITLE_UNSELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_title_text_color))
                        );

                        ((TextView) cardView.findViewById(R.id.content_text)).setTextColor(
                                Utils.getPaletteColor(
                                        palette,
                                        mSharedPrefs.getString(Constants.PALETTE_CONTENT_UNSELECTED, ""),
                                        mContext.getResources().getColor(R.color.lb_basic_card_content_text_color))
                        );
                    }
                }
            });
        }
    }

    @Override
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
        if (item instanceof VideoGroup) {
            VideoGroup group = (VideoGroup) item;
            ViewHolder holder = (ViewHolder) viewHolder;

            holder.mCardView.setTitleText(group.getVideo().getName());
            holder.mCardView.setMainImageDimensions(mCardWidth, mCardHeight);

            if (!group.getVideo().isMovie() && group.getNumOfVideos() > 0) {
                String contentText = mContext.getResources()
                        .getQuantityString(R.plurals.numberOfEpisodesAvailable, group.getNumOfVideos(),
                                group.getNumOfVideos());
                holder.mCardView.setContentText(contentText);
            }

            String url = group.getVideo().getBackgroundImageUrl();
            Picasso.with(mContext)
                    .load(url)
                    .placeholder(R.drawable.placeholder)
                    .resize(mCardWidth, mCardHeight)
                    .centerCrop()
                    .into(holder);
        } else {
            Video video = (Video) item;
            ViewHolder holder = (ViewHolder) viewHolder;

            holder.mCardView.setTitleText(video.getName());
            holder.mCardView.setContentText("");
            holder.mCardView.setMainImageDimensions(mCardWidth, mCardHeight);

            String url = video.getBackgroundImageUrl();

            if (video.getTvShow() != null && video.getTvShow().getEpisode() != null) {
                holder.mCardView.setTitleText(video.getTvShow().getEpisode().getName());

                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = formatter.parse(video.getTvShow().getEpisode().getAirDate());
                    DateFormat df = DateFormat.getDateInstance();
                    String subtitle = String.format(mContext.getString(R.string.aired), df.format(date));
                    holder.mCardView.setContentText(subtitle);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                url = video.getTvShow().getEpisode().getStillPath();
            }

            Picasso.with(mContext)
                    .load(url)
                    .placeholder(R.drawable.placeholder)
                    .resize(mCardWidth, mCardHeight)
                    .centerCrop()
                    .into(holder);

            //rebuild the layout for the info area so it expands when we set the maxlines higher
            FrameLayout.LayoutParams infoLayout = new FrameLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            holder.mCardView.findViewById(R.id.info_field).setLayoutParams(infoLayout);
            RelativeLayout.LayoutParams contentLayout = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            contentLayout.addRule(RelativeLayout.BELOW, R.id.title_text);
            holder.mCardView.findViewById(R.id.content_text).setLayoutParams(contentLayout);

            // set color to standard for when the user scrolls and the view is reused
            Utils.animateColorChange(
                    holder.mCardView.findViewById(R.id.info_field),
                    holder.mCardView.findViewById(R.id.info_field).getDrawingCacheBackgroundColor(),
                    mContext.getResources().getColor(R.color.lb_basic_card_info_bg_color)
            );
        }
    }
}




Java Source Code List

com.jerrellmardis.amphitheatre.activity.BootupActivity.java
com.jerrellmardis.amphitheatre.activity.BrowseActivity.java
com.jerrellmardis.amphitheatre.activity.DetailsActivity.java
com.jerrellmardis.amphitheatre.activity.GridViewActivity.java
com.jerrellmardis.amphitheatre.activity.SearchActivity.java
com.jerrellmardis.amphitheatre.api.ApiClient.java
com.jerrellmardis.amphitheatre.api.GuessItClient.java
com.jerrellmardis.amphitheatre.api.MediaClientFactory.java
com.jerrellmardis.amphitheatre.api.MediaClient.java
com.jerrellmardis.amphitheatre.api.TMDbClient.java
com.jerrellmardis.amphitheatre.api.TVDBClientTest.java
com.jerrellmardis.amphitheatre.api.TVDBClient.java
com.jerrellmardis.amphitheatre.api.TVDBService.java
com.jerrellmardis.amphitheatre.fragment.AddSourceDialogFragment.java
com.jerrellmardis.amphitheatre.fragment.BrowseFragment.java
com.jerrellmardis.amphitheatre.fragment.CustomizeDialogFragment.java
com.jerrellmardis.amphitheatre.fragment.SearchFragment.java
com.jerrellmardis.amphitheatre.fragment.VerticalGridFragment.java
com.jerrellmardis.amphitheatre.fragment.VideoDetailsFragment.java
com.jerrellmardis.amphitheatre.listeners.RowBuilderTaskListener.java
com.jerrellmardis.amphitheatre.listeners.TaskListener.java
com.jerrellmardis.amphitheatre.model.GridGenre.java
com.jerrellmardis.amphitheatre.model.Source.java
com.jerrellmardis.amphitheatre.model.VideoGroup.java
com.jerrellmardis.amphitheatre.model.Video.java
com.jerrellmardis.amphitheatre.model.guessit.Guess.java
com.jerrellmardis.amphitheatre.model.tmdb.Config.java
com.jerrellmardis.amphitheatre.model.tmdb.Episode.java
com.jerrellmardis.amphitheatre.model.tmdb.Genre.java
com.jerrellmardis.amphitheatre.model.tmdb.Movie.java
com.jerrellmardis.amphitheatre.model.tmdb.ProductionCompany.java
com.jerrellmardis.amphitheatre.model.tmdb.SearchResult.java
com.jerrellmardis.amphitheatre.model.tmdb.TvShow.java
com.jerrellmardis.amphitheatre.model.tmdb.Videos.java
com.jerrellmardis.amphitheatre.model.tvdb.BaseResponse.java
com.jerrellmardis.amphitheatre.model.tvdb.EpisodeResponse.java
com.jerrellmardis.amphitheatre.model.tvdb.Episode.java
com.jerrellmardis.amphitheatre.model.tvdb.Language.java
com.jerrellmardis.amphitheatre.model.tvdb.SeriesResult.java
com.jerrellmardis.amphitheatre.model.tvdb.Series.java
com.jerrellmardis.amphitheatre.server.StreamServer.java
com.jerrellmardis.amphitheatre.server.StreamSource.java
com.jerrellmardis.amphitheatre.server.Streamer.java
com.jerrellmardis.amphitheatre.service.LibraryUpdateService.java
com.jerrellmardis.amphitheatre.service.RecommendationsService.java
com.jerrellmardis.amphitheatre.task.DetailRowBuilderTask.java
com.jerrellmardis.amphitheatre.task.DownloadMovieTask.java
com.jerrellmardis.amphitheatre.task.DownloadTaskHelper.java
com.jerrellmardis.amphitheatre.task.DownloadTvShowTask.java
com.jerrellmardis.amphitheatre.task.DownloadVideoTask.java
com.jerrellmardis.amphitheatre.task.GetFilesTask.java
com.jerrellmardis.amphitheatre.task.NetworkSearchTask.java
com.jerrellmardis.amphitheatre.util.ApiConstants.java
com.jerrellmardis.amphitheatre.util.Base64.java
com.jerrellmardis.amphitheatre.util.BlurTransform.java
com.jerrellmardis.amphitheatre.util.Constants.java
com.jerrellmardis.amphitheatre.util.Enums.java
com.jerrellmardis.amphitheatre.util.PicassoBackgroundManagerTarget.java
com.jerrellmardis.amphitheatre.util.RecommendationBuilder.java
com.jerrellmardis.amphitheatre.util.SecurePreferences.java
com.jerrellmardis.amphitheatre.util.Utils.java
com.jerrellmardis.amphitheatre.util.VideoUtils.java
com.jerrellmardis.amphitheatre.widget.CardPresenter.java
com.jerrellmardis.amphitheatre.widget.DetailsDescriptionPresenter.java
com.jerrellmardis.amphitheatre.widget.GridItemPresenter.java
com.jerrellmardis.amphitheatre.widget.SeasonCardPresenter.java
com.jerrellmardis.amphitheatre.widget.SortedObjectAdapter.java
com.jerrellmardis.amphitheatre.widget.TvShowsCardPresenter.java