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

61 lines
2.2 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
2020-07-17 16:35:39 +02:00
2022-03-24 20:08:23 +01:00
open class CustomAlertDialogBuilder(context: Context, theme: String) : AlertDialog.Builder(
2021-11-09 11:12:09 +01:00
context, when (theme) {
"black_green" -> R.style.BlackGreenDialog
"dark_red" -> R.style.DarkRedDialog
"black_red" -> R.style.BlackRedDialog
"dark_blue" -> R.style.DarkBlueDialog
"black_blue" -> R.style.BlackBlueDialog
"dark_yellow" -> R.style.DarkYellowDialog
"black_yellow" -> R.style.BlackYellowDialog
"dark_orange" -> R.style.DarkOrangeDialog
"black_orange" -> R.style.BlackOrangeDialog
"dark_purple" -> R.style.DarkPurpleDialog
"black_purple" -> R.style.BlackPurpleDialog
else -> R.style.DarkGreenDialog
}
) {
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
}
}