Skip to content

Commit b58f493

Browse files
committed
AppCompatSupport: Snackbar Builder & v7 AlertDialog Builder
Signed-off-by: Fung <fython@163.com>
1 parent 2e44bb3 commit b58f493

File tree

5 files changed

+209
-9
lines changed

5 files changed

+209
-9
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@ package moe.feng.kotlinyan
22

33
import android.app.Fragment
44
import android.os.Bundle
5+
import android.support.design.widget.Snackbar
56
import android.view.LayoutInflater
67
import android.view.View
78
import android.view.ViewGroup
89
import moe.feng.kotlinyan.common.AndroidExtensions
10+
import moe.feng.kotlinyan.common.AppCompatExtensions
11+
import moe.feng.kotlinyan.common.SupportDesignExtensions
912
import org.jetbrains.anko.sdk25.coroutines.onClick
1013
import org.jetbrains.anko.toast
1114

12-
class ActivityExtDemoFragment : Fragment(), AndroidExtensions {
15+
class ActivityExtDemoFragment : Fragment(), AndroidExtensions, AppCompatExtensions, SupportDesignExtensions {
1316

1417
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View
1518
= inflater.inflate(R.layout.fragment_activity_ext, container, false)
1619

1720
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
18-
view[R.id.btn_build_and_show].onClick {
21+
view[R.id.btn_alertdialog_native].onClick {
1922
buildAlertDialog {
23+
title = "Test"
24+
message = "Hello nyan! Here is Kotlinyan demo."
25+
positiveButton(android.R.string.ok) { _, _ ->
26+
toast("PositiveButton Clicked!")
27+
}
28+
negativeButton(android.R.string.cancel) { _, _ ->
29+
toast("NegativeButton Clicked!")
30+
}
31+
}.show()
32+
}
33+
view[R.id.btn_alertdialog_v7].onClick {
34+
buildV7AlertDialog {
2035
title = "Test"
2136
message = "Hello nyan! Here is Kotlinyan demo."
2237
isCancelable = true
@@ -34,6 +49,15 @@ class ActivityExtDemoFragment : Fragment(), AndroidExtensions {
3449
}
3550
}.show()
3651
}
52+
view[R.id.btn_show_snackbar].onClick {
53+
view[R.id.root_layout].snackbar {
54+
message = "Here is Snackbar"
55+
duration = Snackbar.LENGTH_SHORT
56+
action(android.R.string.ok) {
57+
toast("OK!")
58+
}
59+
}.show()
60+
}
3761
}
3862

3963
}

demo/src/main/res/layout/fragment_activity_ext.xml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/root_layout"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".ActivityExtDemoFragment">
78

89
<LinearLayout
910
android:orientation="vertical"
@@ -18,11 +19,32 @@
1819
android:text="Build AlertDialog"/>
1920

2021
<Button
21-
android:id="@+id/btn_build_and_show"
22+
android:id="@+id/btn_alertdialog_native"
2223
android:layout_width="wrap_content"
2324
android:layout_height="wrap_content"
24-
android:text="Build &amp; show"
25-
android:backgroundTint="#FFFFFF"/>
25+
android:layout_marginStart="16dp"
26+
android:text="android.app.AlertDialog"/>
27+
28+
<Button
29+
android:id="@+id/btn_alertdialog_v7"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_marginStart="16dp"
33+
android:text="android.support.v7.app.AlertDialog"/>
34+
35+
<TextView
36+
style="@style/HeaderText"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:padding="16dp"
40+
android:text="Snackbar"/>
41+
42+
<Button
43+
android:id="@+id/btn_show_snackbar"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:layout_marginStart="16dp"
47+
android:text="Show Snackbar"/>
2648

2749
</LinearLayout>
2850

kotlinyan-appcompat-support/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ dependencies {
2828
compile fileTree(dir: 'libs', include: ['*.jar'])
2929
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
3030
compile 'com.android.support:appcompat-v7:25.3.1'
31+
compile 'com.android.support:design:25.3.1'
32+
compile project(':kotlinyan-common')
3133
}
3234
repositories {
3335
mavenCentral()
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package moe.feng.kotlinyan.common
2+
3+
import android.app.Activity
4+
import android.app.Fragment
5+
import android.content.DialogInterface
6+
import android.graphics.drawable.Drawable
7+
import android.support.v7.app.AlertDialog
8+
import android.view.View
9+
import android.widget.AdapterView
10+
11+
interface AppCompatExtensions {
12+
13+
// Kotlin-style builders
14+
15+
fun Fragment.buildV7AlertDialog(process: AlertDialog.Builder.() -> Unit) = activity.buildV7AlertDialog(process)
16+
17+
fun android.support.v4.app.Fragment
18+
.buildV7AlertDialog(process: AlertDialog.Builder.() -> Unit) = activity.buildV7AlertDialog(process)
19+
20+
fun Activity.buildV7AlertDialog(process: AlertDialog.Builder.() -> Unit) : AlertDialog {
21+
val builder = AlertDialog.Builder(this)
22+
builder.process()
23+
return builder.create()
24+
}
25+
26+
var AlertDialog.Builder.title : String
27+
get() { throw java.lang.NoSuchMethodException("Title getter is not supported") }
28+
set(value) { this.setTitle(value) }
29+
30+
var AlertDialog.Builder.titleRes : Int
31+
get() { throw java.lang.NoSuchMethodException("Title res id getter is not supported") }
32+
set(value) { this.setTitle(value) }
33+
34+
var AlertDialog.Builder.message : String
35+
get() { throw java.lang.NoSuchMethodException("Message getter is not supported") }
36+
set(value) { this.setMessage(value) }
37+
38+
var AlertDialog.Builder.messageRes : Int
39+
get() { throw java.lang.NoSuchMethodException("Message res id getter is not supported") }
40+
set(value) { this.setMessage(value) }
41+
42+
var AlertDialog.Builder.isCancelable : Boolean
43+
get() { throw java.lang.NoSuchMethodException("isCancelable getter is not supported") }
44+
set(value) { this.setCancelable(value) }
45+
46+
var AlertDialog.Builder.customTitle : View
47+
get() { throw java.lang.NoSuchMethodException("Custom title getter is not supported") }
48+
set(value) { this.setCustomTitle(value) }
49+
50+
var AlertDialog.Builder.icon : Drawable
51+
get() { throw java.lang.NoSuchMethodException("Icon getter is not supported") }
52+
set(value) { this.setIcon(value) }
53+
54+
var AlertDialog.Builder.iconRes : Int
55+
get() { throw java.lang.NoSuchMethodException("Icon res id getter is not supported") }
56+
set(value) { this.setIcon(value) }
57+
58+
var AlertDialog.Builder.iconAttribute : Int
59+
get() { throw java.lang.NoSuchMethodException("Icon attribute getter is not supported") }
60+
set(value) { this.setIconAttribute(value) }
61+
62+
var AlertDialog.Builder.onCancel : (DialogInterface) -> Unit
63+
get() { throw java.lang.NoSuchMethodException("OnCancelListener getter is not supported") }
64+
set(value) { this.setOnCancelListener(value) }
65+
66+
var AlertDialog.Builder.onDismiss : (DialogInterface) -> Unit
67+
get() { throw java.lang.NoSuchMethodException("OnDismissListener getter is not supported") }
68+
set(value) { this.setOnDismissListener(value) }
69+
70+
var AlertDialog.Builder.onKey : DialogInterface.OnKeyListener
71+
get() { throw java.lang.NoSuchMethodException("OnKeyListener getter is not supported") }
72+
set(value) { this.setOnKeyListener(value) }
73+
74+
var AlertDialog.Builder.onItemSelected : AdapterView.OnItemSelectedListener
75+
get() { throw java.lang.NoSuchMethodException("OnItemSelectedListener getter is not supported") }
76+
set(value) { this.setOnItemSelectedListener(value) }
77+
78+
var AlertDialog.Builder.view : View
79+
get() { throw java.lang.NoSuchMethodException("View getter is not supported") }
80+
set(value) { this.setView(value) }
81+
82+
var AlertDialog.Builder.viewRes : Int
83+
get() { throw java.lang.NoSuchMethodException("View res id getter is not supported") }
84+
set(value) { this.setView(value) }
85+
86+
fun AlertDialog.Builder.positiveButton(textId: Int, onClick: (DialogInterface, Int) -> Unit) {
87+
setPositiveButton(textId, onClick)
88+
}
89+
90+
fun AlertDialog.Builder.positiveButton(text: String, onClick: (DialogInterface, Int) -> Unit) {
91+
setPositiveButton(text, onClick)
92+
}
93+
94+
fun AlertDialog.Builder.negativeButton(textId: Int, onClick: (DialogInterface, Int) -> Unit) {
95+
setNegativeButton(textId, onClick)
96+
}
97+
98+
fun AlertDialog.Builder.negativeButton(text: String, onClick: (DialogInterface, Int) -> Unit) {
99+
setNegativeButton(text, onClick)
100+
}
101+
102+
fun AlertDialog.Builder.neutralButton(textId: Int, onClick: (DialogInterface, Int) -> Unit) {
103+
setNeutralButton(textId, onClick)
104+
}
105+
106+
fun AlertDialog.Builder.neutralButton(text: String, onClick: (DialogInterface, Int) -> Unit) {
107+
setNeutralButton(text, onClick)
108+
}
109+
110+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package moe.feng.kotlinyan.common
2+
3+
import android.support.design.widget.Snackbar
4+
import android.view.View
5+
6+
interface SupportDesignExtensions {
7+
8+
fun View.snackbar(process: SnackbarBuilder.() -> Unit) : Snackbar {
9+
val builder = SnackbarBuilder(this)
10+
builder.process()
11+
return builder.build()
12+
}
13+
14+
class SnackbarBuilder(view: View) {
15+
16+
private var snackbar : Snackbar = Snackbar.make(view, "", Snackbar.LENGTH_SHORT)
17+
18+
var message : String? = null
19+
var messageRes : Int = 0
20+
var duration : Int = Snackbar.LENGTH_SHORT
21+
22+
fun action(textRes: Int, callback: (View) -> Unit) {
23+
snackbar.setAction(textRes, callback)
24+
}
25+
26+
fun action(text: String, callback: (View) -> Unit) {
27+
snackbar.setAction(text, callback)
28+
}
29+
30+
internal fun build() : Snackbar {
31+
if (messageRes != 0) {
32+
snackbar.setText(messageRes)
33+
} else if (message != null) {
34+
snackbar.setText(message!!)
35+
}
36+
snackbar.duration = duration
37+
return snackbar
38+
}
39+
40+
}
41+
42+
}

0 commit comments

Comments
 (0)