| 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.* |
| 14 | import org.telegram.ui.Components.LayoutHelper |
| 15 | import java.util.* |
| 16 | |
| 17 | class BottomBuilder(val ctx: Context) { |
| 18 | |
| 19 | val builder = BottomSheet.Builder(ctx, true) |
| 20 | |
| 21 | private val rootView = LinearLayout(ctx).apply { |
| 22 | |
| 23 | orientation = LinearLayout.VERTICAL |
| 24 | |
| 25 | } |
| 26 | |
| 27 | private val _root = LinearLayout(ctx).apply { |
| 28 | |
| 29 | addView(ScrollView(ctx).apply { |
| 30 | |
| 31 | addView(this@BottomBuilder.rootView) |
| 32 | isFillViewport = true |
| 33 | isVerticalScrollBarEnabled = false |
| 34 | |
| 35 | }, LinearLayout.LayoutParams(-1, -1)) |
| 36 | |
| 37 | builder.setCustomView(this) |
| 38 | |
| 39 | } |
| 40 | |
| 41 | |
| 42 | private val buttonsView by lazy { |
| 43 | |
| 44 | FrameLayout(ctx).apply { |
| 45 | |
| 46 | setBackgroundColor(Theme.getColor(Theme.key_dialogBackground)) |
| 47 | |
| 48 | this@BottomBuilder.rootView.addView(this, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 50, Gravity.LEFT or Gravity.BOTTOM)) |
| 49 | |
| 50 | addView(rightButtonsView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP or Gravity.RIGHT)); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | private val rightButtonsView by lazy { |
| 57 | |
| 58 | LinearLayout(ctx).apply { |
| 59 | |
| 60 | orientation = LinearLayout.HORIZONTAL |
| 61 | |
| 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 = HeaderCell(ctx) |
| 84 | |
| 85 | headerCell.setText(if (title is String) AndroidUtilities.replaceTags(title) else title) |
| 86 | |
| 87 | subTitle?.also { |
| 88 | |
| 89 | headerCell.setText2(it) |
| 90 | |
| 91 | } |
| 92 | |
| 93 | rootView.addView(headerCell, LayoutHelper.createLinear(-1, -2).apply { |
| 94 | |
| 95 | bottomMargin = AndroidUtilities.dp(12F) |
| 96 | |
| 97 | }) |
| 98 | |
| 99 | rootView.addView(ShadowSectionCell(ctx, 3)) |
| 100 | |
| 101 | return headerCell |
| 102 | |
| 103 | } |
| 104 | |
| 105 | @JvmOverloads |
| 106 | fun addCheckItem(text: String, value: Boolean, switch: Boolean = false, valueText: String? = null, listener: (cell: TextCheckCell) -> Unit): TextCheckCell |
| 107 | |
| 108 | val checkBoxCell = TextCheckCell(ctx, 21, !switch) |
| 109 | checkBoxCell.setBackgroundDrawable(Theme.getSelectorDrawable(false)) |
| 110 | checkBoxCell.minimumHeight = AndroidUtilities.dp(50F) |
| 111 | rootView.addView(checkBoxCell, LayoutHelper.createLinear(-1, -2)) |
| 112 | |
| 113 | if (valueText == null) { |
| 114 | |
| 115 | checkBoxCell.setTextAndCheck(text, value, true) |
| 116 | |
| 117 | } else { |
| 118 | |
| 119 | checkBoxCell.setTextAndValueAndCheck(text, valueText, value, true, true) |
| 120 | |
| 121 | } |
| 122 | |
| 123 | checkBoxCell.setOnClickListener { |
| 124 | |
| 125 | listener.invoke(checkBoxCell) |
| 126 | |
| 127 | } |
| 128 | |
| 129 | return checkBoxCell |
| 130 | |
| 131 | } |
| 132 | |
| 133 | @JvmOverloads |
| 134 | fun addCheckItems(text: Array<String>, value: (Int) -> Boolean, switch: Boolean = false, valueText: ((Int) -> String)? = null, listener: (index |
| 135 | |
| 136 | val list = mutableListOf<TextCheckCell>() |
| 137 | |
| 138 | text.forEachIndexed { index, textI -> |
| 139 | |
| 140 | list.add(addCheckItem(textI, value(index), switch, valueText?.invoke(index)) { cell -> |
| 141 | |
| 142 | listener(index, textI, cell) |
| 143 | |
| 144 | }) |
| 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 | @JvmOverloads |
| 224 | fun addCancelItem() { |
| 225 | |
| 226 | addItem(LocaleController.getString("Cancel", R.string.Cancel), R.drawable.baseline_cancel_24) { dismiss() } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | @JvmOverloads |
| 231 | fun addCancelButton(left: Boolean = true) { |
| 232 | |
| 233 | addButton(LocaleController.getString("Cancel", R.string.Cancel), left = left) { dismiss() } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | |
| 238 | @JvmOverloads |
| 239 | fun addOkButton(listener: ((TextView) -> Unit)) { |
| 240 | |
| 241 | addButton(LocaleController.getString("OK", R.string.OK)) { listener(it); } |
| 242 | |
| 243 | } |
| 244 | |
| 245 | @JvmOverloads |
| 246 | fun addButton(text: String, red: Boolean = false, left: Boolean = false, listener: ((TextView) -> Unit)): TextView { |
| 247 | |
| 248 | return TextView(ctx).apply { |
| 249 | |
| 250 | setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f) |
| 251 | setTextColor(Theme.getColor(Theme.key_dialogTextBlue4)) |
| 252 | gravity = Gravity.CENTER |
| 253 | isSingleLine = true |
| 254 | ellipsize = TextUtils.TruncateAt.END |
| 255 | setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(Theme.key_dialogButtonSelector), 0)) |
| 256 | setPadding(AndroidUtilities.dp(18f), 0, AndroidUtilities.dp(18f), 0) |
| 257 | setText(text) |
| 258 | typeface = AndroidUtilities.getTypeface("fonts/rmedium.ttf") |
| 259 | (if (left) buttonsView else rightButtonsView).addView(this, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP or Gravity.LEFT)) |
| 260 | setOnClickListener { listener(this) } |
| 261 | |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
| 266 | fun addItem(): TextCell { |
| 267 | |
| 268 | return TextCell(ctx).apply { |
| 269 | |
| 270 | this@BottomBuilder.rootView.addView(this, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT or Gravity.TOP)) |
| 271 | |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | |
| 276 | @JvmOverloads |
| 277 | fun addItem(text: String, icon: Int = 0, red: Boolean = false, listener: ((cell: TextCell) -> Unit)?): TextCell { |
| 278 | |
| 279 | return TextCell(ctx).apply { |
| 280 | |
| 281 | textView.setGravity(Gravity.LEFT) |
| 282 | |
| 283 | background = Theme.getSelectorDrawable(false) |
| 284 | |
| 285 | setTextAndIcon(text, icon, true) |
| 286 | |
| 287 | setOnClickListener { |
| 288 | |
| 289 | if (listener == null) dismiss() else listener(this) |
| 290 | |
| 291 | } |
| 292 | |
| 293 | if (red) { |
| 294 | |
| 295 | setColors("key_dialogTextRed2", "key_dialogTextRed2") |
| 296 | |
| 297 | } |
| 298 | |
| 299 | this@BottomBuilder.rootView.addView(this, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT or Gravity.TOP)) |
| 300 | |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | @JvmOverloads |
| 306 | fun addItems(text: Array<String>, icon: (Int) -> Int = { 0 }, listener: (index: Int, text: String, cell: TextCell) -> Unit |
| 307 | |
| 308 | val list = mutableListOf<TextCell>() |
| 309 | |
| 310 | text.forEachIndexed { index, textI -> |
| 311 | |
| 312 | list.add(addItem(textI, icon(index)) { cell -> |
| 313 | |
| 314 | listener(index, textI, cell) |
| 315 | |
| 316 | }) |
| 317 | |
| 318 | } |
| 319 | |
| 320 | return list |
| 321 | |
| 322 | } |
| 323 | |
| 324 | @JvmOverloads |
| 325 | fun addEditText(hintText: String? = null): EditText { |
| 326 | |
| 327 | return EditText(ctx).apply { |
| 328 | |
| 329 | setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f) |
| 330 | setTextColor(Theme.getColor(Theme.key_dialogTextBlack)) |
| 331 | setHintTextColor(Theme.getColor(Theme.key_dialogTextBlue4)) |
| 332 | hintText?.also { hint = it } |
| 333 | isSingleLine = true |
| 334 | isFocusable = true |
| 335 | setBackgroundDrawable(null) |
| 336 | |
| 337 | 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)) |
| 338 | |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
| 343 | fun create() = builder.create() |
| 344 | fun show() = builder.show() |
| 345 | fun dismiss() { |
| 346 | builder.dismissRunnable.run() |
| 347 | } |
| 348 | |
| 349 | } |