Commit 96f503ea authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Add placeholder image.

parent f35ab141
...@@ -5,7 +5,6 @@ import android.widget.ImageView ...@@ -5,7 +5,6 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.helper.DateTime import chat.rocket.android.helper.DateTime
import chat.rocket.android.helper.OkHttpHelper
import chat.rocket.android.helper.RocketChatUserAvatar import chat.rocket.android.helper.RocketChatUserAvatar
import chat.rocket.android.widget.AbsoluteUrl import chat.rocket.android.widget.AbsoluteUrl
import chat.rocket.android.widget.RocketChatAvatar import chat.rocket.android.widget.RocketChatAvatar
...@@ -20,30 +19,20 @@ class MessageRenderer(val message: Message, val autoLoadImage: Boolean) { ...@@ -20,30 +19,20 @@ class MessageRenderer(val message: Message, val autoLoadImage: Boolean) {
/** /**
* Show user's avatar image in RocketChatAvatar widget. * Show user's avatar image in RocketChatAvatar widget.
*/ */
fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String, userNotFoundAvatarImageView: ImageView, userAvatarSvgImage: ImageView) { fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String, userNotFoundAvatarImageView: ImageView) {
if (message.avatar != null) { val username: String? = message.user?.username
// Load user's avatar image from Oauth provider URI. if (username != null) {
rocketChatAvatarWidget.loadImage(message.avatar) userNotFoundAvatarImageView.visibility = View.GONE
} else { val placeholderDrawable = RocketChatUserAvatar.getTextDrawable(username, rocketChatAvatarWidget.context)
val username: String? = message.user?.username if (message.avatar != null) {
if (username != null) { // Load user's avatar image from Oauth provider URI.
userNotFoundAvatarImageView.visibility = View.GONE rocketChatAvatarWidget.loadImage(message.avatar, placeholderDrawable)
val userAvatarUri = RocketChatUserAvatar.getUri(hostname, username)
val userAvatarImageContentType = OkHttpHelper.getContentType(userAvatarUri)
if (userAvatarImageContentType == "image/svg+xml") {
rocketChatAvatarWidget.visibility = View.GONE
userAvatarSvgImage.setImageDrawable(RocketChatUserAvatar.getTextDrawable(username, userAvatarSvgImage.context))
userAvatarSvgImage.visibility = View.VISIBLE
} else {
userAvatarSvgImage.visibility = View.GONE
rocketChatAvatarWidget.loadImage(userAvatarUri)
rocketChatAvatarWidget.visibility = View.VISIBLE
}
} else { } else {
userAvatarSvgImage.visibility = View.GONE rocketChatAvatarWidget.loadImage(RocketChatUserAvatar.getUri(hostname, username), placeholderDrawable)
rocketChatAvatarWidget.visibility = View.GONE
userNotFoundAvatarImageView.visibility = View.VISIBLE
} }
} else {
rocketChatAvatarWidget.visibility = View.GONE
userNotFoundAvatarImageView.visibility = View.VISIBLE
} }
} }
......
...@@ -16,7 +16,7 @@ class UserRenderer(val user: User) { ...@@ -16,7 +16,7 @@ class UserRenderer(val user: User) {
fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String) { fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String) {
val username: String? = user.username val username: String? = user.username
if (username != null) { if (username != null) {
rocketChatAvatarWidget.loadImage(RocketChatUserAvatar.getUri(hostname, username)) rocketChatAvatarWidget.loadImage(RocketChatUserAvatar.getUri(hostname, username), RocketChatUserAvatar.getTextDrawable(username, rocketChatAvatarWidget.context))
} else { } else {
rocketChatAvatarWidget.visibility = View.GONE rocketChatAvatarWidget.visibility = View.GONE
} }
......
...@@ -2,6 +2,7 @@ package chat.rocket.android.widget; ...@@ -2,6 +2,7 @@ package chat.rocket.android.widget;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -11,35 +12,35 @@ import com.facebook.drawee.view.SimpleDraweeView; ...@@ -11,35 +12,35 @@ import com.facebook.drawee.view.SimpleDraweeView;
public class RocketChatAvatar extends FrameLayout { public class RocketChatAvatar extends FrameLayout {
private SimpleDraweeView draweeView; private SimpleDraweeView simpleDraweeViewAvatar;
public RocketChatAvatar(Context context) { public RocketChatAvatar(Context context) {
super(context); super(context);
initialize(context, null); initialize(context);
} }
public RocketChatAvatar(Context context, AttributeSet attrs) { public RocketChatAvatar(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
initialize(context, attrs); initialize(context);
} }
public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr) { public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
initialize(context, attrs); initialize(context);
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes); super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs); initialize(context);
} }
private void initialize(Context context, AttributeSet attrs) { private void initialize(Context context) {
LayoutInflater.from(context).inflate(R.layout.message_avatar, this, true); LayoutInflater.from(context).inflate(R.layout.message_avatar, this, true);
draweeView = findViewById(R.id.drawee_avatar); simpleDraweeViewAvatar = findViewById(R.id.drawee_avatar);
} }
public void loadImage(String imageUri) { public void loadImage(String imageUri, Drawable placeholderDrawable) {
FrescoHelper.loadImage(draweeView, imageUri); FrescoHelper.loadImage(simpleDraweeViewAvatar, imageUri, placeholderDrawable);
} }
} }
\ No newline at end of file
package chat.rocket.android.widget.helper package chat.rocket.android.widget.helper
import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.support.graphics.drawable.VectorDrawableCompat import android.support.graphics.drawable.VectorDrawableCompat
import chat.rocket.android.widget.R import chat.rocket.android.widget.R
...@@ -10,8 +11,9 @@ import com.facebook.drawee.view.SimpleDraweeView ...@@ -10,8 +11,9 @@ import com.facebook.drawee.view.SimpleDraweeView
class FrescoHelper { class FrescoHelper {
companion object { companion object {
@JvmStatic fun loadImage(draweeView: SimpleDraweeView, imageUri: String) { @JvmStatic fun loadImage(simpleDraweeView: SimpleDraweeView, imageUri: String, placeholderDrawable: Drawable) {
draweeView.setImageURI(imageUri) simpleDraweeView.hierarchy.setPlaceholderImage(placeholderDrawable)
simpleDraweeView.controller = Fresco.newDraweeControllerBuilder().setUri(imageUri).setAutoPlayAnimations(true).build()
} }
/** TODO /** TODO
......
package chat.rocket.android.widget.message.autocomplete.user; package chat.rocket.android.widget.message.autocomplete.user;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
...@@ -7,6 +10,7 @@ import chat.rocket.android.widget.AbsoluteUrl; ...@@ -7,6 +10,7 @@ import chat.rocket.android.widget.AbsoluteUrl;
import chat.rocket.android.widget.R; import chat.rocket.android.widget.R;
import chat.rocket.android.widget.RocketChatAvatar; import chat.rocket.android.widget.RocketChatAvatar;
import chat.rocket.android.widget.message.autocomplete.AutocompleteViewHolder; import chat.rocket.android.widget.message.autocomplete.AutocompleteViewHolder;
import com.amulyakhare.textdrawable.TextDrawable;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
...@@ -16,6 +20,12 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> { ...@@ -16,6 +20,12 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
private final RocketChatAvatar avatar; private final RocketChatAvatar avatar;
private final ImageView status; private final ImageView status;
private static final int[] COLORS = new int[] {
0xFFF44336, 0xFFE91E63, 0xFF9C27B0, 0xFF673AB7, 0xFF3F51B5, 0xFF2196F3,
0xFF03A9F4, 0xFF00BCD4, 0xFF009688, 0xFF4CAF50, 0xFF8BC34A, 0xFFCDDC39,
0xFFFFC107, 0xFFFF9800, 0xFFFF5722, 0xFF795548, 0xFF9E9E9E, 0xFF607D8B
};
public UserViewHolder(View itemView, final AutocompleteViewHolder.OnClickListener<UserItem> onClickListener) { public UserViewHolder(View itemView, final AutocompleteViewHolder.OnClickListener<UserItem> onClickListener) {
super(itemView); super(itemView);
...@@ -44,7 +54,7 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> { ...@@ -44,7 +54,7 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
} }
if (avatar != null) { if (avatar != null) {
avatar.loadImage(getImageUrl(suggestion, userItem.getAbsoluteUrl())); avatar.loadImage(getImageUrl(suggestion, userItem.getAbsoluteUrl()), getTextDrawable(itemView.getContext(), suggestion));
} }
if (status != null) { if (status != null) {
...@@ -73,4 +83,35 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> { ...@@ -73,4 +83,35 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
return null; return null;
} }
} }
private Drawable getTextDrawable(Context context, String username) {
int round = (int) (4 * context.getResources().getDisplayMetrics().density);
return TextDrawable.builder()
.beginConfig()
.useFont(Typeface.SANS_SERIF)
.endConfig()
.buildRoundRect(getUsernameInitials(username), getUserAvatarBackgroundColor(username), round);
}
private String getUsernameInitials(String username) {
if (username.isEmpty()) {
return "?";
}
String[] splitUsername = username.split(".");
if (splitUsername.length > 1) {
return (splitUsername[0].substring(0, 1) + splitUsername[splitUsername.length - 1].substring(0, 1)).toUpperCase();
} else {
if (username.length() > 1) {
return username.substring(0, 2).toUpperCase();
} else {
return username.substring(0, 1).toUpperCase();
}
}
}
private int getUserAvatarBackgroundColor(String username) {
return COLORS[username.length() % COLORS.length];
}
} }
\ 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