Skip to content

Commit 200e2a5

Browse files
committed
show only the selected tabs
1 parent 1e713ab commit 200e2a5

File tree

3 files changed

+83
-24
lines changed

3 files changed

+83
-24
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.ClipData
66
import android.content.Context
77
import android.content.Intent
88
import android.graphics.drawable.ColorDrawable
9+
import android.graphics.drawable.Drawable
910
import android.media.RingtoneManager
1011
import android.os.Bundle
1112
import android.os.Handler
@@ -52,11 +53,13 @@ class MainActivity : SimpleActivity() {
5253
private var storedFontSize = 0
5354
private var storedDateFormat = ""
5455
private var storedTimeFormat = ""
56+
private var storedShowTabs = 0
5557

5658
override fun onCreate(savedInstanceState: Bundle?) {
5759
super.onCreate(savedInstanceState)
5860
setContentView(R.layout.activity_main)
5961
appLaunched(BuildConfig.APPLICATION_ID)
62+
setupTabColors(config.lastUsedViewPagerPage)
6063
storeStateVariables()
6164
mIsPasswordProtectionPending = config.isAppPasswordProtectionOn
6265

@@ -79,6 +82,12 @@ class MainActivity : SimpleActivity() {
7982

8083
override fun onResume() {
8184
super.onResume()
85+
if (storedShowTabs != config.showTabs) {
86+
config.lastUsedViewPagerPage = 0
87+
System.exit(0)
88+
return
89+
}
90+
8291
getAllFragments().forEach {
8392
it?.setupColors(config.textColor, config.primaryColor)
8493
}
@@ -129,7 +138,7 @@ class MainActivity : SimpleActivity() {
129138

130139
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
131140
val favorites = config.favorites
132-
val currentFragment = getCurrentFragment() ?: return true
141+
val currentFragment = getCurrentFragment()
133142

134143
menu!!.apply {
135144
findItem(R.id.search).isVisible = currentFragment is ItemsFragment
@@ -154,10 +163,6 @@ class MainActivity : SimpleActivity() {
154163
}
155164

156165
override fun onOptionsItemSelected(item: MenuItem): Boolean {
157-
if (getCurrentFragment() == null) {
158-
return true
159-
}
160-
161166
when (item.itemId) {
162167
R.id.go_home -> goHome()
163168
R.id.go_to_favorite -> goToFavorite()
@@ -180,7 +185,7 @@ class MainActivity : SimpleActivity() {
180185

181186
override fun onSaveInstanceState(outState: Bundle) {
182187
super.onSaveInstanceState(outState)
183-
outState.putString(PICKED_PATH, items_fragment.currentPath)
188+
outState.putString(PICKED_PATH, items_fragment?.currentPath ?: "")
184189
outState.putBoolean(WAS_PROTECTION_HANDLED, mWasProtectionHandled)
185190
}
186191

@@ -227,7 +232,7 @@ class MainActivity : SimpleActivity() {
227232

228233
override fun onQueryTextChange(newText: String): Boolean {
229234
if (isSearchOpen) {
230-
getCurrentFragment()?.searchQueryChanged(newText)
235+
getCurrentFragment().searchQueryChanged(newText)
231236
}
232237
return true
233238
}
@@ -254,6 +259,7 @@ class MainActivity : SimpleActivity() {
254259
storedFontSize = fontSize
255260
storedDateFormat = dateFormat
256261
storedTimeFormat = context.getTimeFormat()
262+
storedShowTabs = showTabs
257263
}
258264
}
259265

@@ -328,12 +334,10 @@ class MainActivity : SimpleActivity() {
328334
}
329335
)
330336

331-
setupTabColors(tabToOpen)
332-
333337
main_view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
334338
override fun onPageScrollStateChanged(state: Int) {
335339
if (isSearchOpen) {
336-
getCurrentFragment()?.searchQueryChanged("")
340+
getCurrentFragment().searchQueryChanged("")
337341
searchMenuItem?.collapseActionView()
338342
}
339343
}
@@ -348,6 +352,27 @@ class MainActivity : SimpleActivity() {
348352
invalidateOptionsMenu()
349353
}
350354
})
355+
356+
main_tabs_holder.removeAllTabs()
357+
var skippedTabs = 0
358+
tabsList.forEachIndexed { index, value ->
359+
if (config.showTabs and value == 0) {
360+
skippedTabs++
361+
} else {
362+
val tab = main_tabs_holder.newTab().setIcon(getTabIcon(index))
363+
main_tabs_holder.addTab(tab, index - skippedTabs, config.lastUsedViewPagerPage == index - skippedTabs)
364+
}
365+
}
366+
367+
// selecting the proper tab sometimes glitches, add an extra selector to make sure we have it right
368+
main_tabs_holder.onGlobalLayout {
369+
Handler().postDelayed({
370+
main_tabs_holder.getTabAt(config.lastUsedViewPagerPage)?.select()
371+
invalidateOptionsMenu()
372+
}, 100L)
373+
}
374+
375+
main_tabs_holder.beVisibleIf(skippedTabs < tabsList.size - 1)
351376
}
352377

353378
private fun setupTabColors(lastUsedTab: Int) {
@@ -372,6 +397,15 @@ class MainActivity : SimpleActivity() {
372397
main_tabs_holder.getTabAt(main_view_pager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())
373398
}
374399

400+
private fun getTabIcon(position: Int): Drawable {
401+
val drawableId = when (position) {
402+
0 -> R.drawable.ic_folder_vector
403+
else -> R.drawable.ic_clock_vector
404+
}
405+
406+
return resources.getColoredDrawableWithColor(drawableId, config.textColor)
407+
}
408+
375409
private fun checkOTGPath() {
376410
ensureBackgroundThread {
377411
if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) {
@@ -402,23 +436,23 @@ class MainActivity : SimpleActivity() {
402436
}
403437

404438
private fun goHome() {
405-
if (config.homeFolder != getCurrentFragment()?.currentPath) {
439+
if (config.homeFolder != getCurrentFragment().currentPath) {
406440
openPath(config.homeFolder)
407441
}
408442
}
409443

410444
private fun showSortingDialog() {
411-
ChangeSortingDialog(this, getCurrentFragment()!!.currentPath) {
445+
ChangeSortingDialog(this, getCurrentFragment().currentPath) {
412446
(getCurrentFragment() as? ItemsFragment)?.refreshItems()
413447
}
414448
}
415449

416450
private fun addFavorite() {
417-
config.addFavorite(getCurrentFragment()!!.currentPath)
451+
config.addFavorite(getCurrentFragment().currentPath)
418452
}
419453

420454
private fun removeFavorite() {
421-
config.removeFavorite(getCurrentFragment()!!.currentPath)
455+
config.removeFavorite(getCurrentFragment().currentPath)
422456
}
423457

424458
private fun toggleFilenameVisibility() {
@@ -448,7 +482,7 @@ class MainActivity : SimpleActivity() {
448482
favorites.forEachIndexed { index, path ->
449483
val visiblePath = humanizePath(path).replace("/", " / ")
450484
items.add(RadioItem(index, visiblePath, path))
451-
if (path == getCurrentFragment()!!.currentPath) {
485+
if (path == getCurrentFragment().currentPath) {
452486
currFavoriteIndex = index
453487
}
454488
}
@@ -459,12 +493,12 @@ class MainActivity : SimpleActivity() {
459493
}
460494

461495
private fun setAsHome() {
462-
config.homeFolder = getCurrentFragment()!!.currentPath
496+
config.homeFolder = getCurrentFragment().currentPath
463497
toast(R.string.home_folder_updated)
464498
}
465499

466500
private fun changeViewType() {
467-
ChangeViewTypeDialog(this, getCurrentFragment()!!.currentPath, getCurrentFragment() is ItemsFragment) {
501+
ChangeViewTypeDialog(this, getCurrentFragment().currentPath, getCurrentFragment() is ItemsFragment) {
468502
getAllFragments().forEach {
469503
it?.refreshItems()
470504
}
@@ -509,7 +543,7 @@ class MainActivity : SimpleActivity() {
509543
return
510544
}
511545

512-
if (getCurrentFragment()!!.breadcrumbs.childCount <= 1) {
546+
if (getCurrentFragment().breadcrumbs.childCount <= 1) {
513547
if (!wasBackJustPressed && config.pressBackTwice) {
514548
wasBackJustPressed = true
515549
toast(R.string.press_back_again)
@@ -520,8 +554,8 @@ class MainActivity : SimpleActivity() {
520554
finish()
521555
}
522556
} else {
523-
getCurrentFragment()!!.breadcrumbs.removeBreadcrumb()
524-
openPath(getCurrentFragment()!!.breadcrumbs.getLastItem().path)
557+
getCurrentFragment().breadcrumbs.removeBreadcrumb()
558+
openPath(getCurrentFragment().breadcrumbs.getLastItem().path)
525559
}
526560
}
527561

@@ -594,7 +628,19 @@ class MainActivity : SimpleActivity() {
594628

595629
private fun getAllFragments(): ArrayList<MyViewPagerFragment?> = arrayListOf(items_fragment, recents_fragment)
596630

597-
private fun getCurrentFragment() = getAllFragments().getOrNull(main_view_pager.currentItem)
631+
private fun getCurrentFragment(): MyViewPagerFragment {
632+
val showTabs = config.showTabs
633+
val fragments = arrayListOf<MyViewPagerFragment>()
634+
if (showTabs and TAB_FILES != 0) {
635+
fragments.add(items_fragment)
636+
}
637+
638+
if (showTabs and TAB_RECENT_FILES != 0) {
639+
fragments.add(recents_fragment)
640+
}
641+
642+
return fragments[main_view_pager.currentItem]
643+
}
598644

599645
private fun checkWhatsNewDialog() {
600646
arrayListOf<Release>().apply {

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ViewPagerAdapter.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package com.simplemobiletools.filemanager.pro.adapters
33
import android.view.View
44
import android.view.ViewGroup
55
import androidx.viewpager.widget.PagerAdapter
6+
import com.simplemobiletools.commons.helpers.TAB_FILES
7+
import com.simplemobiletools.commons.helpers.TAB_RECENT_FILES
68
import com.simplemobiletools.filemanager.pro.R
79
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
810
import com.simplemobiletools.filemanager.pro.extensions.config
911
import com.simplemobiletools.filemanager.pro.fragments.MyViewPagerFragment
12+
import com.simplemobiletools.filemanager.pro.helpers.tabsList
1013

1114
class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
1215
override fun instantiateItem(container: ViewGroup, position: Int): Any {
@@ -26,12 +29,21 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
2629
container.removeView(item as View)
2730
}
2831

29-
override fun getCount() = 2
32+
override fun getCount() = tabsList.filter { it and activity.config.showTabs != 0 }.size
3033

3134
override fun isViewFromObject(view: View, item: Any) = view == item
3235

3336
private fun getFragment(position: Int): Int {
34-
val fragments = arrayListOf(R.layout.items_fragment, R.layout.recents_fragment)
37+
val showTabs = activity.config.showTabs
38+
val fragments = arrayListOf<Int>()
39+
if (showTabs and TAB_FILES != 0) {
40+
fragments.add(R.layout.items_fragment)
41+
}
42+
43+
if (showTabs and TAB_RECENT_FILES != 0) {
44+
fragments.add(R.layout.recents_fragment)
45+
}
46+
3547
return fragments[position]
3648
}
3749
}

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/ManageVisibleTabsDialog.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package com.simplemobiletools.filemanager.pro.dialogs
33
import androidx.appcompat.app.AlertDialog
44
import com.simplemobiletools.commons.activities.BaseSimpleActivity
55
import com.simplemobiletools.commons.extensions.setupDialogStuff
6-
import com.simplemobiletools.commons.helpers.*
6+
import com.simplemobiletools.commons.helpers.TAB_FILES
7+
import com.simplemobiletools.commons.helpers.TAB_RECENT_FILES
78
import com.simplemobiletools.commons.views.MyAppCompatCheckbox
89
import com.simplemobiletools.filemanager.pro.R
910
import com.simplemobiletools.filemanager.pro.extensions.config

0 commit comments

Comments
 (0)