From 3f849da18f6731d7c69ccefe3752d29ff5dc3fff Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 29 Oct 2020 22:40:36 +0100 Subject: [PATCH] allow changing the column count with gestures and menu buttons --- app/build.gradle | 2 +- .../pro/activities/MainActivity.kt | 6 +++ .../pro/fragments/ItemsFragment.kt | 45 ++++++++++++++++++- .../filemanager/pro/helpers/Config.kt | 19 ++++++++ .../filemanager/pro/helpers/Constants.kt | 3 ++ app/src/main/res/menu/menu.xml | 8 ++++ 6 files changed, 81 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 72c0f441..f416adb4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,7 +58,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.31.15' + implementation 'com.simplemobiletools:commons:5.31.16' implementation 'com.github.Stericson:RootTools:df729dcb13' implementation 'com.github.Stericson:RootShell:1.6' implementation 'com.alexvasilkov:gesture-views:2.5.2' 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 f09ac34b..73f73da9 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 @@ -25,6 +25,7 @@ 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.helpers.MAX_COLUMN_COUNT import com.simplemobiletools.filemanager.pro.helpers.RootHelpers import com.stericson.RootTools.RootTools import kotlinx.android.synthetic.main.activity_main.* @@ -95,6 +96,9 @@ class MainActivity : SimpleActivity() { 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.viewType == VIEW_TYPE_GRID && config.fileColumnCnt < MAX_COLUMN_COUNT + findItem(R.id.reduce_column_count).isVisible = config.viewType == VIEW_TYPE_GRID && config.fileColumnCnt > 1 } return true @@ -111,6 +115,8 @@ class MainActivity : SimpleActivity() { R.id.change_view_type -> changeViewType() R.id.temporarily_show_hidden -> tryToggleTemporarilyShowHidden() R.id.stop_showing_hidden -> tryToggleTemporarilyShowHidden() + R.id.increase_column_count -> fragment.increaseColumnCount() + R.id.reduce_column_count -> fragment.reduceColumnCount() R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java)) R.id.about -> launchAbout() else -> return super.onOptionsItemSelected(item) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt index 5528d7af..98756bf8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt @@ -16,6 +16,7 @@ import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.views.Breadcrumbs import com.simplemobiletools.commons.views.MyGridLayoutManager +import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.activities.MainActivity import com.simplemobiletools.filemanager.pro.activities.SimpleActivity @@ -24,6 +25,7 @@ import com.simplemobiletools.filemanager.pro.dialogs.CreateNewItemDialog import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.extensions.isPathOnRoot import com.simplemobiletools.filemanager.pro.extensions.tryOpenPathIntent +import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT import com.simplemobiletools.filemanager.pro.helpers.PATH import com.simplemobiletools.filemanager.pro.helpers.RootHelpers import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener @@ -45,6 +47,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb private var isSearchOpen = false private var lastSearchedText = "" private var scrollStates = HashMap() + private var zoomListener: MyRecyclerView.MyZoomListener? = null private var storedItems = ArrayList() private var storedTextColor = 0 @@ -69,6 +72,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb breadcrumbs.updateFontSize(context!!.getTextSize()) } setupLayoutManager(false) + initZoomListener() } override fun onSaveInstanceState(outState: Bundle) { @@ -173,6 +177,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb ItemsAdapter(activity as SimpleActivity, storedItems, this@ItemsFragment, items_list, isPickMultipleIntent, items_fastscroller) { itemClicked(it as FileDirItem) }.apply { + setupZoomListener(zoomListener) items_list.adapter = this } @@ -421,12 +426,50 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb private fun setupGridLayoutManager() { val layoutManager = mView.items_list.layoutManager as MyGridLayoutManager - layoutManager.spanCount = 4 + layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3 } private fun setupListLayoutManager() { val layoutManager = mView.items_list.layoutManager as MyGridLayoutManager layoutManager.spanCount = 1 + zoomListener = null + } + + private fun initZoomListener() { + if (context?.config?.viewType == VIEW_TYPE_GRID) { + val layoutManager = mView.items_list.layoutManager as MyGridLayoutManager + zoomListener = object : MyRecyclerView.MyZoomListener { + override fun zoomIn() { + if (layoutManager.spanCount > 1) { + reduceColumnCount() + getRecyclerAdapter()?.finishActMode() + } + } + + override fun zoomOut() { + if (layoutManager.spanCount < MAX_COLUMN_COUNT) { + increaseColumnCount() + getRecyclerAdapter()?.finishActMode() + } + } + } + } else { + zoomListener = null + } + } + + fun increaseColumnCount() { + context?.config?.fileColumnCnt = ++(mView.items_list.layoutManager as MyGridLayoutManager).spanCount + columnCountChanged() + } + + fun reduceColumnCount() { + context?.config?.fileColumnCnt = --(mView.items_list.layoutManager as MyGridLayoutManager).spanCount + columnCountChanged() + } + + private fun columnCountChanged() { + mView.items_list.adapter?.notifyDataSetChanged() } override fun breadcrumbClicked(id: Int) { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Config.kt index 7de38509..8df7a8b1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Config.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.filemanager.pro.helpers import android.content.Context +import android.content.res.Configuration import com.simplemobiletools.commons.extensions.getInternalStoragePath import com.simplemobiletools.commons.helpers.BaseConfig import com.simplemobiletools.commons.helpers.VIEW_TYPE_LIST @@ -90,4 +91,22 @@ class Config(context: Context) : BaseConfig(context) { } fun hasCustomViewType(path: String) = prefs.contains(VIEW_TYPE_PREFIX + path.toLowerCase()) + + var fileColumnCnt: Int + get() = prefs.getInt(getFileColumnsField(), getDefaultFileColumnCount()) + set(fileColumnCnt) = prefs.edit().putInt(getFileColumnsField(), fileColumnCnt).apply() + + private fun getFileColumnsField(): String { + val isPortrait = context.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT + return if (isPortrait) { + FILE_COLUMN_CNT + } else { + FILE_LANDSCAPE_COLUMN_CNT + } + } + + private fun getDefaultFileColumnCount(): Int { + val isPortrait = context.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT + return if (isPortrait) 3 else 5 + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Constants.kt index fdc234f9..d8286937 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/Constants.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.filemanager.pro.helpers const val PATH = "path" +const val MAX_COLUMN_COUNT = 20 // shared preferences const val SHOW_HIDDEN = "show_hidden" @@ -11,6 +12,8 @@ const val ENABLE_ROOT_ACCESS = "enable_root_access" const val EDITOR_TEXT_ZOOM = "editor_text_zoom" const val VIEW_TYPE = "view_type" const val VIEW_TYPE_PREFIX = "view_type_folder_" +const val FILE_COLUMN_CNT = "file_column_cnt" +const val FILE_LANDSCAPE_COLUMN_CNT = "file_landscape_column_cnt" // open as const val OPEN_AS_DEFAULT = 0 diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index c09c8876..c2fb70b6 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -48,6 +48,14 @@ android:id="@+id/stop_showing_hidden" android:title="@string/stop_showing_hidden" app:showAsAction="never"/> + +