| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import android.content.Context |
| 4 | import android.text.TextUtils |
| 5 | import android.util.TypedValue |
| 6 | import android.view.Gravity |
| 7 | import android.widget.* |
| 8 | import org.telegram.messenger.AndroidUtilities |
| 9 | import org.telegram.messenger.LocaleController |
| 10 | import org.telegram.messenger.R |
| 11 | import org.telegram.ui.ActionBar.BottomSheet |
| 12 | import org.telegram.ui.ActionBar.Theme |
| 13 | import org.telegram.ui.Cells.HeaderCell |
| 14 | import org.telegram.ui.Cells.RadioButtonCell |
| 15 | import org.telegram.ui.Cells.TextCell |
| 16 | import org.telegram.ui.Cells.TextCheckCell |
| 17 | import org.telegram.ui.Components.LayoutHelper |
| 18 | import java.util.* |
| 19 | |
| 20 | class BottomBuilder(val ctx: Context) { |
| 21 | val builder = BottomSheet.Builder(ctx, true) |
| 22 | |
| 23 | private val rootView = LinearLayout(ctx).apply { |
| 24 | orientation = LinearLayout.VERTICAL |
| 25 | } |
| 26 | private val rtl = (if (LocaleController.isRTL) Gravity.RIGHT else Gravity.LEFT) |
| 27 | |
| 28 | private val _root = LinearLayout(ctx).apply { |
| 29 | |
| 30 | addView(ScrollView(ctx).apply { |
| 31 | |
| 32 | addView(this@BottomBuilder.rootView) |
| 33 | isFillViewport = true |
| 34 | isVerticalScrollBarEnabled = false |
| 35 | |
| 36 | }, LinearLayout.LayoutParams(-1, -1)) |
| 37 | |
| 38 | builder.setCustomView(this) |
| 39 | |
| 40 | } |
| 41 | |
| 42 | |
| 43 | private val buttonsView by lazy { |
| 44 | |
| 45 | FrameLayout(ctx).apply { |
| 46 | |
| 47 | setBackgroundColor(Theme.getColor(Theme.key_dialogBackground)) |
| 48 | |
| 49 | this@BottomBuilder.rootView.addView(this, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 50, Gravity.LEFT or Gravity.BOTTOM)) |
| 50 | |
| 51 | addView(rightButtonsView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP or Gravity.RIGHT)) |
| 52 | |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | private val rightButtonsView by lazy { |
| 58 | |
| 59 | LinearLayout(ctx).apply { |
| 60 | |
| 61 | orientation = LinearLayout.HORIZONTAL |
| 62 | weightSum = 1F |
| 63 | |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | @JvmOverloads |
| 69 | fun addTitle(title: CharSequence, bigTitle: Boolean = false): HeaderCell { |
| 70 | |
| 71 | return addTitle(title, bigTitle, null) |
| 72 | |
| 73 | } |
| 74 | |
| 75 | fun addTitle(title: CharSequence, subTitle: CharSequence): HeaderCell { |
| 76 | |
| 77 | return addTitle(title, true, subTitle) |
| 78 | |
| 79 | } |
| 80 | |
| 81 | fun addTitle(title: CharSequence, bigTitle: Boolean, subTitle: CharSequence?): HeaderCell { |
| 82 | |
| 83 | val headerCell = if (bigTitle) { |
| 84 | HeaderCell(ctx, Theme.key_dialogTextBlue2, 23, 15, false, true) |
| 85 | } else { |
| 86 | HeaderCell(ctx, Theme.key_dialogTextGray2, 16, 12, false) |
| 87 | } |
| 88 | |
| 89 | headerCell.setText(if (title is String) AndroidUtilities.replaceTags(title) else title) |
| 90 | |
| 91 | subTitle?.also { |
| 92 | |
| 93 | headerCell.setText2(it) |
| 94 | |
| 95 | } |
| 96 | |
| 97 | rootView.addView(headerCell, LayoutHelper.createLinear(-1, -2).apply { |
| 98 | |
| 99 | bottomMargin = AndroidUtilities.dp(8F) |
| 100 | |
| 101 | }) |
| 102 | |
| 103 | return headerCell |
| 104 | |
| 105 | } |
| 106 | |
| 107 | @JvmOverloads |
| 108 | fun addCheckItem(text: String, value: Boolean, switch: Boolean = false, valueText: String? = null, listener: ((cell: TextCheckCell, isChecked: Boolean |
| 109 | |
| 110 | val checkBoxCell = TextCheckCell(ctx, 21, !switch) |
| 111 | checkBoxCell.setBackgroundDrawable(Theme.getSelectorDrawable(false)) |
| 112 | checkBoxCell.minimumHeight = AndroidUtilities.dp(50F) |
| 113 | rootView.addView(checkBoxCell, LayoutHelper.createLinear(-1, -2)) |
| 114 | |
| 115 | if (valueText == null) { |
| 116 | checkBoxCell.setTextAndCheck(text, value, true) |
| 117 | } else { |
| 118 | checkBoxCell.setTextAndValueAndCheck(text, valueText, value, true, true) |
| 119 | } |
| 120 | |
| 121 | checkBoxCell.setOnClickListener { |
| 122 | val target = !checkBoxCell.isChecked |
| 123 | checkBoxCell.isChecked = target |
| 124 | listener?.invoke(checkBoxCell, target) |
| 125 | } |
| 126 | |
| 127 | if (checkBoxCell.checkBox != null) { |
| 128 | checkBoxCell.checkBox.setOnClickListener { checkBoxCell.performClick() } |
| 129 | } else { |
| 130 | checkBoxCell.checkBoxSquare.setOnClickListener { checkBoxCell.performClick() } |
| 131 | } |
| 132 | |
| 133 | return checkBoxCell |
| 134 | |
| 135 | } |
| 136 | |
| 137 | @JvmOverloads |
| 138 | fun addCheckItems(text: Array<String>, value: (Int) -> Boolean, switch: Boolean = false, valueText: ((Int) -> String)? = null, listener: (index |
| 139 | |
| 140 | val list = mutableListOf<TextCheckCell>() |
| 141 | |
| 142 | text.forEachIndexed { index, textI -> |
| 143 | list.add(addCheckItem(textI, value(index), switch, valueText?.invoke(index)) { cell, isChecked -> |
| 144 | listener(index, textI, cell, isChecked) |
| 145 | }) |
| 146 | } |
| 147 | |
| 148 | return list |
| 149 | |
| 150 | } |
| 151 | |
| 152 | private val radioButtonGroup by lazy { LinkedList<RadioButtonCell>() } |
| 153 | |
| 154 | fun doRadioCheck(cell: RadioButtonCell) { |
| 155 | |
| 156 | if (!cell.isChecked) { |
| 157 | |
| 158 | radioButtonGroup.forEach { |
| 159 | |
| 160 | if (it.isChecked) { |
| 161 | |
| 162 | it.setChecked(false, true) |
| 163 | |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | cell.setChecked(true, true) |
| 169 | |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | @JvmOverloads |
| 175 | fun addRadioItem(text: String, value: Boolean, valueText: String? = null, listener: (cell: RadioButtonCell) -> Unit): RadioButtonCell { |
| 176 | |
| 177 | val checkBoxCell = RadioButtonCell(ctx, true) |
| 178 | checkBoxCell.setBackgroundDrawable(Theme.getSelectorDrawable(false)) |
| 179 | checkBoxCell.minimumHeight = AndroidUtilities.dp(50F) |
| 180 | rootView.addView(checkBoxCell, LayoutHelper.createLinear(-1, -2)) |
| 181 | |
| 182 | if (valueText == null) { |
| 183 | |
| 184 | checkBoxCell.setTextAndValue(text, true, value) |
| 185 | |
| 186 | } else { |
| 187 | |
| 188 | checkBoxCell.setTextAndValueAndCheck(text, valueText, true, value) |
| 189 | |
| 190 | } |
| 191 | |
| 192 | radioButtonGroup.add(checkBoxCell) |
| 193 | |
| 194 | checkBoxCell.setOnClickListener { |
| 195 | |
| 196 | listener(checkBoxCell) |
| 197 | |
| 198 | } |
| 199 | |
| 200 | return checkBoxCell |
| 201 | |
| 202 | } |
| 203 | |
| 204 | @JvmOverloads |
| 205 | fun addRadioItems(text: Array<String>, value: (Int, String) -> Boolean, valueText: ((Int, String) -> String)? = null, listener: (index: Int |
| 206 | |
| 207 | val list = mutableListOf<RadioButtonCell>() |
| 208 | |
| 209 | text.forEachIndexed { index, textI -> |
| 210 | |
| 211 | list.add(addRadioItem(textI, value(index, textI), valueText?.invoke(index, textI)) { cell -> |
| 212 | |
| 213 | listener(index, textI, cell) |
| 214 | |
| 215 | }) |
| 216 | |
| 217 | } |
| 218 | |
| 219 | return list |
| 220 | |
| 221 | } |
| 222 | |
| 223 | fun addCancelItem() { |
| 224 | |
| 225 | addItem(LocaleController.getString("Cancel", R.string.Cancel), R.drawable.baseline_cancel_24) {} |
| 226 | |
| 227 | } |
| 228 | |
| 229 | @JvmOverloads |
| 230 | fun addCancelButton(left: Boolean = true) { |
| 231 | |
| 232 | addButton(LocaleController.getString("Cancel", R.string.Cancel), left = left) {} |
| 233 | |
| 234 | } |
| 235 | |
| 236 | |
| 237 | @JvmOverloads |
| 238 | fun addOkButton(listener: ((TextView) -> Unit), noAutoDismiss: Boolean = false) { |
| 239 | |
| 240 | addButton(LocaleController.getString("OK", R.string.OK), noAutoDismiss) { listener(it); } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | @JvmOverloads |
| 245 | fun addButton(text: String, noAutoDismiss: Boolean = false, left: Boolean = false, listener: ((TextView) -> Unit)): TextView { |
| 246 | |
| 247 | return TextView(ctx).apply { |
| 248 | |
| 249 | setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f) |
| 250 | setTextColor(Theme.getColor(Theme.key_dialogTextBlue4)) |
| 251 | gravity = Gravity.CENTER |
| 252 | isSingleLine = true |
| 253 | ellipsize = TextUtils.TruncateAt.END |
| 254 | setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(Theme.key_dialogButtonSelector), 0)) |
| 255 | setPadding(AndroidUtilities.dp(18f), 0, AndroidUtilities.dp(18f), 0) |
| 256 | setText(text) |
| 257 | typeface = AndroidUtilities.getTypeface("fonts/rmedium.ttf") |
| 258 | (if (left) buttonsView else rightButtonsView).addView(this, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, rtl)) |
| 259 | setOnClickListener { if (!noAutoDismiss) dismiss();listener(this) } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | } |
| 264 | |
| 265 | @JvmOverloads |
| 266 | fun addItem(text: String, icon: Int = 0, red: Boolean = false, listener: ((cell: TextCell) -> Unit)?): TextCell { |
| 267 | |
| 268 | return TextCell(ctx).apply { |
| 269 | |
| 270 | background = Theme.getSelectorDrawable(false) |
| 271 | setTextAndIcon(text, icon, false) |
| 272 | |
| 273 | setOnClickListener { |
| 274 | dismiss() |
| 275 | listener?.invoke(this) |
| 276 | } |
| 277 | |
| 278 | if (red) { |
| 279 | setColors("key_dialogTextRed2", "key_dialogTextRed2") |
| 280 | } |
| 281 | |
| 282 | this@BottomBuilder.rootView.addView(this, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 48, rtl)) |
| 283 | |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | |
| 288 | fun addItems(text: Array<String?>, icon: IntArray?, listener: (index: Int, text: String, cell: TextCell) -> Unit): List<TextCell> { |
| 289 | |
| 290 | val list = mutableListOf<TextCell>() |
| 291 | |
| 292 | text.forEachIndexed { index, textI -> |
| 293 | |
| 294 | list.add(addItem(textI ?: return@forEachIndexed, icon?.get(index) ?: 0) { cell -> |
| 295 | |
| 296 | listener(index, textI, cell) |
| 297 | |
| 298 | }) |
| 299 | |
| 300 | } |
| 301 | |
| 302 | return list |
| 303 | |
| 304 | } |
| 305 | |
| 306 | @JvmOverloads |
| 307 | fun addEditText(hintText: String? = null): EditText { |
| 308 | |
| 309 | return EditText(ctx).apply { |
| 310 | |
| 311 | setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f) |
| 312 | setTextColor(Theme.getColor(Theme.key_dialogTextBlack)) |
| 313 | setHintTextColor(Theme.getColor(Theme.key_dialogTextBlue4)) |
| 314 | hintText?.also { hint = it } |
| 315 | isSingleLine = true |
| 316 | isFocusable = true |
| 317 | setBackgroundDrawable(null) |
| 318 | |
| 319 | this@BottomBuilder.rootView.addView(this, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, -2, rtl, AndroidUtilities.dp(6F), 0, 0, 0)) |
| 320 | |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | fun create() = builder.create() |
| 326 | fun show() = builder.show() |
| 327 | fun dismiss() { |
| 328 | builder.dismissRunnable.run() |
| 329 | } |
| 330 | |
| 331 | } |