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

Commit 2c7c805

Browse files
deva6664u7
authored andcommitted
allow alerts to be cancelable (#405)
* add method for setting dialog cancelable * add test for cancelable dialog * use property access syntax
1 parent 6bc217c commit 2c7c805

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

anko/library/robolectricTests/src/test/java/DialogsTest.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import android.view.View
77
import android.widget.Button
88
import org.jetbrains.anko.*
99
import org.jetbrains.anko.sdk15.listeners.onClick
10-
import org.junit.Assert.assertEquals
11-
import org.junit.Assert.assertNotNull
10+
import org.junit.Assert.*
1211
import org.junit.Test
1312
import org.junit.runner.RunWith
1413
import org.robolectric.Robolectric
@@ -41,6 +40,15 @@ open class DialogsTestActivity : Activity() {
4140
}.show()
4241
}
4342
}
43+
button {
44+
id = 4
45+
onClick {
46+
alert("Message", "NonCancelable") {
47+
isCancelable = false
48+
positiveButton("Ok") { dialog -> dialog.dismiss() }
49+
}.show()
50+
}
51+
}
4452
}
4553
}
4654
}
@@ -53,6 +61,7 @@ open class DialogsTestActivity : Activity() {
5361
val button1 = activity.findViewById(1) as Button
5462
val button2 = activity.findViewById(2) as Button
5563
val button3 = activity.findViewById(3) as Button
64+
val button4 = activity.findViewById(4) as Button
5665

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

89+
ShadowLooper.idleMainLooper()
90+
91+
button4.performClick()
92+
val nonCancelableAlert = ShadowAlertDialog.getLatestAlertDialog()
93+
assertNotNull(nonCancelableAlert)
94+
val nonCancelableAlertShadow = ShadowExtractor.extract(nonCancelableAlert) as ShadowAlertDialog
95+
assertEquals("NonCancelable", nonCancelableAlertShadow.title.toString())
96+
assertFalse(nonCancelableAlertShadow.isCancelable)
97+
8098
println("[COMPLETE]")
8199
}
82-
83100
}

anko/library/static/appcompatV7/src/SupportAlertBuilder.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
package org.jetbrains.anko.appcompat.v7
1818

19-
import org.jetbrains.anko.*
2019
import android.content.Context
2120
import android.content.DialogInterface
2221
import android.graphics.drawable.Drawable
2322
import android.support.v7.app.AlertDialog
2423
import android.view.KeyEvent
2524
import android.view.View
25+
import org.jetbrains.anko.AlertBuilder
26+
import org.jetbrains.anko.AlertBuilderFactory
2627
import org.jetbrains.anko.internals.AnkoInternals
2728
import org.jetbrains.anko.internals.AnkoInternals.NO_GETTER
2829
import kotlin.DeprecationLevel.ERROR
@@ -64,6 +65,10 @@ internal class AppcompatAlertBuilder(override val ctx: Context) : AlertBuilder<A
6465
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
6566
set(value) { builder.setView(value) }
6667

68+
override var isCancelable: Boolean
69+
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
70+
set(value) { builder.setCancelable(value) }
71+
6772
override fun onCancelled(handler: (DialogInterface) -> Unit) {
6873
builder.setOnCancelListener(handler)
6974
}

anko/library/static/commons/src/dialogs/AlertBuilder.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ interface AlertBuilder<out D : DialogInterface> {
5858
var customView: View
5959
@Deprecated(NO_GETTER, level = ERROR) get
6060

61+
var isCancelable: Boolean
62+
@Deprecated(NO_GETTER, level = ERROR) get
63+
6164
fun onCancelled(handler: (dialog: DialogInterface) -> Unit)
6265

6366
fun onKeyPressed(handler: (dialog: DialogInterface, keyCode: Int, e: KeyEvent) -> Boolean)

anko/library/static/commons/src/dialogs/AndroidAlertBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<Ale
6363
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
6464
set(value) { builder.setView(value) }
6565

66+
override var isCancelable: Boolean
67+
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
68+
set(value) { builder.setCancelable(value) }
69+
6670
override fun onCancelled(handler: (DialogInterface) -> Unit) {
6771
builder.setOnCancelListener(handler)
6872
}

0 commit comments

Comments
 (0)