fix: HelpCatologueActivity and DocumentationActivity lag
This commit is contained in:
@@ -3,11 +3,15 @@ package com.stardust.scriptdroid.ui.help;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.theme.ThemeColorManagerCompat;
|
||||
import com.stardust.util.UnderuseExecutors;
|
||||
import com.stardust.widget.CommonMarkdownView;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.wang.avi.AVLoadingIndicatorView;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -27,46 +31,60 @@ public class DocumentationActivity extends BaseActivity {
|
||||
.putExtra("path", assetPath));
|
||||
}
|
||||
|
||||
public static void openDocumentation(Context context, String title, int rawResId) {
|
||||
context.startActivity(new Intent(context, DocumentationActivity.class)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
.putExtra("title", title)
|
||||
.putExtra("resId", rawResId));
|
||||
}
|
||||
|
||||
private String mDocumentation;
|
||||
private volatile String mDocumentation;
|
||||
private String mTitle;
|
||||
private AVLoadingIndicatorView mLoadingIndicatorView;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
handleIntent(getIntent());
|
||||
setUpUI();
|
||||
handleIntent(getIntent());
|
||||
}
|
||||
|
||||
private void handleIntent(Intent intent) {
|
||||
mTitle = intent.getStringExtra("title");
|
||||
String path = intent.getStringExtra("path");
|
||||
int rawId = intent.getIntExtra("resId", 0);
|
||||
if (path != null) {
|
||||
try {
|
||||
mDocumentation = PFile.read(getAssets().open("help/" + path));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
final String path = intent.getStringExtra("path");
|
||||
if (path == null)
|
||||
return;
|
||||
UnderuseExecutors.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
mDocumentation = PFile.read(getAssets().open("help/" + path));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
loadDocument();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (rawId != 0) {
|
||||
mDocumentation = PFile.read(getResources().openRawResource(rawId));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void setUpUI() {
|
||||
setContentView(R.layout.activity_document);
|
||||
setToolbarAsBack(mTitle);
|
||||
loadDocument();
|
||||
setUpLoadingView();
|
||||
}
|
||||
|
||||
private void setUpLoadingView() {
|
||||
mLoadingIndicatorView = $(R.id.loading);
|
||||
mLoadingIndicatorView.setIndicatorColor(ThemeColorManagerCompat.getColorPrimary());
|
||||
}
|
||||
|
||||
private void loadDocument() {
|
||||
mCommonMarkdownView = $(R.id.markdown);
|
||||
mCommonMarkdownView.setOnPageFinishedListener(new CommonMarkdownView.OnPageFinishedListener() {
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
mLoadingIndicatorView.hide();
|
||||
}
|
||||
});
|
||||
try {
|
||||
mCommonMarkdownView.loadMarkdown(mDocumentation);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -6,7 +6,6 @@ import android.os.Bundle;
|
||||
import android.support.annotation.Keep;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.DividerItemDecoration;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -20,6 +19,9 @@ import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.theme.ThemeColorManagerCompat;
|
||||
import com.stardust.util.UnderuseExecutors;
|
||||
import com.wang.avi.AVLoadingIndicatorView;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -48,7 +50,7 @@ public class HelpCatalogueActivity extends BaseActivity {
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
}
|
||||
|
||||
private static final Map<String, List<Item>> CATALOGUES;
|
||||
private static Map<String, List<Item>> CATALOGUES;
|
||||
|
||||
private static class Item extends JSONObject {
|
||||
|
||||
@@ -86,6 +88,7 @@ public class HelpCatalogueActivity extends BaseActivity {
|
||||
|
||||
List<Item> mItems;
|
||||
private RecyclerView mRecyclerView;
|
||||
private AVLoadingIndicatorView mLoadingIndicatorView;
|
||||
private String mTitle;
|
||||
private View.OnClickListener mOnItemClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
@@ -100,8 +103,29 @@ public class HelpCatalogueActivity extends BaseActivity {
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
handleIntent();
|
||||
setUpUI();
|
||||
if (CATALOGUES == null) {
|
||||
readCatalogues();
|
||||
} else {
|
||||
mLoadingIndicatorView.hide();
|
||||
handleIntent();
|
||||
}
|
||||
}
|
||||
|
||||
private void readCatalogues() {
|
||||
UnderuseExecutors.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
readCatalogues("help/catalogue.json");
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mLoadingIndicatorView.hide();
|
||||
handleIntent();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleIntent() {
|
||||
@@ -112,21 +136,27 @@ public class HelpCatalogueActivity extends BaseActivity {
|
||||
if (mTitle == null)
|
||||
mTitle = getString(R.string.text_help);
|
||||
mItems = CATALOGUES.get(catalogue);
|
||||
mRecyclerView.setAdapter(new Adapter());
|
||||
}
|
||||
|
||||
private void setUpUI() {
|
||||
setContentView(R.layout.activity_help);
|
||||
setToolbarAsBack(mTitle);
|
||||
setUpRecyclerView();
|
||||
setUpLoadingView();
|
||||
}
|
||||
|
||||
private void setUpRecyclerView() {
|
||||
mRecyclerView = $(R.id.catalogue);
|
||||
mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
|
||||
mRecyclerView.setAdapter(new Adapter());
|
||||
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
|
||||
}
|
||||
|
||||
private void setUpLoadingView() {
|
||||
mLoadingIndicatorView = $(R.id.loading);
|
||||
mLoadingIndicatorView.setIndicatorColor(ThemeColorManagerCompat.getColorPrimary());
|
||||
}
|
||||
|
||||
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
|
||||
|
||||
@Override
|
||||
@@ -166,10 +196,10 @@ public class HelpCatalogueActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
private static void readCatalogues(String path) {
|
||||
Map<String, List<Item>> fromJson;
|
||||
try {
|
||||
String json = PFile.read(App.getApp().getAssets().open("help/catalogue.json"));
|
||||
String json = PFile.read(App.getApp().getAssets().open(path));
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<Map<String, List<Item>>>() {
|
||||
}.getType();
|
||||
|
||||
@@ -28,6 +28,10 @@ import java.util.Collections;
|
||||
|
||||
public class CommonMarkdownView extends WebView {
|
||||
|
||||
public interface OnPageFinishedListener {
|
||||
void onPageFinished(WebView view, String url);
|
||||
}
|
||||
|
||||
private Parser mParser = Parser.builder().build();
|
||||
private HtmlRenderer mHtmlRender = HtmlRenderer.builder()
|
||||
.extensions(Collections.singleton(new HeadingAnchorExtension.Builder().build()))
|
||||
@@ -35,6 +39,7 @@ public class CommonMarkdownView extends WebView {
|
||||
|
||||
private String mMarkdownHtml;
|
||||
private String mPadding = "16px";
|
||||
private OnPageFinishedListener mOnPageFinishedListener;
|
||||
|
||||
public CommonMarkdownView(Context context) {
|
||||
super(context);
|
||||
@@ -60,6 +65,9 @@ public class CommonMarkdownView extends WebView {
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
loadUrl("javascript:document.body.style.margin=\"" + mPadding + "\"; void 0");
|
||||
if (mOnPageFinishedListener != null) {
|
||||
mOnPageFinishedListener.onPageFinished(view, url);
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
@@ -89,6 +97,10 @@ public class CommonMarkdownView extends WebView {
|
||||
loadHtml(mMarkdownHtml);
|
||||
}
|
||||
|
||||
public void setOnPageFinishedListener(OnPageFinishedListener onPageFinishedListener) {
|
||||
mOnPageFinishedListener = onPageFinishedListener;
|
||||
}
|
||||
|
||||
private void loadHtml(String html) {
|
||||
loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,26 @@
|
||||
|
||||
</android.support.design.widget.AppBarLayout>
|
||||
|
||||
<com.stardust.widget.CommonMarkdownView
|
||||
android:id="@+id/markdown"
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:textColor="@android:color/secondary_text_light"/>
|
||||
android:layout_height="match_parent">
|
||||
|
||||
|
||||
<com.stardust.widget.CommonMarkdownView
|
||||
android:id="@+id/markdown"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:textColor="@android:color/secondary_text_light"/>
|
||||
|
||||
<com.wang.avi.AVLoadingIndicatorView
|
||||
android:id="@+id/loading"
|
||||
style="@style/AVLoadingIndicatorView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
app:indicatorName="BallSpinFadeLoaderIndicator"/>
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@@ -21,8 +21,24 @@
|
||||
|
||||
</android.support.design.widget.AppBarLayout>
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/catalogue"
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/catalogue"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
<com.wang.avi.AVLoadingIndicatorView
|
||||
android:id="@+id/loading"
|
||||
style="@style/AVLoadingIndicatorView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
app:indicatorName="BallSpinFadeLoaderIndicator"/>
|
||||
</FrameLayout>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user