Precompute text

pull/174/head
M66B 5 years ago
parent fea6abfc54
commit f5fd230877

@ -103,6 +103,8 @@ import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.graphics.ColorUtils; import androidx.core.graphics.ColorUtils;
import androidx.core.text.PrecomputedTextCompat;
import androidx.core.widget.TextViewCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
@ -1810,9 +1812,18 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
s, e, Spanned.SPAN_INCLUSIVE_INCLUSIVE); s, e, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
} }
try {
return PrecomputedTextCompat.getTextFuture(
builder,
TextViewCompat.getTextMetricsParams(tvBody),
executor)
.get();
} catch (Throwable ex) {
Log.w(ex);
return builder; return builder;
} }
} }
}
@Override @Override
protected void onExecuted(Bundle args, Object result) { protected void onExecuted(Bundle args, Object result) {
@ -1828,7 +1839,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean has_images = args.getBoolean("has_images"); boolean has_images = args.getBoolean("has_images");
if (result instanceof Spanned) { if (result instanceof PrecomputedTextCompat) {
TextViewCompat.setPrecomputedText(tvBody, (PrecomputedTextCompat) result);
tvBody.setTextIsSelectable(false);
tvBody.setTextIsSelectable(true);
tvBody.setMovementMethod(new TouchHandler(message));
} else if (result instanceof Spanned) {
tvBody.setText((Spanned) result); tvBody.setText((Spanned) result);
tvBody.setTextIsSelectable(false); tvBody.setTextIsSelectable(false);
tvBody.setTextIsSelectable(true); tvBody.setTextIsSelectable(true);

@ -90,6 +90,8 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.text.PrecomputedTextCompat;
import androidx.core.widget.TextViewCompat;
import androidx.cursoradapter.widget.SimpleCursorAdapter; import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -4005,7 +4007,7 @@ public class FragmentCompose extends FragmentBase {
args.putLong("id", draft.id); args.putLong("id", draft.id);
args.putBoolean("show_images", show_images); args.putBoolean("show_images", show_images);
new SimpleTask<Spanned[]>() { new SimpleTask<Object[]>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
// Needed to get width for images // Needed to get width for images
@ -4029,7 +4031,7 @@ public class FragmentCompose extends FragmentBase {
} }
@Override @Override
protected Spanned[] onExecute(final Context context, Bundle args) throws Throwable { protected Object[] onExecute(final Context context, Bundle args) throws Throwable {
final long id = args.getLong("id"); final long id = args.getLong("id");
final boolean show_images = args.getBoolean("show_images", false); final boolean show_images = args.getBoolean("show_images", false);
@ -4094,12 +4096,24 @@ public class FragmentCompose extends FragmentBase {
args.putBoolean("ref_has_images", spannedRef != null && args.putBoolean("ref_has_images", spannedRef != null &&
spannedRef.getSpans(0, spannedRef.length(), ImageSpan.class).length > 0); spannedRef.getSpans(0, spannedRef.length(), ImageSpan.class).length > 0);
return new Spanned[]{spannedBody, spannedRef}; PrecomputedTextCompat precomputed = null;
if (spannedRef != null)
try {
precomputed = PrecomputedTextCompat.getTextFuture(
spannedRef,
TextViewCompat.getTextMetricsParams(tvReference),
null)
.get();
} catch (Throwable ex) {
Log.w(ex);
}
return new Spanned[]{spannedBody, precomputed == null ? spannedRef : precomputed};
} }
@Override @Override
protected void onExecuted(Bundle args, Spanned[] text) { protected void onExecuted(Bundle args, Object[] text) {
etBody.setText(text[0]); etBody.setText((Spanned) text[0]);
etBody.setSelection(0); etBody.setSelection(0);
grpBody.setVisibility(View.VISIBLE); grpBody.setVisibility(View.VISIBLE);
@ -4111,7 +4125,10 @@ public class FragmentCompose extends FragmentBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean ref_hint = prefs.getBoolean("compose_reference", true); boolean ref_hint = prefs.getBoolean("compose_reference", true);
tvReference.setText(text[1]); if (text[1] instanceof PrecomputedTextCompat)
TextViewCompat.setPrecomputedText(tvReference, (PrecomputedTextCompat) text[1]);
else
tvReference.setText((Spanned) text[1]);
tvReference.setVisibility(text[1] == null ? View.GONE : View.VISIBLE); tvReference.setVisibility(text[1] == null ? View.GONE : View.VISIBLE);
grpReferenceHint.setVisibility(text[1] == null || !ref_hint ? View.GONE : View.VISIBLE); grpReferenceHint.setVisibility(text[1] == null || !ref_hint ? View.GONE : View.VISIBLE);
ibReferenceEdit.setVisibility(text[1] == null ? View.GONE : View.VISIBLE); ibReferenceEdit.setVisibility(text[1] == null ? View.GONE : View.VISIBLE);

Loading…
Cancel
Save