]> git.ktnx.net Git - mobile-ledger.git/commitdiff
HelpDialog: support for markdown-style links in the message
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 6 Mar 2021 16:08:31 +0000 (18:08 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 6 Mar 2021 16:08:31 +0000 (18:08 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/HelpDialog.java

index 984eeeba02f8562c260857221e7daf1cd3adb2c1..2a2bf036c6b94a86883fd2d17f9f3924402cbb85 100644 (file)
@@ -19,21 +19,61 @@ package net.ktnx.mobileledger.ui;
 
 import android.app.AlertDialog;
 import android.content.Context;
+import android.text.SpannableStringBuilder;
+import android.text.Spanned;
 import android.text.TextUtils;
+import android.text.method.LinkMovementMethod;
+import android.text.style.URLSpan;
+import android.widget.TextView;
 
 import androidx.annotation.ArrayRes;
 import androidx.annotation.StringRes;
 
 import net.ktnx.mobileledger.R;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 public class HelpDialog {
+    private final static Pattern MARKDOWN_LINK_PATTERN =
+            Pattern.compile("\\[([^\\[]+)]\\(([^)]*)\\)");
     public static void show(Context context, @StringRes int title, @ArrayRes int content) {
         AlertDialog.Builder adb = new AlertDialog.Builder(context);
         adb.setTitle(title);
-        adb.setMessage(TextUtils.join("\n\n", context.getResources()
-                                                     .getStringArray(content)));
+        String message = TextUtils.join("\n\n", context.getResources()
+                                                       .getStringArray(content));
+
+        SpannableStringBuilder richTextMessage = new SpannableStringBuilder();
+        while (true) {
+            Matcher m = MARKDOWN_LINK_PATTERN.matcher(message);
+            if (m.find()) {
+                richTextMessage.append(message.substring(0, m.start()));
+                String linkText = m.group(1);
+                assert linkText != null;
+                String linkURL = m.group(2);
+                assert linkURL != null;
+
+                if (linkText.isEmpty())
+                    linkText = linkURL;
+
+                int spanStart = richTextMessage.length();
+                richTextMessage.append(linkText);
+                richTextMessage.setSpan(new URLSpan(linkURL), spanStart,
+                        spanStart + linkText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                URLSpan linkSpan = new URLSpan(linkText);
+
+                message = message.substring(m.end());
+            }
+            else {
+                richTextMessage.append(message);
+                break;
+            }
+        }
+        adb.setMessage(richTextMessage);
         adb.setPositiveButton(R.string.close_button, (dialog, buttonId) -> dialog.dismiss());
-        adb.create()
-           .show();
+        final AlertDialog dialog = adb.create();
+        dialog.show();
+        ((TextView) dialog.findViewById(android.R.id.message)).setMovementMethod(
+                LinkMovementMethod.getInstance());
     }
 }