Browse Source

Fix images not shown in dm

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
b5e66e35bf
  1. 47
      app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageMediaShareViewHolder.java
  2. 25
      app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageMediaViewHolder.java
  3. 23
      app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageRavenMediaViewHolder.java
  4. 19
      app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageReelShareViewHolder.java
  5. 19
      app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageStoryShareViewHolder.java
  6. 9
      app/src/main/java/awais/instagrabber/asyncs/DiscoverFetcher.java
  7. 45
      app/src/main/java/awais/instagrabber/models/direct_messages/DirectItemModel.java
  8. 12
      app/src/main/java/awais/instagrabber/utils/NumberUtils.java
  9. 107
      app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java
  10. 6
      app/src/main/res/layout/fragment_direct_messages_thread.xml
  11. 5
      app/src/main/res/layout/layout_dm_animated_media.xml
  12. 4
      app/src/main/res/layout/layout_dm_media.xml
  13. 2
      app/src/main/res/navigation/direct_messages_nav_graph.xml

47
app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageMediaShareViewHolder.java

@ -2,9 +2,11 @@ package awais.instagrabber.adapters.viewholder.directmessages;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.text.HtmlCompat; import androidx.core.text.HtmlCompat;
import androidx.core.util.Pair;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmBaseBinding;
@ -13,18 +15,24 @@ import awais.instagrabber.models.ProfileModel;
import awais.instagrabber.models.direct_messages.DirectItemModel; import awais.instagrabber.models.direct_messages.DirectItemModel;
import awais.instagrabber.models.direct_messages.DirectItemModel.DirectItemMediaModel; import awais.instagrabber.models.direct_messages.DirectItemModel.DirectItemMediaModel;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.utils.NumberUtils;
import awais.instagrabber.utils.Utils;
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT; import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT;
public class DirectMessageMediaShareViewHolder extends DirectMessageItemViewHolder { public class DirectMessageMediaShareViewHolder extends DirectMessageItemViewHolder {
private final LayoutDmMediaShareBinding binding; private final LayoutDmMediaShareBinding binding;
private final int maxHeight;
private final int maxWidth;
public DirectMessageMediaShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, public DirectMessageMediaShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding,
@NonNull final LayoutDmMediaShareBinding binding, @NonNull final LayoutDmMediaShareBinding binding,
final View.OnClickListener onClickListener) { final View.OnClickListener onClickListener) {
super(baseBinding, onClickListener); super(baseBinding, onClickListener);
this.binding = binding; this.binding = binding;
maxHeight = itemView.getResources().getDimensionPixelSize(R.dimen.dm_media_img_max_height);
maxWidth = (int) (Utils.displayMetrics.widthPixels * 0.8);
setItemView(binding.getRoot()); setItemView(binding.getRoot());
} }
@ -38,9 +46,48 @@ public class DirectMessageMediaShareViewHolder extends DirectMessageItemViewHold
"<small>" + context.getString(R.string.dms_inbox_media_shared_from, modelUser.getUsername()) + "</small>", "<small>" + context.getString(R.string.dms_inbox_media_shared_from, modelUser.getUsername()) + "</small>",
FROM_HTML_MODE_COMPACT)); FROM_HTML_MODE_COMPACT));
} }
final Pair<Integer, Integer> widthHeight = NumberUtils.calculateWidthHeight(
mediaModel.getHeight(),
mediaModel.getWidth(),
maxHeight,
maxWidth
);
final ViewGroup.LayoutParams layoutParams = binding.ivMediaPreview.getLayoutParams();
layoutParams.width = widthHeight.first != null ? widthHeight.first : 0;
layoutParams.height = widthHeight.second != null ? widthHeight.second : 0;
binding.ivMediaPreview.requestLayout();
binding.ivMediaPreview.setImageURI(mediaModel.getThumbUrl()); binding.ivMediaPreview.setImageURI(mediaModel.getThumbUrl());
final MediaItemType modelMediaType = mediaModel.getMediaType(); final MediaItemType modelMediaType = mediaModel.getMediaType();
binding.typeIcon.setVisibility(modelMediaType == MediaItemType.MEDIA_TYPE_VIDEO binding.typeIcon.setVisibility(modelMediaType == MediaItemType.MEDIA_TYPE_VIDEO
|| modelMediaType == MediaItemType.MEDIA_TYPE_SLIDER ? View.VISIBLE : View.GONE); || modelMediaType == MediaItemType.MEDIA_TYPE_SLIDER ? View.VISIBLE : View.GONE);
} }
private class WidthHeight {
private final DirectItemMediaModel mediaModel;
private int height;
private int width;
public WidthHeight(final DirectItemMediaModel mediaModel) {this.mediaModel = mediaModel;}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public WidthHeight invoke() {
height = mediaModel.getHeight();
width = mediaModel.getWidth();
// make height 500dp regardless
width = NumberUtils.getResultingWidth(maxHeight, height, width);
height = maxHeight;
if (width > maxWidth) {
height = NumberUtils.getResultingHeight(maxWidth, height, width);
width = maxWidth;
}
return this;
}
}
} }

