Skip to content

Commit

Permalink
feature(suspend-dialogs) - support CTA for "setItems"
Browse files Browse the repository at this point in the history
  • Loading branch information
xeinebiu committed Nov 12, 2021
1 parent eea445f commit a5f85da
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 59 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ android {
}

dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"

implementation group: 'androidx.lifecycle', name: 'lifecycle-runtime-ktx', version: '2.3.1'
implementation group: 'androidx.lifecycle', name: 'lifecycle-runtime-ktx', version: '2.4.0'

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'com.google.android.material:material:1.4.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation project(":suspend-dialogs")
}
}
53 changes: 33 additions & 20 deletions app/src/main/java/com/xeinebiu/demo/suspendDialogs/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class MainActivity : AppCompatActivity() {
btnSetItems.setOnClickListener {
lifecycleScope.launch {
val result = SuspendAlertDialog.setItems(
listOf("Hello", "World")
items = listOf("Hello", "World"),
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
) {
MaterialAlertDialogBuilder(this@MainActivity)
}
Expand All @@ -81,7 +83,12 @@ class MainActivity : AppCompatActivity() {
btnSetItemsExt.setOnClickListener {
lifecycleScope.launch {
val result = MaterialAlertDialogBuilder(this@MainActivity)
.setItems(listOf("Hello", "World"))
.setItems(
items = listOf("Hello", "World"),
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
)

tvResult.text = result.toString()
}
Expand All @@ -92,9 +99,9 @@ class MainActivity : AppCompatActivity() {
btnConfirm.setOnClickListener {
lifecycleScope.launch {
val result = SuspendAlertDialog.confirm(
positiveButtonText = "Positive",
negativeButtonText = "Negative",
neutralButtonText = "Neutral"
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
) {
MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
Expand All @@ -113,9 +120,9 @@ class MainActivity : AppCompatActivity() {
.setTitle("Title")
.setMessage("Message")
.confirm(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Neutral",
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
)

tvResult.text = result.toString()
Expand All @@ -127,9 +134,9 @@ class MainActivity : AppCompatActivity() {
btnMultiChoice.setOnClickListener {
lifecycleScope.launch {
val multiChoiceResult = SuspendAlertDialog.setMultiChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
items = SuspendAlertDialog.MultiChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
checked = listOf(false, false, false, false)
Expand All @@ -149,9 +156,9 @@ class MainActivity : AppCompatActivity() {
val result = MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setMultiChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
items = SuspendAlertDialog.MultiChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
checked = listOf(false, false, false, false)
Expand All @@ -167,9 +174,9 @@ class MainActivity : AppCompatActivity() {
btnSingleChoice.setOnClickListener {
lifecycleScope.launch {
val singleChoiceResult = SuspendAlertDialog.setSingleChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
items = SuspendAlertDialog.SingleChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
selectedIndex = 1
Expand All @@ -189,9 +196,9 @@ class MainActivity : AppCompatActivity() {
val result = MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setSingleChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
positiveButtonText = POSITIVE,
negativeButtonText = NEGATIVE,
neutralButtonText = NEUTRAL,
items = SuspendAlertDialog.SingleChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
selectedIndex = 1
Expand Down Expand Up @@ -226,4 +233,10 @@ class MainActivity : AppCompatActivity() {
}
}
}

companion object {
private val POSITIVE = "Positive"
private val NEGATIVE = "Negative"
private val NEUTRAL = "Neutral"
}
}
4 changes: 2 additions & 2 deletions suspend-dialogs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ android {
}

dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"

implementation 'androidx.appcompat:appcompat:1.3.1'
Expand All @@ -49,7 +49,7 @@ project.afterEvaluate {
from components.release

groupId = 'com.github.xeinebiu'
version = '1.5.0'
version = '1.6.0'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ suspend inline fun AlertDialog.Builder.confirm(
*/
suspend inline fun AlertDialog.Builder.setItems(
activity: Activity,
@MenuRes menuRes: Int
@MenuRes menuRes: Int,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
) = SuspendAlertDialog.setItems(
activity = activity,
menuRes = menuRes
menuRes = menuRes,
positiveButtonText = positiveButtonText,
negativeButtonText = negativeButtonText,
neutralButtonText = neutralButtonText
) { this }

/**
Expand All @@ -58,8 +64,16 @@ suspend inline fun AlertDialog.Builder.setItems(
* Return the selected [MenuItem]
*/
suspend inline fun AlertDialog.Builder.setItems(
menu: Menu
) = SuspendAlertDialog.setItems(menu) { this }
menu: Menu,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
) = SuspendAlertDialog.setItems(
menu = menu,
positiveButtonText = positiveButtonText,
negativeButtonText = negativeButtonText,
neutralButtonText = neutralButtonText
) { this }

/**
* Display an [AlertDialog] with [items] as CTA
Expand All @@ -68,8 +82,16 @@ suspend inline fun AlertDialog.Builder.setItems(
*
* Return the selected index from [items]
*/
suspend fun AlertDialog.Builder.setItems(items: List<String>) = SuspendAlertDialog.setItems(
items
suspend fun AlertDialog.Builder.setItems(
items: List<String>,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
) = SuspendAlertDialog.setItems(
items = items,
positiveButtonText = positiveButtonText,
negativeButtonText = negativeButtonText,
neutralButtonText = neutralButtonText
) { this }

/**
Expand All @@ -81,11 +103,11 @@ suspend fun AlertDialog.Builder.setItems(items: List<String>) = SuspendAlertDial
*/
suspend inline fun AlertDialog.Builder.setSingleChoiceItems(
activity: Activity,
@MenuRes menuRes: Int,
selectedIndex: Int = -1,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
@MenuRes menuRes: Int,
selectedIndex: Int = -1
): SuspendAlertDialog.SingleChoiceMenuResult = SuspendAlertDialog.setSingleChoiceItems(
activity = activity,
positiveButtonText = positiveButtonText,
Expand All @@ -103,11 +125,11 @@ suspend inline fun AlertDialog.Builder.setSingleChoiceItems(
* dismiss the dialog. Clicking on a button will dismiss the dialog.
*/
suspend inline fun AlertDialog.Builder.setSingleChoiceItems(
menu: Menu,
selectedIndex: Int = -1,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
menu: Menu,
selectedIndex: Int = -1
): SuspendAlertDialog.SingleChoiceMenuResult = SuspendAlertDialog.setSingleChoiceItems(
positiveButtonText = positiveButtonText,
negativeButtonText = negativeButtonText,
Expand All @@ -124,10 +146,10 @@ suspend inline fun AlertDialog.Builder.setSingleChoiceItems(
* dismiss the dialog. Clicking on a button will dismiss the dialog.
*/
suspend inline fun AlertDialog.Builder.setSingleChoiceItems(
items: SuspendAlertDialog.SingleChoiceItems,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
items: SuspendAlertDialog.SingleChoiceItems
) = SuspendAlertDialog.setSingleChoiceItems(
positiveButtonText = positiveButtonText,
negativeButtonText = negativeButtonText,
Expand All @@ -142,10 +164,10 @@ suspend inline fun AlertDialog.Builder.setSingleChoiceItems(
* dismiss the dialog. Clicking on a button will dismiss the dialog.
*/
suspend inline fun AlertDialog.Builder.setMultiChoiceItems(
items: SuspendAlertDialog.MultiChoiceItems,
positiveButtonText: CharSequence? = null,
negativeButtonText: CharSequence? = null,
neutralButtonText: CharSequence? = null,
items: SuspendAlertDialog.MultiChoiceItems
) = SuspendAlertDialog.setMultiChoiceItems(
positiveButtonText = positiveButtonText,
negativeButtonText = negativeButtonText,
Expand Down
Loading

0 comments on commit a5f85da

Please sign in to comment.