Skip to content

Instantly share code, notes, and snippets.

@KaNiShi
Last active June 7, 2019 08:44
Show Gist options
  • Save KaNiShi/36bcd7ac0699e62d2565ac255ee841e3 to your computer and use it in GitHub Desktop.
Save KaNiShi/36bcd7ac0699e62d2565ac255ee841e3 to your computer and use it in GitHub Desktop.
import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
class SimpleDialogFragment: DialogFragment(), DialogInterface.OnClickListener {
companion object {
fun newInstance(title: String = "", content: String = "", buttonType: DialogButtonType = DialogButtonType.OK_ONLY, returnData: Bundle? = null, positiveText: String? = null, negativeText: String? = null, neutralText: String? = null) = SimpleDialogFragment().apply {
arguments = Bundle().apply {
putSerializable(ARG_TEXT_TYPE, DialogTextType.STRING)
putString(ARG_TITLE, title)
putString(ARG_CONTENT, content)
putSerializable(ARG_TYPE, buttonType)
putBundle(ARG_RETURN_DATA, returnData)
putString(ARG_POSITIVE, positiveText)
putString(ARG_NEGATIVE, negativeText)
putString(ARG_NEUTRAL, neutralText)
}
}
fun newInstance(@StringRes title: Int = 0, @StringRes content: Int = 0, buttonType: DialogButtonType = DialogButtonType.OK_ONLY, returnData: Bundle? = null, @StringRes positiveText: Int = 0, @StringRes negativeText: Int = 0, @StringRes neutralText: Int = 0) = SimpleDialogFragment().apply {
arguments = Bundle().apply {
putSerializable(ARG_TEXT_TYPE, DialogTextType.INT)
putInt(ARG_TITLE, title)
putInt(ARG_CONTENT, content)
putSerializable(ARG_TYPE, buttonType)
putBundle(ARG_RETURN_DATA, returnData)
putInt(ARG_POSITIVE, positiveText)
putInt(ARG_NEGATIVE, negativeText)
putInt(ARG_NEUTRAL, neutralText)
}
}
private const val ARG_TEXT_TYPE = "arg_text_type"
private const val ARG_TITLE = "arg_title"
private const val ARG_CONTENT = "arg_content"
private const val ARG_TYPE = "arg_type"
private const val ARG_RETURN_DATA = "arg_return_data"
private const val ARG_POSITIVE = "arg_positive"
private const val ARG_NEGATIVE = "arg_negative"
private const val ARG_NEUTRAL = "arg_neutral"
const val RESULT_POSITIVE = 1
const val RESULT_NEGATIVE = -1
const val RESULT_NEUTRAL = 0
const val RESULT_DATA = "result_data"
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val args = arguments ?: return super.onCreateDialog(savedInstanceState)
isCancelable = false
val params = mutableMapOf<String, String?>()
when(args.getSerializable(ARG_TEXT_TYPE)) {
DialogTextType.STRING -> {
params[ARG_TITLE] = args.getString(ARG_TITLE)
params[ARG_CONTENT] = args.getString(ARG_CONTENT)
params[ARG_POSITIVE] = args.getString(ARG_POSITIVE)
params[ARG_NEGATIVE] = args.getString(ARG_NEGATIVE)
params[ARG_NEUTRAL] = args.getString(ARG_NEUTRAL)
}
DialogTextType.INT -> {
params[ARG_TITLE] = getStringZero(args.getInt(ARG_TITLE))
params[ARG_CONTENT] = getStringZero(args.getInt(ARG_CONTENT))
params[ARG_POSITIVE] = getStringZero(args.getInt(ARG_POSITIVE))
params[ARG_NEGATIVE] = getStringZero(args.getInt(ARG_NEGATIVE))
params[ARG_NEUTRAL] = getStringZero(args.getInt(ARG_NEUTRAL))
}
}
return AlertDialog.Builder(requireContext())
.setTitle(params[ARG_TITLE])
.setMessage(params[ARG_CONTENT])
.apply {
when(args.getSerializable(ARG_TYPE)) {
DialogButtonType.OK_ONLY -> setPositiveButton(android.R.string.ok)
DialogButtonType.OK_NO -> {
setPositiveButton(android.R.string.ok)
setNegativeButton(android.R.string.no)
}
DialogButtonType.YES_ONLY -> setPositiveButton(android.R.string.yes)
DialogButtonType.YES_NO -> {
setPositiveButton(android.R.string.yes)
setNegativeButton(android.R.string.no)
}
DialogButtonType.YES_NO_CANCEL -> {
setPositiveButton(android.R.string.yes)
setNegativeButton(android.R.string.no)
setNeutralButton(android.R.string.cancel)
}
DialogButtonType.NO_YES -> {
setPositiveButton(android.R.string.no)
setNegativeButton(android.R.string.yes)
}
DialogButtonType.CUSTOM -> {
params[ARG_POSITIVE]?.let { text -> setPositiveButton(text) }
params[ARG_NEGATIVE]?.let { text -> setNegativeButton(text) }
params[ARG_NEUTRAL]?.let { text -> setNeutralButton(text) }
}
}
}.create()
}
override fun onClick(dialog: DialogInterface, which: Int) {
val resultCode = when(which) {
DialogInterface.BUTTON_POSITIVE -> RESULT_POSITIVE
DialogInterface.BUTTON_NEGATIVE -> RESULT_NEGATIVE
DialogInterface.BUTTON_NEUTRAL -> RESULT_NEUTRAL
else -> throw IllegalStateException("Unknown which: $which")
}
result(resultCode)
}
private fun result(resultCode: Int) {
targetFragment?.onActivityResult(targetRequestCode, resultCode, Intent().putExtra(RESULT_DATA, arguments?.getBundle(ARG_RETURN_DATA)))
}
private fun AlertDialog.Builder.setPositiveButton(@StringRes textId: Int) = setPositiveButton(textId, this@SimpleDialogFragment)
private fun AlertDialog.Builder.setNegativeButton(@StringRes textId: Int) = setNegativeButton(textId, this@SimpleDialogFragment)
private fun AlertDialog.Builder.setNeutralButton(@StringRes textId: Int) = setNeutralButton(textId, this@SimpleDialogFragment)
private fun AlertDialog.Builder.setPositiveButton(text: String) = setPositiveButton(text, this@SimpleDialogFragment)
private fun AlertDialog.Builder.setNegativeButton(text: String) = setNegativeButton(text, this@SimpleDialogFragment)
private fun AlertDialog.Builder.setNeutralButton(text: String) = setNeutralButton(text, this@SimpleDialogFragment)
private fun getStringZero(@StringRes resId: Int): String? = if(resId == 0) {
null
} else {
getString(resId)
}
enum class DialogButtonType {
OK_ONLY,
OK_NO,
YES_ONLY,
YES_NO,
YES_NO_CANCEL,
NO_YES,
CUSTOM
}
private enum class DialogTextType {
STRING,
INT
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment