Skip to content

Commit 21d90ad

Browse files
committed
Add expandable preferences for shortcuts manager list
Reduces list scroll and optimize navigation by implement categories
1 parent 93fd271 commit 21d90ad

File tree

16 files changed

+269
-137
lines changed

16 files changed

+269
-137
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.wstxda.gsl.fragments.preferences
2+
3+
import android.content.Context
4+
import android.os.Build
5+
import android.os.Bundle
6+
import android.os.Parcelable
7+
import android.util.AttributeSet
8+
import android.widget.ImageView
9+
import androidx.preference.PreferenceGroup
10+
import androidx.preference.PreferenceViewHolder
11+
import com.wstxda.gsl.R
12+
13+
class ExpandablePreferences @JvmOverloads constructor(
14+
context: Context,
15+
attrs: AttributeSet? = null,
16+
) : PreferenceGroup(context, attrs) {
17+
18+
private var expanded = false
19+
private var indicatorView: ImageView? = null
20+
21+
init {
22+
isPersistent = false
23+
layoutResource = R.layout.preference_material_expandable
24+
}
25+
26+
override fun onBindViewHolder(holder: PreferenceViewHolder) {
27+
super.onBindViewHolder(holder)
28+
29+
indicatorView = holder.findViewById(R.id.expanded_indicator) as? ImageView
30+
updateIndicator()
31+
32+
holder.itemView.setOnClickListener {
33+
toggleExpanded()
34+
}
35+
}
36+
37+
private fun toggleExpanded() {
38+
expanded = !expanded
39+
updateChildrenVisibility()
40+
updateIndicator()
41+
}
42+
43+
private fun updateChildrenVisibility() {
44+
for (i in 0 until preferenceCount) {
45+
getPreference(i).isVisible = expanded
46+
}
47+
}
48+
49+
private fun updateIndicator() {
50+
indicatorView?.setImageResource(
51+
if (expanded) R.drawable.ic_arrow_up
52+
else R.drawable.ic_arrow_down
53+
)
54+
}
55+
56+
override fun onAttached() {
57+
super.onAttached()
58+
updateChildrenVisibility()
59+
}
60+
61+
override fun onSaveInstanceState(): Parcelable {
62+
return Bundle().apply {
63+
putParcelable("super", super.onSaveInstanceState())
64+
putBoolean("expanded", expanded)
65+
}
66+
}
67+
68+
override fun onRestoreInstanceState(state: Parcelable?) {
69+
if (state is Bundle) {
70+
expanded = state.getBoolean("expanded", false)
71+
val superState = if (Build.VERSION.SDK_INT >= 33) {
72+
state.getParcelable("super", Parcelable::class.java)
73+
} else {
74+
@Suppress("DEPRECATION") state.getParcelable("super")
75+
}
76+
super.onRestoreInstanceState(superState)
77+
updateChildrenVisibility()
78+
} else {
79+
super.onRestoreInstanceState(state)
80+
}
81+
}
82+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:tint="?attr/colorControlNormal"
5+
android:viewportWidth="960"
6+
android:viewportHeight="960">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M480,599Q472,599 465,596.5Q458,594 452,588L268,404Q257,393 257,376Q257,359 268,348Q279,337 296,337Q313,337 324,348L480,504L636,348Q647,337 664,337Q681,337 692,348Q703,359 703,376Q703,393 692,404L508,588Q502,594 495,596.5Q488,599 480,599Z" />
10+
</vector>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:tint="?attr/colorControlNormal"
5+
android:viewportWidth="960"
6+
android:viewportHeight="960">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M480,432L324,588Q313,599 296,599Q279,599 268,588Q257,577 257,560Q257,543 268,532L452,348Q464,336 480,336Q496,336 508,348L692,532Q703,543 703,560Q703,577 692,588Q681,599 664,599Q647,599 636,588L480,432Z" />
10+
</vector>

app/src/main/res/drawable/preference_background_highlight_top.xml

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="fill_parent"
4+
android:layout_height="wrap_content"
5+
android:layout_marginStart="32dp"
6+
android:layout_marginTop="20dp"
7+
android:layout_marginEnd="32dp"
8+
android:layout_marginBottom="18dp"
9+
android:baselineAligned="false"
10+
android:orientation="horizontal">
11+
12+
<com.google.android.material.textview.MaterialTextView
13+
android:id="@android:id/title"
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:textAppearance="?attr/textAppearanceLabelLarge"
17+
android:textColor="?attr/colorPrimary"
18+
tools:text="@string/pref_category_about" />
19+
</androidx.appcompat.widget.LinearLayoutCompat>
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:app="http://schemas.android.com/apk/res-auto"
32
xmlns:tools="http://schemas.android.com/tools"
43
android:layout_width="match_parent"
54
android:layout_height="wrap_content"
65
android:layout_marginStart="16dp"
6+
android:layout_marginTop="4dp"
77
android:layout_marginEnd="16dp"
8-
android:layout_marginBottom="2dp"
9-
android:background="@drawable/preference_background_highlight_top"
8+
android:layout_marginBottom="4dp"
9+
android:background="@drawable/preference_background_highlight_single"
1010
android:baselineAligned="false"
1111
android:clickable="true"
1212
android:focusable="true"
1313
android:orientation="horizontal">
1414

15-
<com.google.android.material.imageview.ShapeableImageView
16-
android:id="@android:id/icon"
17-
android:layout_width="wrap_content"
18-
android:layout_height="wrap_content"
19-
android:layout_gravity="start|center_vertical"
20-
android:layout_marginStart="16dp"
21-
android:importantForAccessibility="no"
22-
app:tint="?attr/colorOnSecondaryContainer"
23-
tools:src="@drawable/ic_open" />
24-
2515
<androidx.appcompat.widget.LinearLayoutCompat
2616
android:layout_width="0dp"
2717
android:layout_height="wrap_content"
@@ -40,26 +30,14 @@
4030
android:ellipsize="marquee"
4131
android:maxLines="2"
4232
android:textAppearance="?attr/textAppearanceTitleMedium"
43-
android:textColor="?attr/colorOnSecondaryContainer"
44-
tools:text="@string/pref_setup_digital_assistant" />
45-
46-
<com.google.android.material.textview.MaterialTextView
47-
android:id="@android:id/summary"
48-
android:layout_width="wrap_content"
49-
android:layout_height="wrap_content"
50-
android:layout_marginTop="2dp"
51-
android:ellipsize="marquee"
52-
android:maxLines="8"
53-
android:textAppearance="?attr/textAppearanceBodyMedium"
54-
android:textColor="?attr/colorSecondary"
55-
tools:text="@string/pref_setup_digital_assistant_summary" />
33+
tools:text="@string/pref_theme" />
5634
</androidx.appcompat.widget.LinearLayoutCompat>
5735

58-
<androidx.appcompat.widget.LinearLayoutCompat
59-
android:id="@android:id/widget_frame"
60-
android:layout_width="wrap_content"
61-
android:layout_height="wrap_content"
36+
<com.google.android.material.imageview.ShapeableImageView
37+
android:id="@+id/expanded_indicator"
38+
android:layout_width="24dp"
39+
android:layout_height="24dp"
6240
android:layout_gravity="center_vertical"
6341
android:layout_marginEnd="16dp"
64-
android:orientation="vertical" />
42+
android:src="@drawable/ic_arrow_down" />
6543
</androidx.appcompat.widget.LinearLayoutCompat>

app/src/main/res/layout/preference_material_shortcuts_bottom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
android:layout_height="wrap_content"
55
android:layout_marginStart="16dp"
66
android:layout_marginEnd="16dp"
7+
android:layout_marginBottom="4dp"
78
android:background="@drawable/preference_background_bottom"
89
android:baselineAligned="false"
910
android:clickable="true"

app/src/main/res/layout/preference_material_shortcuts_top.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
android:layout_height="wrap_content"
55
android:layout_marginStart="16dp"
66
android:layout_marginEnd="16dp"
7+
android:layout_marginTop="4dp"
78
android:layout_marginBottom="2dp"
89
android:background="@drawable/preference_background_top"
910
android:baselineAligned="false"

app/src/main/res/values-ar/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<string name="app_settings">الإعدادات</string>
55
<string name="app_library">المكتبة</string>
66

7+
<string name="pref_expandable_search">اختصارات البحث</string>
8+
<string name="pref_expandable_manager">اختصارات المدير</string>
9+
<string name="pref_expandable_others">اختصارات أخرى</string>
10+
711
<string name="pref_category_shortcuts_manager">إدارة الاختصارات</string>
812
<string name="pref_category_shortcuts_settings">إعدادات الاختصارات</string>
913
<string name="pref_category_other">أخرى</string>

app/src/main/res/values-es/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<string name="app_settings">Ajustes</string>
55
<string name="app_library">Biblioteca</string>
66

7+
<string name="pref_expandable_search">Atajos de búsqueda</string>
8+
<string name="pref_expandable_manager">Atajos del gestor</string>
9+
<string name="pref_expandable_others">Otros atajos</string>
10+
711
<string name="pref_category_shortcuts_manager">Gestionar atajos</string>
812
<string name="pref_category_shortcuts_settings">Configuración de atajos</string>
913
<string name="pref_category_other">Otros</string>

0 commit comments

Comments
 (0)