]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/HelpDialog.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / HelpDialog.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.app.AlertDialog;
21 import android.content.Context;
22 import android.text.SpannableStringBuilder;
23 import android.text.Spanned;
24 import android.text.TextUtils;
25 import android.text.method.LinkMovementMethod;
26 import android.text.style.URLSpan;
27 import android.widget.TextView;
28
29 import androidx.annotation.ArrayRes;
30 import androidx.annotation.StringRes;
31
32 import net.ktnx.mobileledger.R;
33
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37 public class HelpDialog {
38     private final static Pattern MARKDOWN_LINK_PATTERN =
39             Pattern.compile("\\[([^\\[]+)]\\(([^)]*)\\)");
40     public static void show(Context context, @StringRes int title, @ArrayRes int content) {
41         AlertDialog.Builder adb = new AlertDialog.Builder(context);
42         adb.setTitle(title);
43         String message = TextUtils.join("\n\n", context.getResources()
44                                                        .getStringArray(content));
45
46         SpannableStringBuilder richTextMessage = new SpannableStringBuilder();
47         while (true) {
48             Matcher m = MARKDOWN_LINK_PATTERN.matcher(message);
49             if (m.find()) {
50                 richTextMessage.append(message.substring(0, m.start()));
51                 String linkText = m.group(1);
52                 assert linkText != null;
53                 String linkURL = m.group(2);
54                 assert linkURL != null;
55
56                 if (linkText.isEmpty())
57                     linkText = linkURL;
58
59                 int spanStart = richTextMessage.length();
60                 richTextMessage.append(linkText);
61                 richTextMessage.setSpan(new URLSpan(linkURL), spanStart,
62                         spanStart + linkText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
63                 URLSpan linkSpan = new URLSpan(linkText);
64
65                 message = message.substring(m.end());
66             }
67             else {
68                 richTextMessage.append(message);
69                 break;
70             }
71         }
72         adb.setMessage(richTextMessage);
73         adb.setPositiveButton(R.string.close_button, (dialog, buttonId) -> dialog.dismiss());
74         final AlertDialog dialog = adb.create();
75         dialog.show();
76         ((TextView) dialog.findViewById(android.R.id.message)).setMovementMethod(
77                 LinkMovementMethod.getInstance());
78     }
79 }