25
app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageMediaViewHolder.java

@ -1,32 +1,53 @@
package awais.instagrabber.adapters.viewholder.directmessages; package awais.instagrabber.adapters.viewholder.directmessages;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import awais.instagrabber.R;
import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmBaseBinding;
import awais.instagrabber.databinding.LayoutDmMediaBinding; import awais.instagrabber.databinding.LayoutDmMediaBinding;
import awais.instagrabber.models.direct_messages.DirectItemModel; import awais.instagrabber.models.direct_messages.DirectItemModel;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.utils.NumberUtils;
import awais.instagrabber.utils.Utils;
public class DirectMessageMediaViewHolder extends DirectMessageItemViewHolder { public class DirectMessageMediaViewHolder extends DirectMessageItemViewHolder {
private final LayoutDmMediaBinding binding; private final LayoutDmMediaBinding binding;
private final int maxHeight;
private final int maxWidth;
public DirectMessageMediaViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, public DirectMessageMediaViewHolder(@NonNull final LayoutDmBaseBinding baseBinding,
@NonNull final LayoutDmMediaBinding binding, @NonNull final LayoutDmMediaBinding binding,
final View.OnClickListener onClickListener) { final View.OnClickListener onClickListener) {
super(baseBinding, onClickListener); super(baseBinding, onClickListener);
this.binding = binding; this.binding = binding;
maxHeight = itemView.getResources().getDimensionPixelSize(R.dimen.dm_media_img_max_height);
maxWidth = (int) (Utils.displayMetrics.widthPixels * 0.8);
setItemView(binding.getRoot()); setItemView(binding.getRoot());
} }
@Override @Override
public void bindItem(final DirectItemModel directItemModel) { public void bindItem(final DirectItemModel directItemModel) {
final DirectItemModel.DirectItemMediaModel mediaModel = directItemModel.getMediaModel(); final DirectItemModel.DirectItemMediaModel mediaModel = directItemModel.getMediaModel();
final Pair<Integer, Integer> widthHeight = NumberUtils.calculateWidthHeight(
mediaModel.getHeight(),
mediaModel.getWidth(),
maxHeight,
maxWidth
);
final ViewGroup.LayoutParams layoutParams = binding.ivMediaPreview.getLayoutParams();
layoutParams.width = widthHeight.first != null ? widthHeight.first : 0;
layoutParams.height = widthHeight.second != null ? widthHeight.second : 0;
binding.ivMediaPreview.requestLayout();
binding.ivMediaPreview.requestLayout();
binding.ivMediaPreview.setImageURI(mediaModel.getThumbUrl()); binding.ivMediaPreview.setImageURI(mediaModel.getThumbUrl());
final MediaItemType modelMediaType = mediaModel.getMediaType(); final MediaItemType modelMediaType = mediaModel.getMediaType();
binding.typeIcon.setVisibility(modelMediaType == MediaItemType.MEDIA_TYPE_VIDEO
|| modelMediaType == MediaItemType.MEDIA_TYPE_SLIDER ? View.VISIBLE : View.GONE);
binding.typeIcon.setVisibility(modelMediaType == MediaItemType.MEDIA_TYPE_VIDEO || modelMediaType == MediaItemType.MEDIA_TYPE_SLIDER
? View.VISIBLE
: View.GONE);
} }
} }

23
app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageRavenMediaViewHolder.java

