2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.dialog
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-02-15 02:51:22 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.R
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.data.notEmpty
|
2020-09-29 19:44:56 +02:00
|
|
|
import java.util.*
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class ActionsDialog {
|
2021-06-20 15:12:25 +02:00
|
|
|
|
|
|
|
private val actionList = ArrayList<Action>()
|
|
|
|
|
|
|
|
private class Action(val caption: CharSequence, val action: () -> Unit)
|
|
|
|
|
|
|
|
fun addAction(caption: CharSequence, action: () -> Unit): ActionsDialog {
|
|
|
|
|
|
|
|
actionList.add(Action(caption, action))
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
fun show(context: Context, title: CharSequence? = null): ActionsDialog {
|
|
|
|
AlertDialog.Builder(context).apply {
|
|
|
|
setNegativeButton(R.string.cancel, null)
|
|
|
|
setItems(actionList.map { it.caption }.toTypedArray()) { _, which ->
|
|
|
|
if (which >= 0 && which < actionList.size) {
|
|
|
|
actionList[which].action()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
title?.notEmpty()?.let { setTitle(it) }
|
|
|
|
}.show()
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|