Skip to content

Commit

Permalink
Add + shorthand operator to add preference items
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Sep 7, 2020
1 parent 20ec9a4 commit c1a04d9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 : Preference> T.unaryPlus(): T {
addPreferenceItem(this)
return this
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand Down

0 comments on commit c1a04d9

Please sign in to comment.