Browse Source

Fix toolbar race condition

renovate/androidx.fragment-fragment-ktx-1.x
Ammar Githam 3 years ago
parent
commit
0715819eeb
  1. 30
      app/src/main/java/awais/instagrabber/activities/MainActivity.kt
  2. 14
      app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java
  3. 4
      app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java
  4. 4
      app/src/main/java/awais/instagrabber/fragments/LocationFragment.java
  5. 17
      app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java
  6. 10
      app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.kt
  7. 4
      app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java
  8. 6
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt

30
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<Tab> = 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) {

14
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);
}

4
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);
}

4
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);
}

17
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())
);
}

10
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
}
}

4
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() {

6
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
}

Loading…
Cancel
Save