Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

allow alerts to be cancelable #405

Merged
merged 3 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions anko/library/robolectricTests/src/test/java/DialogsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import android.view.View
import android.widget.Button
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk15.listeners.onClick
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
Expand Down Expand Up @@ -41,6 +40,15 @@ open class DialogsTestActivity : Activity() {
}.show()
}
}
button {
id = 4
onClick {
alert("Message", "NonCancelable") {
isCancelable = false
positiveButton("Ok") { dialog -> dialog.dismiss() }
}.show()
}
}
}
}
}
Expand All @@ -53,6 +61,7 @@ open class DialogsTestActivity : Activity() {
val button1 = activity.findViewById(1) as Button
val button2 = activity.findViewById(2) as Button
val button3 = activity.findViewById(3) as Button
val button4 = activity.findViewById(4) as Button

button1.performClick()
ShadowLooper.idleMainLooper()
Expand All @@ -77,7 +86,15 @@ open class DialogsTestActivity : Activity() {
assertEquals(View.GONE, alert.getButton(DialogInterface.BUTTON_NEUTRAL).visibility)
alert.dismiss()

ShadowLooper.idleMainLooper()

button4.performClick()
val nonCancelableAlert = ShadowAlertDialog.getLatestAlertDialog()
assertNotNull(nonCancelableAlert)
val nonCancelableAlertShadow = ShadowExtractor.extract(nonCancelableAlert) as ShadowAlertDialog
assertEquals("NonCancelable", nonCancelableAlertShadow.title.toString())
assertFalse(nonCancelableAlertShadow.isCancelable)

println("[COMPLETE]")
}

}
7 changes: 6 additions & 1 deletion anko/library/static/appcompatV7/src/SupportAlertBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.jetbrains.anko.appcompat.v7

import org.jetbrains.anko.*
import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.Drawable
import android.support.v7.app.AlertDialog
import android.view.KeyEvent
import android.view.View
import org.jetbrains.anko.AlertBuilder
import org.jetbrains.anko.AlertBuilderFactory
import org.jetbrains.anko.internals.AnkoInternals
import org.jetbrains.anko.internals.AnkoInternals.NO_GETTER
import kotlin.DeprecationLevel.ERROR
Expand Down Expand Up @@ -64,6 +65,10 @@ internal class AppcompatAlertBuilder(override val ctx: Context) : AlertBuilder<A
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setView(value) }

override var isCancelable: Boolean
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setCancelable(value) }

override fun onCancelled(handler: (DialogInterface) -> Unit) {
builder.setOnCancelListener(handler)
}
Expand Down
3 changes: 3 additions & 0 deletions anko/library/static/commons/src/dialogs/AlertBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ interface AlertBuilder<out D : DialogInterface> {
var customView: View
@Deprecated(NO_GETTER, level = ERROR) get

var isCancelable: Boolean
@Deprecated(NO_GETTER, level = ERROR) get

fun onCancelled(handler: (dialog: DialogInterface) -> Unit)

fun onKeyPressed(handler: (dialog: DialogInterface, keyCode: Int, e: KeyEvent) -> Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<Ale
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setView(value) }

override var isCancelable: Boolean
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setCancelable(value) }

override fun onCancelled(handler: (DialogInterface) -> Unit) {
builder.setOnCancelListener(handler)
}
Expand Down