Added delay to markdown processing for performance improvements

This commit is contained in:
William Brawner 2017-07-29 12:28:57 -05:00
parent e16a1a12c7
commit 61c6f8233f

View file

@ -8,6 +8,7 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -37,6 +38,7 @@ public class PreviewFragment extends Fragment {
private static final String TAG = PreviewFragment.class.getSimpleName(); private static final String TAG = PreviewFragment.class.getSimpleName();
private static final int INTERNET_REQUEST = 0; private static final int INTERNET_REQUEST = 0;
private WebView mMarkdownView; private WebView mMarkdownView;
private boolean timeoutActive = false;
@BindView(R.id.markdown_view) @BindView(R.id.markdown_view)
WebView markdownView; WebView markdownView;
@ -87,20 +89,31 @@ public class PreviewFragment extends Fragment {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("markdownData")) { if (intent.hasExtra("markdownData")) {
String data = intent.getStringExtra("markdownData"); String data = intent.getStringExtra("markdownData");
Log.d(TAG, "Markdown Data: " + data);
markdown(data); markdown(data);
} }
} }
} }
private void markdown(String text) { private void markdown(String text) {
final String iText = text;
final Handler handler = new Handler();
Thread setMarkdown = new Thread() {
@Override
public void run() {
MutableDataHolder options = new MutableDataSet(); MutableDataHolder options = new MutableDataSet();
options.setFrom(ParserEmulationProfile.MARKDOWN); options.setFrom(ParserEmulationProfile.MARKDOWN);
options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create())); options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));
Parser parser = Parser.builder(options).build(); Parser parser = Parser.builder(options).build();
Node document = parser.parse(text); Node document = parser.parse(iText);
HtmlRenderer renderer = HtmlRenderer.builder(options).build(); HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String html = renderer.render(document); String html = renderer.render(document);
mMarkdownView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null); mMarkdownView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
timeoutActive = false;
}
};
if (!timeoutActive) {
timeoutActive = true;
handler.postDelayed(setMarkdown, 50);
}
} }
} }