Commit 9959d848 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Make sure RocketChatAbsoluteUrl is loaded and set up before fetching messages

parent b6551f7d
......@@ -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
);
......
......@@ -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())
......
......@@ -17,8 +17,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;
......@@ -131,7 +135,7 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
FrescoHelper.INSTANCE.loadImageWithCustomization((SimpleDraweeView) attachmentView.findViewById(R.id.author_icon), absolutize(author.getIconUrl()));
final TextView authorName = (TextView) attachmentView.findViewById(R.id.author_name);
final TextView authorName = attachmentView.findViewById(R.id.author_name);
authorName.setText(author.getName());
final String link = absolutize(author.getLink());
......@@ -148,7 +152,7 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
}
private void showTitleAttachment(Attachment attachment, View attachmentView) {
TextView titleView = (TextView) attachmentView.findViewById(R.id.title);
TextView titleView = attachmentView.findViewById(R.id.title);
AttachmentTitle title = attachment.getAttachmentTitle();
if (title == null || title.getTitle() == null) {
titleView.setVisibility(View.GONE);
......@@ -189,7 +193,7 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
refBox.setVisibility(VISIBLE);
final SimpleDraweeView thumbImage = (SimpleDraweeView) refBox.findViewById(R.id.thumb);
final SimpleDraweeView thumbImage = refBox.findViewById(R.id.thumb);
final String thumbUrl = attachment.getThumbUrl();
if (TextUtils.isEmpty(thumbUrl)) {
......@@ -199,7 +203,7 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
FrescoHelper.INSTANCE.loadImageWithCustomization(thumbImage, absolutize(thumbUrl));
}
final TextView refText = (TextView) refBox.findViewById(R.id.text);
final TextView refText = refBox.findViewById(R.id.text);
final String refString = attachment.getText();
if (TextUtils.isEmpty(refString)) {
......@@ -242,8 +246,7 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
return;
}
final ViewGroup attachmentContent =
(ViewGroup) attachmentView.findViewById(R.id.attachment_content);
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);
......@@ -268,7 +271,7 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
private void loadImage(final String url, final SimpleDraweeView drawee, final View load,
boolean autoloadImage) {
if (autoloadImage) {
if (autoloadImage || isCached(Uri.parse(url))) {
load.setVisibility(GONE);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
return;
......@@ -283,4 +286,16 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
}
});
}
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);
}
}
......@@ -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"
......
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