MicroUtils/android/alerts/recyclerview/src/androidMain/kotlin/dev/inmo/micro_utils/android/alerts/recyclerview/ActionsAlerts.kt

66 lines
1.9 KiB
Kotlin
Raw Normal View History

2020-11-23 12:10:53 +00:00
@file:Suppress("unused")
2020-11-23 12:32:14 +00:00
package dev.inmo.micro_utils.android.alerts.recyclerview
2020-11-23 12:10:53 +00:00
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.ViewGroup
import android.widget.TextView
2020-11-23 12:32:14 +00:00
import dev.inmo.micro_utils.android.alerts.common.AlertDialogCallback
2020-11-23 12:10:53 +00:00
import dev.inmo.micro_utils.android.recyclerview.*
data class AlertAction(
val title: String,
val callback: (DialogInterface) -> Unit
)
2021-05-12 15:16:05 +00:00
class ActionViewHolder(
2020-11-23 12:10:53 +00:00
container: ViewGroup, dialogInterfaceGetter: () -> DialogInterface
) : AbstractStandardViewHolder<AlertAction>(container, android.R.layout.simple_list_item_1) {
private lateinit var action: AlertAction
private val textView: TextView
get() = itemView.findViewById(android.R.id.text1)
init {
itemView.setOnClickListener {
action.callback(dialogInterfaceGetter())
}
}
override fun onBind(item: AlertAction) {
action = item
textView.text = item.title
}
}
2021-05-12 15:16:05 +00:00
class ActionsRecyclerViewAdapter(
2021-06-15 08:24:00 +00:00
override val data: List<AlertAction>,
2020-11-23 12:10:53 +00:00
private val dialogInterfaceGetter: () -> DialogInterface
2021-06-15 08:24:00 +00:00
) : RecyclerViewAdapter<AlertAction>() {
2020-11-23 12:10:53 +00:00
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<AlertAction> = ActionViewHolder(
parent, dialogInterfaceGetter
)
}
fun Context.createActionsAlertDialog(
actions: List<AlertAction>,
title: Int? = null,
positivePair: Pair<Int, AlertDialogCallback?>? = null,
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
negativePair: Pair<Int, AlertDialogCallback?>? = null,
show: Boolean = true
): AlertDialog {
lateinit var dialogInterface: DialogInterface
return createRecyclerViewDialog(
title, positivePair, neutralPair, negativePair, show
) {
ActionsRecyclerViewAdapter(
actions
) {
dialogInterface
}
}.also { dialogInterface = it }
}