-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide delegates for AlertDialog and Snackbar and Toast
- Loading branch information
Showing
30 changed files
with
1,954 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
appcompat/src/main/kotlin/xyz/tynn/astring/appcompat/AStringAlertDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2021 Christian Schmitz | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
@file:JvmName("AStringAlertDialog") | ||
|
||
package xyz.tynn.astring.appcompat | ||
|
||
import android.content.DialogInterface.* | ||
import android.os.Message | ||
import androidx.annotation.IntDef | ||
import androidx.appcompat.app.AlertDialog | ||
import xyz.tynn.astring.AString | ||
import kotlin.annotation.AnnotationRetention.SOURCE | ||
|
||
@Retention(SOURCE) | ||
@IntDef(BUTTON_POSITIVE, BUTTON_NEGATIVE, BUTTON_NEUTRAL) | ||
private annotation class DialogInterfaceButton | ||
|
||
/** | ||
* Sets a listener to be invoked when the button is pressed | ||
* | ||
* This method has no effect if called after [AlertDialog.show] | ||
* | ||
* @see AlertDialog.setButton | ||
*/ | ||
fun AlertDialog.setButton( | ||
@DialogInterfaceButton whichButton: Int, | ||
text: AString, | ||
listener: OnClickListener?, | ||
) = setButton( | ||
whichButton, | ||
text(context), | ||
listener, | ||
) | ||
|
||
/** | ||
* Sets a message to be sent when a button is pressed | ||
* | ||
* This method has no effect if called after [AlertDialog.show] | ||
* | ||
* @see AlertDialog.setButton | ||
*/ | ||
fun AlertDialog.setButton( | ||
@DialogInterfaceButton whichButton: Int, | ||
text: AString, | ||
msg: Message?, | ||
) = setButton( | ||
whichButton, | ||
text(context), | ||
msg, | ||
) | ||
|
||
/** | ||
* Sets the message to display | ||
* | ||
* @see AlertDialog.setMessage | ||
*/ | ||
fun AlertDialog.setMessage( | ||
message: AString, | ||
) = setMessage( | ||
message(context), | ||
) | ||
|
||
/** | ||
* Sets the title to display | ||
* | ||
* @see AlertDialog.setTitle | ||
*/ | ||
fun AlertDialog.setTitle( | ||
title: AString, | ||
) = setTitle( | ||
title(context), | ||
) |
86 changes: 86 additions & 0 deletions
86
appcompat/src/main/kotlin/xyz/tynn/astring/appcompat/AStringAlertDialogBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021 Christian Schmitz | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
@file:JvmName("AStringAlertDialogBuilder") | ||
|
||
package xyz.tynn.astring.appcompat | ||
|
||
import android.content.DialogInterface.OnClickListener | ||
import androidx.appcompat.app.AlertDialog.Builder | ||
import xyz.tynn.astring.AString | ||
|
||
/** | ||
* Sets a listener to be invoked when the negative button is pressed | ||
* | ||
* @see Builder.setNegativeButton | ||
*/ | ||
fun <B : Builder> B.setNegativeButton( | ||
text: AString, | ||
listener: OnClickListener?, | ||
): B { | ||
setNegativeButton( | ||
text(context), | ||
listener, | ||
) | ||
return this | ||
} | ||
|
||
/** | ||
* Sets a listener to be invoked when the neutral button is pressed | ||
* | ||
* @see Builder.setNeutralButton | ||
*/ | ||
fun <B : Builder> B.setNeutralButton( | ||
text: AString, | ||
listener: OnClickListener?, | ||
): B { | ||
setNeutralButton( | ||
text(context), | ||
listener, | ||
) | ||
return this | ||
} | ||
|
||
/** | ||
* Sets a listener to be invoked when the positive button is pressed | ||
* | ||
* @see Builder.setPositiveButton | ||
*/ | ||
fun <B : Builder> B.setPositiveButton( | ||
text: AString, | ||
listener: OnClickListener?, | ||
): B { | ||
setPositiveButton( | ||
text(context), | ||
listener, | ||
) | ||
return this | ||
} | ||
|
||
/** | ||
* Sets the message | ||
* | ||
* @see Builder.setMessage | ||
*/ | ||
fun <B : Builder> B.setMessage( | ||
message: AString, | ||
): B { | ||
setMessage( | ||
message(context), | ||
) | ||
return this | ||
} | ||
|
||
/** | ||
* Sets the title | ||
* | ||
* @see Builder.setTitle | ||
*/ | ||
fun <B : Builder> B.setTitle( | ||
title: AString, | ||
): B { | ||
setTitle( | ||
title(context), | ||
) | ||
return this | ||
} |
159 changes: 159 additions & 0 deletions
159
appcompat/src/test/java/xyz/tynn/astring/appcompat/AStringAlertDialogBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 2021 Christian Schmitz | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package xyz.tynn.astring.appcompat; | ||
|
||
import android.content.DialogInterface.OnClickListener; | ||
|
||
import androidx.appcompat.app.AlertDialog; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import io.mockk.impl.annotations.MockK; | ||
import xyz.tynn.astring.AString; | ||
|
||
import static org.junit.Assert.assertSame; | ||
import static xyz.tynn.astring.testing.mockk.MockKt.init; | ||
import static xyz.tynn.astring.testing.mockk.MockKt.verify; | ||
|
||
public class AStringAlertDialogBuilderTest { | ||
|
||
@MockK | ||
AString aString; | ||
|
||
@MockK | ||
AlertDialog.Builder builder; | ||
|
||
@MockK | ||
OnClickListener listener; | ||
|
||
@Before | ||
public void setup() { | ||
init(this, true); | ||
} | ||
|
||
@Test | ||
public void setNegativeButton_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setNegativeButton(builder, aString, listener)); | ||
|
||
verify(() -> builder.setNegativeButton(aString.invoke(builder.getContext()), listener)); | ||
} | ||
|
||
@Test | ||
public void setNegativeButton_with_null_listener_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setNegativeButton(builder, aString, null)); | ||
|
||
verify(() -> builder.setNegativeButton(aString.invoke(builder.getContext()), null)); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setNegativeButton_should_throw_on_null_builder() { | ||
AStringAlertDialogBuilder.setNegativeButton(null, aString, listener); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setNegativeButton_should_throw_on_null_string() { | ||
AStringAlertDialogBuilder.setNegativeButton(builder, null, listener); | ||
} | ||
|
||
@Test | ||
public void setNeutralButton_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setNeutralButton(builder, aString, listener)); | ||
|
||
verify(() -> builder.setNeutralButton(aString.invoke(builder.getContext()), listener)); | ||
} | ||
|
||
@Test | ||
public void setNeutralButton_with_null_listener_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setNeutralButton(builder, aString, null)); | ||
|
||
verify(() -> builder.setNeutralButton(aString.invoke(builder.getContext()), null)); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setNeutralButton_should_throw_on_null_builder() { | ||
AStringAlertDialogBuilder.setNeutralButton(null, aString, listener); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setNeutralButton_should_throw_on_null_string() { | ||
AStringAlertDialogBuilder.setNeutralButton(builder, null, listener); | ||
} | ||
|
||
@Test | ||
public void setPositiveButton_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setPositiveButton(builder, aString, listener)); | ||
|
||
verify(() -> builder.setPositiveButton(aString.invoke(builder.getContext()), listener)); | ||
} | ||
|
||
@Test | ||
public void setPositiveButton_with_null_listener_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setPositiveButton(builder, aString, null)); | ||
|
||
verify(() -> builder.setPositiveButton(aString.invoke(builder.getContext()), null)); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setPositiveButton_should_throw_on_null_builder() { | ||
AStringAlertDialogBuilder.setPositiveButton(null, aString, listener); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setPositiveButton_should_throw_on_null_string() { | ||
AStringAlertDialogBuilder.setPositiveButton(builder, null, listener); | ||
} | ||
|
||
@Test | ||
public void setMessage_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setMessage(builder, aString)); | ||
|
||
verify(() -> builder.setMessage(aString.invoke(builder.getContext()))); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setMessage_should_throw_on_null_builder() { | ||
AStringAlertDialogBuilder.setMessage(null, aString); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setMessage_should_throw_on_null_string() { | ||
AStringAlertDialogBuilder.setMessage(builder, null); | ||
} | ||
|
||
@Test | ||
public void setTitle_should_delegate_to_builder() { | ||
assertSame(builder, | ||
AStringAlertDialogBuilder.setTitle(builder, aString)); | ||
|
||
verify(() -> builder.setTitle(aString.invoke(builder.getContext()))); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setTitle_should_throw_on_null_builder() { | ||
AStringAlertDialogBuilder.setTitle(null, aString); | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Test(expected = NullPointerException.class) | ||
public void setTitle_should_throw_on_null_string() { | ||
AStringAlertDialogBuilder.setTitle(builder, null); | ||
} | ||
} |
Oops, something went wrong.