Skip to content

Commit

Permalink
handle some menu items at both tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed May 19, 2021
1 parent be0fba6 commit 9143e07
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.simplemobiletools.filemanager.pro.dialogs.ChangeSortingDialog
import com.simplemobiletools.filemanager.pro.dialogs.ChangeViewTypeDialog
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.extensions.tryOpenPathIntent
import com.simplemobiletools.filemanager.pro.fragments.ItemsFragment
import com.simplemobiletools.filemanager.pro.fragments.MyViewPagerFragment
import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT
import com.simplemobiletools.filemanager.pro.helpers.RootHelpers
Expand Down Expand Up @@ -129,22 +130,22 @@ class MainActivity : SimpleActivity() {

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val favorites = config.favorites
val fragment = getCurrentFragment() ?: return true
val currentFragment = getCurrentFragment()

menu!!.apply {
findItem(R.id.add_favorite).isVisible = !favorites.contains(fragment.currentPath)
findItem(R.id.remove_favorite).isVisible = favorites.contains(fragment.currentPath)
findItem(R.id.add_favorite).isVisible = !favorites.contains(currentFragment.currentPath)
findItem(R.id.remove_favorite).isVisible = favorites.contains(currentFragment.currentPath)
findItem(R.id.go_to_favorite).isVisible = favorites.isNotEmpty()

findItem(R.id.toggle_filename).isVisible = config.getFolderViewType(fragment.currentPath) == VIEW_TYPE_GRID
findItem(R.id.go_home).isVisible = fragment.currentPath != config.homeFolder
findItem(R.id.set_as_home).isVisible = fragment.currentPath != config.homeFolder
findItem(R.id.toggle_filename).isVisible = config.getFolderViewType(currentFragment.currentPath) == VIEW_TYPE_GRID
findItem(R.id.go_home).isVisible = currentFragment.currentPath != config.homeFolder
findItem(R.id.set_as_home).isVisible = currentFragment.currentPath != config.homeFolder

findItem(R.id.temporarily_show_hidden).isVisible = !config.shouldShowHidden
findItem(R.id.stop_showing_hidden).isVisible = config.temporarilyShowHidden

findItem(R.id.increase_column_count).isVisible = config.getFolderViewType(fragment.currentPath) == VIEW_TYPE_GRID && config.fileColumnCnt < MAX_COLUMN_COUNT
findItem(R.id.reduce_column_count).isVisible = config.getFolderViewType(fragment.currentPath) == VIEW_TYPE_GRID && config.fileColumnCnt > 1
findItem(R.id.increase_column_count).isVisible = config.getFolderViewType(currentFragment.currentPath) == VIEW_TYPE_GRID && config.fileColumnCnt < MAX_COLUMN_COUNT
findItem(R.id.reduce_column_count).isVisible = config.getFolderViewType(currentFragment.currentPath) == VIEW_TYPE_GRID && config.fileColumnCnt > 1
}

return true
Expand All @@ -157,13 +158,13 @@ class MainActivity : SimpleActivity() {
R.id.sort -> showSortingDialog()
R.id.add_favorite -> addFavorite()
R.id.remove_favorite -> removeFavorite()
R.id.toggle_filename -> getCurrentFragment().toggleFilenameVisibility()
R.id.toggle_filename -> toggleFilenameVisibility()
R.id.set_as_home -> setAsHome()
R.id.change_view_type -> changeViewType()
R.id.temporarily_show_hidden -> tryToggleTemporarilyShowHidden()
R.id.stop_showing_hidden -> tryToggleTemporarilyShowHidden()
R.id.increase_column_count -> getCurrentFragment().increaseColumnCount()
R.id.reduce_column_count -> getCurrentFragment().reduceColumnCount()
R.id.increase_column_count -> increaseColumnCount()
R.id.reduce_column_count -> reduceColumnCount()
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
R.id.about -> launchAbout()
else -> return super.onOptionsItemSelected(item)
Expand All @@ -173,7 +174,7 @@ class MainActivity : SimpleActivity() {

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(PICKED_PATH, getCurrentFragment().currentPath)
outState.putString(PICKED_PATH, items_fragment.currentPath)
outState.putBoolean(WAS_PROTECTION_HANDLED, mWasProtectionHandled)
}

Expand Down Expand Up @@ -230,13 +231,13 @@ class MainActivity : SimpleActivity() {
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
isSearchOpen = true
getCurrentFragment().searchOpened()
(getCurrentFragment() as? ItemsFragment)?.searchOpened()
return true
}

override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
isSearchOpen = false
getCurrentFragment().searchClosed()
(getCurrentFragment() as? ItemsFragment)?.searchClosed()
return true
}
})
Expand Down Expand Up @@ -289,10 +290,10 @@ class MainActivity : SimpleActivity() {
openPath(config.homeFolder)
}

getCurrentFragment()?.apply {
isGetRingtonePicker = intent.action == RingtoneManager.ACTION_RINGTONE_PICKER
isGetContentIntent = intent.action == Intent.ACTION_GET_CONTENT
isPickMultipleIntent = intent.getBooleanExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
getAllFragments().forEach {
it?.isGetRingtonePicker = intent.action == RingtoneManager.ACTION_RINGTONE_PICKER
it?.isGetContentIntent = intent.action == Intent.ACTION_GET_CONTENT
it?.isPickMultipleIntent = intent.getBooleanExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
}
}

Expand Down Expand Up @@ -386,7 +387,7 @@ class MainActivity : SimpleActivity() {
newPath = internalStoragePath
}

getCurrentFragment()?.openPath(newPath, forceRefresh)
items_fragment?.openPath(newPath, forceRefresh)
}

