Created
October 29, 2017 07:58
-
-
Save vipulasri/30062a7c8664a7094fe9ba593410b674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.application.freadom.activity.home; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.LinearSnapHelper; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.SnapHelper; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.RadioButton; | |
import com.application.freadom.R; | |
import com.application.freadom.model.Category; | |
import com.application.freadom.model.CategoryList; | |
import com.application.freadom.model.Child; | |
import com.application.freadom.model.ChildLeaderboardModel; | |
import com.application.freadom.model.HomeModel; | |
import com.application.freadom.model.NewsFread; | |
import com.application.freadom.model.NewsfreadList; | |
import com.application.freadom.utils.ImageLoadingUtils; | |
import com.application.freadom.utils.Utils; | |
import com.application.freadom.widgets.AspectRatioImageView; | |
import com.application.freadom.widgets.FiraSansTextView; | |
import com.application.freadom.widgets.RadioGroupPlus; | |
import com.application.freadom.widgets.SpaceItemDecoration; | |
import com.bumptech.glide.Glide; | |
import com.bumptech.glide.Priority; | |
import com.bumptech.glide.request.RequestOptions; | |
import java.util.ArrayList; | |
import java.util.List; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
public class HomeFragmentAdapter extends RecyclerView.Adapter implements CategoryAdapter.Callbacks, NewsFreadHomeAdapter.Callbacks{ | |
public interface Callbacks { | |
public void onCategoryClick(Category category); | |
public void onNewsFreadClick(NewsFread newsFread); | |
public void onLeaderboardClick(); | |
} | |
private static final int VIEW_CATEGORY = 1; | |
private static final int VIEW_NEWSFREAD = 2; | |
private static final int VIEW_LEADERBOARD = 3; | |
private static final int VIEW_FOOTER = 4; | |
private Callbacks mCallbacks; | |
private Context context; | |
private List mFeedList; | |
private HomeModel mHomeModel; | |
private LayoutInflater mInflater; | |
private RecyclerView.RecycledViewPool mRecycledViewPool; | |
public HomeFragmentAdapter(HomeModel homeModel) { | |
this.mHomeModel = homeModel; | |
mRecycledViewPool = new RecyclerView.RecycledViewPool(); | |
mFeedList = processModel(mHomeModel); | |
Log.e("size", "->"+getItemCount()); | |
} | |
private List processModel(@NonNull final HomeModel model) { | |
final ArrayList exploreCards = new ArrayList(); | |
// add category view | |
if(model.getCategoryList()!=null && !model.getCategoryList().getCategories().isEmpty()) { | |
Log.e("category", "->"); | |
exploreCards.add(model.getCategoryList()); | |
} | |
// add newsfread view | |
if(model.getNewsfreadList()!=null && !model.getNewsfreadList().getNewsFreads().isEmpty()) { | |
Log.e("newsfread", "->"); | |
exploreCards.add(model.getNewsfreadList()); | |
} | |
//add child leaderboard view | |
if(model.getChildLeaderboardModel()!=null) { | |
exploreCards.add(model.getChildLeaderboardModel()); | |
} | |
// for footer | |
exploreCards.add(null); | |
return exploreCards; | |
} | |
@Override | |
public int getItemViewType(final int position) { | |
final Object item = mFeedList.get(position); | |
if(item instanceof CategoryList) { | |
return VIEW_CATEGORY; | |
} else if (item instanceof NewsfreadList) { | |
return VIEW_NEWSFREAD; | |
} else if (item instanceof ChildLeaderboardModel) { | |
return VIEW_LEADERBOARD; | |
} else if(item == null){ | |
return VIEW_FOOTER; | |
} | |
throw new IllegalArgumentException("Unknown view type."); | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) { | |
context = parent.getContext(); | |
mInflater = LayoutInflater.from(context); | |
switch (viewType) { | |
case VIEW_CATEGORY: | |
return createCategoryListViewHolder(parent); | |
case VIEW_NEWSFREAD: | |
return createNewsfreadListViewHolder(parent); | |
case VIEW_LEADERBOARD: | |
return createChildLeaderboardViewHolder(parent); | |
case VIEW_FOOTER: | |
return createFooterViewHolder(parent); | |
default: | |
throw new IllegalArgumentException("Unknown view type."); | |
} | |
} | |
private @NonNull | |
CategoryListViewHolder createCategoryListViewHolder(final ViewGroup parent) { | |
CategoryListViewHolder holder = new CategoryListViewHolder(mInflater.inflate(R.layout.item_home_categories_view, parent, false)); | |
int columnCount = context.getResources().getInteger(R.integer.item_column); | |
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, columnCount); | |
int spacing = Utils.dpToPx(0.5f, context); | |
holder.categoriesRecyclerView.addItemDecoration(new SpaceItemDecoration(spacing)); | |
holder.categoriesRecyclerView.setLayoutManager(gridLayoutManager); | |
holder.categoriesRecyclerView.setNestedScrollingEnabled(false); | |
holder.categoriesRecyclerView.setRecycledViewPool(mRecycledViewPool); | |
return holder; | |
} | |
private @NonNull | |
NewsfreadListViewHolder createNewsfreadListViewHolder(final ViewGroup parent) { | |
NewsfreadListViewHolder holder = new NewsfreadListViewHolder(mInflater.inflate(R.layout.item_home_newsfread_view, parent, false)); | |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); | |
int spacing = Utils.dpToPx(5, context); | |
holder.newsfreadRecyclerView.addItemDecoration(new SpaceItemDecoration(spacing)); | |
//snapping behaviour | |
SnapHelper snapHelper = new LinearSnapHelper(); | |
snapHelper.attachToRecyclerView(holder.newsfreadRecyclerView); | |
holder.newsfreadRecyclerView.setLayoutManager(linearLayoutManager); | |
holder.newsfreadRecyclerView.setNestedScrollingEnabled(false); | |
holder.newsfreadRecyclerView.setRecycledViewPool(mRecycledViewPool); | |
return holder; | |
} | |
private @NonNull | |
ChildLeaderboardViewHolder createChildLeaderboardViewHolder(final ViewGroup parent) { | |
return new ChildLeaderboardViewHolder(mInflater.inflate(R.layout.item_child_leaderboard, parent, false)); | |
} | |
private @NonNull | |
FooterViewHolder createFooterViewHolder(final ViewGroup parent) { | |
return new FooterViewHolder(mInflater.inflate(R.layout.item_home_footer, parent, false)); | |
} | |
@Override | |
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { | |
switch (getItemViewType(position)) { | |
case VIEW_CATEGORY: | |
bindCategoryList((CategoryListViewHolder) holder, (CategoryList) mFeedList.get(position)); break; | |
case VIEW_NEWSFREAD: | |
bindNewsfreadList((NewsfreadListViewHolder) holder, (NewsfreadList) mFeedList.get(position)); break; | |
case VIEW_LEADERBOARD: | |
bindChildLeaderboard((ChildLeaderboardViewHolder) holder, (ChildLeaderboardModel) mFeedList.get(position)); break; | |
case VIEW_FOOTER: | |
bindFooter((FooterViewHolder) holder, mFeedList.get(position)); break; | |
default: | |
throw new IllegalArgumentException("Unknown view type."); | |
} | |
} | |
private void bindCategoryList(final CategoryListViewHolder holder, final CategoryList categoryList) { | |
CategoryAdapter categoryAdapter = new CategoryAdapter(categoryList.getCategories()); | |
categoryAdapter.setCallbacks(this); | |
holder.categoriesRecyclerView.setAdapter(categoryAdapter); | |
} | |
private void bindNewsfreadList(final NewsfreadListViewHolder holder, final NewsfreadList newsfreadList) { | |
NewsFreadHomeAdapter newsFreadAdapter = new NewsFreadHomeAdapter(newsfreadList.getNewsFreads()); | |
newsFreadAdapter.setCallbacks(this); | |
holder.newsfreadRecyclerView.setAdapter(newsFreadAdapter); | |
} | |
private void bindChildLeaderboard(final ChildLeaderboardViewHolder holder, final ChildLeaderboardModel childLeaderboardModel) { | |
holder.mLeaderboardTitle.setText(R.string.leaderboard); | |
if(childLeaderboardModel.getTopChild()!=null) { | |
holder.topChildView.setVisibility(View.VISIBLE); | |
ImageLoadingUtils.loadChildImage(holder.topChildImage, childLeaderboardModel.getTopChild()); | |
holder.topChildName.setText(childLeaderboardModel.getTopChild().getFullName()); | |
holder.topChildFreados.setText(String.valueOf(childLeaderboardModel.getTopChild().getFreadoPoints())); | |
} else { | |
holder.topChildView.setVisibility(View.GONE); | |
} | |
if(childLeaderboardModel.getChild()!=null) { | |
holder.childView.setVisibility(View.VISIBLE); | |
ImageLoadingUtils.loadChildImage(holder.childImage, childLeaderboardModel.getChild()); | |
holder.childImage.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.background_rounded_dotted)); | |
holder.childName.setText(childLeaderboardModel.getChild().getFullName()); | |
holder.childFreados.setText(String.valueOf(childLeaderboardModel.getChild().getFreadoPoints())); | |
} else { | |
holder.childView.setVisibility(View.GONE); | |
} | |
if(childLeaderboardModel.getNextChild()!=null) { | |
holder.nextChildView.setVisibility(View.VISIBLE); | |
ImageLoadingUtils.loadChildImage(holder.nextChildImage, childLeaderboardModel.getNextChild()); | |
holder.nextChildName.setText(childLeaderboardModel.getNextChild().getFullName()); | |
holder.nextChildFreados.setText(String.valueOf(childLeaderboardModel.getNextChild().getFreadoPoints())); | |
} else { | |
holder.nextChildView.setVisibility(View.GONE); | |
} | |
holder.itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(mCallbacks!=null) | |
mCallbacks.onLeaderboardClick(); | |
} | |
}); | |
} | |
private void bindFooter(final FooterViewHolder holder, final Object objects) { | |
} | |
@Override | |
public void onCategoryClick(Category category) { | |
if(mCallbacks!=null) | |
mCallbacks.onCategoryClick(category); | |
} | |
@Override | |
public void onNewsFreadClick(NewsFread newsFread) { | |
if(mCallbacks!=null) | |
mCallbacks.onNewsFreadClick(newsFread); | |
} | |
@Override | |
public int getItemCount() { | |
return (mFeedList!=null? mFeedList.size():0); | |
} | |
public void setCallbacks(Callbacks callbacks) { | |
this.mCallbacks = callbacks; | |
} | |
public class CategoryListViewHolder extends RecyclerView.ViewHolder { | |
@BindView(R.id.categoriesRecyclerView) | |
RecyclerView categoriesRecyclerView; | |
public CategoryListViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
} | |
} | |
public class NewsfreadListViewHolder extends RecyclerView.ViewHolder { | |
@BindView(R.id.newsfreadRecyclerView) | |
RecyclerView newsfreadRecyclerView; | |
public NewsfreadListViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
} | |
} | |
public class ChildLeaderboardViewHolder extends RecyclerView.ViewHolder { | |
@BindView(R.id.text_home_leaderboard_title) | |
FiraSansTextView mLeaderboardTitle; | |
@BindView(R.id.view_top_child) | |
View topChildView; | |
@BindView(R.id.view_child) | |
View childView; | |
@BindView(R.id.view_next_child) | |
View nextChildView; | |
@BindView(R.id.image_top_child) | |
AspectRatioImageView topChildImage; | |
@BindView(R.id.image_child) | |
AspectRatioImageView childImage; | |
@BindView(R.id.image_next_child) | |
AspectRatioImageView nextChildImage; | |
@BindView(R.id.text_top_child_name) | |
FiraSansTextView topChildName; | |
@BindView(R.id.text_child_name) | |
FiraSansTextView childName; | |
@BindView(R.id.text_next_child_name) | |
FiraSansTextView nextChildName; | |
@BindView(R.id.text_top_child_freados) | |
FiraSansTextView topChildFreados; | |
@BindView(R.id.text_child_freados) | |
FiraSansTextView childFreados; | |
@BindView(R.id.text_next_child_freados) | |
FiraSansTextView nextChildFreados; | |
@BindView(R.id.radio_group_child) | |
RadioGroupPlus childRadioGroup; | |
@BindView(R.id.radio_button_top_child) | |
RadioButton topChildRadioButton; | |
@BindView(R.id.radio_button_child) | |
RadioButton childRadioButton; | |
@BindView(R.id.radio_button_next_child) | |
RadioButton nextChildRadioButton; | |
public ChildLeaderboardViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
} | |
} | |
public class FooterViewHolder extends RecyclerView.ViewHolder { | |
public FooterViewHolder(View itemView) { | |
super(itemView); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment