DroidFS/app/src/main/java/sushi/hardcore/droidfs/widgets/CustomAlertDialogBuilder.kt

69 lines
2.4 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.widgets
import android.content.Context
2021-11-09 11:12:09 +01:00
import android.util.TypedValue
import android.view.WindowManager
2021-09-05 15:00:16 +02:00
import androidx.appcompat.app.AlertDialog
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
2021-11-09 11:12:09 +01:00
import sushi.hardcore.droidfs.R
2023-02-28 22:50:59 +01:00
import sushi.hardcore.droidfs.Theme
2020-07-17 16:35:39 +02:00
2023-02-28 22:50:59 +01:00
open class CustomAlertDialogBuilder(context: Context, theme: Theme) : AlertDialog.Builder(
context, if (theme.black) {
when (theme.color) {
"red" -> R.style.BlackRedDialog
"blue" -> R.style.BlackBlueDialog
"yellow" -> R.style.BlackYellowDialog
"orange" -> R.style.BlackOrangeDialog
"purple" -> R.style.BlackPurpleDialog
"pink" -> R.style.BlackPinkDialog
else -> R.style.BlackGreenDialog
}
} else {
when (theme.color) {
"red" -> R.style.DarkRedDialog
"blue" -> R.style.DarkBlueDialog
"yellow" -> R.style.DarkYellowDialog
"orange" -> R.style.DarkOrangeDialog
"purple" -> R.style.DarkPurpleDialog
"pink" -> R.style.DarkPinkDialog
else -> R.style.DarkGreenDialog
}
}
2021-11-09 11:12:09 +01:00
) {
private var keepFullScreen = false
fun keepFullScreen(): AlertDialog.Builder {
keepFullScreen = true
return this
}
override fun show(): AlertDialog {
val dialog = create()
2020-07-17 16:35:39 +02:00
dialog.show()
if (keepFullScreen){
2021-09-05 15:00:16 +02:00
dialog.window?.let {
WindowInsetsControllerCompat(it, it.decorView)
.hide(WindowInsetsCompat.Type.statusBars())
it.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
}
}
return dialog
2020-07-17 16:35:39 +02:00
}
override fun create(): AlertDialog {
val dialog = super.create()
2021-11-09 11:12:09 +01:00
dialog.setOnShowListener {
val typedValue = TypedValue()
context.theme.resolveAttribute(R.attr.colorAccent, typedValue, true)
for (i in listOf(AlertDialog.BUTTON_POSITIVE, AlertDialog.BUTTON_NEGATIVE, AlertDialog.BUTTON_NEUTRAL)) {
dialog.getButton(i).setTextColor(typedValue.data)
}
}
if (keepFullScreen){
dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
}
2020-07-17 16:35:39 +02:00
return dialog
}
}