From 0715819eebaca26eab70f19fddfca6e283502530 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 13 Jul 2021 06:19:37 +0900 Subject: [PATCH] Fix toolbar race condition --- .../instagrabber/activities/MainActivity.kt | 30 ++++++++++--------- .../fragments/CollectionPostsFragment.java | 14 ++------- .../fragments/HashTagFragment.java | 4 +-- .../fragments/LocationFragment.java | 4 +-- .../fragments/TopicPostsFragment.java | 17 ++--------- .../DirectMessageInboxFragment.kt | 10 +++---- .../fragments/main/FeedFragment.java | 4 +-- .../fragments/main/ProfileFragment.kt | 6 +++- 8 files changed, 37 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.kt b/app/src/main/java/awais/instagrabber/activities/MainActivity.kt index 8a394fae..324c4daf 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.kt +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.kt @@ -78,8 +78,7 @@ class MainActivity : BaseLanguageActivity() { private var userId: Long = 0 private var toolbarOwner: Fragment? = null - lateinit var toolbar: Toolbar - private set + private lateinit var toolbar: Toolbar var currentTabs: List = emptyList() private set @@ -610,16 +609,6 @@ class MainActivity : BaseLanguageActivity() { // } // } - @Synchronized - fun resetToolbar(owner: Fragment) { - if (owner != toolbarOwner) return - binding.appBarLayout.visibility = View.VISIBLE - setScrollingBehaviour() - setSupportActionBar(binding.toolbar) - setupActionBarWithNavController(navController, appBarConfiguration) - toolbarOwner = null - } - val collapsingToolbarView: CollapsingToolbarLayout get() = binding.collapsingToolbarLayout val appbarLayout: AppBarLayout @@ -658,8 +647,11 @@ class MainActivity : BaseLanguageActivity() { val rootView: View get() = binding.root - @Synchronized - fun setToolbar(toolbar: Toolbar, owner: Fragment) { + private val toolbarLock = Any() + + fun getToolbar() = synchronized(toolbarLock) { this.toolbar } + + fun setToolbar(toolbar: Toolbar, owner: Fragment) = synchronized(toolbarLock) { toolbarOwner = owner binding.appBarLayout.visibility = View.GONE removeScrollingBehaviour() @@ -668,6 +660,16 @@ class MainActivity : BaseLanguageActivity() { NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration) } + fun resetToolbar(owner: Fragment) = synchronized(toolbarLock) { + if (owner != toolbarOwner) return + this.toolbar = binding.toolbar + setSupportActionBar(binding.toolbar) + binding.appBarLayout.visibility = View.VISIBLE + setScrollingBehaviour() + setupActionBarWithNavController(navController, appBarConfiguration) + toolbarOwner = null + } + private fun setNavBarDMUnreadCountBadge(unseenCount: Int) { val badge = binding.bottomNavView.getOrCreateBadge(R.id.direct_messages_nav_graph) if (unseenCount == 0) { diff --git a/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java b/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java index 90ab72e9..d6d3087f 100644 --- a/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java @@ -364,18 +364,8 @@ public class CollectionPostsFragment extends Fragment implements SwipeRefreshLay } @Override - public void onDestroy() { - super.onDestroy(); - resetToolbar(); - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - resetToolbar(); - } - - private void resetToolbar() { + public void onStop() { + super.onStop(); fragmentActivity.resetToolbar(this); } diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index 42ca7c04..bb6ae483 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -339,8 +339,8 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe } @Override - public void onDestroyView() { - super.onDestroyView(); + public void onStop() { + super.onStop(); fragmentActivity.resetToolbar(this); } diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index eb185840..4d59428a 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -333,8 +333,8 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR } @Override - public void onDestroyView() { - super.onDestroyView(); + public void onStop() { + super.onStop(); fragmentActivity.resetToolbar(this); } diff --git a/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java b/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java index 8251341d..cd3ba776 100644 --- a/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java @@ -278,18 +278,8 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O } @Override - public void onDestroy() { - super.onDestroy(); - resetToolbar(); - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - resetToolbar(); - } - - private void resetToolbar() { + public void onStop() { + super.onStop(); fragmentActivity.resetToolbar(this); } @@ -378,8 +368,7 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O } private void updateSwipeRefreshState() { - AppExecutors.INSTANCE.getMainThread().execute(() -> - binding.swipeRefreshLayout.setRefreshing(binding.posts.isFetching()) + AppExecutors.INSTANCE.getMainThread().execute(() -> binding.swipeRefreshLayout.setRefreshing(binding.posts.isFetching()) ); } diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.kt b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.kt index 78aa6488..de7a8206 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.kt @@ -72,9 +72,9 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener { super.onPause() isPendingRequestTotalBadgeAttached = false pendingRequestsMenuItem?.let { - val menuItemView = ToolbarUtils.getActionMenuItemView(fragmentActivity.toolbar, it.itemId) + val menuItemView = ToolbarUtils.getActionMenuItemView(fragmentActivity.getToolbar(), it.itemId) if (menuItemView != null) { - BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.toolbar, it.itemId) + BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), it.itemId) pendingRequestTotalBadgeDrawable = null } } @@ -145,11 +145,11 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener { } if (count == null || count == 0) { val menuItemView = ToolbarUtils.getActionMenuItemView( - fragmentActivity.toolbar, + fragmentActivity.getToolbar(), pendingRequestsMenuItem1.itemId ) if (menuItemView != null) { - BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.toolbar, pendingRequestsMenuItem1.itemId) + BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem1.itemId) } isPendingRequestTotalBadgeAttached = false pendingRequestTotalBadgeDrawable?.number = 0 @@ -161,7 +161,7 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener { pendingRequestTotalBadgeDrawable?.number = count if (!isPendingRequestTotalBadgeAttached) { pendingRequestTotalBadgeDrawable?.let { - BadgeUtils.attachBadgeDrawable(it, fragmentActivity.toolbar, pendingRequestsMenuItem1.itemId) + BadgeUtils.attachBadgeDrawable(it, fragmentActivity.getToolbar(), pendingRequestsMenuItem1.itemId) isPendingRequestTotalBadgeAttached = true } } diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index 6c31edbe..6fdc1130 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -336,9 +336,9 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre } @Override - public void onDestroyView() { + public void onStop() { + super.onStop(); fragmentActivity.resetToolbar(this); - super.onDestroyView(); } private void setupFeed() { diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt index c199b461..40ce6632 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt @@ -391,8 +391,12 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall } } - override fun onDestroyView() { + override fun onStop() { + super.onStop() mainActivity.resetToolbar(this) + } + + override fun onDestroyView() { super.onDestroyView() setupPostsDone = false }