From 8c595da617578b21700082eb0293c5e4062c6382 Mon Sep 17 00:00:00 2001 From: Wei Zhuo Date: Thu, 17 Sep 2015 10:58:04 +1000 Subject: [PATCH] Show category grid --- base/src/main/AndroidManifest.xml | 14 ++- .../xwz/base/adapters/EpisodePresenter.java | 2 +- .../xwz/base/adapters/FilmPresenter.java | 20 +++- .../io/github/xwz/base/api/CategoryModel.java | 2 +- .../xwz/base/content/ContentManagerBase.java | 8 ++ .../xwz/base/content/IContentManager.java | 1 + .../xwz/base/fragments/CategoryFragment.java | 92 +++++++++++++++++++ .../xwz/base/fragments/MainFragment.java | 6 +- .../xwz/base/views/EpisodeCardView.java | 6 +- .../xwz/base/views/VideoPlayerView.java | 2 +- iview/src/main/AndroidManifest.xml | 1 + .../iview/activities/CategoryActivity.java | 13 +++ .../xwz/iview/fragments/CategoryFragment.java | 23 +++++ .../xwz/iview/fragments/MainFragment.java | 3 +- .../src/main/res/layout/category_activity.xml | 8 ++ sbs/src/main/AndroidManifest.xml | 1 + .../xwz/sbs/activities/CategoryActivity.java | 13 +++ .../io/github/xwz/sbs/api/SBSRelatedApi.java | 2 +- .../xwz/sbs/fragments/CategoryFragment.java | 23 +++++ .../xwz/sbs/fragments/MainFragment.java | 3 +- sbs/src/main/res/layout/category_activity.xml | 8 ++ 21 files changed, 231 insertions(+), 20 deletions(-) create mode 100644 base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java create mode 100644 iview/src/main/java/io/github/xwz/iview/activities/CategoryActivity.java create mode 100644 iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java create mode 100644 iview/src/main/res/layout/category_activity.xml create mode 100644 sbs/src/main/java/io/github/xwz/sbs/activities/CategoryActivity.java create mode 100644 sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java create mode 100644 sbs/src/main/res/layout/category_activity.xml diff --git a/base/src/main/AndroidManifest.xml b/base/src/main/AndroidManifest.xml index 031060f..a40beee 100644 --- a/base/src/main/AndroidManifest.xml +++ b/base/src/main/AndroidManifest.xml @@ -1,10 +1,8 @@ - - - - - + + diff --git a/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java b/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java index 785a1a3..c81832b 100644 --- a/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java +++ b/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java @@ -18,7 +18,7 @@ public class EpisodePresenter extends Presenter { @Override public ViewHolder onCreateViewHolder(ViewGroup parent) { Context context = parent.getContext(); - return new EpisodeCardView(context, getCardView(context), getCardSize(context)); + return new EpisodeCardView(context, getCardView(context), getCardSize(context), false); } protected Point getCardSize(Context context) { diff --git a/base/src/main/java/io/github/xwz/base/adapters/FilmPresenter.java b/base/src/main/java/io/github/xwz/base/adapters/FilmPresenter.java index 745e10d..ff262e5 100644 --- a/base/src/main/java/io/github/xwz/base/adapters/FilmPresenter.java +++ b/base/src/main/java/io/github/xwz/base/adapters/FilmPresenter.java @@ -8,8 +8,26 @@ import android.view.ViewGroup; import io.github.xwz.base.R; +import io.github.xwz.base.views.EpisodeCardView; public class FilmPresenter extends EpisodePresenter { + + private boolean showDetails = false; + + public FilmPresenter() { + + } + + public FilmPresenter(boolean details) { + showDetails = details; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent) { + Context context = parent.getContext(); + return new EpisodeCardView(context, getCardView(context), getCardSize(context), true); + } + @Override protected Point getCardSize(Context context) { return new Point(context.getResources().getDimensionPixelSize(R.dimen.poster_width), @@ -19,7 +37,7 @@ protected Point getCardSize(Context context) { @Override protected ImageCardView getCardView(Context context) { ImageCardView card = super.getCardView(context); - card.setCardType(BaseCardView.CARD_TYPE_MAIN_ONLY); + card.setCardType(showDetails ? BaseCardView.CARD_TYPE_INFO_UNDER_WITH_EXTRA : BaseCardView.CARD_TYPE_MAIN_ONLY); return card; } } diff --git a/base/src/main/java/io/github/xwz/base/api/CategoryModel.java b/base/src/main/java/io/github/xwz/base/api/CategoryModel.java index 4bd2ba6..f2f940b 100644 --- a/base/src/main/java/io/github/xwz/base/api/CategoryModel.java +++ b/base/src/main/java/io/github/xwz/base/api/CategoryModel.java @@ -64,7 +64,7 @@ public void setCategories(List cats) { @Override public String toString() { - return category; + return "Category: " + category; } @Override diff --git a/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java b/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java index 864bd54..14ae665 100644 --- a/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java +++ b/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java @@ -123,6 +123,14 @@ public List getAllShows() { return mCache.getAllShows(); } + public List getAllShowsByCategory(String cat) { + List all = getAllShowsByCategories().get(cat); + if (all == null) { + all = new ArrayList<>(); + } + return all; + } + public IEpisodeModel getEpisode(String href) { return mCache.getEpisode(href); } diff --git a/base/src/main/java/io/github/xwz/base/content/IContentManager.java b/base/src/main/java/io/github/xwz/base/content/IContentManager.java index 45c1184..45a270d 100644 --- a/base/src/main/java/io/github/xwz/base/content/IContentManager.java +++ b/base/src/main/java/io/github/xwz/base/content/IContentManager.java @@ -54,6 +54,7 @@ public interface IContentManager { String KEY_EXTRA_NAME = SearchManager.EXTRA_DATA_KEY; LinkedHashMap> getAllShowsByCategories(); + List getAllShowsByCategory(String cat); void fetchShowList(boolean force); IEpisodeModel getEpisode(String href); void fetchEpisode(IEpisodeModel episode); diff --git a/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java b/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java new file mode 100644 index 0000000..769ddf5 --- /dev/null +++ b/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java @@ -0,0 +1,92 @@ +package io.github.xwz.base.fragments; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v17.leanback.app.VerticalGridFragment; +import android.support.v17.leanback.widget.ArrayObjectAdapter; +import android.support.v17.leanback.widget.OnItemViewClickedListener; +import android.support.v17.leanback.widget.Presenter; +import android.support.v17.leanback.widget.Row; +import android.support.v17.leanback.widget.RowPresenter; +import android.support.v17.leanback.widget.VerticalGridPresenter; +import android.util.Log; +import android.view.View; + +import java.util.List; + +import io.github.xwz.base.adapters.CardSelector; +import io.github.xwz.base.adapters.EpisodePresenter; +import io.github.xwz.base.adapters.FilmPresenter; +import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.content.IContentManager; + +public abstract class CategoryFragment extends VerticalGridFragment { + private static final String TAG = "CategoryFragment"; + + private static final int FILM_COLUMNS = 10; + private static final int SHOW_COLUMNS = 5; + private String category; + private boolean isFilm = false; + + @Override + public void onCreate(Bundle savedInstanceState) { + Log.d(TAG, "onCreate"); + super.onCreate(savedInstanceState); + category = getActivity().getIntent().getStringExtra(IContentManager.CONTENT_ID); + isFilm = category.contains("Film/") || category.equals("Film"); + setupFragment(); + setupListeners(); + } + + private void setupFragment() { + VerticalGridPresenter gridPresenter = new VerticalGridPresenter(); + gridPresenter.setNumberOfColumns(isFilm ? FILM_COLUMNS : SHOW_COLUMNS); + setGridPresenter(gridPresenter); + setTitle(category); + + List all = getContentManger().getAllShowsByCategory(category); + ArrayObjectAdapter adapter; + if (isFilm) { + adapter = new ArrayObjectAdapter(new FilmPresenter(true)); + } else { + adapter = new ArrayObjectAdapter(new EpisodePresenter()); + } + adapter.addAll(0, all); + + setAdapter(adapter); + } + + private void setupListeners() { + setOnSearchClickedListener(getSearchClickedListener()); + setOnItemViewClickedListener(getItemClickedListener()); + } + + private View.OnClickListener getSearchClickedListener() { + return new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(getActivity(), getSearchActivityClass()); + startActivity(intent); + } + }; + } + + private OnItemViewClickedListener getItemClickedListener() { + return new OnItemViewClickedListener() { + @Override + public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { + if (item instanceof IEpisodeModel) { + Intent intent = new Intent(getActivity(), getDetailsActivityClass()); + intent.putExtra(IContentManager.CONTENT_ID, (IEpisodeModel) item); + startActivity(intent); + } + } + }; + } + + protected abstract IContentManager getContentManger(); + + protected abstract Class getSearchActivityClass(); + + protected abstract Class getDetailsActivityClass(); +} diff --git a/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java b/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java index 80474a9..71980a3 100644 --- a/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java +++ b/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java @@ -41,7 +41,7 @@ public abstract class MainFragment extends BrowseFragment { private static final String TAG = "MainFragment"; private ProgressBar progress; private TextView progressText; - private static final int SHOW_CATEGORY_COUNT = -1; + private static final int SHOW_CATEGORY_COUNT = 20; private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override @@ -129,7 +129,7 @@ private OnItemViewClickedListener getItemClickedListener() { public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { if (item instanceof CategoryModel) { Intent intent = new Intent(getActivity(), getCategoryActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, (IEpisodeModel) item); + intent.putExtra(IContentManager.CONTENT_ID, ((CategoryModel) item).getTitle()); startActivity(intent); } else if (item instanceof IEpisodeModel) { Intent intent = new Intent(getActivity(), getDetailsActivityClass()); @@ -153,7 +153,7 @@ private void updateRows(ArrayObjectAdapter adapter) { List categories = new ArrayList<>(all.keySet()); for (int i = 0; i < newRows; i++) { String category = categories.get(i); - List episodes = all.get(category); + List episodes = new ArrayList<>(all.get(category)); if (SHOW_CATEGORY_COUNT > 0 && episodes.size() > SHOW_CATEGORY_COUNT) { CategoryModel collection = new CategoryModel(category); collection.setEpisodeCount(episodes.size()); diff --git a/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java b/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java index 36f8424..12d64e5 100644 --- a/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java +++ b/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java @@ -17,12 +17,14 @@ public class EpisodeCardView extends Presenter.ViewHolder { private final ImageCardView card; private final Context mContext; private final Point size; + private boolean canShowCover; - public EpisodeCardView(Context context, ImageCardView view, Point s) { + public EpisodeCardView(Context context, ImageCardView view, Point s, boolean showCover) { super(view); mContext = context; card = view; size = s; + canShowCover = showCover; } public void setEpisode(IEpisodeModel ep) { @@ -45,7 +47,7 @@ public void setEpisode(IEpisodeModel ep) { card.setBadgeImage(null); } String image = ep.getThumbnail(); - if (ep.hasCover()) { + if (canShowCover && ep.hasCover()) { image = ep.getCover(); } Picasso.with(mContext) diff --git a/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java b/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java index 583082e..dd094fd 100644 --- a/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java +++ b/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java @@ -94,7 +94,7 @@ public VideoPlayerView(Context context, MediaController controller, View root) { card.setExtraVisibility(View.VISIBLE); Point size = new Point(context.getResources().getDimensionPixelSize(R.dimen.card_width), context.getResources().getDimensionPixelSize(R.dimen.card_height)); - nextEpisode = new EpisodeCardView(context, card, size); + nextEpisode = new EpisodeCardView(context, card, size, false); nextEpisode.getImageCardView().setCardType(BaseCardView.CARD_TYPE_INFO_OVER); debugView.setVisibility(View.GONE); diff --git a/iview/src/main/AndroidManifest.xml b/iview/src/main/AndroidManifest.xml index 7a254fd..e682034 100644 --- a/iview/src/main/AndroidManifest.xml +++ b/iview/src/main/AndroidManifest.xml @@ -30,6 +30,7 @@ + diff --git a/iview/src/main/java/io/github/xwz/iview/activities/CategoryActivity.java b/iview/src/main/java/io/github/xwz/iview/activities/CategoryActivity.java new file mode 100644 index 0000000..afe293b --- /dev/null +++ b/iview/src/main/java/io/github/xwz/iview/activities/CategoryActivity.java @@ -0,0 +1,13 @@ +package io.github.xwz.iview.activities; + +import android.os.Bundle; + +import io.github.xwz.base.activities.BaseActivity; +import io.github.xwz.iview.R; + +public class CategoryActivity extends BaseActivity { + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.category_activity); + } +} \ No newline at end of file diff --git a/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java b/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java new file mode 100644 index 0000000..c600c0e --- /dev/null +++ b/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java @@ -0,0 +1,23 @@ +package io.github.xwz.iview.fragments; + +import io.github.xwz.base.content.IContentManager; +import io.github.xwz.iview.activities.DetailsActivity; +import io.github.xwz.iview.activities.SearchActivity; +import io.github.xwz.iview.content.ContentManager; + +public class CategoryFragment extends io.github.xwz.base.fragments.CategoryFragment { + @Override + protected IContentManager getContentManger() { + return ContentManager.getInstance(); + } + + @Override + protected Class getSearchActivityClass() { + return SearchActivity.class; + } + + @Override + protected Class getDetailsActivityClass() { + return DetailsActivity.class; + } +} diff --git a/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java b/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java index d2797c0..274f94c 100644 --- a/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java +++ b/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java @@ -4,6 +4,7 @@ import android.view.View; import io.github.xwz.iview.R; +import io.github.xwz.iview.activities.CategoryActivity; import io.github.xwz.iview.activities.DetailsActivity; import io.github.xwz.iview.activities.SearchActivity; import io.github.xwz.iview.content.ContentManager; @@ -39,6 +40,6 @@ protected Class getDetailsActivityClass() { @Override protected Class getCategoryActivityClass() { - return null; + return CategoryActivity.class; } } diff --git a/iview/src/main/res/layout/category_activity.xml b/iview/src/main/res/layout/category_activity.xml new file mode 100644 index 0000000..f0f54d3 --- /dev/null +++ b/iview/src/main/res/layout/category_activity.xml @@ -0,0 +1,8 @@ + diff --git a/sbs/src/main/AndroidManifest.xml b/sbs/src/main/AndroidManifest.xml index 1d52caf..3b6a4e8 100644 --- a/sbs/src/main/AndroidManifest.xml +++ b/sbs/src/main/AndroidManifest.xml @@ -27,6 +27,7 @@ + diff --git a/sbs/src/main/java/io/github/xwz/sbs/activities/CategoryActivity.java b/sbs/src/main/java/io/github/xwz/sbs/activities/CategoryActivity.java new file mode 100644 index 0000000..303c30f --- /dev/null +++ b/sbs/src/main/java/io/github/xwz/sbs/activities/CategoryActivity.java @@ -0,0 +1,13 @@ +package io.github.xwz.sbs.activities; + +import android.os.Bundle; + +import io.github.xwz.base.activities.BaseActivity; +import io.github.xwz.sbs.R; + +public class CategoryActivity extends BaseActivity { + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.category_activity); + } +} diff --git a/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java b/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java index c03de12..20416fc 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java +++ b/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java @@ -9,8 +9,8 @@ import java.util.Map; import io.github.xwz.base.ImmutableMap; -import io.github.xwz.base.content.IContentManager; import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.content.IContentManager; import io.github.xwz.sbs.content.ContentManager; public class SBSRelatedApi extends SBSApiBase { diff --git a/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java b/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java new file mode 100644 index 0000000..6bc291d --- /dev/null +++ b/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java @@ -0,0 +1,23 @@ +package io.github.xwz.sbs.fragments; + +import io.github.xwz.base.content.IContentManager; +import io.github.xwz.sbs.activities.DetailsActivity; +import io.github.xwz.sbs.activities.SearchActivity; +import io.github.xwz.sbs.content.ContentManager; + +public class CategoryFragment extends io.github.xwz.base.fragments.CategoryFragment { + @Override + protected IContentManager getContentManger() { + return ContentManager.getInstance(); + } + + @Override + protected Class getSearchActivityClass() { + return SearchActivity.class; + } + + @Override + protected Class getDetailsActivityClass() { + return DetailsActivity.class; + } +} diff --git a/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java b/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java index f43ec50..b0512ee 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java +++ b/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java @@ -5,6 +5,7 @@ import io.github.xwz.base.content.IContentManager; import io.github.xwz.sbs.R; +import io.github.xwz.sbs.activities.CategoryActivity; import io.github.xwz.sbs.activities.DetailsActivity; import io.github.xwz.sbs.activities.SearchActivity; import io.github.xwz.sbs.content.ContentManager; @@ -40,6 +41,6 @@ protected Class getDetailsActivityClass() { @Override protected Class getCategoryActivityClass() { - return null; + return CategoryActivity.class; } } diff --git a/sbs/src/main/res/layout/category_activity.xml b/sbs/src/main/res/layout/category_activity.xml new file mode 100644 index 0000000..167a342 --- /dev/null +++ b/sbs/src/main/res/layout/category_activity.xml @@ -0,0 +1,8 @@ +