Added delay to markdown processing for performance improvements
This commit is contained in:
parent
e16a1a12c7
commit
61c6f8233f
1 changed files with 22 additions and 9 deletions
|
@ -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) {
|
||||||
MutableDataHolder options = new MutableDataSet();
|
final String iText = text;
|
||||||
options.setFrom(ParserEmulationProfile.MARKDOWN);
|
final Handler handler = new Handler();
|
||||||
options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));
|
Thread setMarkdown = new Thread() {
|
||||||
Parser parser = Parser.builder(options).build();
|
@Override
|
||||||
Node document = parser.parse(text);
|
public void run() {
|
||||||
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
|
MutableDataHolder options = new MutableDataSet();
|
||||||
String html = renderer.render(document);
|
options.setFrom(ParserEmulationProfile.MARKDOWN);
|
||||||
mMarkdownView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
|
options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));
|
||||||
|
Parser parser = Parser.builder(options).build();
|
||||||
|
Node document = parser.parse(iText);
|
||||||
|
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
|
||||||
|
String html = renderer.render(document);
|
||||||
|
mMarkdownView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
|
||||||
|
timeoutActive = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (!timeoutActive) {
|
||||||
|
timeoutActive = true;
|
||||||
|
handler.postDelayed(setMarkdown, 50);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue