allow changing the column count with menu buttons

This commit is contained in:
tibbi 2020-11-07 21:12:56 +01:00
parent b253e01e15
commit b04b963ee5
9 changed files with 64 additions and 14 deletions

View File

@ -19,10 +19,13 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.commons.views.MyGridLayoutManager
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.util.* import java.util.*
class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private val MAX_COLUMN_COUNT = 20
private var displayedLaunchers = ArrayList<AppLauncher>() private var displayedLaunchers = ArrayList<AppLauncher>()
private var notDisplayedLaunchers: ArrayList<AppLauncher>? = null private var notDisplayedLaunchers: ArrayList<AppLauncher>? = null
private var mStoredPrimaryColor = 0 private var mStoredPrimaryColor = 0
@ -35,6 +38,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
setupLaunchers() setupLaunchers()
checkWhatsNewDialog() checkWhatsNewDialog()
storeStateVariables() storeStateVariables()
setupGridLayoutManager()
fab.setOnClickListener { fab.setOnClickListener {
if (notDisplayedLaunchers != null) { if (notDisplayedLaunchers != null) {
@ -78,6 +82,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.increase_column_count -> increaseColumnCount()
R.id.reduce_column_count -> reduceColumnCount()
R.id.settings -> launchSettings() R.id.settings -> launchSettings()
R.id.about -> launchAbout() R.id.about -> launchAbout()
else -> return super.onOptionsItemSelected(item) else -> return super.onOptionsItemSelected(item)
@ -135,6 +141,37 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
} }
} }
private fun increaseColumnCount() {
config.columnCnt = ++(launchers_grid.layoutManager as MyGridLayoutManager).spanCount
columnCountChanged()
}
private fun reduceColumnCount() {
config.columnCnt = --(launchers_grid.layoutManager as MyGridLayoutManager).spanCount
columnCountChanged()
}
private fun columnCountChanged() {
invalidateOptionsMenu()
launchers_grid.adapter?.notifyDataSetChanged()
getGridAdapter()?.launchers?.apply {
calculateContentHeight(this)
}
}
private fun calculateContentHeight(directories: List<AppLauncher>) {
val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager
val thumbnailHeight = layoutManager.getChildAt(0)?.height ?: 0
val fullHeight = ((directories.size - 1) / layoutManager.spanCount + 1) * thumbnailHeight
launchers_fastscroller.setContentHeight(fullHeight)
launchers_fastscroller.setScrollToY(launchers_grid.computeVerticalScrollOffset())
}
private fun setupGridLayoutManager() {
val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = config.columnCnt
}
private fun checkInvalidApps() { private fun checkInvalidApps() {
val invalidIds = ArrayList<String>() val invalidIds = ArrayList<String>()
for ((id, name, packageName) in displayedLaunchers) { for ((id, name, packageName) in displayedLaunchers) {

View File

@ -18,7 +18,7 @@ import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import kotlinx.android.synthetic.main.item_app_launcher.view.* import kotlinx.android.synthetic.main.item_app_launcher.view.*
class LaunchersAdapter(activity: SimpleActivity, val launchers: MutableList<AppLauncher>, val listener: RefreshRecyclerViewListener?, class LaunchersAdapter(activity: SimpleActivity, val launchers: ArrayList<AppLauncher>, val listener: RefreshRecyclerViewListener?,
recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) : recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) :
MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.applauncher.helpers package com.simplemobiletools.applauncher.helpers
import android.content.Context import android.content.Context
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.commons.helpers.BaseConfig import com.simplemobiletools.commons.helpers.BaseConfig
class Config(context: Context) : BaseConfig(context) { class Config(context: Context) : BaseConfig(context) {
@ -15,4 +16,8 @@ class Config(context: Context) : BaseConfig(context) {
var closeApp: Boolean var closeApp: Boolean
get() = prefs.getBoolean(CLOSE_APP, true) get() = prefs.getBoolean(CLOSE_APP, true)
set(closeApp) = prefs.edit().putBoolean(CLOSE_APP, closeApp).apply() set(closeApp) = prefs.edit().putBoolean(CLOSE_APP, closeApp).apply()
var columnCnt: Int
get() = prefs.getInt(COLUMN_CNT, context.resources.getInteger(R.integer.column_count))
set(columnCnt) = prefs.edit().putInt(COLUMN_CNT, columnCnt).apply()
} }

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.applauncher.helpers
const val WAS_REMOVE_INFO_SHOWN = "was_remove_info_shown" const val WAS_REMOVE_INFO_SHOWN = "was_remove_info_shown"
const val CLOSE_APP = "close_app" const val CLOSE_APP = "close_app"
const val COLUMN_CNT = "column_cnt"
val predefinedPackageNames = arrayListOf( val predefinedPackageNames = arrayListOf(
"com.simplemobiletools.calculator", "com.simplemobiletools.calculator",

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout" android:id="@+id/coordinator_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -16,18 +15,18 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:clipToPadding="false" android:clipToPadding="false"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin" android:paddingTop="@dimen/activity_margin"
android:paddingBottom="@dimen/activity_margin"
app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager" app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager"
app:spanCount="@integer/columns"/> app:spanCount="@integer/column_count" />
<com.simplemobiletools.commons.views.FastScroller <com.simplemobiletools.commons.views.FastScroller
android:id="@+id/launchers_fastscroller" android:id="@+id/launchers_fastscroller"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_alignTop="@+id/launchers_grid"
android:layout_alignBottom="@+id/launchers_grid" android:layout_alignBottom="@+id/launchers_grid"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/launchers_grid"
android:paddingStart="@dimen/normal_margin"> android:paddingStart="@dimen/normal_margin">
<include layout="@layout/fastscroller_handle_vertical" /> <include layout="@layout/fastscroller_handle_vertical" />

View File

@ -8,4 +8,4 @@
android:padding="@dimen/small_margin" android:padding="@dimen/small_margin"
android:scrollbars="vertical" android:scrollbars="vertical"
app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager" app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager"
app:spanCount="@integer/columns"/> app:spanCount="@integer/column_count"/>

View File

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/increase_column_count"
android:title="@string/increase_column_count"
app:showAsAction="never" />
<item
android:id="@+id/reduce_column_count"
android:title="@string/reduce_column_count"
app:showAsAction="never" />
<item <item
android:id="@+id/settings" android:id="@+id/settings"
android:title="@string/settings" android:title="@string/settings"

View File

@ -1,3 +1,3 @@
<resources> <resources>
<integer name="columns">5</integer> <integer name="column_count">5</integer>
</resources> </resources>

View File

@ -1,3 +1,3 @@
<resources> <resources>
<integer name="columns">4</integer> <integer name="column_count">4</integer>
</resources> </resources>