@ -2,8 +2,10 @@ package awais.instagrabber.adapters.viewholder.directmessages;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmBaseBinding;
@ -12,17 +14,23 @@ import awais.instagrabber.models.direct_messages.DirectItemModel;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.models.enums.RavenExpiringMediaType; import awais.instagrabber.models.enums.RavenExpiringMediaType;
import awais.instagrabber.models.enums.RavenMediaViewType; import awais.instagrabber.models.enums.RavenMediaViewType;
import awais.instagrabber.utils.NumberUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
public class DirectMessageRavenMediaViewHolder extends DirectMessageItemViewHolder { public class DirectMessageRavenMediaViewHolder extends DirectMessageItemViewHolder {
private final LayoutDmRavenMediaBinding binding; private final LayoutDmRavenMediaBinding binding;
private final int maxHeight;
private final int maxWidth;
public DirectMessageRavenMediaViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, public DirectMessageRavenMediaViewHolder(@NonNull final LayoutDmBaseBinding baseBinding,
@NonNull final LayoutDmRavenMediaBinding binding, @NonNull final LayoutDmRavenMediaBinding binding,
final View.OnClickListener onClickListener) { final View.OnClickListener onClickListener) {
super(baseBinding, onClickListener); super(baseBinding, onClickListener);
this.binding = binding; this.binding = binding;
maxHeight = itemView.getResources().getDimensionPixelSize(R.dimen.dm_media_img_max_height);
maxWidth = (int) (Utils.displayMetrics.widthPixels * 0.8);
binding.tvMessage.setVisibility(View.GONE); binding.tvMessage.setVisibility(View.GONE);
setItemView(binding.getRoot()); setItemView(binding.getRoot());
} }
@ -72,8 +80,19 @@ public class DirectMessageRavenMediaViewHolder extends DirectMessageItemViewHold
if (ravenMediaViewType == RavenMediaViewType.PERMANENT || ravenMediaViewType == RavenMediaViewType.REPLAYABLE) { if (ravenMediaViewType == RavenMediaViewType.PERMANENT || ravenMediaViewType == RavenMediaViewType.REPLAYABLE) {
final MediaItemType mediaType = mediaModel.getMediaType(); final MediaItemType mediaType = mediaModel.getMediaType();
textRes = -1; textRes = -1;
binding.typeIcon.setVisibility(mediaType == MediaItemType.MEDIA_TYPE_VIDEO ||
mediaType == MediaItemType.MEDIA_TYPE_SLIDER ? View.VISIBLE : View.GONE);
binding.typeIcon.setVisibility(mediaType == MediaItemType.MEDIA_TYPE_VIDEO || mediaType == MediaItemType.MEDIA_TYPE_SLIDER
? View.VISIBLE
: View.GONE);
final Pair<Integer, Integer> widthHeight = NumberUtils.calculateWidthHeight(
mediaModel.getHeight(),
mediaModel.getWidth(),
maxHeight,
maxWidth
);
final ViewGroup.LayoutParams layoutParams = binding.ivMediaPreview.getLayoutParams();
layoutParams.width = widthHeight.first != null ? widthHeight.first : 0;
layoutParams.height = widthHeight.second != null ? widthHeight.second : 0;
binding.ivMediaPreview.requestLayout();
binding.ivMediaPreview.setImageURI(mediaModel.getThumbUrl()); binding.ivMediaPreview.setImageURI(mediaModel.getThumbUrl());
} }
} }

19
app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageReelShareViewHolder.java

