Make extension settings button inline

This commit is contained in:
arkon 2020-06-06 14:36:04 -04:00
parent 390bdfa93d
commit ca7373c28b
4 changed files with 67 additions and 9 deletions

View file

@ -15,6 +15,7 @@ import androidx.preference.Preference
import androidx.preference.PreferenceGroupAdapter import androidx.preference.PreferenceGroupAdapter
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreferenceCompat
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.DividerItemDecoration.VERTICAL import androidx.recyclerview.widget.DividerItemDecoration.VERTICAL
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -29,12 +30,11 @@ import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController
import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.util.preference.DSL
import eu.kanade.tachiyomi.util.preference.onChange import eu.kanade.tachiyomi.util.preference.onChange
import eu.kanade.tachiyomi.util.preference.onClick
import eu.kanade.tachiyomi.util.preference.preference
import eu.kanade.tachiyomi.util.preference.preferenceCategory import eu.kanade.tachiyomi.util.preference.preferenceCategory
import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.preference.switchSettingsPreference
import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.LocaleHelper
import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.util.view.visible
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
@ -129,8 +129,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
.forEach { source -> .forEach { source ->
val sourcePrefs = mutableListOf<Preference>() val sourcePrefs = mutableListOf<Preference>()
// Source enable/disable val block: (@DSL SwitchPreferenceCompat).() -> Unit = {
switchPreference {
key = getSourceKey(source.id) key = getSourceKey(source.id)
title = if (isMultiSource) { title = if (isMultiSource) {
source.toString() source.toString()
@ -156,14 +155,16 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
.launchIn(scope) .launchIn(scope)
} }
// Source preferences // Source enable/disable
if (source is ConfigurableSource) { if (source is ConfigurableSource) {
preference { switchSettingsPreference {
titleRes = R.string.label_settings block()
onClick { onSettingsClick = View.OnClickListener {
router.pushController(SourcePreferencesController(source.id).withFadeTransaction()) router.pushController(SourcePreferencesController(source.id).withFadeTransaction())
} }
} }
} else {
switchPreference(block)
} }
} }
} }

View file

@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.widget.preference.IntListPreference import eu.kanade.tachiyomi.widget.preference.IntListPreference
import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory
import eu.kanade.tachiyomi.widget.preference.SwitchSettingsPreference
@DslMarker @DslMarker
@Target(AnnotationTarget.TYPE) @Target(AnnotationTarget.TYPE)
@ -51,6 +52,10 @@ inline fun PreferenceGroup.switchPreferenceCategory(block: (@DSL SwitchPreferenc
return initThenAdd(SwitchPreferenceCategory(context), block) return initThenAdd(SwitchPreferenceCategory(context), block)
} }
inline fun PreferenceGroup.switchSettingsPreference(block: (@DSL SwitchSettingsPreference).() -> Unit): SwitchSettingsPreference {
return initThenAdd(SwitchSettingsPreference(context), block)
}
inline fun PreferenceGroup.checkBoxPreference(block: (@DSL CheckBoxPreference).() -> Unit): CheckBoxPreference { inline fun PreferenceGroup.checkBoxPreference(block: (@DSL CheckBoxPreference).() -> Unit): CheckBoxPreference {
return initThenAdd(CheckBoxPreference(context), block) return initThenAdd(CheckBoxPreference(context), block)
} }

View file

@ -0,0 +1,27 @@
package eu.kanade.tachiyomi.widget.preference
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.preference.PreferenceViewHolder
import androidx.preference.SwitchPreferenceCompat
import eu.kanade.tachiyomi.R
import kotlinx.android.synthetic.main.pref_settings.view.button
class SwitchSettingsPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
SwitchPreferenceCompat(context, attrs) {
var onSettingsClick: View.OnClickListener? = null
init {
widgetLayoutResource = R.layout.pref_settings
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
holder.itemView.button.setOnClickListener {
onSettingsClick?.onClick(it)
}
}
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/label_settings"
android:padding="8dp"
app:srcCompat="@drawable/ic_settings_24dp"
app:tint="?attr/colorOnBackground" />
<!-- Matches ID used in SwitchPreferenceCompat -->
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>