Kotlin suspendable dialogs for android

Overview

Android Suspendable Dialogs

A helper library for Android to display Dialogs by suspending the coroutine till finish of the dialog.

Installation

allprojects {
	repositories {
		...
		maven { url 'https://www.jitpack.io' }
	}
}
dependencies {
    implementation 'com.github.xeinebiu:android-suspend-dialogs:1.0.0'
}

Some Differences with and without suspend

Confirm

The below example shows how a normal dialog is shown without any suspension

MaterialAlertDialogBuilder(this@MainActivity)
    .setTitle("Title")
    .setMessage("Message")
    .setPositiveButton("Positive") { _, _ ->
        // code to execute
    }
    .setNegativeButton("Negative") { _, _ ->
        // code to execute
    }
    .setNeutralButton("Neutral") { _, _ ->
        // code to execute
    }
    .show()
        
    // this code is executed before the dialog is finished
    println("Hello World")

Now, using the suspend dialog, we can wait for the dialog to be finish after continue with rest of the code flow

val result = SuspendAlertDialog.confirm(
    positiveButtonText = "Positive",
    negativeButtonText = "Negative",
    neutralButtonText = "Neutral"
) {
    MaterialAlertDialogBuilder(this@MainActivity)
        .setTitle("Title")
        .setMessage("Message")
}

// this line is executed after the dialog above is finish
tvResult.text = result.toString()

If you are fan of extension functions, the below approach can be used to achieve the same behavior as above.

val result = MaterialAlertDialogBuilder(this@MainActivity)
    .setTitle("Title")
    .setMessage("Message")
    .confirm(
        positiveButtonText = "Save",
        negativeButtonText = "Cancel",
        neutralButtonText = "Neutral",
    )

tvResult.text = result.toString()

Alert

SuspendAlertDialog.alert("Ok") {
    MaterialAlertDialogBuilder(this@MainActivity).setTitle("Selected Option")
}

tvResult.text = getString(R.string.alert_finished)

Using extension function

MaterialAlertDialogBuilder(this@MainActivity)
    .setTitle("Selected Option")
    .alert("Ok")

tvResult.text = getString(R.string.alert_finished)

Multi Choice Items

val multiChoiceResult = SuspendAlertDialog.setMultiChoiceItems(
        positiveButtonText = "Save",
        negativeButtonText = "Cancel",
        neutralButtonText = "Minimize",
        items = SuspendAlertDialog.MultiChoiceItems(
            items = listOf("Hello", "World", "Berlin", "Germany"),
            checked = listOf(false, false, false, false)
        )
) {
    MaterialAlertDialogBuilder(this@MainActivity).setTitle("Title")
}

tvResult.text = multiChoiceResult.toString()

Using extension function

val result = MaterialAlertDialogBuilder(this@MainActivity)
    .setTitle("Title")
    .setMultiChoiceItems(
        positiveButtonText = "Save",
        negativeButtonText = "Cancel",
        neutralButtonText = "Minimize",
        items = SuspendAlertDialog.MultiChoiceItems(
            items = listOf("Hello", "World", "Berlin", "Germany"),
            checked = listOf(false, false, false, false)
        )
    )

tvResult.text = result.toString()

Single Choice Items

val singleChoiceResult = SuspendAlertDialog.setSingleChoiceItems(
    positiveButtonText = "Save",
    negativeButtonText = "Cancel",
    neutralButtonText = "Minimize",
    items = SuspendAlertDialog.SingleChoiceItems(
        items = listOf("Hello", "World", "Berlin", "Germany"),
        selectedIndex = 1
    )
) {
  MaterialAlertDialogBuilder(this@MainActivity).setTitle("Title")
}

tvResult.text = singleChoiceResult.toString()

Using extension function

val result = MaterialAlertDialogBuilder(this@MainActivity)
    .setTitle("Title")
    .setSingleChoiceItems(
        positiveButtonText = "Save",
        negativeButtonText = "Cancel",
        neutralButtonText = "Minimize",
        items = SuspendAlertDialog.SingleChoiceItems(
            items = listOf("Hello", "World", "Berlin", "Germany"),
            selectedIndex = 1
        )
    )

tvResult.text = result.toString()
You might also like...
A Kotlin Native program to show the time since a date, using Kotlin LibUI
A Kotlin Native program to show the time since a date, using Kotlin LibUI

