NekoX/TMessagesProj/src/main/java/tw/nekomimi/nekogram/ui/BottomBuilder.kt

269 lines
9.7 KiB
Kotlin
Raw Normal View History

2022-01-17 08:45:37 +01:00
package tw.nekomimi.nekogram.ui
2020-06-25 17:25:17 +02:00
import android.content.Context
2022-04-26 08:38:28 +02:00
import android.content.DialogInterface
2020-06-25 17:25:17 +02:00
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity
import android.widget.*
import org.telegram.messenger.AndroidUtilities
import org.telegram.messenger.LocaleController
import org.telegram.messenger.R
import org.telegram.ui.ActionBar.BottomSheet
import org.telegram.ui.ActionBar.Theme
import org.telegram.ui.Cells.HeaderCell
import org.telegram.ui.Cells.RadioButtonCell
import org.telegram.ui.Cells.TextCell
import org.telegram.ui.Cells.TextCheckCell
2020-06-25 17:25:17 +02:00
import org.telegram.ui.Components.LayoutHelper
import java.util.*
2022-07-12 16:29:42 +02:00
class BottomBuilder(val ctx: Context, val needFocus: Boolean = true, val bgColor: Int = Theme.getColor(Theme.key_dialogBackground)) {
2022-07-12 16:29:42 +02:00
constructor(ctx: Context) : this(ctx, true) {}
constructor(ctx: Context, needFocus: Boolean) : this(ctx, needFocus, Theme.getColor(Theme.key_dialogBackground)) {}
2022-07-12 16:29:42 +02:00
val builder = BottomSheet.Builder(ctx, needFocus)
2020-06-25 17:25:17 +02:00
private val rootView = LinearLayout(ctx).apply {
orientation = LinearLayout.VERTICAL
}
2021-02-04 18:59:00 +01:00
private val rtl = (if (LocaleController.isRTL) Gravity.RIGHT else Gravity.LEFT)
2020-06-25 17:25:17 +02:00
private val _root = LinearLayout(ctx).apply {
addView(ScrollView(ctx).apply {
addView(this@BottomBuilder.rootView)
isFillViewport = true
isVerticalScrollBarEnabled = false
}, LinearLayout.LayoutParams(-1, -1))
builder.setCustomView(this)
}
private val buttonsView by lazy {
FrameLayout(ctx).apply {
setBackgroundColor(bgColor)
2020-06-25 17:25:17 +02:00
this@BottomBuilder.rootView.addView(this, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 50, Gravity.LEFT or Gravity.BOTTOM))
2021-02-04 18:59:00 +01:00
addView(rightButtonsView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP or Gravity.RIGHT))
2020-06-25 17:25:17 +02:00
}
}
private val rightButtonsView by lazy {
LinearLayout(ctx).apply {
orientation = LinearLayout.HORIZONTAL
weightSum = 1F
}
}
@JvmOverloads
fun addTitle(title: CharSequence, bigTitle: Boolean = false): HeaderCell {
return addTitle(title, bigTitle, null)
}
fun addTitle(title: CharSequence, subTitle: CharSequence): HeaderCell {
return addTitle(title, true, subTitle)
}
fun addTitle(title: CharSequence, bigTitle: Boolean, subTitle: CharSequence?): HeaderCell {
2022-07-06 14:07:52 +02:00
val headerCell = HeaderCell(ctx, Theme.key_dialogTextBlue2, 23, 15, false)
headerCell.setBigTitle(bigTitle)
2020-06-25 17:25:17 +02:00
headerCell.setText(if (title is String) AndroidUtilities.replaceTags(title) else title)
subTitle?.also {
headerCell.setText2(it)
}
rootView.addView(headerCell, LayoutHelper.createLinear(-1, -2).apply {
bottomMargin = AndroidUtilities.dp(8F)
2020-06-25 17:25:17 +02:00
})
return headerCell
}
@JvmOverloads
2021-01-31 18:28:28 +01:00
fun addCheckItem(text: String, value: Boolean, switch: Boolean = false, valueText: String? = null, listener: ((cell: TextCheckCell, isChecked: Boolean) -> Unit)?): TextCheckCell {
2020-06-25 17:25:17 +02:00
val checkBoxCell = TextCheckCell(ctx, 21, !switch)
checkBoxCell.setBackgroundDrawable(Theme.getSelectorDrawable(false))
checkBoxCell.minimumHeight = AndroidUtilities.dp(50F)
rootView.addView(checkBoxCell, LayoutHelper.createLinear(-1, -2))
if (valueText == null) {
checkBoxCell.setTextAndCheck(text, value, true)
} else {
checkBoxCell.setTextAndValueAndCheck(text, valueText, value, true, true)
}
checkBoxCell.setOnClickListener {
2021-01-31 18:28:28 +01:00
val target = !checkBoxCell.isChecked
checkBoxCell.isChecked = target
listener?.invoke(checkBoxCell, target)
}
2020-06-25 17:25:17 +02:00
2021-01-31 18:28:28 +01:00
if (checkBoxCell.checkBox != null) {
checkBoxCell.checkBox.setOnClickListener { checkBoxCell.performClick() }
} else {
checkBoxCell.checkBoxSquare.setOnClickListener { checkBoxCell.performClick() }
2020-06-25 17:25:17 +02:00
}
return checkBoxCell
}
@JvmOverloads
2021-01-31 18:28:28 +01:00
fun addCheckItems(text: Array<String>, value: (Int) -> Boolean, switch: Boolean = false, valueText: ((Int) -> String)? = null, listener: (index: Int, text: String, cell: TextCheckCell, isChecked: Boolean) -> Unit): List<TextCheckCell> {
2020-06-25 17:25:17 +02:00
val list = mutableListOf<TextCheckCell>()
text.forEachIndexed { index, textI ->
2021-01-31 18:28:28 +01:00
list.add(addCheckItem(textI, value(index), switch, valueText?.invoke(index)) { cell, isChecked ->
listener(index, textI, cell, isChecked)
2020-06-25 17:25:17 +02:00
})
}
return list
}
private val radioButtonGroup by lazy { LinkedList<RadioButtonCell>() }
fun doRadioCheck(cell: RadioButtonCell) {
if (!cell.isChecked) {
radioButtonGroup.forEach {
if (it.isChecked) {
it.setChecked(false, true)
}
}
cell.setChecked(true, true)
}
}
@JvmOverloads
fun addRadioItem(text: String, value: Boolean, valueText: String? = null, listener: (cell: RadioButtonCell) -> Unit): RadioButtonCell {
val checkBoxCell = RadioButtonCell(ctx, true)
checkBoxCell.setBackgroundDrawable(Theme.getSelectorDrawable(false))
checkBoxCell.minimumHeight = AndroidUtilities.dp(50F)
rootView.addView(checkBoxCell, LayoutHelper.createLinear(-1, -2))
if (valueText == null) {
checkBoxCell.setTextAndValue(text, true, value)
} else {
checkBoxCell.setTextAndValueAndCheck(text, valueText, true, value)
}
radioButtonGroup.add(checkBoxCell)
checkBoxCell.setOnClickListener {
listener(checkBoxCell)
}
return checkBoxCell
}
@JvmOverloads
fun addRadioItems(text: Array<String>, value: (Int, String) -> Boolean, valueText: ((Int, String) -> String)? = null, listener: (index: Int, text: String, cell: RadioButtonCell) -> Unit): List<RadioButtonCell> {
val list = mutableListOf<RadioButtonCell>()
text.forEachIndexed { index, textI ->
list.add(addRadioItem(textI, value(index, textI), valueText?.invoke(index, textI)) { cell ->
listener(index, textI, cell)
})
}
return list
}
fun addCancelItem() {
2020-07-10 12:10:21 +02:00
addItem(LocaleController.getString("Cancel", R.string.Cancel), R.drawable.baseline_cancel_24) {}
2020-06-25 17:25:17 +02:00
}
@JvmOverloads
fun addCancelButton(left: Boolean = true) {
2020-07-10 12:10:21 +02:00
addButton(LocaleController.getString("Cancel", R.string.Cancel), left = left) {}
2020-06-25 17:25:17 +02:00
}
@JvmOverloads
2021-01-31 18:28:28 +01:00
fun addOkButton(listener: ((TextView) -> Unit), noAutoDismiss: Boolean = false) {
addButton(LocaleController.getString("OK", R.string.OK), noAutoDismiss) { listener(it); }
2020-06-25 17:25:17 +02:00
}
@JvmOverloads
2020-10-25 14:26:45 +01:00
fun addButton(text: String, noAutoDismiss: Boolean = false, left: Boolean = false, listener: ((TextView) -> Unit)): TextView {
2020-06-25 17:25:17 +02:00
return TextView(ctx).apply {
setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f)
setTextColor(Theme.getColor(Theme.key_dialogTextBlue4))
gravity = Gravity.CENTER
isSingleLine = true
ellipsize = TextUtils.TruncateAt.END
setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(Theme.key_dialogButtonSelector), 0))
setPadding(AndroidUtilities.dp(18f), 0, AndroidUtilities.dp(18f), 0)
setText(text)
typeface = AndroidUtilities.getTypeface("fonts/rmedium.ttf")
2021-02-04 18:59:00 +01:00
(if (left) buttonsView else rightButtonsView).addView(this, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, rtl))
2020-10-25 14:26:45 +01:00
setOnClickListener { if (!noAutoDismiss) dismiss();listener(this) }
2020-06-25 17:25:17 +02:00
}
}
@JvmOverloads
fun addItem(text: String, icon: Int = 0, red: Boolean = false, listener: ((cell: TextCell) -> Unit)?): TextCell {
return TextCell(ctx).apply {
background = Theme.getSelectorDrawable(false)
setTextAndIcon(text, icon, false)
2020-06-25 17:25:17 +02:00
setOnClickListener {
2020-07-10 12:10:21 +02:00
dismiss()
listener?.invoke(this)
2020-06-25 17:25:17 +02:00
}
if (red) {
setColors("key_dialogTextRed2", "key_dialogTextRed2")
}
2021-02-04 18:59:00 +01:00
this@BottomBuilder.rootView.addView(this, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 48, rtl))
2020-06-25 17:25:17 +02:00
}
}
2021-02-04 18:59:00 +01:00
fun addItems(text: Array<String?>, icon: IntArray?, listener: (index: Int, text: String, cell: TextCell) -> Unit): List<TextCell> {
2020-06-25 17:25:17 +02:00
val list = mutableListOf<TextCell>()
text.forEachIndexed { index, textI ->
2021-02-03 15:14:57 +01:00
list.add(addItem(textI ?: return@forEachIndexed, icon?.get(index) ?: 0) { cell ->
2020-06-25 17:25:17 +02:00
listener(index, textI, cell)
})
}
return list
}
@JvmOverloads
fun addEditText(hintText: String? = null): EditText {
return EditText(ctx).apply {
setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f)
setTextColor(Theme.getColor(Theme.key_dialogTextBlack))
setHintTextColor(Theme.getColor(Theme.key_dialogTextBlue4))
hintText?.also { hint = it }
isSingleLine = true
isFocusable = true
setBackgroundDrawable(null)
2021-02-04 18:59:00 +01:00
this@BottomBuilder.rootView.addView(this, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, -2, rtl, AndroidUtilities.dp(6F), 0, 0, 0))
2020-06-25 17:25:17 +02:00
}
}
fun create() = builder.create()
fun show() = builder.show()
fun dismiss() {
builder.dismissRunnable.run()
}
2022-04-26 08:38:28 +02:00
fun setOnPreDismissListener(onDismissListener: DialogInterface.OnDismissListener?): BottomBuilder {
builder.setOnPreDismissListener(onDismissListener)
return this
}
2020-06-25 17:25:17 +02:00
}