@ -1,19 +1,26 @@
package awais.instagrabber.adapters.viewholder.directmessages; package awais.instagrabber.adapters.viewholder.directmessages;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import awais.instagrabber.R;
import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmBaseBinding;
import awais.instagrabber.databinding.LayoutDmRavenMediaBinding; import awais.instagrabber.databinding.LayoutDmRavenMediaBinding;
import awais.instagrabber.interfaces.MentionClickListener; import awais.instagrabber.interfaces.MentionClickListener;
import awais.instagrabber.models.direct_messages.DirectItemModel; import awais.instagrabber.models.direct_messages.DirectItemModel;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.utils.NumberUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
public class DirectMessageReelShareViewHolder extends DirectMessageItemViewHolder { public class DirectMessageReelShareViewHolder extends DirectMessageItemViewHolder {
private final LayoutDmRavenMediaBinding binding; private final LayoutDmRavenMediaBinding binding;
private final int maxHeight;
private final int maxWidth;
public DirectMessageReelShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, public DirectMessageReelShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding,
@NonNull final LayoutDmRavenMediaBinding binding, @NonNull final LayoutDmRavenMediaBinding binding,
@ -21,6 +28,8 @@ public class DirectMessageReelShareViewHolder extends DirectMessageItemViewHolde
final MentionClickListener mentionClickListener) { final MentionClickListener mentionClickListener) {
super(baseBinding, onClickListener); super(baseBinding, onClickListener);
this.binding = binding; this.binding = binding;
maxHeight = itemView.getResources().getDimensionPixelSize(R.dimen.dm_media_img_max_height);
maxWidth = (int) (Utils.displayMetrics.widthPixels * 0.8);
binding.tvMessage.setMentionClickListener(mentionClickListener); binding.tvMessage.setMentionClickListener(mentionClickListener);
setItemView(binding.getRoot()); setItemView(binding.getRoot());
} }
@ -42,6 +51,16 @@ public class DirectMessageReelShareViewHolder extends DirectMessageItemViewHolde
} else { } else {
binding.typeIcon.setVisibility(mediaType == MediaItemType.MEDIA_TYPE_VIDEO || binding.typeIcon.setVisibility(mediaType == MediaItemType.MEDIA_TYPE_VIDEO ||
mediaType == MediaItemType.MEDIA_TYPE_SLIDER ? View.VISIBLE : View.GONE); mediaType == MediaItemType.MEDIA_TYPE_SLIDER ? View.VISIBLE : View.GONE);
final Pair<Integer, Integer> widthHeight = NumberUtils.calculateWidthHeight(
reelShareMedia.getHeight(),
reelShareMedia.getWidth(),
maxHeight,
maxWidth
);
final ViewGroup.LayoutParams layoutParams = binding.ivMediaPreview.getLayoutParams();
layoutParams.width = widthHeight.first != null ? widthHeight.first : 0;
layoutParams.height = widthHeight.second != null ? widthHeight.second : 0;
binding.ivMediaPreview.requestLayout();
binding.ivMediaPreview.setImageURI(reelShareMedia.getThumbUrl()); binding.ivMediaPreview.setImageURI(reelShareMedia.getThumbUrl());
} }
} }

19
app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectMessageStoryShareViewHolder.java

@ -1,27 +1,36 @@
package awais.instagrabber.adapters.viewholder.directmessages; package awais.instagrabber.adapters.viewholder.directmessages;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.text.HtmlCompat; import androidx.core.text.HtmlCompat;
import androidx.core.util.Pair;
import awais.instagrabber.R;
import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmBaseBinding;
import awais.instagrabber.databinding.LayoutDmStoryShareBinding; import awais.instagrabber.databinding.LayoutDmStoryShareBinding;
import awais.instagrabber.models.direct_messages.DirectItemModel; import awais.instagrabber.models.direct_messages.DirectItemModel;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.utils.NumberUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT; import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT;
public class DirectMessageStoryShareViewHolder extends DirectMessageItemViewHolder { public class DirectMessageStoryShareViewHolder extends DirectMessageItemViewHolder {
private final LayoutDmStoryShareBinding binding; private final LayoutDmStoryShareBinding binding;
private final int maxHeight;
private final int maxWidth;
public DirectMessageStoryShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, public DirectMessageStoryShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding,
@NonNull final LayoutDmStoryShareBinding binding, @NonNull final LayoutDmStoryShareBinding binding,
final View.OnClickListener onClickListener) { final View.OnClickListener onClickListener) {
super(baseBinding, onClickListener); super(baseBinding, onClickListener);
this.binding = binding; this.binding = binding;
maxHeight = itemView.getResources().getDimensionPixelSize(R.dimen.dm_media_img_max_height);
maxWidth = (int) (Utils.displayMetrics.widthPixels * 0.8);
binding.tvMessage.setVisibility(View.GONE); binding.tvMessage.setVisibility(View.GONE);
setItemView(binding.getRoot()); setItemView(binding.getRoot());
} }
@ -41,6 +50,16 @@ public class DirectMessageStoryShareViewHolder extends DirectMessageItemViewHold
final DirectItemModel.DirectItemMediaModel reelShareMedia = reelShare.getMedia(); final DirectItemModel.DirectItemMediaModel reelShareMedia = reelShare.getMedia();
final MediaItemType mediaType = reelShareMedia.getMediaType(); final MediaItemType mediaType = reelShareMedia.getMediaType();
binding.typeIcon.setVisibility(mediaType == MediaItemType.MEDIA_TYPE_VIDEO ? View.VISIBLE : View.GONE); binding.typeIcon.setVisibility(mediaType == MediaItemType.MEDIA_TYPE_VIDEO ? View.VISIBLE : View.GONE);
final Pair<Integer, Integer> widthHeight = NumberUtils.calculateWidthHeight(
reelShareMedia.getHeight(),
reelShareMedia.getWidth(),
maxHeight,
maxWidth
);
final ViewGroup.LayoutParams layoutParams = binding.ivMediaPreview.getLayoutParams();
layoutParams.width = widthHeight.first != null ? widthHeight.first : 0;
layoutParams.height = widthHeight.second != null ? widthHeight.second : 0;
binding.ivMediaPreview.requestLayout();
binding.ivMediaPreview.setImageURI(reelShareMedia.getThumbUrl()); binding.ivMediaPreview.setImageURI(reelShareMedia.getThumbUrl());
} }
} }

