@@ -2,13 +2,18 @@ package moe.feng.kotlinyan.common
22
33import android.annotation.SuppressLint
44import android.app.Activity
5+ import android.app.AlertDialog
6+ import android.content.DialogInterface
57import android.content.pm.PackageManager
68import android.graphics.Color
9+ import android.graphics.drawable.Drawable
710import android.os.Build
811import android.os.Process
12+ import android.view.KeyEvent
913import android.view.Menu
1014import android.view.MenuItem
1115import android.view.View
16+ import android.widget.AdapterView
1217
1318@SuppressLint(" NewApi" )
1419interface ActivityExtensions {
@@ -137,6 +142,98 @@ interface ActivityExtensions {
137142 }
138143 }
139144
145+ // Kotlin-style builders
146+
147+ fun Activity.buildAlertDialog (process : AlertDialog .Builder .() -> Unit ) : AlertDialog {
148+ val builder = AlertDialog .Builder (this )
149+ builder.process()
150+ return builder.create()
151+ }
152+
153+ var AlertDialog .Builder .title : String
154+ get() { throw NoSuchMethodException (" Title getter is not supported" )}
155+ set(value) { this .setTitle(value) }
156+
157+ var AlertDialog .Builder .titleRes : Int
158+ get() { throw NoSuchMethodException (" Title res id getter is not supported" )}
159+ set(value) { this .setTitle(value) }
160+
161+ var AlertDialog .Builder .message : String
162+ get() { throw NoSuchMethodException (" Message getter is not supported" )}
163+ set(value) { this .setMessage(value) }
164+
165+ var AlertDialog .Builder .messageRes : Int
166+ get() { throw NoSuchMethodException (" Message res id getter is not supported" )}
167+ set(value) { this .setMessage(value) }
168+
169+ var AlertDialog .Builder .isCancelable : Boolean
170+ get() { throw NoSuchMethodException (" isCancelable getter is not supported" )}
171+ set(value) { this .setCancelable(value) }
172+
173+ var AlertDialog .Builder .customTitle : View
174+ get() { throw NoSuchMethodException (" Custom title getter is not supported" )}
175+ set(value) { this .setCustomTitle(value) }
176+
177+ var AlertDialog .Builder .icon : Drawable
178+ get() { throw NoSuchMethodException (" Icon getter is not supported" )}
179+ set(value) { this .setIcon(value) }
180+
181+ var AlertDialog .Builder .iconRes : Int
182+ get() { throw NoSuchMethodException (" Icon res id getter is not supported" )}
183+ set(value) { this .setIcon(value) }
184+
185+ var AlertDialog .Builder .iconAttribute : Int
186+ get() { throw NoSuchMethodException (" Icon attribute getter is not supported" )}
187+ set(value) { this .setIconAttribute(value) }
188+
189+ var AlertDialog .Builder .onCancel : (DialogInterface ) -> Unit
190+ get() { throw NoSuchMethodException (" OnCancelListener getter is not supported" )}
191+ set(value) { this .setOnCancelListener(value) }
192+
193+ var AlertDialog .Builder .onDismiss : (DialogInterface ) -> Unit
194+ get() { throw NoSuchMethodException (" OnDismissListener getter is not supported" )}
195+ set(value) { this .setOnDismissListener(value) }
196+
197+ var AlertDialog .Builder .onKey : DialogInterface .OnKeyListener
198+ get() { throw NoSuchMethodException (" OnKeyListener getter is not supported" )}
199+ set(value) { this .setOnKeyListener(value) }
200+
201+ var AlertDialog .Builder .onItemSelected : AdapterView .OnItemSelectedListener
202+ get() { throw NoSuchMethodException (" OnItemSelectedListener getter is not supported" )}
203+ set(value) { this .setOnItemSelectedListener(value) }
204+
205+ var AlertDialog .Builder .view : View
206+ get() { throw NoSuchMethodException (" View getter is not supported" )}
207+ set(value) { this .setView(value) }
208+
209+ var AlertDialog .Builder .viewRes : Int
210+ get() { throw NoSuchMethodException (" View res id getter is not supported" )}
211+ set(value) { this .setView(value) }
212+
213+ fun AlertDialog.Builder.positiveButton (textId : Int , onClick : (DialogInterface , Int ) -> Unit ) {
214+ setPositiveButton(textId, onClick)
215+ }
216+
217+ fun AlertDialog.Builder.positiveButton (text : String , onClick : (DialogInterface , Int ) -> Unit ) {
218+ setPositiveButton(text, onClick)
219+ }
220+
221+ fun AlertDialog.Builder.negativeButton (textId : Int , onClick : (DialogInterface , Int ) -> Unit ) {
222+ setNegativeButton(textId, onClick)
223+ }
224+
225+ fun AlertDialog.Builder.negativeButton (text : String , onClick : (DialogInterface , Int ) -> Unit ) {
226+ setNegativeButton(text, onClick)
227+ }
228+
229+ fun AlertDialog.Builder.neutralButton (textId : Int , onClick : (DialogInterface , Int ) -> Unit ) {
230+ setNeutralButton(textId, onClick)
231+ }
232+
233+ fun AlertDialog.Builder.neutralButton (text : String , onClick : (DialogInterface , Int ) -> Unit ) {
234+ setNeutralButton(text, onClick)
235+ }
236+
140237 companion object {
141238
142239 private val activityPermissionsCallbacks = hashMapOf<String , () - > Unit > ()
0 commit comments