Skip to content

Commit

Permalink
Move Processing of Text in TextReader to Background
Browse files Browse the repository at this point in the history
  • Loading branch information
mytlogos committed Sep 13, 2019
1 parent 4ee5c0c commit b55e1b2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ boolean checkEmptyList(List<?> list, View root, View listView) {
return false;
}

void showToast(String msg) {
void showToast(CharSequence msg) {
showToast(msg, Toast.LENGTH_SHORT);
}

void showToast(String msg, int duration) {
void showToast(CharSequence msg, int duration) {
Toast.makeText(requireContext(), msg, duration).show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,27 @@ private void changeFont() {
}

@Override
void displayData(String data) {
// this does not work really, can't scroll to the bottom
// and displays characters like ' or ´ incorrectly
CharSequence text;
try {
CharSequence processData(String data) {
if (data != null && data.length() < 200) {
showToast(data);
data = null;
}
if (data == null) {
data = "No Content Found";
} else {
try {
// text = new HtmlToPlainText().getPlainText(Jsoup.parse(data).body());
String html = Jsoup.parse(data).body().html();
text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);
} catch (Exception ignored) {
text = data;
String html = Jsoup.parse(data).body().html();
return HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);
} catch (Exception ignored) {
}
}
textDisplay.setText(text);
return data;
}

@Override
void displayData(CharSequence data) {
textDisplay.setText(data);
scrollView.scrollTo(0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ void navigateEpisode(SwipyRefreshLayoutDirection direction) {
if (index >= this.readableEpisodes.size()) {
// TODO: 26.07.2019 check with if there are more episodes and save them
showToast("You are already reading the last saved episode");
this.swipeLayout.setRefreshing(false);
return;
} else if (index < 0) {
// TODO: 26.07.2019 check with if there are more episodes and save them
showToast("You are already reading the first saved episode");
this.swipeLayout.setRefreshing(false);
return;
}
this.currentlyReading = this.readableEpisodes.get(index);
Expand All @@ -162,36 +164,45 @@ static class ReadableEpisode extends SimpleEpisode {
}
}

void displayData(String data) {
webView.loadData(
data,
"text/html; charset=utf-8",
"UTF-8"
);
void displayData(CharSequence data) {
if (data instanceof String) {
webView.loadData(
(String) data,
"text/html; charset=utf-8",
"UTF-8"
);
} else {
showToast("Error while loading Episode, Contact Dev and hit him");
}
}

CharSequence processData(String data) {
if (data != null && data.length() < 200) {
showToast(data);
data = null;
}
if (data == null) {
data = "<html><head></head><body>No Content Found.</body></html>";
} else {
// TODO: 15.06.2019 escape # with %23 (chromium complains about it, but is # even there?
data = data.replaceAll("#", "%23");
}
return data;
}

@SuppressLint("StaticFieldLeak")
class OpenEpisodeTask extends AsyncTask<Void, Void, String> {
class OpenEpisodeTask extends AsyncTask<Void, Void, CharSequence> {

@Override
protected String doInBackground(Void... voids) {
protected CharSequence doInBackground(Void... voids) {
TextContentTool bookTool = FileTools.getTextContentTool();
return bookTool.openEpisode(currentBook, currentlyReading.file);
String data = bookTool.openEpisode(currentBook, currentlyReading.file);
return processData(data);
}

@SuppressLint("DefaultLocale")
@Override
protected void onPostExecute(String data) {
if (data != null && data.length() < 200) {
showToast(data);
data = null;
}
if (data == null) {
data = "<html><head></head><body>No Content Found.</body></html>";
} else {
// TODO: 15.06.2019 escape # with %23 (chromium complains about it, but is # even there?
data = data.replaceAll("#", "%23");
}
protected void onPostExecute(CharSequence data) {
if (currentlyReading != null) {
if (currentlyReading.getPartialIndex() > 0) {
setTitle(String.format("Episode %d.%d", currentlyReading.getTotalIndex(), currentlyReading.getPartialIndex()));
Expand All @@ -208,10 +219,10 @@ protected void onPostExecute(String data) {

void loadZip() {
@SuppressLint("StaticFieldLeak")
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
AsyncTask<Void, Void, CharSequence> task = new AsyncTask<Void, Void, CharSequence>() {

@Override
protected String doInBackground(Void... voids) {
protected CharSequence doInBackground(Void... voids) {
TextContentTool bookTool = FileTools.getTextContentTool();
Map<Integer, String> episodeFileMap = bookTool.getEpisodePaths(TextViewerFragment.this.currentBook);

Expand Down Expand Up @@ -241,22 +252,13 @@ protected String doInBackground(Void... voids) {
if (currentlyReading == null || currentlyReading.file == null || currentlyReading.file.isEmpty()) {
return "Selected Episode is not available";
}
return bookTool.openEpisode(currentBook, currentlyReading.file);
String data = bookTool.openEpisode(currentBook, currentlyReading.file);
return processData(data);
}

@SuppressLint("DefaultLocale")
@Override
protected void onPostExecute(String data) {
if (data != null && data.length() < 200) {
showToast(data);
data = null;
}
if (data == null) {
data = "<html><head></head><body>No Content Found.</body></html>";
} else {
// TODO: 15.06.2019 escape # with %23 (chromium complains about it, but is # even there?
data = data.replaceAll("#", "%23");
}
protected void onPostExecute(CharSequence data) {
if (currentlyReading != null) {
if (currentlyReading.getPartialIndex() > 0) {
setTitle(String.format("Episode %d.%d", currentlyReading.getTotalIndex(), currentlyReading.getPartialIndex()));
Expand Down

0 comments on commit b55e1b2

Please sign in to comment.