Commit 4eeeae4b authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update RoomToolbar.java

parent 3eaa5096
...@@ -9,6 +9,7 @@ import android.support.annotation.Nullable; ...@@ -9,6 +9,7 @@ import android.support.annotation.Nullable;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.graphics.drawable.VectorDrawableCompat; import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.TextViewCompat; import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.AppCompatImageView; import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
...@@ -23,9 +24,6 @@ import java.lang.reflect.Field; ...@@ -23,9 +24,6 @@ import java.lang.reflect.Field;
public class RoomToolbar extends Toolbar { public class RoomToolbar extends Toolbar {
private TextView titleTextView;
private ImageView badgeImageView;
public RoomToolbar(Context context) { public RoomToolbar(Context context) {
super(context); super(context);
initialize(context, null); initialize(context, null);
...@@ -44,65 +42,67 @@ public class RoomToolbar extends Toolbar { ...@@ -44,65 +42,67 @@ public class RoomToolbar extends Toolbar {
private void initialize(Context context, @Nullable AttributeSet attrs) { private void initialize(Context context, @Nullable AttributeSet attrs) {
View.inflate(context, R.layout.room_toolbar, this); View.inflate(context, R.layout.room_toolbar, this);
titleTextView = (TextView) findViewById(R.id.toolbar_title); titleTextView = findViewById(R.id.toolbar_title);
roomIconImageView = findViewById(R.id.roomIconImageView);
if (titleTextView == null) { userStatusDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_user_status_black_24dp, null);
return; privateChannelDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_lock_black_24dp, null);
} publicChannelDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_hashtag_black_24dp, null);
TypedArray typedArrayBase = context.getTheme().obtainStyledAttributes(new int[]{
R.attr.titleTextAppearance
});
try {
TextViewCompat.setTextAppearance(titleTextView,
typedArrayBase.getResourceId(0,
android.support.v7.appcompat.R.style.TextAppearance_Widget_AppCompat_Toolbar_Title));
} finally {
typedArrayBase.recycle();
}
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.RoomToolbar,
0, 0);
try {
titleTextView.setText(typedArray.getText(R.styleable.RoomToolbar_titleText));
titleTextView.setCompoundDrawablePadding(
typedArray.getLayoutDimension(R.styleable.RoomToolbar_titleDrawablePadding, 0));
} finally {
typedArray.recycle();
}
} }
@Override @Override
public void setTitle(@StringRes int resId) { public void setTitle(@StringRes int resId) {
if (titleTextView != null) { titleTextView.setText(getContext().getText(resId));
titleTextView.setText(resId);
return;
}
super.setTitle(resId);
} }
@Override @Override
public void setTitle(CharSequence title) { public void setTitle(CharSequence title) {
if (titleTextView != null) { titleTextView.setText(title);
titleTextView.setText(title);
return;
}
super.setTitle(title);
} }
public void setRoomIcon(@DrawableRes int drawableResId) { public void showPrivateChannelIcon() {
if (titleTextView == null) { roomIconImageView.setImageDrawable(privateChannelDrawable);
return; }
public void showPublicChannelIcon() {
roomIconImageView.setImageDrawable(publicChannelDrawable);
}
public void showUserStatusIcon(int status) {
wrapDrawable(userStatusDrawable);
switch (status) {
case STATUS_ONLINE:
tintDrawable(userStatusDrawable, R.color.color_user_status_online);
break;
case STATUS_BUSY:
tintDrawable(userStatusDrawable, R.color.color_user_status_busy);
break;
case STATUS_AWAY:
tintDrawable(userStatusDrawable, R.color.color_user_status_away);
break;
case STATUS_OFFLINE:
tintDrawable(userStatusDrawable, R.color.color_user_status_offline);
break;
default:
tintDrawable(userStatusDrawable, R.color.color_user_status_offline);
break;
} }
Drawable drawable = drawableResId > 0 roomIconImageView.setImageDrawable(userStatusDrawable);
? VectorDrawableCompat.create(getResources(), drawableResId, null) }
: null;
private void wrapDrawable(Drawable drawable) {
DrawableCompat.wrap(drawable);
}
titleTextView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null); /**
* REMARK: You MUST always wrap the drawable before tint it.
* @param drawable The drawable to tint.
* @param color The color to tint the drawable.
* @see #wrapDrawable(Drawable)
*/
private void tintDrawable(Drawable drawable, int color) {
DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), color));
} }
public void setUnreadBudge(int numUnreadChannels, int numMentionsSum) { public void setUnreadBudge(int numUnreadChannels, int numMentionsSum) {
...@@ -141,8 +141,6 @@ public class RoomToolbar extends Toolbar { ...@@ -141,8 +141,6 @@ public class RoomToolbar extends Toolbar {
.buildRound(icon, ContextCompat.getColor(getContext(), R.color.badge_color)); .buildRound(icon, ContextCompat.getColor(getContext(), R.color.badge_color));
} }
@Override @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom); super.onLayout(changed, left, top, right, bottom);
...@@ -170,4 +168,12 @@ public class RoomToolbar extends Toolbar { ...@@ -170,4 +168,12 @@ public class RoomToolbar extends Toolbar {
} }
} }
} private TextView titleTextView;
private ImageView roomIconImageView;
private ImageView badgeImageView;
Drawable privateChannelDrawable, publicChannelDrawable, userStatusDrawable;
public static final int STATUS_ONLINE = 1;
public static final int STATUS_BUSY = 2;
public static final int STATUS_AWAY = 3;
public static final int STATUS_OFFLINE = 4;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment