Skip to content

Commit 046287e

Browse files
committed
ActivityExtensions: Build Alertdialog
Signed-off-by: Fung <fython@163.com>
1 parent 3f9cba2 commit 046287e

File tree

6 files changed

+185
-10
lines changed

6 files changed

+185
-10
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package moe.feng.kotlinyan
2+
3+
import android.app.Fragment
4+
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import moe.feng.kotlinyan.common.AndroidExtensions
9+
import org.jetbrains.anko.sdk25.coroutines.onClick
10+
import org.jetbrains.anko.toast
11+
12+
class ActivityExtDemoFragment : Fragment(), AndroidExtensions {
13+
14+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View
15+
= inflater.inflate(R.layout.fragment_activity_ext, container, false)
16+
17+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
18+
view[R.id.btn_build_and_show].onClick {
19+
buildAlertDialog {
20+
title = "Test"
21+
message = "Hello nyan! Here is Kotlinyan demo."
22+
isCancelable = true
23+
positiveButton(android.R.string.ok) { _, _ ->
24+
toast("PositiveButton Clicked!")
25+
}
26+
negativeButton(android.R.string.cancel) { _, _ ->
27+
toast("NegativeButton Clicked!")
28+
}
29+
onCancel = {
30+
31+
}
32+
onDismiss = {
33+
34+
}
35+
}.show()
36+
}
37+
}
38+
39+
}

demo/src/main/kotlin/moe/feng/kotlinyan/MainActivity.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package moe.feng.kotlinyan
22

33
import android.Manifest
4-
import android.app.AlertDialog
54
import android.app.Fragment
65
import android.app.FragmentManager
76
import android.os.Bundle
@@ -49,14 +48,14 @@ class MainActivity : AppCompatActivity(), AndroidExtensions, ColorExtensions {
4948
toast("Permission granted")
5049
} ?: run {
5150
toast("Permission should show rationale.")
52-
AlertDialog.Builder(this)
53-
.setTitle("Permission Denied")
54-
.setMessage("Without storage permission, you cannot read or save files.")
55-
.setPositiveButton("Request") {
56-
_, _ -> continueRunWithPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
57-
}
58-
.setNegativeButton(android.R.string.cancel, {_, _ -> })
59-
.show()
51+
buildAlertDialog {
52+
title = "Permission Denied"
53+
message = "Without storage permission, you cannot read or save files."
54+
positiveButton("Request") {
55+
_, _ -> continueRunWithPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
56+
}
57+
negativeButton(android.R.string.cancel) { _, _ -> }
58+
}.show()
6059
}
6160
}
6261
}
@@ -74,6 +73,7 @@ class MainActivity : AppCompatActivity(), AndroidExtensions, ColorExtensions {
7473

7574
val items = arrayOf<Pair<String, Fragment>>(
7675
"ViewExtension" to ViewExtDemoFragment(),
76+
"Activity" to ActivityExtDemoFragment(),
7777
"Network" to NetworkExtDemoFragment(),
7878
"Picasso" to PicassoExtDemoFragment(),
7979
"Glide" to GlideExtDemoFragment(),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context=".ActivityExtDemoFragment">
7+
8+
<LinearLayout
9+
android:orientation="vertical"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content">
12+
13+
<TextView
14+
style="@style/HeaderText"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:padding="16dp"
18+
android:text="Build AlertDialog"/>
19+
20+
<Button
21+
android:id="@+id/btn_build_and_show"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:text="Build &amp; show"
25+
android:backgroundTint="#FFFFFF"/>
26+
27+
</LinearLayout>
28+
29+
</ScrollView>

kotlinyan-common/src/main/kotlin/moe/feng/kotlinyan/common/ActivityExtensions.kt

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package moe.feng.kotlinyan.common
22

33
import android.annotation.SuppressLint
44
import android.app.Activity
5+
import android.app.AlertDialog
6+
import android.content.DialogInterface
57
import android.content.pm.PackageManager
68
import android.graphics.Color
9+
import android.graphics.drawable.Drawable
710
import android.os.Build
811
import android.os.Process
12+
import android.view.KeyEvent
913
import android.view.Menu
1014
import android.view.MenuItem
1115
import android.view.View
16+
import android.widget.AdapterView
1217

1318
@SuppressLint("NewApi")
1419
interface 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>()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package moe.feng.kotlinyan.common
22

3-
interface AndroidExtensions : ActivityExtensions, ViewExtensions, ResourcesExtensions
3+
interface AndroidExtensions : ActivityExtensions, ViewExtensions, ResourcesExtensions, FragmentExtensions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package moe.feng.kotlinyan.common
2+
3+
import android.app.AlertDialog
4+
import android.app.Fragment
5+
6+
interface FragmentExtensions: ActivityExtensions {
7+
8+
fun Fragment.buildAlertDialog(process: AlertDialog.Builder.() -> Unit) = activity.buildAlertDialog(process)
9+
10+
}

0 commit comments

Comments
 (0)