Check if UI bindings are valid when selecting preview text

Moved text marking logic into a separate method and extracted
preconditions out from the ifology ladder.

Fixes #9018

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
This commit is contained in:
Chris Narkiewicz 2021-10-06 22:56:26 +01:00
parent 302c927bec
commit 0af07dd015
No known key found for this signature in database
GPG key ID: 30D28CA4CCC665C6

View file

@ -20,7 +20,9 @@
package com.owncloud.android.ui.preview;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.net.Uri;
@ -28,6 +30,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
@ -143,17 +146,7 @@ public abstract class PreviewTextFragment extends FileFragment implements Search
FileDisplayActivity fileDisplayActivity = (FileDisplayActivity) getActivity();
fileDisplayActivity.setSearchQuery(query);
}
handler.postDelayed(() -> {
if (query != null && !query.isEmpty()) {
if (getContext() != null && getContext().getResources() != null) {
String coloredText = StringUtils.searchAndColor(originalText, query,
getContext().getResources().getColor(R.color.primary));
binding.textPreview.setText(Html.fromHtml(coloredText.replace("\n", "<br \\>")));
}
} else {
setText(binding.textPreview, originalText, getFile(), getActivity());
}
}, delay);
handler.postDelayed(() -> markText(query), delay);
}
if (delay == 0 && searchView != null) {
@ -161,6 +154,30 @@ public abstract class PreviewTextFragment extends FileFragment implements Search
}
}
private void markText(String query) {
// called asynchronously - must check preconditions in case of UI detachment
if (binding == null) {
return;
}
final Activity activity = getActivity();
if (activity == null) {
return;
}
final Resources resources = activity.getResources();
if (resources == null) {
return;
}
if (!TextUtils.isEmpty(query)) {
String coloredText = StringUtils.searchAndColor(originalText,
query,
resources.getColor(R.color.primary));
binding.textPreview.setText(Html.fromHtml(coloredText.replace("\n", "<br \\>")));
} else {
setText(binding.textPreview, originalText, getFile(), activity);
}
}
protected static Spanned getRenderedMarkdownText(Activity activity, String markdown) {
Prism4j prism4j = new Prism4j(new MarkwonGrammarLocator());
Prism4jTheme prism4jTheme = Prism4jThemeDefault.create();