9
app/src/main/java/awais/instagrabber/asyncs/DiscoverFetcher.java

@ -166,10 +166,11 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
final MediaItemType mediaType = ResponseBodyUtils.getMediaItemType(media.getInt("media_type")); final MediaItemType mediaType = ResponseBodyUtils.getMediaItemType(media.getInt("media_type"));
final ResponseBodyUtils.ThumbnailDetails thumbnailUrl = ResponseBodyUtils.getThumbnailUrl(media, mediaType);
final DiscoverItemModel model = new DiscoverItemModel(mediaType, final DiscoverItemModel model = new DiscoverItemModel(mediaType,
media.getString("pk"), media.getString("pk"),
media.getString("code"), media.getString("code"),
ResponseBodyUtils.getThumbnailUrl(media, mediaType));
thumbnailUrl != null ? thumbnailUrl.url : null);
final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download" + final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download" +
(Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/" + username) : "")); (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/" + username) : ""));
@ -179,9 +180,9 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
if (settingsHelper.getBoolean(FOLDER_SAVE_TO)) { if (settingsHelper.getBoolean(FOLDER_SAVE_TO)) {
final String customPath = settingsHelper.getString(FOLDER_PATH); final String customPath = settingsHelper.getString(FOLDER_PATH);
if (!TextUtils.isEmpty(customPath)) customDir = new File(customPath + if (!TextUtils.isEmpty(customPath)) customDir = new File(customPath +
(Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER)
? "/" + username
: ""));
(Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER)
? "/" + username
: ""));
} }
DownloadUtils.checkExistence(downloadDir, customDir, mediaType == MediaItemType.MEDIA_TYPE_SLIDER, model); DownloadUtils.checkExistence(downloadDir, customDir, mediaType == MediaItemType.MEDIA_TYPE_SLIDER, model);

45
app/src/main/java/awais/instagrabber/models/direct_messages/DirectItemModel.java

