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