Commit b235eb16 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update some classes

parent 0595999e
......@@ -6,8 +6,6 @@ import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.graphics.drawable.VectorDrawableCompat;
import chat.rocket.android.R;
import chat.rocket.android.log.RCLog;
import chat.rocket.android.widget.AbsoluteUrl;
import chat.rocket.android.widget.RocketChatAvatar;
......@@ -87,17 +85,11 @@ public class Avatar {
/**
* render avatar into RocketChatAvatar.
*/
public void into(final RocketChatAvatar rocketChatAvatar) {
final Context context = rocketChatAvatar.getContext();
rocketChatAvatar.loadImage(getImageUrl(), getTextDrawable(context));
}
/**
* render error avatar into RocketChatAvatar.
*/
public void errorInto(final RocketChatAvatar rocketChatAvatar) {
final Context context = rocketChatAvatar.getContext();
rocketChatAvatar.loadImage(VectorDrawableCompat.create(context.getResources(), R.drawable.ic_error_outline_black_24dp, null));
public void into(final RocketChatAvatar rocketChatAvatar, boolean showFailureImage) {
if (showFailureImage)
rocketChatAvatar.showFailureImage();
else
rocketChatAvatar.loadImage(getImageUrl());
}
public Drawable getTextDrawable(Context context) {
......
......@@ -25,13 +25,13 @@ public abstract class AbstractMessageViewHolder extends ModelViewHolder<PairedMe
*/
public AbstractMessageViewHolder(View itemView, AbsoluteUrl absoluteUrl) {
super(itemView);
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);
avatar = itemView.findViewById(R.id.user_avatar);
username = itemView.findViewById(R.id.username);
subUsername = itemView.findViewById(R.id.sub_username);
timestamp = itemView.findViewById(R.id.timestamp);
userAndTimeContainer = itemView.findViewById(R.id.user_and_timestamp_container);
newDayContainer = itemView.findViewById(R.id.newday_container);
newDayText = (TextView) itemView.findViewById(R.id.newday_text);
newDayText = itemView.findViewById(R.id.newday_text);
this.absoluteUrl = absoluteUrl;
}
......@@ -39,12 +39,10 @@ public abstract class AbstractMessageViewHolder extends ModelViewHolder<PairedMe
* bind the view model.
*/
public final void bind(PairedMessage pairedMessage, boolean autoloadImages) {
if (pairedMessage.target != null) {
if (pairedMessage.target.getSyncState() != SyncState.SYNCED)
itemView.setAlpha(0.6f);
else
itemView.setAlpha(1.0f);
}
if (pairedMessage.target.getSyncState() != SyncState.SYNCED)
itemView.setAlpha(0.6f);
else
itemView.setAlpha(1.0f);
bindMessage(pairedMessage, autoloadImages);
renderNewDayAndSequential(pairedMessage);
......
......@@ -58,12 +58,12 @@ public class RoomUserAdapter extends RecyclerView.Adapter<RoomUserViewHolder> {
.setUtcOffset(0)
.build();
new UserRenderer(context, user)
.avatarInto(holder.avatar, absoluteUrl)
.avatarInto(holder.avatar, absoluteUrl, false)
.usernameInto(holder.username);
} else {
new UserRenderer(context, realmUser.asUser())
.statusColorInto(holder.status)
.avatarInto(holder.avatar, absoluteUrl)
.avatarInto(holder.avatar, absoluteUrl, false)
.usernameInto(holder.username);
}
}
......
......@@ -28,7 +28,7 @@ public class SuggestUserAdapter extends RealmAutoCompleteAdapter<RealmUser> {
protected void onBindItemView(View itemView, RealmUser user) {
new UserRenderer(itemView.getContext(), user.asUser())
.statusColorInto((ImageView) itemView.findViewById(R.id.room_user_status))
.avatarInto((RocketChatAvatar) itemView.findViewById(R.id.room_user_avatar), absoluteUrl);
.avatarInto((RocketChatAvatar) itemView.findViewById(R.id.room_user_avatar), absoluteUrl, false);
}
@Override
......
......@@ -4,7 +4,6 @@ import android.content.Context;
import android.view.View;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.helper.Avatar;
import chat.rocket.android.helper.DateTime;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.widget.AbsoluteUrl;
......@@ -15,7 +14,6 @@ import chat.rocket.android.widget.message.RocketChatMessageUrlsLayout;
import chat.rocket.core.SyncState;
import chat.rocket.core.models.Attachment;
import chat.rocket.core.models.Message;
import chat.rocket.core.models.User;
import chat.rocket.core.models.WebContent;
import java.util.List;
......@@ -37,20 +35,20 @@ public class MessageRenderer extends AbstractRenderer<Message> {
* show Avatar image.
*/
public MessageRenderer avatarInto(RocketChatAvatar rocketChatAvatar, AbsoluteUrl absoluteUrl) {
if (!shouldHandle(rocketChatAvatar)) {
if (!shouldHandle(rocketChatAvatar))
return this;
}
if (object.getSyncState() == SyncState.FAILED)
// rocketChatAvatar.loadImage(VectorDrawableCompat.create(context.getResources(), R.drawable.ic_error_outline_black_24dp, null));
userRenderer.errorAvatarInto(rocketChatAvatar);
else if (TextUtils.isEmpty(object.getAvatar()))
userRenderer.avatarInto(rocketChatAvatar, absoluteUrl);
else {
final User user = object.getUser();
setAvatarInto(object.getAvatar(), absoluteUrl, user == null ? null : user.getUsername(), rocketChatAvatar);
switch (object.getSyncState()){
case SyncState.FAILED:
userRenderer.avatarInto(rocketChatAvatar, absoluteUrl, true);
break;
default:
if (TextUtils.isEmpty(object.getAvatar()))
userRenderer.avatarInto(rocketChatAvatar, absoluteUrl, false);
else
rocketChatAvatar.loadImage(object.getAvatar());
break;
}
return this;
}
......@@ -60,12 +58,10 @@ public class MessageRenderer extends AbstractRenderer<Message> {
public MessageRenderer usernameInto(TextView usernameTextView, TextView subUsernameTextView) {
if (TextUtils.isEmpty(object.getAlias())) {
userRenderer.usernameInto(usernameTextView);
if (subUsernameTextView != null) {
if (subUsernameTextView != null)
subUsernameTextView.setVisibility(View.GONE);
}
} else {
} else
aliasAndUsernameInto(usernameTextView, subUsernameTextView);
}
return this;
}
......@@ -73,9 +69,8 @@ public class MessageRenderer extends AbstractRenderer<Message> {
* show timestamp in textView.
*/
public MessageRenderer timestampInto(TextView textView) {
if (!shouldHandle(textView)) {
if (!shouldHandle(textView))
return this;
}
switch (object.getSyncState()) {
case SyncState.SYNCING:
......@@ -91,7 +86,6 @@ public class MessageRenderer extends AbstractRenderer<Message> {
textView.setText(DateTime.fromEpocMs(object.getTimestamp(), DateTime.Format.TIME));
break;
}
return this;
}
......@@ -99,12 +93,10 @@ public class MessageRenderer extends AbstractRenderer<Message> {
* show body in RocketChatMessageLayout.
*/
public MessageRenderer bodyInto(RocketChatMessageLayout rocketChatMessageLayout) {
if (!shouldHandle(rocketChatMessageLayout)) {
if (!shouldHandle(rocketChatMessageLayout))
return this;
}
rocketChatMessageLayout.setText(object.getMessage());
return this;
}
......@@ -112,59 +104,48 @@ public class MessageRenderer extends AbstractRenderer<Message> {
* show urls in RocketChatMessageUrlsLayout.
*/
public MessageRenderer urlsInto(RocketChatMessageUrlsLayout urlsLayout) {
if (!shouldHandle(urlsLayout)) {
if (!shouldHandle(urlsLayout))
return this;
}
List<WebContent> webContents = object.getWebContents();
if (webContents == null || webContents.size() == 0) {
if (webContents == null || webContents.size() == 0)
urlsLayout.setVisibility(View.GONE);
} else {
else {
urlsLayout.setVisibility(View.VISIBLE);
urlsLayout.setUrls(webContents, autoloadImages);
}
return this;
}
/**
* show urls in RocketChatMessageUrlsLayout.
*/
public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout,
AbsoluteUrl absoluteUrl) {
if (!shouldHandle(attachmentsLayout)) {
public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout, AbsoluteUrl absoluteUrl) {
if (!shouldHandle(attachmentsLayout))
return this;
}
List<Attachment> attachments = object.getAttachments();
if (attachments == null || attachments.size() == 0) {
if (attachments == null || attachments.size() == 0)
attachmentsLayout.setVisibility(View.GONE);
} else {
else {
attachmentsLayout.setVisibility(View.VISIBLE);
attachmentsLayout.setAbsoluteUrl(absoluteUrl);
attachmentsLayout.setAttachments(attachments, autoloadImages);
}
return this;
}
private void setAvatarInto(String avatar, AbsoluteUrl absoluteUrl, String username, RocketChatAvatar imageView) {
imageView.loadImage(avatar, new Avatar(absoluteUrl, username).getTextDrawable(context));
}
private void aliasAndUsernameInto(TextView aliasTextView, TextView usernameTextView) {
if (shouldHandle(aliasTextView)) {
if (shouldHandle(aliasTextView))
aliasTextView.setText(object.getAlias());
}
if (shouldHandle(usernameTextView)) {
if (object.getUser() != null) {
usernameTextView.setText("@" + object.getUser().getUsername());
usernameTextView.setVisibility(View.VISIBLE);
} else {
} else
usernameTextView.setVisibility(View.GONE);
}
}
}
}
}
\ No newline at end of file
......@@ -21,27 +21,14 @@ public class UserRenderer extends AbstractRenderer<User> {
/**
* show Avatar image
*/
public UserRenderer avatarInto(RocketChatAvatar rocketChatAvatar, AbsoluteUrl absoluteUrl) {
public UserRenderer avatarInto(RocketChatAvatar rocketChatAvatar, AbsoluteUrl absoluteUrl, boolean showFailureImage) {
if (!shouldHandle(rocketChatAvatar)) {
return this;
}
if (!TextUtils.isEmpty(object.getUsername())) {
new Avatar(absoluteUrl, object.getUsername()).into(rocketChatAvatar);
}
return this;
}
/**
* show Avatar error image
*/
public UserRenderer errorAvatarInto(RocketChatAvatar rocketChatAvatar) {
if (!shouldHandle(rocketChatAvatar)) {
return this;
}
if (!TextUtils.isEmpty(object.getUsername())) {
new Avatar(null, object.getUsername()).errorInto(rocketChatAvatar);
new Avatar(absoluteUrl, object.getUsername())
.into(rocketChatAvatar, showFailureImage);
}
return this;
}
......
......@@ -2,15 +2,11 @@ package chat.rocket.android.widget;
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 chat.rocket.android.widget.helper.FrescoAvatarHelper;
import com.facebook.drawee.view.SimpleDraweeView;
public class RocketChatAvatar extends FrameLayout {
......@@ -39,36 +35,19 @@ public class RocketChatAvatar extends FrameLayout {
}
private void initialize(Context context, AttributeSet attrs) {
LayoutInflater.from(context)
LayoutInflater
.from(context)
.inflate(R.layout.message_avatar, this, true);
draweeView = (SimpleDraweeView) findViewById(R.id.drawee_avatar);
draweeView = 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?
final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
hierarchy.setPlaceholderImage(drawable);
hierarchy.setFailureImage(drawable);
final DraweeController controller = Fresco.newDraweeControllerBuilder()
.setAutoPlayAnimations(true)
.build();
draweeView.setController(controller);
public void loadImage(String imageUrl) {
FrescoAvatarHelper
.loadImage(draweeView, imageUrl);
}
public void loadImage(String url, Drawable placeholder) {
final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
hierarchy.setPlaceholderImage(placeholder);
hierarchy.setFailureImage(placeholder);
final DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(Uri.parse(url))
.setAutoPlayAnimations(true)
.build();
draweeView.setController(controller);
public void showFailureImage() {
FrescoAvatarHelper
.showFailureImage(draweeView);
}
}
}
\ No newline at end of file
package chat.rocket.android.widget;
import android.content.Context;
import chat.rocket.android.widget.fresco.CustomImageFormatConfigurator;
import com.facebook.drawee.backends.pipeline.DraweeConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import chat.rocket.android.widget.fresco.ImageFormatConfigurator;
import okhttp3.OkHttpClient;
public class RocketChatWidgets {
......@@ -16,14 +14,12 @@ public class RocketChatWidgets {
ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
.newBuilder(context, okHttpClient)
.setDownsampleEnabled(true)
.setImageDecoderConfig(ImageFormatConfigurator.createImageDecoderConfig())
.setImageDecoderConfig(CustomImageFormatConfigurator.createImageDecoderConfig())
.build();
DraweeConfig.Builder draweeConfigBuilder = DraweeConfig.newBuilder();
ImageFormatConfigurator.addCustomDrawableFactories(draweeConfigBuilder);
CustomImageFormatConfigurator.addCustomDrawableFactories(draweeConfigBuilder);
Fresco.initialize(context, config, draweeConfigBuilder.build());
Fresco.getImagePipeline().clearCaches();
}
}
}
\ No newline at end of file
......@@ -22,10 +22,8 @@ public class FrescoHelper {
public static void setupDrawee(SimpleDraweeView draweeView) {
final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
hierarchy.setPlaceholderImage(
VectorDrawableCompat.create(draweeView.getResources(), R.drawable.image_dummy, null));
hierarchy.setFailureImage(
VectorDrawableCompat.create(draweeView.getResources(), R.drawable.image_error, null));
hierarchy.setPlaceholderImage(VectorDrawableCompat.create(draweeView.getResources(), R.drawable.image_dummy, null));
hierarchy.setFailureImage(VectorDrawableCompat.create(draweeView.getResources(), R.drawable.image_error, null));
hierarchy.setProgressBarImage(new ProgressBarDrawable());
}
......@@ -37,4 +35,4 @@ public class FrescoHelper {
.build();
draweeView.setController(controller);
}
}
}
\ No newline at end of file
......@@ -54,20 +54,14 @@ public class UserViewHolder extends AutocompleteViewHolder<UserItem> {
final String suggestion = userItem.getSuggestion();
if (titleTextView != null) {
if (titleTextView != null)
titleTextView.setText(suggestion);
}
if (avatar != null) {
avatar.loadImage(
getImageUrl(suggestion, userItem.getAbsoluteUrl()),
getTextDrawable(itemView.getContext(), suggestion)
);
}
if (avatar != null)
avatar.loadImage(getImageUrl(suggestion, userItem.getAbsoluteUrl()));
if (status != null) {
if (status != null)
status.setImageResource(userItem.getStatusResId());
}
}
@Override
......
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