@ -139,11 +139,19 @@ public final class DirectItemModel implements Serializable, Comparable<DirectIte
public final static class DirectItemAnimatedMediaModel implements Serializable { public final static class DirectItemAnimatedMediaModel implements Serializable {
private final boolean isRandom, isSticker; private final boolean isRandom, isSticker;
private final String id; private final String id;
private final String gifUrl, webpUrl, mp4Url;
private final String gifUrl;
private final String webpUrl;
private final String mp4Url;
private final int height, width; private final int height, width;
public DirectItemAnimatedMediaModel(final boolean isRandom, final boolean isSticker, final String id, final String gifUrl,
final String webpUrl, final String mp4Url, final int height, final int width) {
public DirectItemAnimatedMediaModel(final boolean isRandom,
final boolean isSticker,
final String id,
final String gifUrl,
final String webpUrl,
final String mp4Url,
final int height,
final int width) {
this.isRandom = isRandom; this.isRandom = isRandom;
this.isSticker = isSticker; this.isSticker = isSticker;
this.id = id; this.id = id;
@ -370,11 +378,24 @@ public final class DirectItemModel implements Serializable, Comparable<DirectIte
public final static class DirectItemMediaModel implements Serializable { public final static class DirectItemMediaModel implements Serializable {
private final MediaItemType mediaType; private final MediaItemType mediaType;
private final long expiringAt, pk; private final long expiringAt, pk;
private final String id, thumbUrl, videoUrl, code;
private final String id;
private final String thumbUrl;
private final String videoUrl;
private final String code;
private final ProfileModel user; private final ProfileModel user;
public DirectItemMediaModel(final MediaItemType mediaType, final long expiringAt, final long pk, final String id,
final String thumbUrl, final String videoUrl, final ProfileModel user, final String code) {
private final int height;
private final int width;
public DirectItemMediaModel(final MediaItemType mediaType,
final long expiringAt,
final long pk,
final String id,
final String thumbUrl,
final String videoUrl,
final ProfileModel user,
final String code,
final int height,
final int width) {
this.mediaType = mediaType; this.mediaType = mediaType;
this.expiringAt = expiringAt; this.expiringAt = expiringAt;
this.pk = pk; this.pk = pk;
@ -383,6 +404,8 @@ public final class DirectItemModel implements Serializable, Comparable<DirectIte
this.videoUrl = videoUrl; this.videoUrl = videoUrl;
this.user = user; this.user = user;
this.code = code; this.code = code;
this.height = height;
this.width = width;
} }
public MediaItemType getMediaType() { public MediaItemType getMediaType() {
@ -419,6 +442,14 @@ public final class DirectItemModel implements Serializable, Comparable<DirectIte
else return thumbUrl; else return thumbUrl;
} }
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
} }
public final static class DirectItemRavenMediaModel implements Serializable { public final static class DirectItemRavenMediaModel implements Serializable {

12
app/src/main/java/awais/instagrabber/utils/NumberUtils.java

@ -1,6 +1,7 @@
package awais.instagrabber.utils; package awais.instagrabber.utils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import java.util.Random; import java.util.Random;
@ -53,4 +54,15 @@ public final class NumberUtils {
} }
return r; return r;
} }
@NonNull
public static Pair<Integer, Integer> calculateWidthHeight(final int height, final int width, final int maxHeight, final int maxWidth) {
int tempWidth = NumberUtils.getResultingWidth(maxHeight, height, width);
int tempHeight = maxHeight;
if (width > maxWidth) {
tempHeight = NumberUtils.getResultingHeight(maxWidth, height, width);
tempWidth = maxWidth;
}
return new Pair<>(tempWidth, tempHeight);
}
} }

107
app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java

@ -10,7 +10,6 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
import awais.instagrabber.BuildConfig; import awais.instagrabber.BuildConfig;
import awais.instagrabber.models.ProfileModel; import awais.instagrabber.models.ProfileModel;
@ -24,6 +23,8 @@ import awais.instagrabber.models.enums.RavenMediaViewType;
import awaisomereport.LogCollector; import awaisomereport.LogCollector;
public final class ResponseBodyUtils { public final class ResponseBodyUtils {
private static final String TAG = "ResponseBodyUtils";
// isI: true if the content was requested from i.instagram.com instead of graphql // isI: true if the content was requested from i.instagram.com instead of graphql
@Nullable @Nullable
public static String getHighQualityPost(final JSONArray resources, final boolean isVideo, final boolean isI, final boolean low) { public static String getHighQualityPost(final JSONArray resources, final boolean isVideo, final boolean isI, final boolean low) {
@ -103,37 +104,38 @@ public final class ResponseBodyUtils {
return src; return src;
} }
public static String getItemThumbnail(@NonNull final JSONArray jsonArray) {
String thumbnail = null;
public static ThumbnailDetails getItemThumbnail(@NonNull final JSONArray jsonArray) {
final ThumbnailDetails thumbnailDetails = new ThumbnailDetails();
final int imageResLen = jsonArray.length(); final int imageResLen = jsonArray.length();
for (int i = 0; i < imageResLen; ++i) { for (int i = 0; i < imageResLen; ++i) {
final JSONObject imageResource = jsonArray.optJSONObject(i); final JSONObject imageResource = jsonArray.optJSONObject(i);
try { try {
final int width = imageResource.getInt("width");
final int height = imageResource.getInt("height");
final float ratio = Float.parseFloat(String.format(Locale.ENGLISH, "%.2f", (float) height / width));
if (ratio >= 0.95f && ratio <= 1.0f) {
thumbnail = imageResource.getString("url");
break;
}
// final float ratio = (float) height / width;
// if (ratio >= 0.95f && ratio <= 1.0f) {
thumbnailDetails.height = imageResource.getInt("height");
thumbnailDetails.width = imageResource.getInt("width");
thumbnailDetails.url = imageResource.getString("url");
break;
// }
} catch (final Exception e) { } catch (final Exception e) {
if (Utils.logCollector != null) if (Utils.logCollector != null)
Utils.logCollector.appendException(e, LogCollector.LogFile.UTILS, "getItemThumbnail"); Utils.logCollector.appendException(e, LogCollector.LogFile.UTILS, "getItemThumbnail");
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
thumbnail = null;
if (BuildConfig.DEBUG) Log.e(TAG, "", e);
} }
} }
// if (TextUtils.isEmpty(thumbnail)) thumbnail = jsonArray.optJSONObject(0).optString("url");
return thumbnailDetails;
}
if (TextUtils.isEmpty(thumbnail)) thumbnail = jsonArray.optJSONObject(0).optString("url");
return thumbnail;
public static class ThumbnailDetails {
int width;
int height;
public String url;
} }
@Nullable @Nullable
public static String getThumbnailUrl(@NonNull final JSONObject mediaObj, final MediaItemType mediaType) throws Exception {
String thumbnail = null;
public static ThumbnailDetails getThumbnailUrl(@NonNull final JSONObject mediaObj, final MediaItemType mediaType) throws Exception {
ThumbnailDetails thumbnail = null;
if (mediaType == MediaItemType.MEDIA_TYPE_IMAGE || mediaType == MediaItemType.MEDIA_TYPE_VIDEO) { if (mediaType == MediaItemType.MEDIA_TYPE_IMAGE || mediaType == MediaItemType.MEDIA_TYPE_VIDEO) {
final JSONObject imageVersions = mediaObj.optJSONObject("image_versions2"); final JSONObject imageVersions = mediaObj.optJSONObject("image_versions2");
if (imageVersions != null) if (imageVersions != null)
@ -143,19 +145,18 @@ public final class ResponseBodyUtils {
final JSONArray carouselMedia = mediaObj.optJSONArray("carousel_media"); final JSONArray carouselMedia = mediaObj.optJSONArray("carousel_media");
if (carouselMedia != null) if (carouselMedia != null)
thumbnail = getItemThumbnail(carouselMedia.getJSONObject(0) thumbnail = getItemThumbnail(carouselMedia.getJSONObject(0)
.getJSONObject("image_versions2").getJSONArray("candidates"));
.getJSONObject("image_versions2")
.getJSONArray("candidates"));
} }
return thumbnail; return thumbnail;
} }
public static String getVideoUrl(@NonNull final JSONObject mediaObj) throws Exception {
public static String getVideoUrl(@NonNull final JSONObject mediaObj) {
String thumbnail = null; String thumbnail = null;
final JSONArray imageVersions = mediaObj.optJSONArray("video_versions"); final JSONArray imageVersions = mediaObj.optJSONArray("video_versions");
if (imageVersions != null)
thumbnail = getItemThumbnail(imageVersions);
if (imageVersions != null) {
thumbnail = getItemThumbnail(imageVersions).url;
}
return thumbnail; return thumbnail;
} }
@ -193,14 +194,18 @@ public final class ResponseBodyUtils {
String id = mediaObj.optString("id"); String id = mediaObj.optString("id");
if (TextUtils.isEmpty(id)) id = null; if (TextUtils.isEmpty(id)) id = null;
mediaModel = new DirectItemModel.DirectItemMediaModel(mediaType,
mediaObj.optLong("expiring_at"),
mediaObj.optLong("pk"),
id,
getThumbnailUrl(mediaObj, mediaType),
mediaType == MediaItemType.MEDIA_TYPE_VIDEO ? getVideoUrl(mediaObj) : null,
user,
mediaObj.optString("code"));
final ThumbnailDetails thumbnailDetails = getThumbnailUrl(mediaObj, mediaType);
mediaModel = new DirectItemModel.DirectItemMediaModel(
mediaType,
mediaObj.optLong("expiring_at"),
mediaObj.optLong("pk"),
id,
thumbnailDetails != null ? thumbnailDetails.url : null,
mediaType == MediaItemType.MEDIA_TYPE_VIDEO ? getVideoUrl(mediaObj) : null,
user,
mediaObj.optString("code"),
thumbnailDetails != null ? thumbnailDetails.height : 0,
thumbnailDetails != null ? thumbnailDetails.width : 0);
} }
return mediaModel; return mediaModel;
} }
@ -314,14 +319,15 @@ public final class ResponseBodyUtils {
final JSONObject animatedMedia = itemObject.getJSONObject("animated_media"); final JSONObject animatedMedia = itemObject.getJSONObject("animated_media");
final JSONObject stickerImage = animatedMedia.getJSONObject("images").getJSONObject("fixed_height"); final JSONObject stickerImage = animatedMedia.getJSONObject("images").getJSONObject("fixed_height");
animatedMediaModel = new DirectItemModel.DirectItemAnimatedMediaModel(animatedMedia.getBoolean("is_random"),
animatedMedia.getBoolean("is_sticker"),
animatedMedia.getString("id"),
stickerImage.getString("url"),
stickerImage.optString("webp"),
stickerImage.optString("mp4"),
stickerImage.getInt("height"),
stickerImage.getInt("width"));
animatedMediaModel = new DirectItemModel.DirectItemAnimatedMediaModel(
animatedMedia.getBoolean("is_random"),
animatedMedia.getBoolean("is_sticker"),
animatedMedia.getString("id"),
stickerImage.getString("url"),
stickerImage.optString("webp"),
stickerImage.optString("mp4"),
stickerImage.getInt("height"),
stickerImage.getInt("width"));
} }
break; break;
@ -340,9 +346,11 @@ public final class ResponseBodyUtils {
} }
} }
voiceMediaModel = new DirectItemModel.DirectItemVoiceMediaModel(voiceMedia.getString("id"),
audio.getString("audio_src"), audio.getLong("duration"),
waveformData);
voiceMediaModel = new DirectItemModel.DirectItemVoiceMediaModel(
voiceMedia.getString("id"),
audio.getString("audio_src"),
audio.getLong("duration"),
waveformData);
} }
break; break;
@ -360,10 +368,11 @@ public final class ResponseBodyUtils {
); );
} }
linkModel = new DirectItemModel.DirectItemLinkModel(linkObj.getString("text"),
linkObj.getString("client_context"),
linkObj.optString("mutation_token"),
itemLinkContext);
linkModel = new DirectItemModel.DirectItemLinkModel(
linkObj.getString("text"),
linkObj.getString("client_context"),
linkObj.optString("mutation_token"),
itemLinkContext);
} }
break; break;

