1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57:29 +00:00

open on giphy & download voice; close #792

This commit is contained in:
Austin Huang 2021-03-16 16:51:50 -04:00
parent ac9b5385d2
commit 22a927e9e7
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
5 changed files with 82 additions and 38 deletions

View File

@ -543,6 +543,15 @@ public abstract class DirectItemViewHolder extends RecyclerView.ViewHolder imple
if (thread.getInputMode() != 1 && messageDirection == MessageDirection.OUTGOING) { if (thread.getInputMode() != 1 && messageDirection == MessageDirection.OUTGOING) {
builder.add(new DirectItemContextMenu.MenuItem(R.id.unsend, R.string.dms_inbox_unsend)); builder.add(new DirectItemContextMenu.MenuItem(R.id.unsend, R.string.dms_inbox_unsend));
} }
final DirectItemType itemType = item.getItemType();
switch (itemType) {
case ANIMATED_MEDIA:
builder.add(new DirectItemContextMenu.MenuItem(R.id.detail, R.string.dms_inbox_giphy));
break;
case VOICE_MEDIA:
builder.add(new DirectItemContextMenu.MenuItem(R.id.detail, R.string.action_download));
break;
}
final boolean showReactions = thread.getInputMode() != 1 && allowReaction(); final boolean showReactions = thread.getInputMode() != 1 && allowReaction();
final ImmutableList<DirectItemContextMenu.MenuItem> menuItems = builder.build(); final ImmutableList<DirectItemContextMenu.MenuItem> menuItems = builder.build();
if (!showReactions && menuItems.isEmpty()) return; if (!showReactions && menuItems.isEmpty()) return;

View File

