Skip to content

Commit

Permalink
Use applyIf
Browse files Browse the repository at this point in the history
  • Loading branch information
avdyushin committed Nov 8, 2019
1 parent 10df591 commit 10b8071
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions alerts/src/androidLibMain/kotlin/Alerts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Copyright 2019 Splendo Consulting B.V. The Netherlands
*/

inline fun <T> T.applyIf(condition: Boolean, block: T.() -> Unit): T = apply {

This comment has been minimized.

Copy link
@thoutbeckers

thoutbeckers Nov 8, 2019

Collaborator

Can move to the main package maybe? If you put it in components we'll split it to a base package later.

if (condition) block(this)
}

actual class AlertBuilder(private val context: Context) : BaseAlertBuilder() {
override fun create() = AlertInterface(createAlert(), context)
}
Expand Down Expand Up @@ -51,26 +55,23 @@ actual class AlertInterface(
alertDialog = AlertDialog.Builder(context)
.setTitle(alert.title)
.setMessage(alert.message)
.apply {
if (alert.style == Alert.Style.ACTION_LIST) {
val actions = alert.actions.toTypedArray()
val titles = actions.map { it.title }.toTypedArray()
setItems(titles) { _, which ->
val action = alert.actions[which].apply { handler() }
afterHandler(action)
}
.applyIf(alert.style == Alert.Style.ACTION_LIST) {
val titles = alert.actions.map { it.title }.toTypedArray()
setItems(titles) { _, which ->
val action = alert.actions[which].apply { handler() }
afterHandler(action)
}
}
.create()
.apply {
if (alert.style == Alert.Style.ALERT) {
alert.actions.forEach { action ->
setButton(transform(action.style), action.title) { _, _ ->
action.handler()
afterHandler(action)
}
.applyIf(alert.style == Alert.Style.ALERT) {
alert.actions.forEach { action ->
setButton(transform(action.style), action.title) { _, _ ->
action.handler()
afterHandler(action)
}
}
}
.apply {
setOnDismissListener { alertDialog = null }
setOnCancelListener { afterHandler(null) }
show()
Expand Down

0 comments on commit 10b8071

Please sign in to comment.