encapsulate NekoX's BottomSheet into BottomSheet.Builder interface

This commit is contained in:
luvletter2333 2023-03-04 06:53:39 +08:00
parent 7fe115e83c
commit d0bf3ef05b
No known key found for this signature in database
GPG Key ID: 9EB7723F3A0ACF92
6 changed files with 146 additions and 16 deletions

View File

@ -60,6 +60,7 @@ import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.camera.CameraView;
import org.telegram.ui.Cells.HeaderCell;
import org.telegram.ui.Components.AnimationProperties;
import org.telegram.ui.Components.Bulletin;
import org.telegram.ui.Components.CubicBezierInterpolator;
@ -67,6 +68,9 @@ import org.telegram.ui.Components.LayoutHelper;
import java.util.ArrayList;
import kotlin.Unit;
import tw.nekomimi.nekogram.ui.BottomBuilder;
public class BottomSheet extends Dialog {
private final static boolean AVOID_SYSTEM_CUTOUT_FULLSCREEN = false;
@ -1748,6 +1752,131 @@ public class BottomSheet extends Dialog {
}
}
public static class NekoXBuilder {
// do the same thing as BottomBuilder
private final BottomBuilder nekoxBuilder;
private HeaderCell title;
private final BottomSheet bottomSheet;
// in Telegram interface, BottomSheet only have one title
public NekoXBuilder(Context context) {
this(context, false);
}
public NekoXBuilder(Context context, int bgColor) {
this(context, false, bgColor);
}
public NekoXBuilder(Context context, boolean needFocus) {
this(context, needFocus, Theme.getColor(Theme.key_dialogBackground));
}
public NekoXBuilder(Context context, boolean needFocus, int bgColor) {
this.nekoxBuilder = new BottomBuilder(context, needFocus, bgColor);
this.bottomSheet = nekoxBuilder.getBuilder().bottomSheet;
}
public NekoXBuilder setItems(CharSequence[] items, final OnClickListener onClickListener) {
nekoxBuilder.addItems(items, null, (index, text, cell) -> {
onClickListener.onClick(null, index);
return Unit.INSTANCE;
});
return this;
}
public NekoXBuilder setItems(CharSequence[] items, int[] icons, final OnClickListener onClickListener) {
nekoxBuilder.addItems(items, icons, (index, text, cell) -> {
onClickListener.onClick(null, index);
return Unit.INSTANCE;
});
return this;
}
public NekoXBuilder setTitle(CharSequence title) {
return setTitle(title, false);
}
public NekoXBuilder setTitle(CharSequence title, boolean big) {
this.title = nekoxBuilder.addTitle(title, big);
return this;
}
public NekoXBuilder setTitleMultipleLines(boolean allowMultipleLines) {
if (this.title != null) {
var textView = this.title.getTextView();
if (allowMultipleLines) {
textView.setSingleLine(false);
textView.setMaxLines(5);
textView.setEllipsize(TextUtils.TruncateAt.END);
} else {
textView.setLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
}
}
return this;
}
public BottomSheet create() {
return nekoxBuilder.create();
}
public BottomSheet setDimBehind(boolean value) {
bottomSheet.dimBehind = value;
return bottomSheet;
}
public BottomSheet show() {
bottomSheet.show();
return bottomSheet;
}
public NekoXBuilder setTag(int tag) {
bottomSheet.tag = tag;
return this;
}
public NekoXBuilder setUseHardwareLayer(boolean value) {
bottomSheet.useHardwareLayer = value;
return this;
}
public NekoXBuilder setDelegate(BottomSheetDelegate delegate) {
bottomSheet.setDelegate(delegate);
return this;
}
public NekoXBuilder setApplyTopPadding(boolean value) {
bottomSheet.applyTopPadding = value;
return this;
}
public NekoXBuilder setApplyBottomPadding(boolean value) {
bottomSheet.applyBottomPadding = value;
return this;
}
public Runnable getDismissRunnable() {
return bottomSheet.dismissRunnable;
}
public BottomSheet setUseFullWidth(boolean value) {
bottomSheet.fullWidth = value;
return bottomSheet;
}
public BottomSheet setUseFullscreen(boolean value) {
bottomSheet.isFullscreen = value;
return bottomSheet;
}
public NekoXBuilder setOnPreDismissListener(OnDismissListener onDismissListener) {
nekoxBuilder.getBuilder().bottomSheet.setOnHideListener(onDismissListener);
return this;
}
}
public int getLeftInset() {
if (lastInsets != null && Build.VERSION.SDK_INT >= 21) {
float inset;

View File

@ -239,7 +239,7 @@ public class TextCell extends FrameLayout {
}
}
public void setText(String text, boolean divider) {
public void setText(CharSequence text, boolean divider) {
imageLeft = 21;
textView.setText(text);
valueTextView.setText(null, false);
@ -250,7 +250,7 @@ public class TextCell extends FrameLayout {
setWillNotDraw(!needDivider);
}
public void setTextAndIcon(String text, int resId, boolean divider) {
public void setTextAndIcon(CharSequence text, int resId, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);
@ -268,7 +268,7 @@ public class TextCell extends FrameLayout {
setWillNotDraw(!needDivider);
}
public void setTextAndIcon(String text, Drawable drawable, boolean divider) {
public void setTextAndIcon(CharSequence text, Drawable drawable, boolean divider) {
offsetFromImage = 68;
imageLeft = 18;
textView.setText(text);
@ -295,11 +295,11 @@ public class TextCell extends FrameLayout {
this.imageLeft = imageLeft;
}
public void setTextAndValue(String text, String value, boolean divider) {
public void setTextAndValue(CharSequence text, String value, boolean divider) {
setTextAndValue(text, value, false, divider);
}
public void setTextAndValue(String text, String value, boolean animated, boolean divider) {
public void setTextAndValue(CharSequence text, String value, boolean animated, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);
@ -314,11 +314,11 @@ public class TextCell extends FrameLayout {
}
}
public void setTextAndValueAndIcon(String text, String value, int resId, boolean divider) {
public void setTextAndValueAndIcon(CharSequence text, String value, int resId, boolean divider) {
setTextAndValueAndIcon(text, value, false, resId, divider);
}
public void setTextAndValueAndIcon(String text, String value, boolean animated, int resId, boolean divider) {
public void setTextAndValueAndIcon(CharSequence text, String value, boolean animated, int resId, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);
@ -348,7 +348,7 @@ public class TextCell extends FrameLayout {
imageView.setBackground(Theme.createRoundRectDrawable(AndroidUtilities.dp(8), color));
}
public void setTextAndCheck(String text, boolean checked, boolean divider) {
public void setTextAndCheck(CharSequence text, boolean checked, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);
@ -363,7 +363,7 @@ public class TextCell extends FrameLayout {
setWillNotDraw(!needDivider);
}
public void setTextAndCheckAndIcon(String text, boolean checked, int resId, boolean divider) {
public void setTextAndCheckAndIcon(CharSequence text, boolean checked, int resId, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);
@ -380,7 +380,7 @@ public class TextCell extends FrameLayout {
setWillNotDraw(!needDivider);
}
public void setTextAndCheckAndIcon(String text, boolean checked, Drawable resDrawable, boolean divider) {
public void setTextAndCheckAndIcon(CharSequence text, boolean checked, Drawable resDrawable, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);
@ -397,7 +397,7 @@ public class TextCell extends FrameLayout {
setWillNotDraw(!needDivider);
}
public void setTextAndValueDrawable(String text, Drawable drawable, boolean divider) {
public void setTextAndValueDrawable(CharSequence text, Drawable drawable, boolean divider) {
imageLeft = 21;
offsetFromImage = 71;
textView.setText(text);

View File

@ -3853,7 +3853,7 @@ public class AlertsCreator {
return null;
}
BottomSheet.Builder builder = new BottomSheet.Builder(fragment.getParentActivity(), false, resourcesProvider);
BottomSheet.NekoXBuilder builder = new BottomSheet.NekoXBuilder(fragment.getParentActivity(), false);
builder.setTitle(LocaleController.getString("Notifications", R.string.Notifications), true);
CharSequence[] items = new CharSequence[]{
LocaleController.formatString("MuteFor", R.string.MuteFor, LocaleController.formatPluralString("Hours", 1)),

View File

@ -6549,7 +6549,8 @@ public class DialogsActivity extends BaseFragment implements NotificationCenter.
private void onArchiveLongPress(View view) {
if (!NekoConfig.disableVibration.Bool()) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
} BottomSheet.Builder builder = new BottomSheet.Builder(getParentActivity());
}
BottomSheet.NekoXBuilder builder = new BottomSheet.NekoXBuilder(getParentActivity());
final boolean hasUnread = getMessagesStorage().getArchiveUnreadCount() != 0;
int[] icons = new int[]{

View File

@ -2167,7 +2167,7 @@ public class GroupCallActivity extends BottomSheet implements NotificationCenter
iconsArray[i] = icons.get(i);
}
BottomSheet.Builder builder = new BottomSheet.Builder(context)
BottomSheet.NekoXBuilder builder = new BottomSheet.NekoXBuilder(context)
.setTitle(LocaleController.getString("VoipSelectAudioOutput", R.string.VoipSelectAudioOutput), true)
.setItems(itemsArray, iconsArray, (dialog, which) -> {
if (VoIPService.getSharedInstance() == null) {

View File

@ -215,7 +215,7 @@ class BottomBuilder(val ctx: Context, val needFocus: Boolean = true, val bgColor
}
@JvmOverloads
fun addItem(text: String, icon: Int = 0, red: Boolean = false, listener: ((cell: TextCell) -> Unit)?): TextCell {
fun addItem(text: CharSequence, icon: Int = 0, red: Boolean = false, listener: ((cell: TextCell) -> Unit)?): TextCell {
return TextCell(ctx).apply {
background = Theme.getSelectorDrawable(false)
setTextAndIcon(text, icon, false)
@ -230,7 +230,7 @@ class BottomBuilder(val ctx: Context, val needFocus: Boolean = true, val bgColor
}
}
fun addItems(text: Array<String?>, icon: IntArray?, listener: (index: Int, text: String, cell: TextCell) -> Unit): List<TextCell> {
fun addItems(text: Array<CharSequence?>, icon: IntArray?, listener: (index: Int, text: CharSequence, cell: TextCell) -> Unit): List<TextCell> {
val list = mutableListOf<TextCell>()
text.forEachIndexed { index, textI ->
list.add(addItem(textI ?: return@forEachIndexed, icon?.get(index) ?: 0) { cell ->