From 200e2a51d23a771647c08a34079903c1ebf7cd70 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 21 May 2021 15:19:35 +0200 Subject: [PATCH] show only the selected tabs --- .../pro/activities/MainActivity.kt | 88 ++++++++++++++----- .../pro/adapters/ViewPagerAdapter.kt | 16 +++- .../pro/dialogs/ManageVisibleTabsDialog.kt | 3 +- 3 files changed, 83 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt index 78bc13e0..749d64c3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt @@ -6,6 +6,7 @@ import android.content.ClipData import android.content.Context import android.content.Intent import android.graphics.drawable.ColorDrawable +import android.graphics.drawable.Drawable import android.media.RingtoneManager import android.os.Bundle import android.os.Handler @@ -52,11 +53,13 @@ class MainActivity : SimpleActivity() { private var storedFontSize = 0 private var storedDateFormat = "" private var storedTimeFormat = "" + private var storedShowTabs = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) appLaunched(BuildConfig.APPLICATION_ID) + setupTabColors(config.lastUsedViewPagerPage) storeStateVariables() mIsPasswordProtectionPending = config.isAppPasswordProtectionOn @@ -79,6 +82,12 @@ class MainActivity : SimpleActivity() { override fun onResume() { super.onResume() + if (storedShowTabs != config.showTabs) { + config.lastUsedViewPagerPage = 0 + System.exit(0) + return + } + getAllFragments().forEach { it?.setupColors(config.textColor, config.primaryColor) } @@ -129,7 +138,7 @@ class MainActivity : SimpleActivity() { override fun onPrepareOptionsMenu(menu: Menu?): Boolean { val favorites = config.favorites - val currentFragment = getCurrentFragment() ?: return true + val currentFragment = getCurrentFragment() menu!!.apply { findItem(R.id.search).isVisible = currentFragment is ItemsFragment @@ -154,10 +163,6 @@ class MainActivity : SimpleActivity() { } override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (getCurrentFragment() == null) { - return true - } - when (item.itemId) { R.id.go_home -> goHome() R.id.go_to_favorite -> goToFavorite() @@ -180,7 +185,7 @@ class MainActivity : SimpleActivity() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putString(PICKED_PATH, items_fragment.currentPath) + outState.putString(PICKED_PATH, items_fragment?.currentPath ?: "") outState.putBoolean(WAS_PROTECTION_HANDLED, mWasProtectionHandled) } @@ -227,7 +232,7 @@ class MainActivity : SimpleActivity() { override fun onQueryTextChange(newText: String): Boolean { if (isSearchOpen) { - getCurrentFragment()?.searchQueryChanged(newText) + getCurrentFragment().searchQueryChanged(newText) } return true } @@ -254,6 +259,7 @@ class MainActivity : SimpleActivity() { storedFontSize = fontSize storedDateFormat = dateFormat storedTimeFormat = context.getTimeFormat() + storedShowTabs = showTabs } } @@ -328,12 +334,10 @@ class MainActivity : SimpleActivity() { } ) - setupTabColors(tabToOpen) - main_view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { if (isSearchOpen) { - getCurrentFragment()?.searchQueryChanged("") + getCurrentFragment().searchQueryChanged("") searchMenuItem?.collapseActionView() } } @@ -348,6 +352,27 @@ class MainActivity : SimpleActivity() { invalidateOptionsMenu() } }) + + main_tabs_holder.removeAllTabs() + var skippedTabs = 0 + tabsList.forEachIndexed { index, value -> + if (config.showTabs and value == 0) { + skippedTabs++ + } else { + val tab = main_tabs_holder.newTab().setIcon(getTabIcon(index)) + main_tabs_holder.addTab(tab, index - skippedTabs, config.lastUsedViewPagerPage == index - skippedTabs) + } + } + + // selecting the proper tab sometimes glitches, add an extra selector to make sure we have it right + main_tabs_holder.onGlobalLayout { + Handler().postDelayed({ + main_tabs_holder.getTabAt(config.lastUsedViewPagerPage)?.select() + invalidateOptionsMenu() + }, 100L) + } + + main_tabs_holder.beVisibleIf(skippedTabs < tabsList.size - 1) } private fun setupTabColors(lastUsedTab: Int) { @@ -372,6 +397,15 @@ class MainActivity : SimpleActivity() { main_tabs_holder.getTabAt(main_view_pager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor()) } + private fun getTabIcon(position: Int): Drawable { + val drawableId = when (position) { + 0 -> R.drawable.ic_folder_vector + else -> R.drawable.ic_clock_vector + } + + return resources.getColoredDrawableWithColor(drawableId, config.textColor) + } + private fun checkOTGPath() { ensureBackgroundThread { if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) { @@ -402,23 +436,23 @@ class MainActivity : SimpleActivity() { } private fun goHome() { - if (config.homeFolder != getCurrentFragment()?.currentPath) { + if (config.homeFolder != getCurrentFragment().currentPath) { openPath(config.homeFolder) } } private fun showSortingDialog() { - ChangeSortingDialog(this, getCurrentFragment()!!.currentPath) { + ChangeSortingDialog(this, getCurrentFragment().currentPath) { (getCurrentFragment() as? ItemsFragment)?.refreshItems() } } private fun addFavorite() { - config.addFavorite(getCurrentFragment()!!.currentPath) + config.addFavorite(getCurrentFragment().currentPath) } private fun removeFavorite() { - config.removeFavorite(getCurrentFragment()!!.currentPath) + config.removeFavorite(getCurrentFragment().currentPath) } private fun toggleFilenameVisibility() { @@ -448,7 +482,7 @@ class MainActivity : SimpleActivity() { favorites.forEachIndexed { index, path -> val visiblePath = humanizePath(path).replace("/", " / ") items.add(RadioItem(index, visiblePath, path)) - if (path == getCurrentFragment()!!.currentPath) { + if (path == getCurrentFragment().currentPath) { currFavoriteIndex = index } } @@ -459,12 +493,12 @@ class MainActivity : SimpleActivity() { } private fun setAsHome() { - config.homeFolder = getCurrentFragment()!!.currentPath + config.homeFolder = getCurrentFragment().currentPath toast(R.string.home_folder_updated) } private fun changeViewType() { - ChangeViewTypeDialog(this, getCurrentFragment()!!.currentPath, getCurrentFragment() is ItemsFragment) { + ChangeViewTypeDialog(this, getCurrentFragment().currentPath, getCurrentFragment() is ItemsFragment) { getAllFragments().forEach { it?.refreshItems() } @@ -509,7 +543,7 @@ class MainActivity : SimpleActivity() { return } - if (getCurrentFragment()!!.breadcrumbs.childCount <= 1) { + if (getCurrentFragment().breadcrumbs.childCount <= 1) { if (!wasBackJustPressed && config.pressBackTwice) { wasBackJustPressed = true toast(R.string.press_back_again) @@ -520,8 +554,8 @@ class MainActivity : SimpleActivity() { finish() } } else { - getCurrentFragment()!!.breadcrumbs.removeBreadcrumb() - openPath(getCurrentFragment()!!.breadcrumbs.getLastItem().path) + getCurrentFragment().breadcrumbs.removeBreadcrumb() + openPath(getCurrentFragment().breadcrumbs.getLastItem().path) } } @@ -594,7 +628,19 @@ class MainActivity : SimpleActivity() { private fun getAllFragments(): ArrayList = arrayListOf(items_fragment, recents_fragment) - private fun getCurrentFragment() = getAllFragments().getOrNull(main_view_pager.currentItem) + private fun getCurrentFragment(): MyViewPagerFragment { + val showTabs = config.showTabs + val fragments = arrayListOf() + if (showTabs and TAB_FILES != 0) { + fragments.add(items_fragment) + } + + if (showTabs and TAB_RECENT_FILES != 0) { + fragments.add(recents_fragment) + } + + return fragments[main_view_pager.currentItem] + } private fun checkWhatsNewDialog() { arrayListOf().apply { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ViewPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ViewPagerAdapter.kt index 39def11d..e0158b82 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ViewPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ViewPagerAdapter.kt @@ -3,10 +3,13 @@ package com.simplemobiletools.filemanager.pro.adapters import android.view.View import android.view.ViewGroup import androidx.viewpager.widget.PagerAdapter +import com.simplemobiletools.commons.helpers.TAB_FILES +import com.simplemobiletools.commons.helpers.TAB_RECENT_FILES import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.activities.SimpleActivity import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.fragments.MyViewPagerFragment +import com.simplemobiletools.filemanager.pro.helpers.tabsList class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() { override fun instantiateItem(container: ViewGroup, position: Int): Any { @@ -26,12 +29,21 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() { container.removeView(item as View) } - override fun getCount() = 2 + override fun getCount() = tabsList.filter { it and activity.config.showTabs != 0 }.size override fun isViewFromObject(view: View, item: Any) = view == item private fun getFragment(position: Int): Int { - val fragments = arrayListOf(R.layout.items_fragment, R.layout.recents_fragment) + val showTabs = activity.config.showTabs + val fragments = arrayListOf() + if (showTabs and TAB_FILES != 0) { + fragments.add(R.layout.items_fragment) + } + + if (showTabs and TAB_RECENT_FILES != 0) { + fragments.add(R.layout.recents_fragment) + } + return fragments[position] } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/ManageVisibleTabsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/ManageVisibleTabsDialog.kt index 447faa5e..0af6b3ab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/ManageVisibleTabsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/ManageVisibleTabsDialog.kt @@ -3,7 +3,8 @@ package com.simplemobiletools.filemanager.pro.dialogs import androidx.appcompat.app.AlertDialog import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.setupDialogStuff -import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.commons.helpers.TAB_FILES +import com.simplemobiletools.commons.helpers.TAB_RECENT_FILES import com.simplemobiletools.commons.views.MyAppCompatCheckbox import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.extensions.config