From a0b47f3ca39f37bded5bcb704431021638196932 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 29 Jun 2020 05:25:54 -0700 Subject: [PATCH] Precompute TextView in I/O thread for performance --- .../magisk/databinding/AdaptersGeneric.kt | 14 ++++++++++++++ app/src/main/res/layout/include_log_magisk.xml | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/databinding/AdaptersGeneric.kt b/app/src/main/java/com/topjohnwu/magisk/databinding/AdaptersGeneric.kt index 0038cb7af..f845545b2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/databinding/AdaptersGeneric.kt +++ b/app/src/main/java/com/topjohnwu/magisk/databinding/AdaptersGeneric.kt @@ -1,9 +1,14 @@ package com.topjohnwu.magisk.databinding import android.view.View +import android.widget.TextView +import androidx.core.text.PrecomputedTextCompat import androidx.core.view.isGone import androidx.core.view.isInvisible +import androidx.core.widget.TextViewCompat import androidx.databinding.BindingAdapter +import com.topjohnwu.magisk.extensions.subscribeK +import io.reactivex.Single @BindingAdapter("gone") fun setGone(view: View, gone: Boolean) { @@ -24,3 +29,12 @@ fun setGoneUnless(view: View, goneUnless: Boolean) { fun setInvisibleUnless(view: View, invisibleUnless: Boolean) { setInvisible(view, invisibleUnless.not()) } + +@BindingAdapter("precomputedText") +fun setPrecomputedText(tv: TextView, text: CharSequence) { + Single.fromCallable { + PrecomputedTextCompat.create(text, TextViewCompat.getTextMetricsParams(tv)) + }.subscribeK { + TextViewCompat.setPrecomputedText(tv, it); + } +} diff --git a/app/src/main/res/layout/include_log_magisk.xml b/app/src/main/res/layout/include_log_magisk.xml index e481f314a..2fa83e3f6 100644 --- a/app/src/main/res/layout/include_log_magisk.xml +++ b/app/src/main/res/layout/include_log_magisk.xml @@ -27,7 +27,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="monospace" - android:text="@{viewModel.consoleText}" + precomputedText="@{viewModel.consoleText}" android:textAppearance="@style/AppearanceFoundation.Caption" android:textSize="10sp" android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size}"