Unverified Commit 9e846b7f authored by Leonardo Aramaki's avatar Leonardo Aramaki Committed by GitHub

Merge pull request #616 from RocketChat/fix/image-loading

[FIX] Image not loading when changing rooms
parents 13e6026e 4cca8edf
......@@ -21,6 +21,7 @@ import chat.rocket.core.models.Settings;
import chat.rocket.core.models.User;
import chat.rocket.core.repositories.RoomRepository;
import chat.rocket.core.repositories.UserRepository;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
......@@ -66,7 +67,6 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
getRoomHistoryStateInfo();
getMessages();
getUserPreferences();
getAbsoluteUrl();
}
@Override
......@@ -134,7 +134,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override
public void replyMessage(@NonNull Message message, boolean justQuote) {
this.absoluteUrlHelper.getRocketChatAbsoluteUrl()
final Disposable subscription = this.absoluteUrlHelper.getRocketChatAbsoluteUrl()
.cache()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
......@@ -333,14 +333,21 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
}
private void getMessages() {
final Disposable subscription = roomRepository.getById(roomId)
final Disposable subscription = Flowable.zip(roomRepository.getById(roomId),
absoluteUrlHelper.getRocketChatAbsoluteUrl().toFlowable().cache(), Pair::new)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.map(pair -> {
view.setupWith(pair.second.orNull());
return pair.first;
})
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(messageInteractor::getAllFrom)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
messages -> view.showMessages(messages),
view::showMessages,
Logger::report
);
......
......@@ -12,7 +12,8 @@ ext {
constraintLayout : "com.android.support.constraint:constraint-layout:${constraintLayoutVersion}",
cardView : "com.android.support:cardview-v7:${supportLibraryVersion}",
supportV13 : "com.android.support:support-v13:${supportLibraryVersion}",
multidex : "com.android.support:multidex:1.0.2"
multidex : "com.android.support:multidex:1.0.2",
customTabs : "com.android.support:customtabs:${supportLibraryVersion}"
]
extraDependencies = [
okHTTP : "com.squareup.okhttp3:okhttp:${okHttpVersion}",
......
......@@ -39,6 +39,7 @@ dependencies {
compile supportDependencies.designSupportLibrary
compile supportDependencies.constraintLayout
compile supportDependencies.supportV13
compile supportDependencies.customTabs
compile rxbindingDependencies.rxBinding
compile rxbindingDependencies.rxBindingSupport
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$rootProject.ext.kotlinVersion"
......
......@@ -10,7 +10,6 @@ import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.drawable.ProgressBarDrawable
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.facebook.drawee.generic.RoundingParams
import com.facebook.drawee.view.SimpleDraweeView
object FrescoHelper {
......@@ -39,7 +38,6 @@ object FrescoHelper {
val hierarchy: GenericDraweeHierarchy = draweeView.hierarchy
hierarchy.setPlaceholderImage(VectorDrawableCompat.create(draweeView.resources, R.drawable.image_dummy, null))
hierarchy.setFailureImage(VectorDrawableCompat.create(draweeView.resources, R.drawable.image_error, null))
hierarchy.roundingParams = RoundingParams().setCornersRadii(5F, 5F, 5F, 5F)
hierarchy.actualImageScaleType = ScalingUtils.ScaleType.FIT_CENTER
hierarchy.setProgressBarImage(ProgressBarDrawable())
......
......@@ -7,6 +7,7 @@ import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat;
import android.text.TextUtils;
......@@ -17,8 +18,12 @@ import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.cache.common.CacheKey;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.cache.DefaultCacheKeyFactory;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.imagepipeline.request.ImageRequest;
import java.util.List;
......@@ -33,254 +38,262 @@ import chat.rocket.core.models.AttachmentTitle;
/**
*/
public class RocketChatMessageAttachmentsLayout extends LinearLayout {
private LayoutInflater inflater;
private AbsoluteUrl absoluteUrl;
private List<Attachment> attachments;
public RocketChatMessageAttachmentsLayout(Context context) {
super(context);
initialize(context, null);
}
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
}
private void initialize(Context context, AttributeSet attrs) {
inflater = LayoutInflater.from(context);
setOrientation(VERTICAL);
}
public void setAbsoluteUrl(AbsoluteUrl absoluteUrl) {
this.absoluteUrl = absoluteUrl;
}
public void setAttachments(List<Attachment> attachments, boolean autoloadImages) {
if (this.attachments != null && this.attachments.equals(attachments)) {
return;
private LayoutInflater inflater;
private AbsoluteUrl absoluteUrl;
private List<Attachment> attachments;
public RocketChatMessageAttachmentsLayout(Context context) {
super(context);
initialize(context, null);
}
this.attachments = attachments;
for (int i = 0, size = attachments.size(); i < size; i++) {
appendAttachmentView(attachments.get(i), autoloadImages, true);
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
}
public void appendAttachmentView(Attachment attachment, boolean autoloadImages, boolean showAttachmentStrip) {
if (attachment == null) {
return;
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
removeAllViews();
View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false);
colorizeAttachmentBar(attachment, attachmentView, showAttachmentStrip);
showAuthorAttachment(attachment, attachmentView);
showTitleAttachment(attachment, attachmentView);
showReferenceAttachment(attachment, attachmentView);
showImageAttachment(attachment, attachmentView, autoloadImages);
// audio
// video
showFieldsAttachment(attachment, attachmentView);
addView(attachmentView);
}
private void colorizeAttachmentBar(Attachment attachment, View attachmentView, boolean showAttachmentStrip) {
final View attachmentStrip = attachmentView.findViewById(R.id.attachment_strip);
if (showAttachmentStrip) {
final String colorString = attachment.getColor();
if (TextUtils.isEmpty(colorString)) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
return;
}
try {
attachmentStrip.setBackgroundColor(Color.parseColor(colorString));
} catch (Exception e) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
}
} else {
attachmentStrip.setVisibility(GONE);
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
}
}
private void showAuthorAttachment(Attachment attachment, View attachmentView) {
final View authorBox = attachmentView.findViewById(R.id.author_box);
AttachmentAuthor author = attachment.getAttachmentAuthor();
if (author == null) {
authorBox.setVisibility(GONE);
return;
private void initialize(Context context, AttributeSet attrs) {
inflater = LayoutInflater.from(context);
setOrientation(VERTICAL);
}
authorBox.setVisibility(VISIBLE);
public void setAbsoluteUrl(AbsoluteUrl absoluteUrl) {
this.absoluteUrl = absoluteUrl;
}
FrescoHelper.INSTANCE.loadImageWithCustomization((SimpleDraweeView) attachmentView.findViewById(R.id.author_icon), absolutize(author.getIconUrl()));
public void setAttachments(List<Attachment> attachments, boolean autoloadImages) {
if (this.attachments != null && this.attachments.equals(attachments)) {
return;
}
this.attachments = attachments;
final TextView authorName = (TextView) attachmentView.findViewById(R.id.author_name);
authorName.setText(author.getName());
for (int i = 0, size = attachments.size(); i < size; i++) {
appendAttachmentView(attachments.get(i), autoloadImages, true);
}
}
public void appendAttachmentView(Attachment attachment, boolean autoloadImages, boolean showAttachmentStrip) {
if (attachment == null) {
return;
}
final String link = absolutize(author.getLink());
authorName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
}
});
removeAllViews();
View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false);
// timestamp and link - need to format time
}
colorizeAttachmentBar(attachment, attachmentView, showAttachmentStrip);
showAuthorAttachment(attachment, attachmentView);
showTitleAttachment(attachment, attachmentView);
showReferenceAttachment(attachment, attachmentView);
showImageAttachment(attachment, attachmentView, autoloadImages);
// audio
// video
showFieldsAttachment(attachment, attachmentView);
private void showTitleAttachment(Attachment attachment, View attachmentView) {
TextView titleView = (TextView) attachmentView.findViewById(R.id.title);
AttachmentTitle title = attachment.getAttachmentTitle();
if (title == null || title.getTitle() == null) {
titleView.setVisibility(View.GONE);
return;
addView(attachmentView);
}
titleView.setVisibility(View.VISIBLE);
titleView.setText(title.getTitle());
if (title.getLink() == null) {
titleView.setOnClickListener(null);
titleView.setClickable(false);
} else {
final String link = absolutize(title.getLink());
titleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Context context = view.getContext();
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
private void colorizeAttachmentBar(Attachment attachment, View attachmentView, boolean showAttachmentStrip) {
final View attachmentStrip = attachmentView.findViewById(R.id.attachment_strip);
if (showAttachmentStrip) {
final String colorString = attachment.getColor();
if (TextUtils.isEmpty(colorString)) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
return;
}
try {
attachmentStrip.setBackgroundColor(Color.parseColor(colorString));
} catch (Exception e) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
}
} else {
attachmentStrip.setVisibility(GONE);
}
});
TextViewCompat.setTextAppearance(titleView,
R.style.TextAppearance_RocketChat_MessageAttachment_Title_Link);
}
}
private void showReferenceAttachment(Attachment attachment, View attachmentView) {
final View refBox = attachmentView.findViewById(R.id.ref_box);
if (attachment.getThumbUrl() == null && attachment.getText() == null) {
refBox.setVisibility(GONE);
return;
}
private void showAuthorAttachment(Attachment attachment, View attachmentView) {
final View authorBox = attachmentView.findViewById(R.id.author_box);
AttachmentAuthor author = attachment.getAttachmentAuthor();
if (author == null) {
authorBox.setVisibility(GONE);
return;
}
authorBox.setVisibility(VISIBLE);
refBox.setVisibility(VISIBLE);
FrescoHelper.INSTANCE.loadImageWithCustomization((SimpleDraweeView) attachmentView.findViewById(R.id.author_icon), absolutize(author.getIconUrl()));
final SimpleDraweeView thumbImage = (SimpleDraweeView) refBox.findViewById(R.id.thumb);
final TextView authorName = attachmentView.findViewById(R.id.author_name);
authorName.setText(author.getName());
final String thumbUrl = attachment.getThumbUrl();
if (TextUtils.isEmpty(thumbUrl)) {
thumbImage.setVisibility(GONE);
} else {
thumbImage.setVisibility(VISIBLE);
FrescoHelper.INSTANCE.loadImageWithCustomization(thumbImage, absolutize(thumbUrl));
final String link = absolutize(author.getLink());
authorName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
}
});
// timestamp and link - need to format time
}
final TextView refText = (TextView) refBox.findViewById(R.id.text);
private void showTitleAttachment(Attachment attachment, View attachmentView) {
TextView titleView = attachmentView.findViewById(R.id.title);
AttachmentTitle title = attachment.getAttachmentTitle();
if (title == null || title.getTitle() == null) {
titleView.setVisibility(View.GONE);
return;
}
final String refString = attachment.getText();
if (TextUtils.isEmpty(refString)) {
refText.setVisibility(GONE);
} else {
refText.setVisibility(VISIBLE);
refText.setText(refString);
titleView.setVisibility(View.VISIBLE);
titleView.setText(title.getTitle());
if (title.getLink() == null) {
titleView.setOnClickListener(null);
titleView.setClickable(false);
} else {
final String link = absolutize(title.getLink());
titleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
new CustomTabsIntent.Builder()
.setToolbarColor(ContextCompat.getColor(getContext(), R.color.colorPrimary))
.build()
.launchUrl(getContext(), Uri.parse(link));
}
});
TextViewCompat.setTextAppearance(titleView,
R.style.TextAppearance_RocketChat_MessageAttachment_Title_Link);
}
}
}
private void showImageAttachment(Attachment attachment, View attachmentView, boolean autoloadImages) {
final View imageContainer = attachmentView.findViewById(R.id.image_container);
if (attachment.getImageUrl() == null) {
imageContainer.setVisibility(GONE);
return;
private void showReferenceAttachment(Attachment attachment, View attachmentView) {
final View refBox = attachmentView.findViewById(R.id.ref_box);
if (attachment.getThumbUrl() == null && attachment.getText() == null) {
refBox.setVisibility(GONE);
return;
}
refBox.setVisibility(VISIBLE);
final SimpleDraweeView thumbImage = refBox.findViewById(R.id.thumb);
final String thumbUrl = attachment.getThumbUrl();
if (TextUtils.isEmpty(thumbUrl)) {
thumbImage.setVisibility(GONE);
} else {
thumbImage.setVisibility(VISIBLE);
FrescoHelper.INSTANCE.loadImageWithCustomization(thumbImage, absolutize(thumbUrl));
}
final TextView refText = refBox.findViewById(R.id.text);
final String refString = attachment.getText();
if (TextUtils.isEmpty(refString)) {
refText.setVisibility(GONE);
} else {
refText.setVisibility(VISIBLE);
refText.setText(refString);
}
}
imageContainer.setVisibility(VISIBLE);
private void showImageAttachment(Attachment attachment, View attachmentView, boolean autoloadImages) {
final View imageContainer = attachmentView.findViewById(R.id.image_container);
if (attachment.getImageUrl() == null) {
imageContainer.setVisibility(GONE);
return;
}
final SimpleDraweeView attachedImage = attachmentView.findViewById(R.id.image);
final View load = attachmentView.findViewById(R.id.image_load);
imageContainer.setVisibility(VISIBLE);
// Fix for https://fabric.io/rocketchat3/android/apps/chat.rocket.android/issues/59982403be077a4dcc4d7dc3/sessions/599F217000CF00015C771EEF2021AA0F_f9320e3f88fd11e7935256847afe9799_0_v2?
// From: https://github.com/facebook/fresco/issues/1176#issuecomment-216830098
// android.support.v4.content.ContextCompat creates your vector drawable
Drawable placeholderDrawable = ContextCompat.getDrawable(getContext(), R.drawable.image_dummy);
final SimpleDraweeView attachedImage = attachmentView.findViewById(R.id.image);
final View load = attachmentView.findViewById(R.id.image_load);
// Set the placeholder image to the placeholder vector drawable
attachedImage.setHierarchy(
GenericDraweeHierarchyBuilder.newInstance(getResources())
.setPlaceholderImage(placeholderDrawable)
.build());
// Fix for https://fabric.io/rocketchat3/android/apps/chat.rocket.android/issues/59982403be077a4dcc4d7dc3/sessions/599F217000CF00015C771EEF2021AA0F_f9320e3f88fd11e7935256847afe9799_0_v2?
// From: https://github.com/facebook/fresco/issues/1176#issuecomment-216830098
// android.support.v4.content.ContextCompat creates your vector drawable
Drawable placeholderDrawable = ContextCompat.getDrawable(getContext(), R.drawable.image_dummy);
loadImage(absolutize(attachment.getImageUrl()), attachedImage, load, autoloadImages);
}
// Set the placeholder image to the placeholder vector drawable
attachedImage.setHierarchy(
GenericDraweeHierarchyBuilder.newInstance(getResources())
.setPlaceholderImage(placeholderDrawable)
.build());
private void showFieldsAttachment(Attachment attachment, View attachmentView) {
List<AttachmentField> fields = attachment.getAttachmentFields();
if (fields == null || fields.size() == 0) {
return;
loadImage(absolutize(attachment.getImageUrl()), attachedImage, load, autoloadImages);
}
final ViewGroup attachmentContent =
(ViewGroup) attachmentView.findViewById(R.id.attachment_content);
private void showFieldsAttachment(Attachment attachment, View attachmentView) {
List<AttachmentField> fields = attachment.getAttachmentFields();
if (fields == null || fields.size() == 0) {
return;
}
final ViewGroup attachmentContent = attachmentView.findViewById(R.id.attachment_content);
for (int i = 0, size = fields.size(); i < size; i++) {
final AttachmentField attachmentField = fields.get(i);
if (attachmentField.getTitle() == null
|| attachmentField.getText() == null) {
return;
}
MessageAttachmentFieldLayout fieldLayout = new MessageAttachmentFieldLayout(getContext());
fieldLayout.setTitle(attachmentField.getTitle());
fieldLayout.setValue(attachmentField.getText());
for (int i = 0, size = fields.size(); i < size; i++) {
final AttachmentField attachmentField = fields.get(i);
if (attachmentField.getTitle() == null
|| attachmentField.getText() == null) {
return;
}
MessageAttachmentFieldLayout fieldLayout = new MessageAttachmentFieldLayout(getContext());
fieldLayout.setTitle(attachmentField.getTitle());
fieldLayout.setValue(attachmentField.getText());
attachmentContent.addView(fieldLayout);
attachmentContent.addView(fieldLayout);
}
}
private String absolutize(String url) {
if (absoluteUrl == null) {
return url;
}
return absoluteUrl.from(url);
}
}
private String absolutize(String url) {
if (absoluteUrl == null) {
return url;
private void loadImage(final String url, final SimpleDraweeView drawee, final View load,
boolean autoloadImage) {
if (autoloadImage || isCached(Uri.parse(url))) {
load.setVisibility(GONE);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
return;
}
load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
load.setVisibility(GONE);
load.setOnClickListener(null);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
}
});
}
return absoluteUrl.from(url);
}
private void loadImage(final String url, final SimpleDraweeView drawee, final View load,
boolean autoloadImage) {
if (autoloadImage) {
load.setVisibility(GONE);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
return;
private boolean isCached(Uri loadUri) {
if (loadUri == null) {
return false;
}
ImageRequest imageRequest = ImageRequest.fromUri(loadUri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
.getEncodedCacheKey(imageRequest, null);
return ImagePipelineFactory.getInstance()
.getMainFileCache().hasKey(cacheKey);
}
load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
load.setVisibility(GONE);
load.setOnClickListener(null);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
}
});
}
}
......@@ -100,15 +100,15 @@
android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_gravity="start"
android:layout_marginTop="8dp"
android:padding="5dp"
android:background="@drawable/inline_attachment_background">
android:background="@drawable/inline_attachment_background"
android:padding="5dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="200dp"/>
android:layout_height="200dp" />
<TextView
android:id="@+id/image_load"
......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_accent">#FF2D91FA</color>
<color name="colorPrimary">#044b76</color>
<color name="colorPrimaryDark">#FFF</color>
<color name="color_shadow">#FFE6E6E7</color>
<color name="color_icon_composer">#FFA8A8A8</color>
......
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