private fun goHome() {
Expand All @@ -397,7 +398,7 @@ class MainActivity : SimpleActivity() {

private fun showSortingDialog() {
ChangeSortingDialog(this, getCurrentFragment().currentPath) {
getCurrentFragment().refreshItems()
(getCurrentFragment() as? ItemsFragment)?.refreshItems()
}
}

Expand All @@ -409,6 +410,24 @@ class MainActivity : SimpleActivity() {
config.removeFavorite(getCurrentFragment().currentPath)
}

private fun toggleFilenameVisibility() {
getAllFragments().forEach {
it?.toggleFilenameVisibility()
}
}

private fun increaseColumnCount() {
getAllFragments().forEach {
it?.increaseColumnCount()
}
}

private fun reduceColumnCount() {
getAllFragments().forEach {
it?.reduceColumnCount()
}
}

private fun goToFavorite() {
val favorites = config.favorites
val items = ArrayList<RadioItem>(favorites.size)
Expand Down Expand Up @@ -469,7 +488,12 @@ class MainActivity : SimpleActivity() {
}

override fun onBackPressed() {
if (getCurrentFragment().breadcrumbs.childCount <= 1) {
if (getCurrentFragment() !is ItemsFragment) {
super.onBackPressed()
return
}

if (getCurrentFragment().breadcrumbs.childCount ?: 2 <= 1) {
if (!wasBackJustPressed && config.pressBackTwice) {
wasBackJustPressed = true
toast(R.string.press_back_again)
Expand Down Expand Up @@ -554,7 +578,10 @@ class MainActivity : SimpleActivity() {

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

private fun getCurrentFragment() = items_fragment
private fun getCurrentFragment(): MyViewPagerFragment = when (main_view_pager.currentItem) {
TAB_FILES -> items_fragment
else -> recents_fragment
}

private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ import java.util.*
import kotlin.collections.ArrayList

class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener, Breadcrumbs.BreadcrumbsListener {
var currentPath = ""
var isGetContentIntent = false
var isGetRingtonePicker = false
var isPickMultipleIntent = false

private var activity: SimpleActivity? = null
private var showHidden = false
private var skipItemUpdating = false
Expand Down Expand Up @@ -476,12 +471,12 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
items_fastscroller.setScrollToY(items_list.computeVerticalScrollOffset())
}

fun increaseColumnCount() {
override fun increaseColumnCount() {
context?.config?.fileColumnCnt = ++(items_list.layoutManager as MyGridLayoutManager).spanCount
columnCountChanged()
}

fun reduceColumnCount() {
override fun reduceColumnCount() {
context?.config?.fileColumnCnt = --(items_list.layoutManager as MyGridLayoutManager).spanCount
columnCountChanged()
}
Expand All @@ -494,7 +489,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
}
}

fun toggleFilenameVisibility() {
override fun toggleFilenameVisibility() {
context?.config?.displayFilenames = !context!!.config.displayFilenames
getRecyclerAdapter()?.updateDisplayFilenamesInGrid()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import android.widget.RelativeLayout
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity

abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
var currentPath = ""
var isGetContentIntent = false
var isGetRingtonePicker = false
var isPickMultipleIntent = false

abstract fun setupFragment(activity: SimpleActivity)

abstract fun setupColors(textColor: Int, adjustedPrimaryColor: Int)
Expand All @@ -17,4 +22,12 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
abstract fun searchQueryChanged(text: String)

abstract fun finishActMode()

abstract fun toggleFilenameVisibility()

abstract fun increaseColumnCount()

abstract fun reduceColumnCount()

abstract fun refreshItems()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyGridLayoutManager
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.extensions.tryOpenPathIntent
import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener
import com.simplemobiletools.filemanager.pro.models.ListItem
Expand All @@ -19,14 +21,25 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
private var activity: SimpleActivity? = null

override fun setupFragment(activity: SimpleActivity) {
this.activity = activity
if (this.activity == null) {
this.activity = activity
recents_swipe_refresh.setOnRefreshListener { refreshItems() }
}

refreshItems()
}

override fun refreshItems() {
ensureBackgroundThread {
getRecents { recents ->
ItemsAdapter(activity, recents, this, recents_list, false, null, recents_swipe_refresh) {
activity.tryOpenPathIntent((it as FileDirItem).path, false)
recents_swipe_refresh?.isRefreshing = false
ItemsAdapter(activity as SimpleActivity, recents, this, recents_list, false, null, recents_swipe_refresh) {
activity?.tryOpenPathIntent((it as FileDirItem).path, false)
}.apply {
recents_list.adapter = this
}

recents_list.scheduleLayoutAnimation()
}
}
}
Expand Down Expand Up @@ -66,7 +79,27 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage

private fun getRecyclerAdapter() = recents_list.adapter as? ItemsAdapter

override fun refreshItems() {}
override fun toggleFilenameVisibility() {
context?.config?.displayFilenames = !context!!.config.displayFilenames
getRecyclerAdapter()?.updateDisplayFilenamesInGrid()
}

override fun increaseColumnCount() {
context?.config?.fileColumnCnt = ++(recents_list.layoutManager as MyGridLayoutManager).spanCount
columnCountChanged()
}

override fun reduceColumnCount() {
context?.config?.fileColumnCnt = --(recents_list.layoutManager as MyGridLayoutManager).spanCount
columnCountChanged()
}

private fun columnCountChanged() {
activity?.invalidateOptionsMenu()
getRecyclerAdapter()?.apply {
notifyItemRangeChanged(0, listItems.size)
}
}

override fun deleteFiles(files: ArrayList<FileDirItem>) {}

Expand Down

0 comments on commit 9143e07

Please sign in to comment.