Bläddra i källkod

Remove top margin of PreferenceCategory if no title set (#5168)

Ivan Iskandar 3 år sedan
förälder
incheckning
b5a700276a

+ 2 - 1
app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt

@@ -16,6 +16,7 @@ import androidx.preference.PreferenceScreen
 import androidx.preference.SwitchPreferenceCompat
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.util.system.getResourceColor
+import eu.kanade.tachiyomi.widget.preference.AdaptiveTitlePreferenceCategory
 import eu.kanade.tachiyomi.widget.preference.IntListPreference
 import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory
 import eu.kanade.tachiyomi.widget.preference.SwitchSettingsPreference
@@ -77,7 +78,7 @@ inline fun PreferenceGroup.multiSelectListPreference(block: (@DSL MultiSelectLis
 }
 
 inline fun PreferenceScreen.preferenceCategory(block: (@DSL PreferenceCategory).() -> Unit): PreferenceCategory {
-    return addThenInit(PreferenceCategory(context), block)
+    return addThenInit(AdaptiveTitlePreferenceCategory(context), block)
 }
 
 inline fun PreferenceScreen.preferenceScreen(block: (@DSL PreferenceScreen).() -> Unit): PreferenceScreen {

+ 22 - 0
app/src/main/java/eu/kanade/tachiyomi/widget/preference/AdaptiveTitlePreferenceCategory.kt

@@ -0,0 +1,22 @@
+package eu.kanade.tachiyomi.widget.preference
+
+import android.content.Context
+import androidx.core.view.updateLayoutParams
+import androidx.preference.PreferenceCategory
+import androidx.preference.PreferenceViewHolder
+import androidx.recyclerview.widget.RecyclerView
+
+/**
+ * PreferenceCategory that hides the title placeholder layout if the title is unset
+ */
+class AdaptiveTitlePreferenceCategory(context: Context) : PreferenceCategory(context) {
+    override fun onBindViewHolder(holder: PreferenceViewHolder) {
+        super.onBindViewHolder(holder)
+        if (title.isNullOrBlank()) {
+            holder.itemView.updateLayoutParams<RecyclerView.LayoutParams> {
+                height = 0
+                topMargin = 0
+            }
+        }
+    }
+}