mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-03 15:19:44 +00:00
add Android.Alerts project
This commit is contained in:
17
android/alerts/build.gradle
Normal file
17
android/alerts/build.gradle
Normal file
@@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppAndroidProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
androidMain {
|
||||
dependencies {
|
||||
api "androidx.appcompat:appcompat-resources:$appcompat_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
android/alerts/src/main/AndroidManifest.xml
Normal file
1
android/alerts/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.micro_utils.android.alerts"/>
|
@@ -0,0 +1,55 @@
|
||||
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||
|
||||
package dev.inmo.micro_utils.android.alerts
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
|
||||
typealias AlertDialogCallback = (DialogInterface) -> Unit
|
||||
|
||||
inline fun Context.createAlertDialogTemplate(
|
||||
title: String? = null,
|
||||
positivePair: Pair<String, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<String, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<String, AlertDialogCallback?>? = null
|
||||
): AlertDialog.Builder {
|
||||
val builder = AlertDialog.Builder(this)
|
||||
|
||||
title ?.let {
|
||||
builder.setTitle(title)
|
||||
}
|
||||
|
||||
positivePair ?. let {
|
||||
builder.setPositiveButton(it.first) { di, _ -> it.second ?. invoke(di) }
|
||||
}
|
||||
negativePair ?. let {
|
||||
builder.setNegativeButton(it.first) { di, _ -> it.second ?. invoke(di) }
|
||||
}
|
||||
neutralPair ?. let {
|
||||
builder.setNeutralButton(it.first) { di, _ -> it.second ?. invoke(di) }
|
||||
}
|
||||
|
||||
return builder
|
||||
}
|
||||
|
||||
inline fun Context.createAlertDialogTemplateWithResources(
|
||||
title: Int? = null,
|
||||
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<Int, AlertDialogCallback?>? = null
|
||||
): AlertDialog.Builder = createAlertDialogTemplate(
|
||||
title ?.let { getString(it) },
|
||||
positivePair ?.let { getString(it.first) to it.second },
|
||||
neutralPair ?.let { getString(it.first) to it.second },
|
||||
negativePair ?.let { getString(it.first) to it.second }
|
||||
)
|
||||
|
||||
inline fun AlertDialog.setDismissChecker(noinline checker: () -> Boolean) : AlertDialog {
|
||||
setOnDismissListener {
|
||||
if (!checker()) {
|
||||
show()
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||
|
||||
package dev.inmo.micro_utils.android.alerts
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
|
||||
inline fun <T: View> Context.createCustomViewAlertDialog(
|
||||
title: String? = null,
|
||||
positivePair: Pair<String, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<String, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<String, AlertDialogCallback?>? = null,
|
||||
show: Boolean = true,
|
||||
viewCreator: (Context) -> T
|
||||
): AlertDialog = createAlertDialogTemplate(
|
||||
title, positivePair, neutralPair, negativePair
|
||||
).apply {
|
||||
setView(viewCreator(this@createCustomViewAlertDialog))
|
||||
}.create().apply {
|
||||
if (show) show()
|
||||
}
|
||||
|
||||
inline fun <T: View> Context.createCustomViewAlertDialogWithResources(
|
||||
title: Int? = null,
|
||||
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
show: Boolean = true,
|
||||
viewCreator: (Context) -> T
|
||||
): AlertDialog = createCustomViewAlertDialog(
|
||||
title ?.let { getString(it) },
|
||||
positivePair ?.let { getString(it.first) to it.second },
|
||||
neutralPair ?.let { getString(it.first) to it.second },
|
||||
negativePair ?.let { getString(it.first) to it.second },
|
||||
show,
|
||||
viewCreator
|
||||
)
|
@@ -0,0 +1,45 @@
|
||||
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||
|
||||
package dev.inmo.micro_utils.android.alerts
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import androidx.annotation.StringRes
|
||||
|
||||
inline fun Context.createSimpleTextAlertDialog(
|
||||
text: String,
|
||||
title: String? = null,
|
||||
positivePair: Pair<String, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<String, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<String, AlertDialogCallback?>? = null,
|
||||
show: Boolean = true
|
||||
): AlertDialog = createAlertDialogTemplate(
|
||||
title,
|
||||
positivePair,
|
||||
neutralPair,
|
||||
negativePair
|
||||
).apply {
|
||||
setMessage(text)
|
||||
}.create().apply {
|
||||
if (show) {
|
||||
show()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Context.createSimpleTextAlertDialog(
|
||||
@StringRes
|
||||
text: Int,
|
||||
@StringRes
|
||||
title: Int? = null,
|
||||
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
show: Boolean = true
|
||||
): AlertDialog = createSimpleTextAlertDialog(
|
||||
getString(text),
|
||||
title ?.let { getString(it) },
|
||||
positivePair ?.let { getString(it.first) to it.second },
|
||||
neutralPair ?.let { getString(it.first) to it.second },
|
||||
negativePair ?.let { getString(it.first) to it.second },
|
||||
show
|
||||
)
|
Reference in New Issue
Block a user