From c5aa9714b2e5fbcd0d3f0ba241181fed43f52bfb Mon Sep 17 00:00:00 2001 From: EmmanuelMess Date: Thu, 15 Jul 2021 12:03:42 -0300 Subject: [PATCH] Correctly manage views when changing tabs with a QuickView active --- .../ui/fragments/MainFragment.java | 39 +++++++++++++------ .../filemanager/ui/fragments/TabFragment.java | 30 ++++++++------ .../fragments/quickview/QuickViewFragment.kt | 35 ++++++++++++++--- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/ui/fragments/MainFragment.java b/app/src/main/java/com/amaze/filemanager/ui/fragments/MainFragment.java index db29c0d0ed..5e837fdcd2 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/fragments/MainFragment.java +++ b/app/src/main/java/com/amaze/filemanager/ui/fragments/MainFragment.java @@ -79,7 +79,6 @@ import com.amaze.filemanager.ui.views.DividerItemDecoration; import com.amaze.filemanager.ui.views.FastScroller; import com.amaze.filemanager.ui.views.WarnableTextInputValidator; -import com.amaze.filemanager.ui.views.appbar.BottomBar; import com.amaze.filemanager.utils.BottomBarButtonPath; import com.amaze.filemanager.utils.DataUtils; import com.amaze.filemanager.utils.MainActivityHelper; @@ -737,6 +736,28 @@ public void onDestroyActionMode(ActionMode mode) { } }; + /** When TabFragment detects this fragment is in view it calls this method */ + public void onWorkspaceSelected() { + if (getCurrentPath() != null) { + getMainActivity().getDrawer().selectCorrectDrawerItemForPath(getCurrentPath()); + } + + final QuickViewFragment quickViewFragment = getCurrentQuickView(); + + if (quickViewFragment != null) { + quickViewFragment.runOpenVisibilityChanges(); + } + } + + /** When TabFragment detects this fragment is not in view it calls this method */ + public void onWorkspaceUnselected() { + final QuickViewFragment quickViewFragment = getCurrentQuickView(); + + if (quickViewFragment != null) { + quickViewFragment.runExitVisibilityChanges(); + } + } + private BroadcastReceiver receiver2 = new BroadcastReceiver() { @@ -915,14 +936,6 @@ public void onQuickViewClicked(LayoutElementParcelable layoutElementParcelable) final QuickViewType quickViewType = new QuickViewImage(iconData, layoutElementParcelable.title); final QuickViewFragment fragment = QuickViewFragment.newInstance(quickViewType); - final Menu menu = getMainActivity().getAppbar().getToolbar().getMenu(); - menu.setGroupVisible(0, false); - - final BottomBar bottomBar = getMainActivity().getAppbar().getBottomBar(); - bottomBar.setIsClickEnabled(false); - - getMainActivity().getFAB().getMainFab().setVisibility(View.GONE); - getChildFragmentManager() .beginTransaction() .replace(R.id.quickViewContainer, fragment, MainActivity.TAG_QUICK_VIEW_FRAGMENT) @@ -930,8 +943,7 @@ public void onQuickViewClicked(LayoutElementParcelable layoutElementParcelable) } private void assureNoQuickView() { - QuickViewFragment fragment = - (QuickViewFragment) getChildFragmentManager().findFragmentById(R.id.quickViewContainer); + QuickViewFragment fragment = getCurrentQuickView(); if (fragment == null) { return; @@ -940,6 +952,11 @@ private void assureNoQuickView() { fragment.exit(); } + @Nullable + private QuickViewFragment getCurrentQuickView() { + return (QuickViewFragment) getChildFragmentManager().findFragmentById(R.id.quickViewContainer); + } + public void updateTabWithDb(Tab tab) { mainFragmentViewModel.setCurrentPath(tab.path); mainFragmentViewModel.setHome(tab.home); diff --git a/app/src/main/java/com/amaze/filemanager/ui/fragments/TabFragment.java b/app/src/main/java/com/amaze/filemanager/ui/fragments/TabFragment.java index b588a5b9be..23b36a8b83 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/fragments/TabFragment.java +++ b/app/src/main/java/com/amaze/filemanager/ui/fragments/TabFragment.java @@ -64,7 +64,7 @@ /** Created by Arpit on 15-12-2014. */ public class TabFragment extends Fragment implements ViewPager.OnPageChangeListener { - public List fragments = new ArrayList<>(); + public List fragments = new ArrayList<>(2); public ScreenSlidePagerAdapter mSectionsPagerAdapter; public DisablableViewPager mViewPager; @@ -265,7 +265,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } @Override - public void onPageSelected(int p1) { + public void onPageSelected(int selectedTabId) { mainActivity .getAppbar() .getAppbarLayout() @@ -274,7 +274,7 @@ public void onPageSelected(int p1) { .setInterpolator(new DecelerateInterpolator(2)) .start(); - MainActivity.currentTab = p1; + MainActivity.currentTab = selectedTabId; if (sharedPrefs != null) { sharedPrefs @@ -283,19 +283,25 @@ public void onPageSelected(int p1) { .apply(); } - // Log.d(getClass().getSimpleName(), "Page Selected: " + MainActivity.currentTab, new - // Exception()); + Fragment notInView = fragments.get(selectedTabId == 0 ? 1 : 0); + if (notInView instanceof MainFragment) { + ((MainFragment) notInView).onWorkspaceUnselected(); + } + + Fragment inView = fragments.get(selectedTabId); + if (inView instanceof MainFragment) { + MainFragment mainFragment = (MainFragment) inView; - Fragment fragment = fragments.get(p1); - if (fragment != null && fragment instanceof MainFragment) { - MainFragment ma = (MainFragment) fragment; - if (ma.getCurrentPath() != null) { - mainActivity.getDrawer().selectCorrectDrawerItemForPath(ma.getCurrentPath()); - updateBottomBar(ma); + if (mainFragment.getCurrentPath() != null) { + updateBottomBar(mainFragment); } + + ((MainFragment) inView).onWorkspaceSelected(); } - if (circleDrawable1 != null && circleDrawable2 != null) updateIndicator(p1); + if (circleDrawable1 != null && circleDrawable2 != null) { + updateIndicator(selectedTabId); + } } @Override diff --git a/app/src/main/java/com/amaze/filemanager/ui/fragments/quickview/QuickViewFragment.kt b/app/src/main/java/com/amaze/filemanager/ui/fragments/quickview/QuickViewFragment.kt index bdd59e2b5a..f3280eee76 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/fragments/quickview/QuickViewFragment.kt +++ b/app/src/main/java/com/amaze/filemanager/ui/fragments/quickview/QuickViewFragment.kt @@ -69,19 +69,25 @@ class QuickViewFragment : Fragment() { } } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setHasOptionsMenu(true) // HACK its needed to get a onPrepareOptionsMenu() callback + } + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - setHasOptionsMenu(true) - + ): View { return inflater.inflate(R.layout.quick_view_fragment, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + runOpenVisibilityChanges() + val constraintLayout = view.findViewById(R.id.frameLayout) constraintLayout.setOnClickListener { exit() @@ -225,13 +231,32 @@ class QuickViewFragment : Fragment() { * see [MainFragment.onQuickViewClicked] */ fun exit() { - // TODO Fix fab visibility when changing workspaces - parentFragmentManager .beginTransaction() .remove(this) .commit() + runExitVisibilityChanges() + } + + /** + * Run the visibility changes needed for this fragment to look good + */ + fun runOpenVisibilityChanges() { + (requireActivity() as MainActivity) + .appbar.toolbar.menu.setGroupVisible(0, false) + + (requireActivity() as MainActivity) + .appbar.bottomBar.setIsClickEnabled(false) + + (requireActivity() as MainActivity) + .fab.mainFab.visibility = View.GONE + } + + /** + * Undo the visibility changes needed for this fragment to look good + */ + fun runExitVisibilityChanges() { (requireActivity() as MainActivity) .appbar.toolbar.menu.setGroupVisible(0, true)