From b55e1b26928716504e96ba894528ce1def344c99 Mon Sep 17 00:00:00 2001 From: Mytlogos Date: Fri, 13 Sep 2019 17:39:13 +0200 Subject: [PATCH] Move Processing of Text in TextReader to Background --- .../mytlogos/enterprise/ui/BaseFragment.java | 4 +- .../enterprise/ui/TextReaderFragmentText.java | 29 +++++--- .../enterprise/ui/TextViewerFragment.java | 70 ++++++++++--------- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/mytlogos/enterprise/ui/BaseFragment.java b/app/src/main/java/com/mytlogos/enterprise/ui/BaseFragment.java index 5b511ee..6c94906 100644 --- a/app/src/main/java/com/mytlogos/enterprise/ui/BaseFragment.java +++ b/app/src/main/java/com/mytlogos/enterprise/ui/BaseFragment.java @@ -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(); } diff --git a/app/src/main/java/com/mytlogos/enterprise/ui/TextReaderFragmentText.java b/app/src/main/java/com/mytlogos/enterprise/ui/TextReaderFragmentText.java index 0844a46..819838f 100644 --- a/app/src/main/java/com/mytlogos/enterprise/ui/TextReaderFragmentText.java +++ b/app/src/main/java/com/mytlogos/enterprise/ui/TextReaderFragmentText.java @@ -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); } } diff --git a/app/src/main/java/com/mytlogos/enterprise/ui/TextViewerFragment.java b/app/src/main/java/com/mytlogos/enterprise/ui/TextViewerFragment.java index aa02253..265dd95 100644 --- a/app/src/main/java/com/mytlogos/enterprise/ui/TextViewerFragment.java +++ b/app/src/main/java/com/mytlogos/enterprise/ui/TextViewerFragment.java @@ -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); @@ -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 = "No Content Found."; + } 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 { + class OpenEpisodeTask extends AsyncTask { @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 = "No Content Found."; - } 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())); @@ -208,10 +219,10 @@ protected void onPostExecute(String data) { void loadZip() { @SuppressLint("StaticFieldLeak") - AsyncTask task = new AsyncTask() { + AsyncTask task = new AsyncTask() { @Override - protected String doInBackground(Void... voids) { + protected CharSequence doInBackground(Void... voids) { TextContentTool bookTool = FileTools.getTextContentTool(); Map episodeFileMap = bookTool.getEpisodePaths(TextViewerFragment.this.currentBook); @@ -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 = "No Content Found."; - } 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()));