Fix toolbar race condition

This commit is contained in:
Ammar Githam 2021-07-13 06:19:37 +09:00
parent fae25ff690
commit 0715819eeb
8 changed files with 37 additions and 52 deletions

View File

@ -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) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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