package com.pan.materialdrawer.model;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.mikepenz.iconics.typeface.IIcon;
import com.pan.materialdrawer.holder.ColorHolder;
import com.pan.materialdrawer.holder.ImageHolder;
import com.pan.materialdrawer.holder.StringHolder;
import com.pan.materialdrawer.model.interfaces.IProfile;
import com.pan.materialdrawer.model.interfaces.Tagable;
import com.pan.materialdrawer.model.interfaces.Typefaceable;
import com.pan.materialdrawer.model.utils.ViewHolderFactory;
import com.pan.materialdrawer.util.DrawerImageLoader;
import com.pan.materialdrawer.util.DrawerUIUtils;
import com.mikepenz.materialize.util.UIUtils;
/**
* Created by mikepenz on 03.02.15.
*/
public class ProfileDrawerItem extends AbstractDrawerItem<ProfileDrawerItem> implements IProfile<ProfileDrawerItem>, Tagable<ProfileDrawerItem>, Typefaceable<ProfileDrawerItem> {
protected boolean nameShown = false;
protected ImageHolder icon;
protected StringHolder name;
protected StringHolder email;
protected ColorHolder selectedColor;
protected ColorHolder textColor;
protected Typeface typeface = null;
@Override
public ProfileDrawerItem withIcon(Drawable icon) {
this.icon = new ImageHolder(icon);
return this;
}
@Override
public ProfileDrawerItem withIcon(@DrawableRes int iconRes) {
this.icon = new ImageHolder(iconRes);
return this;
}
@Override
public ProfileDrawerItem withIcon(Bitmap iconBitmap) {
this.icon = new ImageHolder(iconBitmap);
return this;
}
@Override
public ProfileDrawerItem withIcon(IIcon icon) {
this.icon = new ImageHolder(icon);
return this;
}
@Override
public ProfileDrawerItem withIcon(String url) {
this.icon = new ImageHolder(url);
return this;
}
@Override
public ProfileDrawerItem withIcon(Uri uri) {
this.icon = new ImageHolder(uri);
return this;
}
public ProfileDrawerItem withName(String name) {
this.name = new StringHolder(name);
return this;
}
public ProfileDrawerItem withEmail(String email) {
this.email = new StringHolder(email);
return this;
}
public ProfileDrawerItem withNameShown(boolean nameShown) {
this.nameShown = nameShown;
return this;
}
public ProfileDrawerItem withSelectedColor(@ColorInt int selectedColor) {
this.selectedColor = ColorHolder.fromColor(selectedColor);
return this;
}
public ProfileDrawerItem withSelectedColorRes(@ColorRes int selectedColorRes) {
this.selectedColor = ColorHolder.fromColorRes(selectedColorRes);
return this;
}
public ProfileDrawerItem withTextColor(@ColorInt int textColor) {
this.textColor = ColorHolder.fromColor(textColor);
return this;
}
public ProfileDrawerItem withTextColorRes(@ColorRes int textColorRes) {
this.textColor = ColorHolder.fromColorRes(textColorRes);
return this;
}
public ProfileDrawerItem withTypeface(Typeface typeface) {
this.typeface = typeface;
return this;
}
public boolean isNameShown() {
return nameShown;
}
public void setNameShown(boolean nameShown) {
this.nameShown = nameShown;
}
public ColorHolder getSelectedColor() {
return selectedColor;
}
public ColorHolder getTextColor() {
return textColor;
}
@Override
public Typeface getTypeface() {
return typeface;
}
public ImageHolder getIcon() {
return icon;
}
@Override
public StringHolder getName() {
return name;
}
public StringHolder getEmail() {
return email;
}
@Override
public String getType() {
return "PROFILE_ITEM";
}
@Override
@LayoutRes
public int getLayoutRes() {
return com.pan.materialdrawer.R.layout.material_drawer_item_profile;
}
@Override
public void bindView(RecyclerView.ViewHolder holder) {
Context ctx = holder.itemView.getContext();
//get our viewHolder
ViewHolder viewHolder = (ViewHolder) holder;
//set the identifier from the drawerItem here. It can be used to run tests
viewHolder.itemView.setId(getIdentifier());
//set the item selected if it is
viewHolder.itemView.setSelected(isSelected());
//get the correct color for the background
int selectedColor = ColorHolder.color(getSelectedColor(), ctx, com.pan.materialdrawer.R.attr.material_drawer_selected, com.pan.materialdrawer.R.color.material_drawer_selected);
//get the correct color for the text
int color = ColorHolder.color(getTextColor(), ctx, com.pan.materialdrawer.R.attr.material_drawer_primary_text, com.pan.materialdrawer.R.color.material_drawer_primary_text);
UIUtils.setBackground(viewHolder.view, DrawerUIUtils.getSelectableBackground(ctx, selectedColor));
if (nameShown) {
viewHolder.name.setVisibility(View.VISIBLE);
StringHolder.applyTo(this.getName(), viewHolder.name);
} else {
viewHolder.name.setVisibility(View.GONE);
}
//the MaterialDrawer follows the Google Apps. those only show the e-mail
//within the profile switcher. The problem this causes some confusion for
//some developers. And if you only set the name, the item would be empty
//so here's a small fallback which will prevent this issue of empty items ;)
if (!nameShown && this.getEmail() == null && this.getName() != null) {
StringHolder.applyTo(this.getName(), viewHolder.email);
} else {
StringHolder.applyTo(this.getEmail(), viewHolder.email);
}
if (getTypeface() != null) {
viewHolder.name.setTypeface(getTypeface());
viewHolder.email.setTypeface(getTypeface());
}
if (nameShown) {
viewHolder.name.setTextColor(color);
}
viewHolder.email.setTextColor(color);
//cancel previous started image loading processes
DrawerImageLoader.getInstance().cancelImage(viewHolder.profileIcon);
//set the icon
ImageHolder.applyToOrSetInvisible(getIcon(), viewHolder.profileIcon, DrawerImageLoader.Tags.PROFILE_DRAWER_ITEM.name());
//for android API 17 --> Padding not applied via xml
DrawerUIUtils.setDrawerVerticalPadding(viewHolder.view);
//call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
onPostBindView(this, holder.itemView);
}
@Override
public ViewHolderFactory getFactory() {
return new ItemFactory();
}
public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
public ViewHolder factory(View v) {
return new ViewHolder(v);
}
}
private static class ViewHolder extends RecyclerView.ViewHolder {
private View view;
private ImageView profileIcon;
private TextView name;
private TextView email;
private ViewHolder(View view) {
super(view);
this.view = view;
this.profileIcon = (ImageView) view.findViewById(com.pan.materialdrawer.R.id.material_drawer_profileIcon);
this.name = (TextView) view.findViewById(com.pan.materialdrawer.R.id.material_drawer_name);
this.email = (TextView) view.findViewById(com.pan.materialdrawer.R.id.material_drawer_email);
}
}
}