From 185e54bab2fd6b97e897e4bb1ff50c2fd021a4a7 Mon Sep 17 00:00:00 2001
From: MM20 <15646950+MM2-0@users.noreply.github.com>
Date: Fri, 17 Dec 2021 20:31:13 +0100
Subject: [PATCH] Make number of grid columns adjustable
---
.../main/res/xml/preferences_appearance.xml | 22 ++++---
.../src/main/res/values-w312dp/integers.xml | 0
.../src/main/res/values-w400dp/integers.xml | 0
.../src/main/res/values-w408dp/integers.xml | 0
.../src/main/res/values-w480dp/integers.xml | 0
.../src/main/res/values-w504dp/integers.xml | 0
.../src/main/res/values-w600dp/integers.xml | 0
i18n/src/main/res/values-de/strings.xml | 3 +
i18n/src/main/res/values/strings.xml | 3 +
preferences/build.gradle.kts | 1 +
.../preferences/LauncherPreferences.kt | 2 +
.../ui/legacy/activity/LauncherActivity.kt | 2 -
.../ui/legacy/component/FavoritesView.kt | 3 +-
.../ui/legacy/search/SearchGridView.kt | 59 +++++++++++++------
ui/src/main/res/layout/view_application.xml | 3 +-
ui/src/main/res/layout/view_favorites.xml | 3 +-
ui/src/main/res/values/SearchGridView.xml | 1 -
17 files changed, 68 insertions(+), 34 deletions(-)
rename {ui => base}/src/main/res/values-w312dp/integers.xml (100%)
rename {ui => base}/src/main/res/values-w400dp/integers.xml (100%)
rename {ui => base}/src/main/res/values-w408dp/integers.xml (100%)
rename {ui => base}/src/main/res/values-w480dp/integers.xml (100%)
rename {ui => base}/src/main/res/values-w504dp/integers.xml (100%)
rename {ui => base}/src/main/res/values-w600dp/integers.xml (100%)
diff --git a/app/src/main/res/xml/preferences_appearance.xml b/app/src/main/res/xml/preferences_appearance.xml
index 6bdbe910..e055770e 100644
--- a/app/src/main/res/xml/preferences_appearance.xml
+++ b/app/src/main/res/xml/preferences_appearance.xml
@@ -10,24 +10,32 @@
app:title="@string/preference_theme" />
+ app:key="card_background"
+ app:summary="%s"
+ app:title="@string/preference_screen_colors" />
+
+
+
+
+ app:title="@string/preference_themed_icons" />
Eingefärbte Symbole
Symbole an das Farbschema der App anpassen
Keine Wetterdaten verfügbar.
+
+ Raster
+ Spaltenanzahl
\ No newline at end of file
diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml
index f5d370ff..ef2b5308 100644
--- a/i18n/src/main/res/values/strings.xml
+++ b/i18n/src/main/res/values/strings.xml
@@ -454,4 +454,7 @@
This feature is not available in this version of %1$s
Clock
Clock style
+
+ Grid
+ Number of columns
diff --git a/preferences/build.gradle.kts b/preferences/build.gradle.kts
index e3f8343a..7c4c79ad 100644
--- a/preferences/build.gradle.kts
+++ b/preferences/build.gradle.kts
@@ -66,6 +66,7 @@ dependencies {
implementation(project(":ktx"))
implementation(project(":i18n"))
+ implementation(project(":base"))
implementation(project(":crashreporter"))
}
\ No newline at end of file
diff --git a/preferences/src/main/java/de/mm20/launcher2/preferences/LauncherPreferences.kt b/preferences/src/main/java/de/mm20/launcher2/preferences/LauncherPreferences.kt
index 67fbc4d0..f41625f0 100644
--- a/preferences/src/main/java/de/mm20/launcher2/preferences/LauncherPreferences.kt
+++ b/preferences/src/main/java/de/mm20/launcher2/preferences/LauncherPreferences.kt
@@ -106,6 +106,8 @@ class LauncherPreferences(val context: Application, version: Int = 3) {
var easterEggEnabled by BooleanPreference("easter_egg", default = false)
+ var gridColumnCount by IntPreference("grid_column_count", default = context.resources.getInteger(R.integer.config_columnCount))
+
fun doOnPreferenceChange(vararg keys: String, action: (String) -> Unit) {
preferences.registerOnSharedPreferenceChangeListener { _, key ->
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/legacy/activity/LauncherActivity.kt b/ui/src/main/java/de/mm20/launcher2/ui/legacy/activity/LauncherActivity.kt
index 01f559ec..0340aef1 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/legacy/activity/LauncherActivity.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/legacy/activity/LauncherActivity.kt
@@ -332,8 +332,6 @@ class LauncherActivity : AppCompatActivity() {
).apply {
setMargins((8 * dp).toInt())
}
- hiddenItemsGrid.columnCount =
- resources.getInteger(R.integer.config_columnCount)
val hiddenItems = favoritesViewModel.hiddenItems
hiddenItems.observe(this) {
hiddenItemsGrid.submitItems(it)
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/legacy/component/FavoritesView.kt b/ui/src/main/java/de/mm20/launcher2/ui/legacy/component/FavoritesView.kt
index e176ab9d..a0da3040 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/legacy/component/FavoritesView.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/legacy/component/FavoritesView.kt
@@ -9,6 +9,7 @@ import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.*
import de.mm20.launcher2.favorites.FavoritesViewModel
+import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.search.data.Searchable
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.databinding.ViewFavoritesBinding
@@ -27,7 +28,7 @@ class FavoritesView : FrameLayout {
init {
val viewModel: FavoritesViewModel by (context as AppCompatActivity).viewModel()
- favorites = viewModel.getFavorites(context.resources.getInteger(R.integer.config_columnCount))
+ favorites = viewModel.getFavorites(LauncherPreferences.instance.gridColumnCount)
favorites.observe(context as AppCompatActivity, Observer {
visibility = if (it?.isEmpty() == true) View.GONE else View.VISIBLE
binding.favoritesGrid.submitItems(it)
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt b/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt
index 4e13f9dc..c3aefb9f 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt
@@ -12,6 +12,7 @@ import de.mm20.launcher2.ktx.ceilToInt
import de.mm20.launcher2.ktx.lifecycleScope
import de.mm20.launcher2.legacy.helper.ActivityStarter
import de.mm20.launcher2.legacy.helper.ActivityStarterCallback
+import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.search.data.Searchable
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.legacy.searchable.SearchableView
@@ -40,21 +41,22 @@ class SearchGridView : ViewGroup, ActivityStarterCallback {
@ObsoleteCoroutinesApi
private val updateActor = lifecycleScope
- .actor>(Dispatchers.Main, capacity = Channel.CONFLATED) {
- for (newItems in channel) {
- val oldItems = currentItems
- val diffResult = withContext(Dispatchers.Default) {
- SearchDiffUtil.calculateDiff(oldItems, newItems)
- }
- currentItems = newItems
- applyDiff(diffResult)
+ .actor>(Dispatchers.Main, capacity = Channel.CONFLATED) {
+ for (newItems in channel) {
+ val oldItems = currentItems
+ val diffResult = withContext(Dispatchers.Default) {
+ SearchDiffUtil.calculateDiff(oldItems, newItems)
}
+ currentItems = newItems
+ applyDiff(diffResult)
}
+ }
@ObsoleteCoroutinesApi
fun submitItems(items: List?) {
if (items == null) return
- if (items.getOrNull(expandedItem)?.key != currentItems.getOrNull(expandedItem)?.key) expandedItem = -1
+ if (items.getOrNull(expandedItem)?.key != currentItems.getOrNull(expandedItem)?.key) expandedItem =
+ -1
lifecycleScope.launch {
updateActor.send(items)
}
@@ -79,10 +81,14 @@ class SearchGridView : ViewGroup, ActivityStarterCallback {
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
- constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int) : super(context, attrs, defStyleRes) {
+ constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int) : super(
+ context,
+ attrs,
+ defStyleRes
+ ) {
attrs?.let {
- val ta = context.theme.obtainStyledAttributes(it, R.styleable.SearchGridView, 0, defStyleRes)
- columnCount = ta.getInt(R.styleable.SearchGridView_columnCount, 1)
+ val ta =
+ context.theme.obtainStyledAttributes(it, R.styleable.SearchGridView, 0, defStyleRes)
rowHeight = ta.getDimensionPixelSize(R.styleable.SearchGridView_rowHeight, -1)
ta.recycle()
}
@@ -93,11 +99,16 @@ class SearchGridView : ViewGroup, ActivityStarterCallback {
ActivityStarter.registerCallback(this)
}
+ init {
+ columnCount = LauncherPreferences.instance.gridColumnCount.takeIf { it > 1 }
+ ?: context.resources.getInteger(R.integer.config_columnCount)
+ }
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val widthSpec = MeasureSpec.makeMeasureSpec(
- (MeasureSpec.getSize(widthMeasureSpec) - paddingLeft - paddingRight) / columnCount,
- MeasureSpec.EXACTLY
+ (MeasureSpec.getSize(widthMeasureSpec) - paddingLeft - paddingRight) / columnCount,
+ MeasureSpec.EXACTLY
)
val heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
@@ -118,7 +129,10 @@ class SearchGridView : ViewGroup, ActivityStarterCallback {
val width = when (MeasureSpec.getMode(widthMeasureSpec)) {
MeasureSpec.EXACTLY -> MeasureSpec.getSize(widthMeasureSpec)
- MeasureSpec.AT_MOST -> min(colWidth * columnCount + paddingLeft + paddingRight, MeasureSpec.getSize(widthMeasureSpec))
+ MeasureSpec.AT_MOST -> min(
+ colWidth * columnCount + paddingLeft + paddingRight,
+ MeasureSpec.getSize(widthMeasureSpec)
+ )
MeasureSpec.UNSPECIFIED -> colWidth * columnCount + paddingLeft + paddingRight
else -> colWidth * columnCount
}
@@ -127,13 +141,16 @@ class SearchGridView : ViewGroup, ActivityStarterCallback {
val visibleChildCount = children.count { it.visibility != View.GONE }
val rowCount = (visibleChildCount / columnCount.toFloat()).ceilToInt()
var height = rowHeight * rowCount + (getChildAt(expandedItem)?.measuredHeight
- ?: 0) + paddingTop + paddingBottom
+ ?: 0) + paddingTop + paddingBottom
if (expandedItem == childCount - 1 && (childCount % columnCount == 1) || expandedItem != -1 && columnCount == 1) {
height -= rowHeight
}
- setMeasuredDimension(View.resolveSize(width, widthMeasureSpec), View.resolveSize(height, heightMeasureSpec))
+ setMeasuredDimension(
+ View.resolveSize(width, widthMeasureSpec),
+ View.resolveSize(height, heightMeasureSpec)
+ )
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
@@ -190,7 +207,8 @@ class SearchGridView : ViewGroup, ActivityStarterCallback {
postponedDiffs.push(diff)
return
}
- val representation = if (columnCount == 1) SearchableView.REPRESENTATION_LIST else SearchableView.REPRESENTATION_GRID
+ val representation =
+ if (columnCount == 1) SearchableView.REPRESENTATION_LIST else SearchableView.REPRESENTATION_GRID
while (diff.isNotEmpty()) {
val action = diff.poll() ?: continue
if (action.action == DiffAction.ACTION_INSERT) {
@@ -241,7 +259,10 @@ class QueueUpdateCallback : ListUpdateCallback {
override fun onRemoved(position: Int, count: Int) {
for (i in 1..count) {
- operations += DiffAction(action = DiffAction.ACTION_DELETE, position = position + (count - i))
+ operations += DiffAction(
+ action = DiffAction.ACTION_DELETE,
+ position = position + (count - i)
+ )
}
}
diff --git a/ui/src/main/res/layout/view_application.xml b/ui/src/main/res/layout/view_application.xml
index 4baf59bf..e49557f6 100644
--- a/ui/src/main/res/layout/view_application.xml
+++ b/ui/src/main/res/layout/view_application.xml
@@ -17,6 +17,5 @@
android:layout_height="wrap_content"
android:padding="8dp"
android:clipChildren="false"
- android:clipToPadding="false"
- app:columnCount="@integer/config_columnCount"/>
+ android:clipToPadding="false"/>
\ No newline at end of file
diff --git a/ui/src/main/res/layout/view_favorites.xml b/ui/src/main/res/layout/view_favorites.xml
index 0cc809ba..d62b0db4 100644
--- a/ui/src/main/res/layout/view_favorites.xml
+++ b/ui/src/main/res/layout/view_favorites.xml
@@ -16,6 +16,5 @@
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
- android:padding="8dp"
- app:columnCount="@integer/config_columnCount" />
+ android:padding="8dp" />
\ No newline at end of file
diff --git a/ui/src/main/res/values/SearchGridView.xml b/ui/src/main/res/values/SearchGridView.xml
index cc51b0da..e1d052ea 100644
--- a/ui/src/main/res/values/SearchGridView.xml
+++ b/ui/src/main/res/values/SearchGridView.xml
@@ -1,7 +1,6 @@
-