-
Notifications
You must be signed in to change notification settings - Fork 2
/
ActionsAlerts.kt
65 lines (56 loc) · 1.93 KB
/
ActionsAlerts.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@file:Suppress("unused")
package dev.inmo.micro_utils.android.alerts.recyclerview
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.ViewGroup
import android.widget.TextView
import dev.inmo.micro_utils.android.alerts.common.AlertDialogCallback
import dev.inmo.micro_utils.android.recyclerview.*
data class AlertAction(
val title: String,
val callback: (DialogInterface) -> Unit
)
class ActionViewHolder(
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
}
}
class ActionsRecyclerViewAdapter(
override val data: List<AlertAction>,
private val dialogInterfaceGetter: () -> DialogInterface
) : RecyclerViewAdapter<AlertAction>() {
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 }
}