Skip to content

Commit

Permalink
feat: provide delegates for AlertDialog and Snackbar and Toast
Browse files Browse the repository at this point in the history
  • Loading branch information
tynn committed Jan 16, 2021
1 parent b2d5eaa commit a26f29a
Show file tree
Hide file tree
Showing 30 changed files with 1,954 additions and 6 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public interface AString {
The library is implemented with _Kotlin_ for _Java_.
The _Kotlin Standard Library_ is not required to use _AString_.

## Installation

repositories {
jcenter()
}

dependencies {
implementation "xyz.tynn.astring:core:$aStringVersion"
implementation "xyz.tynn.astring:appcompat:$aStringVersion"
implementation "xyz.tynn.astring:material:$aStringVersion"
}


## Usage

Expand Down Expand Up @@ -54,7 +66,7 @@ There are several _core_ implementations of `AString` for:
* quantity string resources delegation
* formatted quantity string resources delegation

### Supported `View` types
### Supported _Android_ types

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

* `Toast`

* `AlertDialog`
* `AlertDialog.Builder`

#### AppCompat module

Views provided by _AndroidX_ appcompat:

* `Toobar`

* `AlertDialog`
* `AlertDialog.Builder`

#### Material module

Views provided by the _Material_ components by _Google_:

* `TextInputLayout`

* `Snackbar`

* `MaterialAlertDialogBuilder`


## License

Expand Down
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),
)
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
}
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);
}
}
Loading

0 comments on commit a26f29a

Please sign in to comment.