TimeSince A Kotlin Native program to show the time since a date, using Kotlin LibUI Report Bug . Request Feature About The Project TimeSince is a Kotl

RoomJetpackCompose is an app written in Kotlin and shows a simple solution to perform CRUD operations in the Room database using Kotlin Flow in clean architecture.
RoomJetpackCompose is an app written in Kotlin and shows a simple solution to perform CRUD operations in the Room database using Kotlin Flow in clean architecture.

RoomJetpackCompose is an app written in Kotlin and shows a simple solution to perform CRUD operations in the Room database using Kotlin Flow in clean architecture.

Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web 본 저장소는 코틀린 멀티플랫폼 기반 웹 프로그래밍 워크숍(강좌)을 위해 작성된 템플릿 프로젝트가 있는 곳입니다. 워크숍 과정에서 코틀린 멀티플랫폼을 기반으로 프론트엔드(front-end)는 Ko

Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform 본 저장소는 INFCON 2022에서 코틀린 멀티플랫폼 기반 웹 프로그래밍 핸즈온랩을 위해 작성된 템플릿 프로젝트가 있는 곳입니다. 핸즈온 과정에서 코틀린 멀티플랫폼을

Android MVVM framework write in kotlin, develop Android has never been so fun.

KBinding 中文版 Android MVVM framework write in kotlin, base on anko, simple but powerful. It depends on my another project AutoAdapter(A library for sim

Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android.
Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android.

Klimatic Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android. Built using Android Architectu

Gits-android-extensions - A collection of Kotlin extensions to simplify Android development

gits-android-extensions A collection of Kotlin extensions to simplify Android de

Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.
Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.

Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.

Kotlin library for Android
Kotlin library for Android

KAndroid Kotlin library for Android providing useful extensions to eliminate boilerplate code in Android SDK and focus on productivity. Download Downl

Comments
  • Fix deprecated OnLifecycleEvent

    Fix deprecated OnLifecycleEvent

    OnLifecycleEvent https://developer.android.com/reference/androidx/lifecycle/OnLifecycleEvent

    OnLifecycleEvent annotation vs DefaultLifecycleObserver https://stackoverflow.com/questions/70384129/lifecycle-onlifecycleevent-is-deprecated

    opened by gresolio 0
Releases(1.6.2)
Owner
null
Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Cash App 1.5k Dec 30, 2022
A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

common sense OSS 0 Jan 20, 2022
Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = ❤️

kotlin-android-template ?? A simple Github template that lets you create an Android/Kotlin project and be up and running in a few seconds. This templa

Nicola Corti 1.5k Jan 3, 2023
LifecycleMvp 1.2 0.0 Kotlin is MVP architecture implementation with Android Architecture Components and Kotlin language features

MinSDK 14+ Download Gradle Add to project level build.gradle allprojects { repositories { ... maven { url 'https://jitpack.io' }

Robert 20 Nov 9, 2021
👋 A common toolkit (utils) ⚒️ built to help you further reduce Kotlin boilerplate code and improve development efficiency. Do you think 'kotlin-stdlib' or 'android-ktx' is not sweet enough? You need this! 🍭

Toolkit [ ?? Work in progress ⛏ ?? ??️ ?? ] Snapshot version: repositories { maven("https://s01.oss.sonatype.org/content/repositories/snapshots") }

凛 35 Jul 23, 2022
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

libill 4 Nov 20, 2022
Opinionated Redux-like implementation backed by Kotlin Coroutines and Kotlin Multiplatform Mobile

CoRed CoRed is Redux-like implementation that maintains the benefits of Redux's core idea without the boilerplate. No more action types, action creato

Kittinun Vantasin 28 Dec 10, 2022
Provides Kotlin libs and some features for building Kotlin plugins

Kotlin Plugin Provides Kotlin libs and some features for building awesome Kotlin plugins. Can be used instead of CreeperFace's KotlinLib (don't use to

null 3 Dec 24, 2021
Notes-App-Kotlin - Notes App Built Using Kotlin

Notes-App-Kotlin Splash Screen Home Page Adding New Notes Filter Feature Search

Priyanka 4 Oct 2, 2022
Kotlin-client-dsl - A kotlin-based dsl project for a (Client) -> (Plugin) styled program

kotlin-client-dsl a kotlin-based dsl project for a (Client) -> (Plugin) styled p

jackson 3 Dec 10, 2022