diff --git a/app/build.gradle b/app/build.gradle index 9b39a1a2..f6096214 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -105,7 +105,7 @@ dependencies { def nav_version = '2.3.4' def exoplayer_version = '2.13.2' - implementation 'com.google.android.material:material:1.4.0-alpha01' + implementation 'com.google.android.material:material:1.4.0-alpha02' implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version" implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version" diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 95c801ef..5a464bd1 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -47,6 +47,7 @@ import androidx.navigation.ui.NavigationUI; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; +import com.google.android.material.badge.BadgeDrawable; import com.google.android.material.behavior.HideBottomViewOnScrollBehavior; import com.google.android.material.bottomnavigation.BottomNavigationView; @@ -83,6 +84,7 @@ import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; import awais.instagrabber.webservices.RetrofitFactory; +import awais.instagrabber.viewmodels.DirectInboxViewModel; import awais.instagrabber.webservices.SearchService; import retrofit2.Call; import retrofit2.Callback; @@ -183,6 +185,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage initEmojiCompat(); searchService = SearchService.getInstance(); // initDmService(); + initDmUnreadCount(); } private void initDmService() { @@ -192,6 +195,16 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage DMSyncAlarmReceiver.setAlarm(this); } + private void initDmUnreadCount() { + if (!isLoggedIn) return; + final DirectInboxViewModel directInboxViewModel = new ViewModelProvider(this).get(DirectInboxViewModel.class); + directInboxViewModel.getUnseenCount().observe(this, unseenCountResource -> { + if (unseenCountResource == null) return; + final Integer unseenCount = unseenCountResource.data; + setNavBarDMUnreadCountBadge(unseenCount == null ? 0 : unseenCount); + }); + } + @Override public boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); @@ -857,4 +870,19 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage public View getRootView() { return binding.getRoot(); } + + private void setNavBarDMUnreadCountBadge(final int unseenCount) { + final BadgeDrawable badge = binding.bottomNavView.getOrCreateBadge(R.id.direct_messages_nav_graph); + if (badge == null) return; + if (unseenCount == 0) { + badge.setVisible(false); + badge.clearNumber(); + return; + } + if (badge.getVerticalOffset() != 10) { + badge.setVerticalOffset(10); + } + badge.setNumber(unseenCount); + badge.setVisible(true); + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java b/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java index e1469ac5..2b1f410e 100644 --- a/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; +import java.util.function.Function; import awais.instagrabber.adapters.viewholder.directmessages.DirectItemActionLogViewHolder; import awais.instagrabber.adapters.viewholder.directmessages.DirectItemAnimatedMediaViewHolder; @@ -404,7 +405,7 @@ public final class DirectItemsAdapter extends RecyclerView.Adapter callback); } public interface DirectItemInternalLongClickListener { diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemAnimatedMediaViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemAnimatedMediaViewHolder.java index c08bb1d9..7bc5d173 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemAnimatedMediaViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemAnimatedMediaViewHolder.java @@ -8,8 +8,13 @@ import androidx.core.util.Pair; import androidx.recyclerview.widget.ItemTouchHelper; import com.facebook.drawee.backends.pipeline.Fresco; +import com.google.common.collect.ImmutableList; +import java.util.List; + +import awais.instagrabber.R; import awais.instagrabber.adapters.DirectItemsAdapter.DirectItemCallback; +import awais.instagrabber.customviews.DirectItemContextMenu; import awais.instagrabber.databinding.LayoutDmAnimatedMediaBinding; import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.repositories.responses.AnimatedMediaFixedHeight; @@ -19,6 +24,7 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectItemAnimatedMedia; import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.utils.NumberUtils; +import awais.instagrabber.utils.Utils; public class DirectItemAnimatedMediaViewHolder extends DirectItemViewHolder { @@ -65,4 +71,14 @@ public class DirectItemAnimatedMediaViewHolder extends DirectItemViewHolder { public int getSwipeDirection() { return ItemTouchHelper.ACTION_STATE_IDLE; } + + @Override + protected List getLongClickOptions() { + return ImmutableList.of( + new DirectItemContextMenu.MenuItem(R.id.detail, R.string.dms_inbox_giphy, item -> { + Utils.openURL(itemView.getContext(), "https://giphy.com/gifs/" + item.getAnimatedMedia().getId()); + return null; + }) + ); + } } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemLinkViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemLinkViewHolder.java index 2129cc2b..98c9fb14 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemLinkViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemLinkViewHolder.java @@ -5,7 +5,13 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +import awais.instagrabber.R; import awais.instagrabber.adapters.DirectItemsAdapter.DirectItemCallback; +import awais.instagrabber.customviews.DirectItemContextMenu; import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmLinkBinding; import awais.instagrabber.repositories.responses.User; @@ -14,6 +20,7 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItemLink; import awais.instagrabber.repositories.responses.directmessages.DirectItemLinkContext; import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.utils.TextUtils; +import awais.instagrabber.utils.Utils; public class DirectItemLinkViewHolder extends DirectItemViewHolder { @@ -80,4 +87,16 @@ public class DirectItemLinkViewHolder extends DirectItemViewHolder { protected boolean showBackground() { return true; } + + @Override + protected List getLongClickOptions() { + return ImmutableList.of( + new DirectItemContextMenu.MenuItem(R.id.copy, R.string.copy, item -> { + final DirectItemLink link = item.getLink(); + if (link == null || TextUtils.isEmpty(link.getText())) return null; + Utils.copyText(itemView.getContext(), link.getText()); + return null; + }) + ); + } } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java index 8874e818..dddd9315 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java @@ -12,11 +12,14 @@ import androidx.recyclerview.widget.ItemTouchHelper; import com.facebook.drawee.drawable.ScalingUtils; import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder; import com.facebook.drawee.generic.RoundingParams; +import com.google.common.collect.ImmutableList; +import java.util.List; import java.util.Objects; import awais.instagrabber.R; import awais.instagrabber.adapters.DirectItemsAdapter.DirectItemCallback; +import awais.instagrabber.customviews.DirectItemContextMenu; import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmMediaShareBinding; import awais.instagrabber.models.enums.DirectItemType; @@ -30,6 +33,7 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItemFelixS import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.utils.NumberUtils; import awais.instagrabber.utils.ResponseBodyUtils; +import awais.instagrabber.utils.Utils; public class DirectItemMediaShareViewHolder extends DirectItemViewHolder { private static final String TAG = DirectItemMediaShareViewHolder.class.getSimpleName(); @@ -38,6 +42,7 @@ public class DirectItemMediaShareViewHolder extends DirectItemViewHolder { private final RoundingParams incomingRoundingParams; private final RoundingParams outgoingRoundingParams; private DirectItemType itemType; + private Caption caption; public DirectItemMediaShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, @NonNull final LayoutDmMediaShareBinding binding, @@ -113,7 +118,7 @@ public class DirectItemMediaShareViewHolder extends DirectItemViewHolder { } private void setupCaption(@NonNull final Media media) { - final Caption caption = media.getCaption(); + caption = media.getCaption(); if (caption != null) { binding.caption.setVisibility(View.VISIBLE); binding.caption.setText(caption.getText()); @@ -177,4 +182,16 @@ public class DirectItemMediaShareViewHolder extends DirectItemViewHolder { } return super.getSwipeDirection(); } + + @Override + protected List getLongClickOptions() { + final ImmutableList.Builder builder = ImmutableList.builder(); + if (caption != null && !TextUtils.isEmpty(caption.getText())) { + builder.add(new DirectItemContextMenu.MenuItem(R.id.copy, R.string.copy_caption, item -> { + Utils.copyText(itemView.getContext(), caption.getText()); + return null; + })); + } + return builder.build(); + } } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java index bb139332..aa1666ab 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java @@ -8,13 +8,16 @@ import androidx.constraintlayout.widget.ConstraintLayout; import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder; import com.facebook.drawee.generic.RoundingParams; +import com.google.common.collect.ImmutableList; + +import java.util.List; import awais.instagrabber.R; import awais.instagrabber.adapters.DirectItemsAdapter.DirectItemCallback; +import awais.instagrabber.customviews.DirectItemContextMenu; import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmReelShareBinding; import awais.instagrabber.models.enums.MediaItemType; -import awais.instagrabber.repositories.responses.ImageVersions2; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.directmessages.DirectItem; @@ -22,10 +25,12 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItemReelSh import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.TextUtils; +import awais.instagrabber.utils.Utils; public class DirectItemReelShareViewHolder extends DirectItemViewHolder { private final LayoutDmReelShareBinding binding; + private String type; public DirectItemReelShareViewHolder(@NonNull final LayoutDmBaseBinding baseBinding, @NonNull final LayoutDmReelShareBinding binding, @@ -40,7 +45,7 @@ public class DirectItemReelShareViewHolder extends DirectItemViewHolder { @Override public void bindItem(final DirectItem item, final MessageDirection messageDirection) { final DirectItemReelShare reelShare = item.getReelShare(); - final String type = reelShare.getType(); + type = reelShare.getType(); if (type == null) return; final boolean isSelf = isSelf(item); final Media media = reelShare.getMedia(); @@ -170,4 +175,20 @@ public class DirectItemReelShareViewHolder extends DirectItemViewHolder { protected boolean canForward() { return false; } + + @Override + protected List getLongClickOptions() { + final ImmutableList.Builder builder = ImmutableList.builder(); + if (type != null && type.equals("reply")) { + builder.add(new DirectItemContextMenu.MenuItem(R.id.copy, R.string.copy_reply, item -> { + final DirectItemReelShare reelShare = item.getReelShare(); + if (reelShare == null) return null; + final String text = reelShare.getText(); + if (TextUtils.isEmpty(text)) return null; + Utils.copyText(itemView.getContext(), text); + return null; + })); + } + return builder.build(); + } } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemTextViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemTextViewHolder.java index b0eaa779..d95817ea 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemTextViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemTextViewHolder.java @@ -2,12 +2,20 @@ package awais.instagrabber.adapters.viewholder.directmessages; import androidx.annotation.NonNull; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +import awais.instagrabber.R; import awais.instagrabber.adapters.DirectItemsAdapter.DirectItemCallback; +import awais.instagrabber.customviews.DirectItemContextMenu; import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmTextBinding; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectThread; +import awais.instagrabber.utils.TextUtils; +import awais.instagrabber.utils.Utils; public class DirectItemTextViewHolder extends DirectItemViewHolder { @@ -35,4 +43,15 @@ public class DirectItemTextViewHolder extends DirectItemViewHolder { protected boolean showBackground() { return true; } + + @Override + protected List getLongClickOptions() { + return ImmutableList.of( + new DirectItemContextMenu.MenuItem(R.id.copy, R.string.copy, item -> { + if (TextUtils.isEmpty(item.getText())) return null; + Utils.copyText(itemView.getContext(), item.getText()); + return null; + }) + ); + } } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java index 15814b3c..96476b10 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java @@ -543,22 +543,13 @@ public abstract class DirectItemViewHolder extends RecyclerView.ViewHolder imple if (thread.getInputMode() != 1 && messageDirection == MessageDirection.OUTGOING) { 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 ImmutableList menuItems = builder.build(); if (!showReactions && menuItems.isEmpty()) return; final DirectItemContextMenu menu = new DirectItemContextMenu(itemView.getContext(), showReactions, menuItems); menu.setOnDismissListener(() -> setSelected(false)); menu.setOnReactionClickListener(emoji -> callback.onReaction(item, emoji)); - menu.setOnOptionSelectListener(itemId -> callback.onOptionSelect(item, itemId)); + menu.setOnOptionSelectListener((itemId, cb) -> callback.onOptionSelect(item, itemId, cb)); menu.show(itemView, location); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemVoiceMediaViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemVoiceMediaViewHolder.java index 13399b06..255cceae 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemVoiceMediaViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemVoiceMediaViewHolder.java @@ -12,12 +12,14 @@ import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.common.collect.ImmutableList; import com.google.common.primitives.Floats; import java.util.List; import awais.instagrabber.R; import awais.instagrabber.adapters.DirectItemsAdapter.DirectItemCallback; +import awais.instagrabber.customviews.DirectItemContextMenu; import awais.instagrabber.databinding.LayoutDmBaseBinding; import awais.instagrabber.databinding.LayoutDmVoiceMediaBinding; import awais.instagrabber.repositories.responses.Audio; @@ -174,6 +176,13 @@ public class DirectItemVoiceMediaViewHolder extends DirectItemViewHolder { return false; } + @Override + protected List getLongClickOptions() { + return ImmutableList.of( + new DirectItemContextMenu.MenuItem(R.id.download, R.string.action_download) + ); + } + private static class AudioItemState { private boolean prepared; diff --git a/app/src/main/java/awais/instagrabber/customviews/DirectItemContextMenu.java b/app/src/main/java/awais/instagrabber/customviews/DirectItemContextMenu.java index f615b7fc..65a18141 100644 --- a/app/src/main/java/awais/instagrabber/customviews/DirectItemContextMenu.java +++ b/app/src/main/java/awais/instagrabber/customviews/DirectItemContextMenu.java @@ -29,12 +29,14 @@ import androidx.constraintlayout.widget.ConstraintLayout; import androidx.core.util.Pair; import java.util.List; +import java.util.function.Function; import awais.instagrabber.R; import awais.instagrabber.animations.RoundedRectRevealOutlineProvider; import awais.instagrabber.customviews.emoji.Emoji; import awais.instagrabber.customviews.emoji.ReactionsManager; import awais.instagrabber.databinding.LayoutDirectItemOptionsBinding; +import awais.instagrabber.repositories.responses.directmessages.DirectItem; import static android.view.View.MeasureSpec.makeMeasureSpec; @@ -345,7 +347,7 @@ public class DirectItemContextMenu extends PopupWindow { textView.setText(context.getString(menuItem.getTitleRes())); textView.setOnClickListener(v -> { if (onOptionSelectListener != null) { - onOptionSelectListener.onSelect(menuItem.getItemId()); + onOptionSelectListener.onSelect(menuItem.getItemId(), menuItem.getCallback()); } dismiss(); }); @@ -397,9 +399,19 @@ public class DirectItemContextMenu extends PopupWindow { @StringRes private final int titleRes; + /** + * Callback function + */ + private final Function callback; + public MenuItem(@IdRes final int itemId, @StringRes final int titleRes) { + this(itemId, titleRes, null); + } + + public MenuItem(@IdRes final int itemId, @StringRes final int titleRes, @Nullable final Function callback) { this.itemId = itemId; this.titleRes = titleRes; + this.callback = callback; } public int getItemId() { @@ -409,10 +421,14 @@ public class DirectItemContextMenu extends PopupWindow { public int getTitleRes() { return titleRes; } + + public Function getCallback() { + return callback; + } } public interface OnOptionSelectListener { - void onSelect(int itemId); + void onSelect(int itemId, @Nullable Function callback); } public interface OnReactionClickListener { diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java index 34d4ed1c..bf13abb6 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java @@ -20,8 +20,6 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; -import androidx.lifecycle.ViewModelStoreOwner; -import androidx.navigation.NavController; import androidx.navigation.NavDirections; import androidx.navigation.fragment.NavHostFragment; import androidx.recyclerview.widget.LinearLayoutManager; @@ -29,7 +27,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.android.material.badge.BadgeDrawable; import com.google.android.material.badge.BadgeUtils; -import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.snackbar.Snackbar; import java.util.List; @@ -66,9 +63,7 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh super.onCreate(savedInstanceState); fragmentActivity = (MainActivity) getActivity(); if (fragmentActivity != null) { - final NavController navController = NavHostFragment.findNavController(this); - final ViewModelStoreOwner viewModelStoreOwner = navController.getViewModelStoreOwner(R.id.direct_messages_nav_graph); - viewModel = new ViewModelProvider(viewModelStoreOwner).get(DirectInboxViewModel.class); + viewModel = new ViewModelProvider(fragmentActivity).get(DirectInboxViewModel.class); } setHasOptionsMenu(true); } @@ -101,6 +96,7 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh } } + @SuppressLint("UnsafeExperimentalUsageError") @Override public void onPause() { super.onPause(); @@ -201,11 +197,6 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh break; } }); - viewModel.getUnseenCount().observe(getViewLifecycleOwner(), unseenCountResource -> { - if (unseenCountResource == null) return; - final Integer unseenCount = unseenCountResource.data; - setBottomNavBarBadge(unseenCount == null ? 0 : unseenCount); - }); viewModel.getPendingRequestsTotal().observe(getViewLifecycleOwner(), this::attachPendingRequestsBadge); } @@ -213,12 +204,7 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh private void attachPendingRequestsBadge(@Nullable final Integer count) { if (pendingRequestsMenuItem == null) { final Handler handler = new Handler(); - handler.postDelayed(new Runnable() { - @Override - public void run() { - attachPendingRequestsBadge(count); - } - }, 500); + handler.postDelayed(() -> attachPendingRequestsBadge(count), 500); return; } if (pendingRequestTotalBadgeDrawable == null) { @@ -277,20 +263,4 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh }); binding.inboxList.addOnScrollListener(lazyLoader); } - - private void setBottomNavBarBadge(final int unseenCount) { - final BottomNavigationView bottomNavView = fragmentActivity.getBottomNavView(); - final BadgeDrawable badge = bottomNavView.getOrCreateBadge(R.id.direct_messages_nav_graph); - if (badge == null) return; - if (unseenCount == 0) { - badge.setVisible(false); - badge.clearNumber(); - return; - } - if (badge.getVerticalOffset() != 10) { - badge.setVerticalOffset(10); - } - badge.setNumber(unseenCount); - badge.setVisible(true); - } } diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java index a960cd36..f98e4094 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java @@ -79,11 +79,13 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi final DirectMessageSettingsFragmentArgs args = DirectMessageSettingsFragmentArgs.fromBundle(arguments); final MainActivity fragmentActivity = (MainActivity) requireActivity(); final AppStateViewModel appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class); - viewModel = new ViewModelProvider(this, new DirectSettingsViewModelFactory(fragmentActivity.getApplication(), - args.getThreadId(), - args.getPending(), - appStateViewModel.getCurrentUser())) - .get(DirectSettingsViewModel.class); + final DirectSettingsViewModelFactory viewModelFactory = new DirectSettingsViewModelFactory( + fragmentActivity.getApplication(), + args.getThreadId(), + args.getPending(), + appStateViewModel.getCurrentUser() + ); + viewModel = new ViewModelProvider(this, viewModelFactory).get(DirectSettingsViewModel.class); } @NonNull diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index acb1dcde..a6b1cb4c 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -58,6 +58,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.function.Function; import awais.instagrabber.ProfileNavGraphDirections; import awais.instagrabber.R; @@ -258,7 +259,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact } @Override - public void onOptionSelect(final DirectItem item, final int itemId) { + public void onOptionSelect(final DirectItem item, final int itemId, final Function cb) { if (itemId == R.id.unsend) { handleSentMessage(viewModel.unsend(item)); return; @@ -275,21 +276,17 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact final NavController navController = NavHostFragment.findNavController(DirectMessageThreadFragment.this); 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; - } + if (itemId == R.id.download) { + downloadItem(item); + return; + } + // otherwise call callback if present + if (cb != null) { + cb.apply(item); } } }; + private final DirectItemLongClickListener directItemLongClickListener = position -> { // viewModel.setSelectedPosition(position); }; @@ -319,6 +316,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact }; private final MutableLiveData inputLength = new MutableLiveData<>(0); private MenuItem markAsSeenMenuItem; + private Media tempMedia; @Override public void onCreate(@Nullable final Bundle savedInstanceState) { @@ -329,11 +327,13 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact final Bundle arguments = getArguments(); if (arguments == null) return; final DirectMessageThreadFragmentArgs fragmentArgs = DirectMessageThreadFragmentArgs.fromBundle(arguments); - viewModel = new ViewModelProvider(this, new DirectThreadViewModelFactory(fragmentActivity.getApplication(), - fragmentArgs.getThreadId(), - fragmentArgs.getPending(), - appStateViewModel.getCurrentUser())) - .get(DirectThreadViewModel.class); + final DirectThreadViewModelFactory viewModelFactory = new DirectThreadViewModelFactory( + fragmentActivity.getApplication(), + fragmentArgs.getThreadId(), + fragmentArgs.getPending(), + appStateViewModel.getCurrentUser() + ); + viewModel = new ViewModelProvider(this, viewModelFactory).get(DirectThreadViewModel.class); setHasOptionsMenu(true); } @@ -454,7 +454,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact final Context context = getContext(); if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - // downloadItem(context); + if (tempMedia == null) return; + downloadItem(context, tempMedia); + return; } if (requestCode == AUDIO_RECORD_PERM_REQUEST_CODE) { if (PermissionUtils.hasAudioRecordPerms(context)) { @@ -1317,18 +1319,31 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact appExecutors.mainThread().execute(prevTitleRunnable, 1000); } + private void downloadItem(final DirectItem item) { + final Context context = getContext(); + if (context == null) return; + final DirectItemType itemType = item.getItemType(); + //noinspection SwitchStatementWithTooFewBranches + switch (itemType) { + case VOICE_MEDIA: + downloadItem(context, item.getVoiceMedia() == null ? null : item.getVoiceMedia().getMedia()); + break; + } + } + // currently ONLY for voice - private void downloadItem(final Media media, final Context context) { + private void downloadItem(@NonNull final Context context, final Media media) { if (media == null) { Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - } else { - if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) { - DownloadUtils.download(context, media); - } else { - requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); - } - Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show(); + return; } + if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) { + DownloadUtils.download(context, media); + Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show(); + return; + } + tempMedia = media; + requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @NonNull diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java index 8ee0d1c4..bf459ef1 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java @@ -13,8 +13,6 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; -import androidx.lifecycle.ViewModelStoreOwner; -import androidx.navigation.NavController; import androidx.navigation.fragment.NavHostFragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -24,7 +22,6 @@ import com.google.android.material.snackbar.Snackbar; import java.util.Collections; import java.util.List; -import awais.instagrabber.R; import awais.instagrabber.activities.MainActivity; import awais.instagrabber.adapters.DirectMessageInboxAdapter; import awais.instagrabber.customviews.helpers.RecyclerLazyLoaderAtEdge; @@ -51,9 +48,7 @@ public class DirectPendingInboxFragment extends Fragment implements SwipeRefresh super.onCreate(savedInstanceState); fragmentActivity = (MainActivity) getActivity(); if (fragmentActivity != null) { - final NavController navController = NavHostFragment.findNavController(this); - final ViewModelStoreOwner viewModelStoreOwner = navController.getViewModelStoreOwner(R.id.direct_messages_nav_graph); - viewModel = new ViewModelProvider(viewModelStoreOwner).get(DirectPendingInboxViewModel.class); + viewModel = new ViewModelProvider(fragmentActivity).get(DirectPendingInboxViewModel.class); } } diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 4b3bfcfd..c0998f13 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -12,7 +12,6 @@ import android.text.style.RelativeSizeSpan; import android.text.style.StyleSpan; import android.util.Log; import android.view.ActionMode; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -604,12 +603,16 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe usernameTemp = usernameTemp.substring(1); } if (TextUtils.isEmpty(usernameTemp)) { - profileModel = appStateViewModel.getCurrentUser(); - username = profileModel.getUsername(); - setUsernameDelayed(); - setProfileDetails(); + appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), user -> { + if (user == null) return; + profileModel = user; + username = profileModel.getUsername(); + setUsernameDelayed(); + setProfileDetails(); + }); + return; } - else if (isLoggedIn) { + if (isLoggedIn) { userService.getUsernameInfo(usernameTemp, new ServiceCallback() { @Override public void onSuccess(final User user) { @@ -643,26 +646,25 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } catch (final Throwable ignored) {} } }); + return; } - else { - graphQLService.fetchUser(usernameTemp, new ServiceCallback() { - @Override - public void onSuccess(final User user) { - profileModel = user; - setProfileDetails(); - } + graphQLService.fetchUser(usernameTemp, new ServiceCallback() { + @Override + public void onSuccess(final User user) { + profileModel = user; + setProfileDetails(); + } - @Override - public void onFailure(final Throwable t) { - Log.e(TAG, "Error fetching profile", t); - final Context context = getContext(); - try { - if (t == null) Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_LONG).show(); - else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); - } catch (final Throwable ignored) {} - } - }); - } + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error fetching profile", t); + final Context context = getContext(); + try { + if (t == null) Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_LONG).show(); + else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } catch (final Throwable ignored) {} + } + }); } private void setProfileDetails() { @@ -987,6 +989,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } private void updateAccountInfo() { + if (profileModel == null) return; accountRepository.insertOrUpdateAccount( profileModel.getPk(), profileModel.getUsername(), diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/NotificationsPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/NotificationsPreferencesFragment.java index 72f69f79..50d284e7 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/NotificationsPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/NotificationsPreferencesFragment.java @@ -15,7 +15,7 @@ public class NotificationsPreferencesFragment extends BasePreferencesFragment { final Context context = getContext(); if (context == null) return; screen.addPreference(getActivityNotificationsPreference(context)); - screen.addPreference(getDMNotificationsPreference(context)); + // screen.addPreference(getDMNotificationsPreference(context)); } private Preference getActivityNotificationsPreference(@NonNull final Context context) { diff --git a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java index dcea12ed..ab1cbabb 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -1,9 +1,12 @@ package awais.instagrabber.viewmodels; import android.app.Application; +import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; import awais.instagrabber.db.datasources.AccountDataSource; import awais.instagrabber.db.repositories.AccountRepository; @@ -21,8 +24,8 @@ public class AppStateViewModel extends AndroidViewModel { private final String cookie; private final boolean isLoggedIn; + private final MutableLiveData currentUser = new MutableLiveData<>(); - private User currentUser; private AccountRepository accountRepository; private UserService userService; @@ -38,6 +41,10 @@ public class AppStateViewModel extends AndroidViewModel { } public User getCurrentUser() { + return currentUser.getValue(); + } + + public LiveData getCurrentUserLiveData() { return currentUser; } @@ -46,11 +53,13 @@ public class AppStateViewModel extends AndroidViewModel { userService.getUserInfo(uid, new ServiceCallback() { @Override public void onSuccess(final User user) { - currentUser = user; + currentUser.postValue(user); } @Override - public void onFailure(final Throwable t) {} + public void onFailure(final Throwable t) { + Log.e(TAG, "onFailure: ", t); + } }); } } diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 7319700f..8f77fc7f 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 86b2d1ac..5321f043 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -483,4 +483,6 @@ Select an email app to send crash logs Not found! Your IP has been rate limited by Instagram. Wait for an hour and try again. + Copy caption + Copy reply