diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-12-19 18:23:32 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-12-21 13:49:02 +0300 |
commit | cf2b67de73f0f7a5f28f93da6427897c969ce9a0 (patch) | |
tree | 7021309aef77549a62446648451417c662ae3f14 | |
parent | e068f6406526f309b1cec71e2b981b80bfd64ac7 (diff) |
Enhance search highlighting
Signed-off-by: Stefan Niedermann <info@niedermann.it>
7 files changed, 93 insertions, 17 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java index 9db572867..2dca3ec2e 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java @@ -162,6 +162,25 @@ public class CardDetailsFragment extends BrandedFragment implements OnDateSetLis } binding.descriptionToggle.setVisibility(TextUtils.isEmpty(newText) ? GONE : VISIBLE); }); + + // FIXME remove thread + StringBuilder searchText = new StringBuilder("a"); + new Thread(() -> { + while (true) { + try { + if(searchText.length() > 5) { + searchText.replace(0, 1, ""); + } else { + searchText.append("a"); + } + binding.descriptionViewer.setSearchText(searchText.toString()); + binding.descriptionEditor.setSearchText(searchText.toString()); + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); } else { binding.descriptionEditor.setEnabled(false); binding.descriptionEditor.setVisibility(VISIBLE); diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditor.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditor.java index e9d97500e..dd9b957d4 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditor.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditor.java @@ -34,6 +34,10 @@ public interface MarkdownEditor { void setEnabled(boolean enabled); default void setSearchText(@Nullable CharSequence searchText) { + setSearchText(searchText, null); + } + + default void setSearchText(@Nullable CharSequence searchText, @Nullable Integer current) { // Optional } }
\ No newline at end of file diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownEditor.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownEditor.java index a27dd524f..f33382f2e 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownEditor.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownEditor.java @@ -64,13 +64,14 @@ public class MarkwonMarkdownEditor extends AppCompatEditText implements Markdown } @Override - public void setSearchText(@Nullable CharSequence searchText) { + public void setSearchText(@Nullable CharSequence searchText, @Nullable Integer current) { final SearchHighlightTextWatcher searchHighlightTextWatcher = combinedWatcher.get(SearchHighlightTextWatcher.class); if (searchHighlightTextWatcher == null) { Log.w(TAG, SearchHighlightTextWatcher.class.getSimpleName() + " is not a registered " + TextWatcher.class.getSimpleName()); } else { searchHighlightTextWatcher.setSearchText(searchText); - post(() -> setMarkdownString(unrenderedText$.getValue())); + searchHighlightTextWatcher.setCurrent(current); + post(() -> searchHighlightTextWatcher.afterTextChanged(getText())); } } diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java index 14539fb7a..86629bb0e 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java @@ -30,6 +30,7 @@ import io.noties.prism4j.Prism4j; import io.noties.prism4j.annotations.PrismBundle; import it.niedermann.android.markdown.markwon.model.EListType; import it.niedermann.android.markdown.markwon.plugins.NextcloudMentionsPlugin; +import it.niedermann.android.markdown.markwon.plugins.SearchHighlightPlugin; import it.niedermann.android.markdown.markwon.plugins.ThemePlugin; import it.niedermann.android.markdown.markwon.span.SearchSpan; @@ -57,7 +58,8 @@ public class MarkwonMarkdownUtil { .usePlugin(StrikethroughPlugin.create()) .usePlugin(SimpleExtPlugin.create()) .usePlugin(ImagesPlugin.create()) - .usePlugin(MarkwonInlineParserPlugin.create()); + .usePlugin(MarkwonInlineParserPlugin.create()) + .usePlugin(SearchHighlightPlugin.create()); } public static Markwon.Builder initMarkwonViewer(@NonNull Context context) { diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownViewer.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownViewer.java index 70d11194c..f5c927135 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownViewer.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownViewer.java @@ -1,12 +1,10 @@ package it.niedermann.android.markdown.markwon; import android.content.Context; -import android.graphics.Color; -import android.text.Editable; -import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextUtils; import android.util.AttributeSet; +import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -19,11 +17,12 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonPlugin; import it.niedermann.android.markdown.MarkdownEditor; +import it.niedermann.android.markdown.markwon.plugins.SearchHighlightPlugin; import static androidx.lifecycle.Transformations.distinctUntilChanged; import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.initMarkwonViewer; -import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.searchAndColor; public class MarkwonMarkdownViewer extends AppCompatTextView implements MarkdownEditor { @@ -31,6 +30,8 @@ public class MarkwonMarkdownViewer extends AppCompatTextView implements Markdown private Markwon markwon; private final MutableLiveData<CharSequence> unrenderedText$ = new MutableLiveData<>(); + private Spanned renderedText; + private final ExecutorService renderService; public MarkwonMarkdownViewer(@NonNull Context context) { @@ -57,21 +58,25 @@ public class MarkwonMarkdownViewer extends AppCompatTextView implements Markdown if (!text.equals(previousText)) { setText(text); this.renderService.execute(() -> { - final Spanned markdown = this.markwon.toMarkdown(text.toString()); - post(() -> this.markwon.setParsedMarkdown(this, markdown)); + this.renderedText = this.markwon.toMarkdown(text.toString()); + post(() -> this.markwon.setParsedMarkdown(this, renderedText)); }); } - } } @Override - public void setSearchText(@Nullable CharSequence searchText) { - this.renderService.execute(() -> { - final Editable content = new SpannableStringBuilder(getText()); - searchAndColor(content, searchText, getContext(), 0, Color.BLUE, Color.YELLOW); - post(() -> setText(content, BufferType.SPANNABLE)); - }); + public void setSearchText(@Nullable CharSequence searchText, @Nullable Integer current) { + final SearchHighlightPlugin searchHighlightPlugin = this.markwon.getPlugin(SearchHighlightPlugin.class); + if (searchHighlightPlugin == null) { + Log.w(TAG, SearchHighlightPlugin.class.getSimpleName() + " is not a registered " + MarkwonPlugin.class.getSimpleName()); + } else { + searchHighlightPlugin.setSearchText(searchText); + searchHighlightPlugin.setCurrent(current); + if (renderedText != null) { + post(() -> this.markwon.setParsedMarkdown(this, renderedText)); + } + } } @Override diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java new file mode 100644 index 000000000..93ca39ff2 --- /dev/null +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java @@ -0,0 +1,40 @@ +package it.niedermann.android.markdown.markwon.plugins; + +import android.graphics.Color; +import android.text.Editable; +import android.text.SpannableStringBuilder; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.MarkwonPlugin; + +import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.searchAndColor; + +public class SearchHighlightPlugin extends AbstractMarkwonPlugin { + + private CharSequence searchText; + private Integer current; + + public static MarkwonPlugin create() { + return new SearchHighlightPlugin(); + } + + public void setSearchText(@Nullable CharSequence searchText) { + this.searchText = searchText; + } + + public void setCurrent(@Nullable Integer current) { + this.current = current; + } + + @Override + public void afterSetText(@NonNull TextView textView) { + super.afterSetText(textView); + final Editable coloredContent = new SpannableStringBuilder(textView.getText()); + searchAndColor(coloredContent, searchText, textView.getContext(), current, Color.BLUE, Color.YELLOW); + textView.setText(coloredContent, TextView.BufferType.SPANNABLE); + } +} diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/SearchHighlightTextWatcher.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/SearchHighlightTextWatcher.java index 1956a7e73..f02b7c892 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/SearchHighlightTextWatcher.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/SearchHighlightTextWatcher.java @@ -14,6 +14,7 @@ public class SearchHighlightTextWatcher extends InterceptorTextWatcher { private final MarkwonMarkdownEditor editText; private CharSequence searchText; + private Integer current; public SearchHighlightTextWatcher(@NonNull TextWatcher originalWatcher, @NonNull MarkwonMarkdownEditor editText) { super(originalWatcher); @@ -24,9 +25,13 @@ public class SearchHighlightTextWatcher extends InterceptorTextWatcher { this.searchText = searchText; } + public void setCurrent(@Nullable Integer current) { + this.current = current; + } + @Override public void afterTextChanged(Editable s) { originalWatcher.afterTextChanged(s); - MarkwonMarkdownUtil.searchAndColor(s, searchText, editText.getContext(), 0, Color.MAGENTA, Color.GREEN); + MarkwonMarkdownUtil.searchAndColor(s, searchText, editText.getContext(), current, Color.MAGENTA, Color.GREEN); } } |