From b5a700276a00f5b630d35d1b902bd624905c6c01 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Mon, 24 May 2021 20:10:13 +0700 Subject: [PATCH] Remove top margin of PreferenceCategory if no title set (#5168) --- .../util/preference/PreferenceDSL.kt | 3 ++- .../AdaptiveTitlePreferenceCategory.kt | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/AdaptiveTitlePreferenceCategory.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt b/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt index 148b91ae2b..03a74b48dc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt +++ b/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 { diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/AdaptiveTitlePreferenceCategory.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/AdaptiveTitlePreferenceCategory.kt new file mode 100644 index 0000000000..d0448549d7 --- /dev/null +++ b/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 { + height = 0 + topMargin = 0 + } + } + } +}