Commit 9e61eb4a authored by Tiago Cunha's avatar Tiago Cunha

RocketChatAvatar widget to deal with avatar loading.

No more Picasso
parent 3ed31364
......@@ -108,7 +108,6 @@ dependencies {
compile 'com.google.android.gms:play-services-gcm:10.0.0'
compile rootProject.ext.okhttp3
compile rootProject.ext.picasso
compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.4.1'
......
......@@ -28,6 +28,7 @@ import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.renderer.UserRenderer;
import chat.rocket.android.widget.message.RocketChatAvatar;
public class SidebarMainFragment extends AbstractFragment {
......@@ -147,7 +148,7 @@ public class SidebarMainFragment extends AbstractFragment {
private void onRenderCurrentUser(User user) {
if (user != null && !TextUtils.isEmpty(hostname)) {
new UserRenderer(getContext(), user)
.avatarInto((ImageView) rootView.findViewById(R.id.current_user_avatar), hostname)
.avatarInto((RocketChatAvatar) rootView.findViewById(R.id.current_user_avatar), hostname)
.usernameInto((TextView) rootView.findViewById(R.id.current_user_name))
.statusColorInto((ImageView) rootView.findViewById(R.id.current_user_status));
}
......
......@@ -6,13 +6,12 @@ import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.squareup.picasso.Picasso;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import chat.rocket.android.log.RCLog;
import chat.rocket.android.widget.message.RocketChatAvatar;
/**
* Helper for rendering user avatar image.
......@@ -80,18 +79,15 @@ public class Avatar {
}
/**
* render avatar into imageView.
* render avatar into RocketChatAvatar.
*/
public void into(final ImageView imageView) {
if (ViewDataCache.isStored(username, imageView)) {
public void into(final RocketChatAvatar rocketChatAvatar) {
if (ViewDataCache.isStored(username, rocketChatAvatar)) {
return;
}
final Context context = imageView.getContext();
Picasso.with(context)
.load(getImageUrl())
.placeholder(getTextDrawable(context))
.into(imageView);
final Context context = rocketChatAvatar.getContext();
rocketChatAvatar.loadImage(getImageUrl(), getTextDrawable(context));
}
public Drawable getTextDrawable(Context context) {
......
......@@ -10,9 +10,10 @@ import chat.rocket.android.helper.DateTime;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.realm_helper.RealmModelViewHolder;
import chat.rocket.android.widget.message.RocketChatAvatar;
public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<PairedMessage> {
protected final ImageView avatar;
protected final RocketChatAvatar avatar;
protected final TextView username;
protected final TextView subUsername;
protected final TextView timestamp;
......@@ -28,7 +29,7 @@ public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<Pai
*/
public AbstractMessageViewHolder(View itemView, String hostname, String userId, String token) {
super(itemView);
avatar = (ImageView) itemView.findViewById(R.id.user_avatar);
avatar = (RocketChatAvatar) itemView.findViewById(R.id.user_avatar);
username = (TextView) itemView.findViewById(R.id.username);
subUsername = (TextView) itemView.findViewById(R.id.sub_username);
timestamp = (TextView) itemView.findViewById(R.id.timestamp);
......
......@@ -6,13 +6,14 @@ import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.widget.message.RocketChatAvatar;
/**
* ViewHolder for UsersOfRoom.
*/
public class RoomUserViewHolder extends RecyclerView.ViewHolder {
ImageView status;
ImageView avatar;
RocketChatAvatar avatar;
TextView username;
/**
......@@ -21,7 +22,7 @@ public class RoomUserViewHolder extends RecyclerView.ViewHolder {
public RoomUserViewHolder(View itemView) {
super(itemView);
status = (ImageView) itemView.findViewById(R.id.room_user_status);
avatar = (ImageView) itemView.findViewById(R.id.room_user_avatar);
avatar = (RocketChatAvatar) itemView.findViewById(R.id.room_user_avatar);
username = (TextView) itemView.findViewById(R.id.room_user_name);
}
}
......@@ -10,6 +10,7 @@ import chat.rocket.android.R;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmAutoCompleteAdapter;
import chat.rocket.android.renderer.UserRenderer;
import chat.rocket.android.widget.message.RocketChatAvatar;
/**
* adapter to suggest user names.
......@@ -26,7 +27,7 @@ public class SuggestUserAdapter extends RealmAutoCompleteAdapter<User> {
protected void onBindItemView(View itemView, User user) {
new UserRenderer(itemView.getContext(), user)
.statusColorInto((ImageView) itemView.findViewById(R.id.room_user_status))
.avatarInto((ImageView) itemView.findViewById(R.id.room_user_avatar), hostname);
.avatarInto((RocketChatAvatar) itemView.findViewById(R.id.room_user_avatar), hostname);
}
@Override
......
package chat.rocket.android.renderer;
import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import chat.rocket.android.R;
import chat.rocket.android.helper.Avatar;
......@@ -13,6 +12,7 @@ import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.widget.message.RocketChatAvatar;
import chat.rocket.android.widget.message.RocketChatMessageAttachmentsLayout;
import chat.rocket.android.widget.message.RocketChatMessageLayout;
import chat.rocket.android.widget.message.RocketChatMessageUrlsLayout;
......@@ -32,15 +32,16 @@ public class MessageRenderer extends AbstractRenderer<Message> {
/**
* show Avatar image.
*/
public MessageRenderer avatarInto(ImageView imageView, String hostname) {
public MessageRenderer avatarInto(RocketChatAvatar rocketChatAvatar, String hostname) {
if (object.getSyncState() == SyncState.FAILED) {
imageView.setImageResource(R.drawable.ic_error_outline_black_24dp);
rocketChatAvatar.loadImage(VectorDrawableCompat
.create(context.getResources(), R.drawable.ic_error_outline_black_24dp, null));
} else if (TextUtils.isEmpty(object.getAvatar())) {
userRenderer.avatarInto(imageView, hostname);
userRenderer.avatarInto(rocketChatAvatar, hostname);
} else {
final User user = object.getUser();
setAvatarInto(object.getAvatar(), hostname, user == null ? null : user.getUsername(),
imageView);
rocketChatAvatar);
}
return this;
}
......@@ -134,12 +135,9 @@ public class MessageRenderer extends AbstractRenderer<Message> {
return this;
}
private void setAvatarInto(String avatar, String hostname, String username, ImageView imageView) {
Picasso.with(context)
.load(avatar)
.placeholder(
new Avatar(hostname, username).getTextDrawable(context))
.into(imageView);
private void setAvatarInto(String avatar, String hostname, String username,
RocketChatAvatar imageView) {
imageView.loadImage(avatar, new Avatar(hostname, username).getTextDrawable(context));
}
private void aliasAndUsernameInto(TextView aliasTextView, TextView usernameTextView) {
......
......@@ -8,6 +8,7 @@ import chat.rocket.android.R;
import chat.rocket.android.helper.Avatar;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.widget.message.RocketChatAvatar;
/**
* Renderer for User model.
......@@ -21,13 +22,13 @@ public class UserRenderer extends AbstractRenderer<User> {
/**
* show Avatar image
*/
public UserRenderer avatarInto(ImageView imageView, String hostname) {
if (!shouldHandle(imageView)) {
public UserRenderer avatarInto(RocketChatAvatar rocketChatAvatar, String hostname) {
if (!shouldHandle(rocketChatAvatar)) {
return this;
}
if (!TextUtils.isEmpty(object.getUsername())) {
new Avatar(hostname, object.getUsername()).into(imageView);
new Avatar(hostname, object.getUsername()).into(rocketChatAvatar);
}
return this;
}
......
......@@ -30,7 +30,7 @@
android:layout_width="@dimen/margin_8"
android:layout_height="wrap_content" />
<ImageView
<chat.rocket.android.widget.message.RocketChatAvatar
android:id="@+id/current_user_avatar"
android:layout_width="40dp"
android:layout_height="40dp" />
......
......@@ -13,12 +13,11 @@
android:layout_height="wrap_content"
android:layout_marginBottom="2dp">
<ImageView
<chat.rocket.android.widget.message.RocketChatAvatar
android:id="@+id/user_avatar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="8dp"
tools:src="@drawable/ic_default_avatar" />
android:layout_margin="8dp" />
<LinearLayout
android:layout_width="match_parent"
......
......@@ -12,12 +12,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
<chat.rocket.android.widget.message.RocketChatAvatar
android:id="@+id/user_avatar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="8dp"
tools:src="@drawable/ic_default_avatar" />
android:layout_margin="8dp" />
<LinearLayout
android:layout_width="match_parent"
......
......@@ -11,7 +11,7 @@
android:layout_height="8dp"
android:layout_margin="@dimen/margin_8" />
<ImageView
<chat.rocket.android.widget.message.RocketChatAvatar
android:id="@+id/room_user_avatar"
android:layout_width="24dp"
android:layout_height="24dp" />
......
......@@ -24,8 +24,6 @@ ext {
rxJava = 'io.reactivex:rxjava:1.2.3'
boltsTask = 'com.parse.bolts:bolts-tasks:1.4.0'
okhttp3 = 'com.squareup.okhttp3:okhttp:3.5.0'
picasso = 'com.squareup.picasso:picasso:2.5.2'
picasso2Okhttp3Downloader = 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
textDrawable = 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
preDexLibs = !"true".equals(System.getenv("CI"))
......
package chat.rocket.android.widget.message;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import chat.rocket.android.widget.R;
public class RocketChatAvatar extends FrameLayout {
private SimpleDraweeView draweeView;
public RocketChatAvatar(Context context) {
super(context);
initialize(context, null);
}
public RocketChatAvatar(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatAvatar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
}
private void initialize(Context context, AttributeSet attrs) {
LayoutInflater.from(context)
.inflate(R.layout.message_avatar, this, true);
draweeView = (SimpleDraweeView) findViewById(R.id.drawee_avatar);
}
public void loadImage(Drawable drawable) {
final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
hierarchy.setImage(drawable, 100, true); // Is there a better way?
}
public void loadImage(String url, Drawable placeholder) {
final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
hierarchy.setPlaceholderImage(placeholder);
final DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(Uri.parse(url))
.setAutoPlayAnimations(true)
.build();
draweeView.setController(controller);
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.facebook.drawee.view.SimpleDraweeView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawee_avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
fresco:actualImageScaleType="fitCenter" />
\ 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