6
app/src/main/res/layout/fragment_direct_messages_thread.xml

@ -1,22 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="?actionBarSize"
android:orientation="vertical"> android:orientation="vertical">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout" android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"> android:layout_weight="1">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageList" android:id="@+id/messageList"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:listitem="@layout/layout_dm_base"/>
tools:listitem="@layout/layout_dm_base" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout <LinearLayout

5
app/src/main/res/layout/layout_dm_animated_media.xml

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<com.facebook.drawee.view.SimpleDraweeView xmlns:android="http://schemas.android.com/apk/res/android" <com.facebook.drawee.view.SimpleDraweeView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ivAnimatedMessage" android:id="@+id/ivAnimatedMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="@dimen/dm_media_img_max_height" />
android:layout_width="match_parent"
android:layout_height="@dimen/dm_media_img_max_height" />

4
app/src/main/res/layout/layout_dm_media.xml

@ -7,9 +7,7 @@
<com.facebook.drawee.view.SimpleDraweeView <com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/ivMediaPreview" android:id="@+id/ivMediaPreview"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="@dimen/dm_media_img_max_height" />
android:layout_height="wrap_content" />
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/typeIcon" android:id="@+id/typeIcon"

2
app/src/main/res/navigation/direct_messages_nav_graph.xml

@ -13,7 +13,7 @@
<argument <argument
android:name="username" android:name="username"
app:argType="string" app:argType="string"
app:nullable="false" />
app:nullable="true" />
</action> </action>
<include app:graph="@navigation/location_nav_graph" /> <include app:graph="@navigation/location_nav_graph" />

Loading…
Cancel
Save