Skip to content

Commit a26f29a

Browse files
committed
feat: provide delegates for AlertDialog and Snackbar and Toast
1 parent b2d5eaa commit a26f29a

File tree

30 files changed

+1954
-6
lines changed

30 files changed

+1954
-6
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ public interface AString {
1313
The library is implemented with _Kotlin_ for _Java_.
1414
The _Kotlin Standard Library_ is not required to use _AString_.
1515

16+
## Installation
17+
18+
repositories {
19+
jcenter()
20+
}
21+
22+
dependencies {
23+
implementation "xyz.tynn.astring:core:$aStringVersion"
24+
implementation "xyz.tynn.astring:appcompat:$aStringVersion"
25+
implementation "xyz.tynn.astring:material:$aStringVersion"
26+
}
27+
1628

1729
## Usage
1830

@@ -54,7 +66,7 @@ There are several _core_ implementations of `AString` for:
5466
* quantity string resources delegation
5567
* formatted quantity string resources delegation
5668

57-
### Supported `View` types
69+
### Supported _Android_ types
5870

5971
There are several (_Kotlin_) extension overloads for methods taking
6072
a `CharSequence` as an argument.
@@ -72,18 +84,30 @@ Views provided by the _Android_ framework and _AndroidX_ core:
7284
* `ToggleButton`
7385
* `TextSwitcher`
7486

87+
* `Toast`
88+
89+
* `AlertDialog`
90+
* `AlertDialog.Builder`
91+
7592
#### AppCompat module
7693

7794
Views provided by _AndroidX_ appcompat:
7895

7996
* `Toobar`
8097

98+
* `AlertDialog`
99+
* `AlertDialog.Builder`
100+
81101
#### Material module
82102

83103
Views provided by the _Material_ components by _Google_:
84104

85105
* `TextInputLayout`
86106

107+
* `Snackbar`
108+
109+
* `MaterialAlertDialogBuilder`
110+
87111

88112
## License
89113

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2021 Christian Schmitz
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
@file:JvmName("AStringAlertDialog")
5+
6+
package xyz.tynn.astring.appcompat
7+
8+
import android.content.DialogInterface.*
9+
import android.os.Message
10+
import androidx.annotation.IntDef
11+
import androidx.appcompat.app.AlertDialog
12+
import xyz.tynn.astring.AString
13+
import kotlin.annotation.AnnotationRetention.SOURCE
14+
15+
@Retention(SOURCE)
16+
@IntDef(BUTTON_POSITIVE, BUTTON_NEGATIVE, BUTTON_NEUTRAL)
17+
private annotation class DialogInterfaceButton
18+
19+
/**
20+
* Sets a listener to be invoked when the button is pressed
21+
*
22+
* This method has no effect if called after [AlertDialog.show]
23+
*
24+
* @see AlertDialog.setButton
25+
*/
26+
fun AlertDialog.setButton(
27+
@DialogInterfaceButton whichButton: Int,
28+
text: AString,
29+
listener: OnClickListener?,
30+
) = setButton(
31+
whichButton,
32+
text(context),
33+
listener,
34+
)
35+
36+
/**
37+
* Sets a message to be sent when a button is pressed
38+
*
39+
* This method has no effect if called after [AlertDialog.show]
40+
*
41+
* @see AlertDialog.setButton
42+
*/
43+
fun AlertDialog.setButton(
44+
@DialogInterfaceButton whichButton: Int,
45+
text: AString,
46+
msg: Message?,
47+
) = setButton(
48+
whichButton,
49+
text(context),
50+
msg,
51+
)
52+
53+
/**
54+
* Sets the message to display
55+
*
56+
* @see AlertDialog.setMessage
57+
*/
58+
fun AlertDialog.setMessage(
59+
message: AString,
60+
) = setMessage(
61+
message(context),
62+
)
63+
64+
/**
65+
* Sets the title to display
66+
*
67+
* @see AlertDialog.setTitle
68+
*/
69+
fun AlertDialog.setTitle(
70+
title: AString,
71+
) = setTitle(
72+
title(context),
73+
)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2021 Christian Schmitz
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
@file:JvmName("AStringAlertDialogBuilder")
5+
6+
package xyz.tynn.astring.appcompat
7+
8+
import android.content.DialogInterface.OnClickListener
9+
import androidx.appcompat.app.AlertDialog.Builder
10+
import xyz.tynn.astring.AString
11+
12+
/**
13+
* Sets a listener to be invoked when the negative button is pressed
14+
*
15+
* @see Builder.setNegativeButton
16+
*/
17+
fun <B : Builder> B.setNegativeButton(
18+
text: AString,
19+
listener: OnClickListener?,
20+
): B {
21+
setNegativeButton(
22+
text(context),
23+
listener,
24+
)
25+
return this
26+
}
27+
28+
/**
29+
* Sets a listener to be invoked when the neutral button is pressed
30+
*
31+
* @see Builder.setNeutralButton
32+
*/
33+
fun <B : Builder> B.setNeutralButton(
34+
text: AString,
35+
listener: OnClickListener?,
36+
): B {
37+
setNeutralButton(
38+
text(context),
39+
listener,
40+
)
41+
return this
42+
}
43+
44+
/**
45+
* Sets a listener to be invoked when the positive button is pressed
46+
*
47+
* @see Builder.setPositiveButton
48+
*/
49+
fun <B : Builder> B.setPositiveButton(
50+
text: AString,
51+
listener: OnClickListener?,
52+
): B {
53+
setPositiveButton(
54+
text(context),
55+
listener,
56+
)
57+
return this
58+
}
59+
60+
/**
61+
* Sets the message
62+
*
63+
* @see Builder.setMessage
64+
*/
65+
fun <B : Builder> B.setMessage(
66+
message: AString,
67+
): B {
68+
setMessage(
69+
message(context),
70+
)
71+
return this
72+
}
73+
74+
/**
75+
* Sets the title
76+
*
77+
* @see Builder.setTitle
78+
*/
79+
fun <B : Builder> B.setTitle(
80+
title: AString,
81+
): B {
82+
setTitle(
83+
title(context),
84+
)
85+
return this
86+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Copyright 2021 Christian Schmitz
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package xyz.tynn.astring.appcompat;
5+
6+
import android.content.DialogInterface.OnClickListener;
7+
8+
import androidx.appcompat.app.AlertDialog;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import io.mockk.impl.annotations.MockK;
14+
import xyz.tynn.astring.AString;
15+
16+
import static org.junit.Assert.assertSame;
17+
import static xyz.tynn.astring.testing.mockk.MockKt.init;
18+
import static xyz.tynn.astring.testing.mockk.MockKt.verify;
19+
20+
public class AStringAlertDialogBuilderTest {
21+
22+
@MockK
23+
AString aString;
24+
25+
@MockK
26+
AlertDialog.Builder builder;
27+
28+
@MockK
29+
OnClickListener listener;
30+
31+
@Before
32+
public void setup() {
33+
init(this, true);
34+
}
35+
36+
@Test
37+
public void setNegativeButton_should_delegate_to_builder() {
38+
assertSame(builder,
39+
AStringAlertDialogBuilder.setNegativeButton(builder, aString, listener));
40+
41+
verify(() -> builder.setNegativeButton(aString.invoke(builder.getContext()), listener));
42+
}
43+
44+
@Test
45+
public void setNegativeButton_with_null_listener_should_delegate_to_builder() {
46+
assertSame(builder,
47+
AStringAlertDialogBuilder.setNegativeButton(builder, aString, null));
48+
49+
verify(() -> builder.setNegativeButton(aString.invoke(builder.getContext()), null));
50+
}
51+
52+
@SuppressWarnings("ConstantConditions")
53+
@Test(expected = NullPointerException.class)
54+
public void setNegativeButton_should_throw_on_null_builder() {
55+
AStringAlertDialogBuilder.setNegativeButton(null, aString, listener);
56+
}
57+
58+
@SuppressWarnings("ConstantConditions")
59+
@Test(expected = NullPointerException.class)
60+
public void setNegativeButton_should_throw_on_null_string() {
61+
AStringAlertDialogBuilder.setNegativeButton(builder, null, listener);
62+
}
63+
64+
@Test
65+
public void setNeutralButton_should_delegate_to_builder() {
66+
assertSame(builder,
67+
AStringAlertDialogBuilder.setNeutralButton(builder, aString, listener));
68+
69+
verify(() -> builder.setNeutralButton(aString.invoke(builder.getContext()), listener));
70+
}
71+
72+
@Test
73+
public void setNeutralButton_with_null_listener_should_delegate_to_builder() {
74+
assertSame(builder,
75+
AStringAlertDialogBuilder.setNeutralButton(builder, aString, null));
76+
77+
verify(() -> builder.setNeutralButton(aString.invoke(builder.getContext()), null));
78+
}
79+
80+
@SuppressWarnings("ConstantConditions")
81+
@Test(expected = NullPointerException.class)
82+
public void setNeutralButton_should_throw_on_null_builder() {
83+
AStringAlertDialogBuilder.setNeutralButton(null, aString, listener);
84+
}
85+
86+
@SuppressWarnings("ConstantConditions")
87+
@Test(expected = NullPointerException.class)
88+
public void setNeutralButton_should_throw_on_null_string() {
89+
AStringAlertDialogBuilder.setNeutralButton(builder, null, listener);
90+
}
91+
92+
@Test
93+
public void setPositiveButton_should_delegate_to_builder() {
94+
assertSame(builder,
95+
AStringAlertDialogBuilder.setPositiveButton(builder, aString, listener));
96+
97+
verify(() -> builder.setPositiveButton(aString.invoke(builder.getContext()), listener));
98+
}
99+
100+
@Test
101+
public void setPositiveButton_with_null_listener_should_delegate_to_builder() {
102+
assertSame(builder,
103+
AStringAlertDialogBuilder.setPositiveButton(builder, aString, null));
104+
105+
verify(() -> builder.setPositiveButton(aString.invoke(builder.getContext()), null));
106+
}
107+
108+
@SuppressWarnings("ConstantConditions")
109+
@Test(expected = NullPointerException.class)
110+
public void setPositiveButton_should_throw_on_null_builder() {
111+
AStringAlertDialogBuilder.setPositiveButton(null, aString, listener);
112+
}
113+
114+
@SuppressWarnings("ConstantConditions")
115+
@Test(expected = NullPointerException.class)
116+
public void setPositiveButton_should_throw_on_null_string() {
117+
AStringAlertDialogBuilder.setPositiveButton(builder, null, listener);
118+
}
119+
120+
@Test
121+
public void setMessage_should_delegate_to_builder() {
122+
assertSame(builder,
123+
AStringAlertDialogBuilder.setMessage(builder, aString));
124+
125+
verify(() -> builder.setMessage(aString.invoke(builder.getContext())));
126+
}
127+
128+
@SuppressWarnings("ConstantConditions")
129+
@Test(expected = NullPointerException.class)
130+
public void setMessage_should_throw_on_null_builder() {
131+
AStringAlertDialogBuilder.setMessage(null, aString);
132+
}
133+
134+
@SuppressWarnings("ConstantConditions")
135+
@Test(expected = NullPointerException.class)
136+
public void setMessage_should_throw_on_null_string() {
137+
AStringAlertDialogBuilder.setMessage(builder, null);
138+
}
139+
140+
@Test
141+
public void setTitle_should_delegate_to_builder() {
142+
assertSame(builder,
143+
AStringAlertDialogBuilder.setTitle(builder, aString));
144+
145+
verify(() -> builder.setTitle(aString.invoke(builder.getContext())));
146+
}
147+
148+
@SuppressWarnings("ConstantConditions")
149+
@Test(expected = NullPointerException.class)
150+
public void setTitle_should_throw_on_null_builder() {
151+
AStringAlertDialogBuilder.setTitle(null, aString);
152+
}
153+
154+
@SuppressWarnings("ConstantConditions")
155+
@Test(expected = NullPointerException.class)
156+
public void setTitle_should_throw_on_null_string() {
157+
AStringAlertDialogBuilder.setTitle(builder, null);
158+
}
159+
}

0 commit comments

Comments
 (0)