@ -30,6 +30,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData; import androidx.lifecycle.MediatorLiveData;
@ -57,6 +58,7 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import awais.instagrabber.ProfileNavGraphDirections; import awais.instagrabber.ProfileNavGraphDirections;
import awais.instagrabber.R; import awais.instagrabber.R;
@ -86,6 +88,7 @@ import awais.instagrabber.fragments.PostViewV2Fragment;
import awais.instagrabber.fragments.UserSearchFragment; import awais.instagrabber.fragments.UserSearchFragment;
import awais.instagrabber.fragments.UserSearchFragmentDirections; import awais.instagrabber.fragments.UserSearchFragmentDirections;
import awais.instagrabber.models.Resource; import awais.instagrabber.models.Resource;
import awais.instagrabber.models.enums.DirectItemType;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.repositories.requests.StoryViewerOptions; import awais.instagrabber.repositories.requests.StoryViewerOptions;
import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.Media;
@ -97,6 +100,7 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItemVisual
import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.repositories.responses.directmessages.RankedRecipient; import awais.instagrabber.repositories.responses.directmessages.RankedRecipient;
import awais.instagrabber.utils.AppExecutors; import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.utils.DownloadUtils;
import awais.instagrabber.utils.PermissionUtils; import awais.instagrabber.utils.PermissionUtils;
import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.ResponseBodyUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
@ -121,6 +125,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
private ConstraintLayout root; private ConstraintLayout root;
private boolean shouldRefresh = true; private boolean shouldRefresh = true;
private List<DirectItemOrHeader> itemOrHeaders; private List<DirectItemOrHeader> itemOrHeaders;
private List<User> users;
private FragmentDirectMessagesThreadBinding binding; private FragmentDirectMessagesThreadBinding binding;
private Tooltip tooltip; private Tooltip tooltip;
private float initialSendX; private float initialSendX;
@ -258,6 +263,19 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
final NavController navController = NavHostFragment.findNavController(DirectMessageThreadFragment.this); final NavController navController = NavHostFragment.findNavController(DirectMessageThreadFragment.this);
navController.navigate(actionGlobalUserSearch); navController.navigate(actionGlobalUserSearch);
} }
if (itemId == R.id.detail) {
final Context context = getContext();
if (context == null) return;
final DirectItemType itemType = item.getItemType();
switch (itemType) {
case ANIMATED_MEDIA:
Utils.openURL(context, "https://giphy.com/gifs/" + item.getAnimatedMedia().getId());
break;
case VOICE_MEDIA:
downloadItem(item.getVoiceMedia() == null ? null : item.getVoiceMedia().getMedia(), context);
break;
}
}
} }
}; };
private final DirectItemLongClickListener directItemLongClickListener = position -> { private final DirectItemLongClickListener directItemLongClickListener = position -> {
@ -906,6 +924,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
itemsAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY); itemsAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
binding.chats.setAdapter(itemsAdapter); binding.chats.setAdapter(itemsAdapter);
registerDataObserver(); registerDataObserver();
users = thread.getUsers();
final List<DirectItem> items = viewModel.getItems().getValue(); final List<DirectItem> items = viewModel.getItems().getValue();
if (items != null && itemsAdapter.getItems() != items) { if (items != null && itemsAdapter.getItems() != items) {
submitItemsToAdapter(items); submitItemsToAdapter(items);
@ -1230,25 +1249,19 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
appExecutors.mainThread().execute(prevTitleRunnable, 1000); appExecutors.mainThread().execute(prevTitleRunnable, 1000);
} }
// private void downloadItem(final Context context) { // currently ONLY for voice
// final DirectUser user = getUser(directItemModel.getUserId()); private void downloadItem(final Media media, final Context context) {
// final DirectItemMediaModel selectedItem = directItemModel.getItemType() == DirectItemType.MEDIA if (media == null) {
// ? (DirectItemMediaModel) directItemModel.getMediaModel() Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
// : ((DirectItemRavenMediaModel) directItemModel.getMediaModel()).getMedia(); } else {
// final String url = selectedItem.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) {
// ? selectedItem.getVideoUrl() DownloadUtils.download(context, media);
// : selectedItem.getThumbUrl(); } else {
// if (url == null) { requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE);
// Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); }
// } else { Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show();
// if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) { }
// DownloadUtils.dmDownload(context, user != null ? user.getUsername() : "", selectedItem.getId(), url); }
// } else {
// requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE);
// }
// Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show();
// }
// }
// private void sendText(final String text, final String itemId, final boolean delete) { // private void sendText(final String text, final String itemId, final boolean delete) {
// DirectThreadBroadcaster.TextBroadcastOptions textOptions = null; // DirectThreadBroadcaster.TextBroadcastOptions textOptions = null;
@ -1339,14 +1352,14 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
// broadcaster.execute(broadcastOptions); // broadcaster.execute(broadcastOptions);
// } // }
// @NonNull @NonNull
// private DirectUser getUser(final long userId) { private User getUser(final long userId) {
// for (final DirectUser user : users) { for (final User user : users) {
// if (userId != user.getPk()) continue; if (userId != user.getPk()) continue;
// return user; return user;
// } }
// return null; return null;
// } }
// private void searchUsername(final String text) { // private void searchUsername(final String text) {
// final Bundle bundle = new Bundle(); // final Bundle bundle = new Bundle();

View File

@ -39,6 +39,7 @@ import java.util.regex.Pattern;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.models.StoryModel; import awais.instagrabber.models.StoryModel;
import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.repositories.responses.Audio;
import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.Media;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.VideoVersion; import awais.instagrabber.repositories.responses.VideoVersion;
@ -89,17 +90,17 @@ public final class DownloadUtils {
return dir; return dir;
} }
public static void dmDownload(@NonNull final Context context, // public static void dmDownload(@NonNull final Context context,
@Nullable final String username, // @Nullable final String username,
final String modelId, // final String modelId,
final String url) { // final String url) {
if (url == null) return; // if (url == null) return;
if (ContextCompat.checkSelfPermission(context, PERMS[0]) == PackageManager.PERMISSION_GRANTED) { // if (ContextCompat.checkSelfPermission(context, PERMS[0]) == PackageManager.PERMISSION_GRANTED) {
dmDownloadImpl(context, username, modelId, url); // dmDownloadImpl(context, username, modelId, url);
} else if (context instanceof Activity) { // } else if (context instanceof Activity) {
ActivityCompat.requestPermissions((Activity) context, PERMS, 8020); // ActivityCompat.requestPermissions((Activity) context, PERMS, 8020);
} // }
} // }
private static void dmDownloadImpl(@NonNull final Context context, private static void dmDownloadImpl(@NonNull final Context context,
@Nullable final String username, @Nullable final String username,
@ -303,6 +304,17 @@ public final class DownloadUtils {
map.put(url, file.getAbsolutePath()); map.put(url, file.getAbsolutePath());
break; break;
} }
case MEDIA_TYPE_VOICE: {
final String url = getUrlOfType(media);
String fileName = media.getId();
final User user = media.getUser();
if (user != null) {
fileName = user.getUsername() + "_" + fileName;
}
final File file = getDownloadSaveFile(downloadDir, fileName, url);
map.put(url, file.getAbsolutePath());
break;
}
case MEDIA_TYPE_SLIDER: case MEDIA_TYPE_SLIDER:
final List<Media> sliderItems = media.getCarouselMedia(); final List<Media> sliderItems = media.getCarouselMedia();
for (int i = 0; i < sliderItems.size(); i++) { for (int i = 0; i < sliderItems.size(); i++) {
@ -338,6 +350,14 @@ public final class DownloadUtils {
} }
return url; return url;
} }
case MEDIA_TYPE_VOICE: {
final Audio audio = media.getAudio();
String url = null;
if (audio != null) {
url = audio.getAudioSrc();
}
return url;
}
} }
return null; return null;
} }

View File

@ -3,5 +3,6 @@
<item name="reply" type="id" /> <item name="reply" type="id" />
<item name="unsend" type="id" /> <item name="unsend" type="id" />
<item name="forward" type="id" /> <item name="forward" type="id" />
<item name="detail" type="id" />
<item name="pending_requests" type="id" /> <item name="pending_requests" type="id" />
</resources> </resources>

View File

@ -180,6 +180,7 @@
<string name="dms_inbox_unlike">Unlike message</string> <string name="dms_inbox_unlike">Unlike message</string>
<string name="dms_inbox_unsend">Unsend message</string> <string name="dms_inbox_unsend">Unsend message</string>
<string name="dms_inbox_author">View author profile</string> <string name="dms_inbox_author">View author profile</string>
<string name="dms_inbox_giphy">View on GIPHY</string>
<string name="dms_inbox_shared_post">%s shared a post by @%s</string> <string name="dms_inbox_shared_post">%s shared a post by @%s</string>
<string name="dms_inbox_shared_image">%s shared an image</string> <string name="dms_inbox_shared_image">%s shared an image</string>
<string name="dms_inbox_shared_video">%s shared a video</string> <string name="dms_inbox_shared_video">%s shared a video</string>