From 65a72827bc2187202a371a4ef9ce665e26910c6f Mon Sep 17 00:00:00 2001 From: Millesimus Date: Fri, 20 Aug 2021 21:51:51 +0200 Subject: [PATCH] New helper to help with quotes. --- .../conversations/ui/util/QuoteHelper.java | 51 +++++++++++++++++++ .../siacs/conversations/utils/UIHelper.java | 28 ++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java diff --git a/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java b/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java new file mode 100644 index 000000000..cfd2a2e63 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java @@ -0,0 +1,51 @@ +package eu.siacs.conversations.ui.util; + +import eu.siacs.conversations.utils.UIHelper; + +public class QuoteHelper { + + public static boolean isPositionQuoteCharacter(CharSequence body, int pos){ + return body.charAt(pos) == '>'; + } + + public static boolean isPositionFollowedByQuoteChar(CharSequence body, int pos) { + return body.length() > pos + 1 && isPositionQuoteCharacter(body, pos +1 ); + } + + // 'Prequote' means anything we require or can accept in front of a QuoteChar + public static boolean isPositionPrecededByPrequote(CharSequence body, int pos){ + return UIHelper.isPositionPrecededByLineStart(body, pos); + } + + public static boolean isPositionQuoteStart (CharSequence body, int pos){ + return isPositionQuoteCharacter(body, pos) + && isPositionPrecededByPrequote(body, pos) + && (UIHelper.isPositionFollowedByWhitespace(body, pos) + || isPositionFollowedByQuoteChar(body, pos)); + } + + public static boolean bodyContainsQuoteStart (CharSequence body){ + for (int i = 0; i < body.length(); i++){ + if (isPositionQuoteStart(body, i)){ + return true; + } + } + return false; + } + /*public static int getQuoteColors(XmppActivity activity, boolean darkBackground, int quoteDepth){ + int[] colorsLight = R.style.ConversationsTheme_Dark; + int[] colorsDark = Config.QUOTE_COLOR_ARRAY_DARK; + + Collections.rotate(Collections.singletonList(colorsLight), quoteDepth); + Collections.rotate(Collections.singletonList(colorsDark), quoteDepth); + + Arrays.stream(colorsLight).toArray(); + + int quoteColors = darkBackground ? ContextCompat.getColor(activity, colorsLight[quoteDepth-1]) + : ContextCompat.getColor(activity, colorsDark[quoteDepth-1]); + + Collections.rotate + + return quoteColors; + };*/ +} diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index 6dcf517a0..26e6af2fa 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -372,6 +372,34 @@ public class UIHelper { return input.length() > 256 ? StylingHelper.subSequence(input, 0, 256) : input; } + public static boolean isPositionFollowedByWhitespace(CharSequence body, int pos){ + return Character.isWhitespace(body.charAt(pos + 1)); + } + + public static boolean isPositionPrecededByWhitespace(CharSequence body, int pos){ + return Character.isWhitespace(body.charAt(pos -1 )); + } + + public static boolean isPositionPrecededByBodyStart(CharSequence body, int pos){ + // true if not a single linebreak before current position + for (int i = pos - 1; i >= 0; i--){ + if (body.charAt(i) != ' '){ + return false; + } + } + return true; + } + + public static boolean isPositionPrecededByLineStart(CharSequence body, int pos){ + if (isPositionPrecededByBodyStart(body, pos)){ + return true; + } + if (body.charAt(pos - 1) == '\n'){ + return true; + } + return false; + } + public static boolean isPositionFollowedByQuoteableCharacter(CharSequence body, int pos) { return !isPositionFollowedByNumber(body, pos) && !isPositionFollowedByEmoticon(body, pos)