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
import android.widget.TextView
import chat.rocket.android.R
import chat.rocket.android.helper.DateTime
import chat.rocket.android.helper.OkHttpHelper
import chat.rocket.android.helper.RocketChatUserAvatar
import chat.rocket.android.widget.AbsoluteUrl
import chat.rocket.android.widget.RocketChatAvatar
......@@ -20,30 +19,20 @@ class MessageRenderer(val message: Message, val autoLoadImage: Boolean) {
/**
* Show user's avatar image in RocketChatAvatar widget.
*/
fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String, userNotFoundAvatarImageView: ImageView, userAvatarSvgImage: ImageView) {
if (message.avatar != null) {
// Load user's avatar image from Oauth provider URI.
rocketChatAvatarWidget.loadImage(message.avatar)
} else {
val username: String? = message.user?.username
if (username != null) {
userNotFoundAvatarImageView.visibility = View.GONE
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
}
fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String, userNotFoundAvatarImageView: ImageView) {
val username: String? = message.user?.username
if (username != null) {
userNotFoundAvatarImageView.visibility = View.GONE
val placeholderDrawable = RocketChatUserAvatar.getTextDrawable(username, rocketChatAvatarWidget.context)
if (message.avatar != null) {
// Load user's avatar image from Oauth provider URI.
rocketChatAvatarWidget.loadImage(message.avatar, placeholderDrawable)
} else {
userAvatarSvgImage.visibility = View.GONE
rocketChatAvatarWidget.visibility = View.GONE
userNotFoundAvatarImageView.visibility = View.VISIBLE
rocketChatAvatarWidget.loadImage(RocketChatUserAvatar.getUri(hostname, username), placeholderDrawable)
}
} else {
rocketChatAvatarWidget.visibility = View.GONE
userNotFoundAvatarImageView.visibility = View.VISIBLE
}
}
......
......@@ -16,7 +16,7 @@ class UserRenderer(val user: User) {
fun showAvatar(rocketChatAvatarWidget: RocketChatAvatar, hostname: String) {
val username: String? = user.username
if (username != null) {
rocketChatAvatarWidget.loadImage(RocketChatUserAvatar.getUri(hostname, username))
rocketChatAvatarWidget.loadImage(RocketChatUserAvatar.getUri(hostname, username), RocketChatUserAvatar.getTextDrawable(username, rocketChatAvatarWidget.context))
} else {
rocketChatAvatarWidget.visibility = View.GONE
}
......
......@@ -2,6 +2,7 @@ package chat.rocket.android.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
......@@ -11,35 +12,35 @@ import com.facebook.drawee.view.SimpleDraweeView;
public class RocketChatAvatar extends FrameLayout {
private SimpleDraweeView draweeView;
private SimpleDraweeView simpleDraweeViewAvatar;
public RocketChatAvatar(Context context) {
super(context);
initialize(context, null);
initialize(context);
}
public RocketChatAvatar(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
initialize(context);
}
public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
initialize(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr, int 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);
draweeView = findViewById(R.id.drawee_avatar);
simpleDraweeViewAvatar = findViewById(R.id.drawee_avatar);
}
public void loadImage(String imageUri) {
FrescoHelper.loadImage(draweeView, imageUri);
public void loadImage(String imageUri, Drawable placeholderDrawable) {
FrescoHelper.loadImage(simpleDraweeViewAvatar, imageUri, placeholderDrawable);
}
}
\ No newline at end of file
package chat.rocket.android.widget.helper
import android.graphics.drawable.Drawable
import android.net.Uri
import android.support.graphics.drawable.VectorDrawableCompat
import chat.rocket.android.widget.R
......@@ -10,8 +11,9 @@ import com.facebook.drawee.view.SimpleDraweeView
class FrescoHelper {
companion object {
@JvmStatic fun loadImage(draweeView: SimpleDraweeView, imageUri: String) {
draweeView.setImageURI(imageUri)
@JvmStatic fun loadImage(simpleDraweeView: SimpleDraweeView, imageUri: String, placeholderDrawable: Drawable) {
simpleDraweeView.hierarchy.setPlaceholderImage(placeholderDrawable)
simpleDraweeView.controller = Fresco.newDraweeControllerBuilder().setUri(imageUri).setAutoPlayAnimations(true).build()
}
/** TODO
......
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.widget.ImageView;
import android.widget.TextView;
......@@ -7,6 +10,7 @@ import chat.rocket.android.widget.AbsoluteUrl;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.RocketChatAvatar;
import chat.rocket.android.widget.message.autocomplete.AutocompleteViewHolder;
import com.amulyakhare.textdrawable.TextDrawable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
......@@ -16,6 +20,12 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
private final RocketChatAvatar avatar;
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) {
super(itemView);
......@@ -44,7 +54,7 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
}
if (avatar != null) {
avatar.loadImage(getImageUrl(suggestion, userItem.getAbsoluteUrl()));
avatar.loadImage(getImageUrl(suggestion, userItem.getAbsoluteUrl()), getTextDrawable(itemView.getContext(), suggestion));
}
if (status != null) {
......@@ -73,4 +83,35 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
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