From c1a04d91a07bb15a859b56e3c57e816ed4a2a68d Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Mon, 7 Sep 2020 23:51:22 +0200 Subject: [PATCH] Add + shorthand operator to add preference items --- .../java/de/Maxr1998/modernpreferences/Preferences.kt | 9 +++++++++ .../modernpreferences/preferences/CollapsePreference.kt | 6 +++--- .../java/de/Maxr1998/modernpreferences/example/Common.kt | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/de/Maxr1998/modernpreferences/Preferences.kt b/library/src/main/java/de/Maxr1998/modernpreferences/Preferences.kt index 70663f45..cfe89e9b 100644 --- a/library/src/main/java/de/Maxr1998/modernpreferences/Preferences.kt +++ b/library/src/main/java/de/Maxr1998/modernpreferences/Preferences.kt @@ -457,5 +457,14 @@ class PreferenceScreen private constructor(builder: Builder) : Preference(builde fun interface Appendable { fun addPreferenceItem(p: Preference) + + operator fun plusAssign(p: Preference) { + addPreferenceItem(p) + } + + operator fun T.unaryPlus(): T { + addPreferenceItem(this) + return this + } } } \ No newline at end of file diff --git a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/CollapsePreference.kt b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/CollapsePreference.kt index 23c13fb1..2a2129e9 100644 --- a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/CollapsePreference.kt +++ b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/CollapsePreference.kt @@ -39,11 +39,11 @@ class CollapsePreference(screen: PreferenceScreen.Builder, key: String) : Prefer } override fun addPreferenceItem(p: Preference) { - checkNotNull(screen) { + val screen = checkNotNull(screen) { "Don't call clearContext before you've finished all addPreferenceItem operations!" } - screen!!.addPreferenceItem(p) - preferences.add(p.apply { visible = false }) + screen += p + preferences += p.apply { visible = false } } fun clearContext() { diff --git a/testapp/src/main/java/de/Maxr1998/modernpreferences/example/Common.kt b/testapp/src/main/java/de/Maxr1998/modernpreferences/example/Common.kt index a8c7fa63..7d40dbda 100644 --- a/testapp/src/main/java/de/Maxr1998/modernpreferences/example/Common.kt +++ b/testapp/src/main/java/de/Maxr1998/modernpreferences/example/Common.kt @@ -114,10 +114,10 @@ object Common { true } } - addPreferenceItem(TestDialog().apply { + +TestDialog().apply { title = "Show dialog" iconRes = R.drawable.ic_info_24dp - }) + } expandText("expand-text") { title = "Expandable text" text = "This is an example implementation of ModernAndroidPreferences, check out " +