From 783acf417467bc74be88a8d5e2aa3737501df110 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 30 Mar 2021 21:27:15 +0900 Subject: [PATCH] Move dm unseen count observer to main activity. Fixes https://github.com/austinhuang0131/barinsta/issues/873 --- .../instagrabber/activities/MainActivity.java | 28 +++++++++++++++ .../DirectMessageInboxFragment.java | 36 ++----------------- .../DirectMessageSettingsFragment.java | 12 ++++--- .../DirectMessageThreadFragment.java | 12 ++++--- .../DirectPendingInboxFragment.java | 7 +--- 5 files changed, 46 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 408a841a..23a9929f 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; @@ -82,6 +83,7 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; +import awais.instagrabber.viewmodels.DirectInboxViewModel; import awais.instagrabber.webservices.SearchService; import retrofit2.Call; import retrofit2.Callback; @@ -181,6 +183,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage initEmojiCompat(); searchService = SearchService.getInstance(); // initDmService(); + initDmUnreadCount(); } private void initDmService() { @@ -190,6 +193,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); @@ -850,4 +863,19 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage public Toolbar getToolbar() { return binding.toolbar; } + + 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/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..fe36cdbf 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(fragmentActivity, 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..1c7f3dde 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -329,11 +329,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(fragmentActivity, viewModelFactory).get(DirectThreadViewModel.class); setHasOptionsMenu(true); } 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); } }