From ea400f4e6544c013d42470c1ba87a4c79e166bce Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 14 Sep 2022 19:22:23 +0300 Subject: [PATCH] fix: improved handling, messages of some errors --- .../activities/downloads/DownloadsActivity.java | 2 +- .../activities/main/recent/RecentAdapter.java | 2 +- .../mthmmy/activities/topic/TopicAdapter.java | 4 ++-- .../topic/tasks/PrepareForEditTask.java | 8 ++++++-- .../topic/tasks/PrepareForReplyResult.java | 10 +++++----- .../topic/tasks/PrepareForReplyTask.java | 16 ++++++++-------- .../utils/parsing/ThmmyDateTimeParser.java | 6 +++--- .../thmmy/mthmmy/viewmodel/TopicViewModel.java | 4 ++-- .../utils/UploadsCoursesJSONReadingTest.java | 2 +- 9 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java index 0e9ba74f..cb684da0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java @@ -283,7 +283,7 @@ public class DownloadsActivity extends BaseActivity implements DownloadsAdapter. parsedDownloads.add(download); } } catch (Exception e) { - throw new ParseException("Parsing failed (DownloadsActivity)"); + throw new ParseException("Parsing failed (DownloadsActivity) for url " + downloadsUrl); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java index 9d1ab90d..c5c782c8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java @@ -47,7 +47,7 @@ class RecentAdapter extends RecyclerView.Adapter { if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { String timestamp = topicSummary.getLastPostTimestamp(); try { - holder.mDateTimeView.setReferenceTime(Long.valueOf(timestamp)); + holder.mDateTimeView.setReferenceTime(Long.parseLong(timestamp)); } catch (NumberFormatException e) { Timber.e(e, "Invalid number format: %s", timestamp); holder.mDateTimeView.setText(topicSummary.getLastPostSimplifiedDateTime()); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 79528a0f..076414f8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -709,8 +709,8 @@ class TopicAdapter extends RecyclerView.Adapter { SharedPreferences drafts = context.getSharedPreferences(context.getString(R.string.pref_topic_drafts_key), Context.MODE_PRIVATE); replyText += drafts.getString(String.valueOf(viewModel.getTopicId()), ""); - if (viewModel.getBuildedQuotes() != null && !viewModel.getBuildedQuotes().isEmpty()) - replyText += viewModel.getBuildedQuotes(); + if (viewModel.getBuiltQuotes() != null && !viewModel.getBuiltQuotes().isEmpty()) + replyText += viewModel.getBuiltQuotes(); } holder.replyEditor.setText(replyText); holder.replyEditor.getEditText().setSelection(holder.replyEditor.getText().length()); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForEditTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForEditTask.java index 8d62aed1..3428bb8e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForEditTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForEditTask.java @@ -61,8 +61,12 @@ public class PrepareForEditTask extends AsyncTaskoption[selected]").first().attr("value"); return new PrepareForEditResult(postText, commitEditURL, numReplies, seqnum, sc, topic, icon, position, true); - } catch (IOException | Selector.SelectorParseException e) { - Timber.e(e, "Prepare failed."); + } catch (NullPointerException | Selector.SelectorParseException e) { + // TODO: Convert this task to (New)ParseTask (?) / handle parsing errors in a better way + Timber.e(e, "Preparing for edit failed (parsing error)."); + return new PrepareForEditResult(null, null, null, null, null, null, null, position, false); + } catch (Exception e) { + Timber.i("Preparing for edit failed (other error)."); return new PrepareForEditResult(null, null, null, null, null, null, null, position, false); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyResult.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyResult.java index 3e44f423..9b9a14d6 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyResult.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyResult.java @@ -1,17 +1,17 @@ package gr.thmmy.mthmmy.activities.topic.tasks; public class PrepareForReplyResult { - private final String numReplies, seqnum, sc, topic, buildedQuotes; + private final String numReplies, seqnum, sc, topic, builtQuotes; private boolean successful; - public PrepareForReplyResult(boolean successful, String numReplies, String seqnum, String sc, String topic, String buildedQuotes) { + public PrepareForReplyResult(boolean successful, String numReplies, String seqnum, String sc, String topic, String builtQuotes) { this.successful = successful; this.numReplies = numReplies; this.seqnum = seqnum; this.sc = sc; this.topic = topic; - this.buildedQuotes = buildedQuotes; + this.builtQuotes = builtQuotes; } public String getNumReplies() { @@ -30,8 +30,8 @@ public class PrepareForReplyResult { return topic; } - public String getBuildedQuotes() { - return buildedQuotes; + public String getBuiltQuotes() { + return builtQuotes; } public boolean isSuccessful() { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java index bd342477..b67b2157 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java @@ -49,16 +49,16 @@ public class PrepareForReplyTask extends AsyncTask") + 7, body.indexOf(""))); - buildedQuotes.append("\n\n"); + builtQuotes.append(body.substring(body.indexOf("") + 7, body.indexOf(""))); + builtQuotes.append("\n\n"); } catch (IOException | Selector.SelectorParseException e) { Timber.e(e, "Quote building failed."); return new PrepareForReplyResult(false, null, null, null, null, null); } } - return new PrepareForReplyResult(true, numReplies, seqnum, sc, topic, buildedQuotes.toString()); + return new PrepareForReplyResult(true, numReplies, seqnum, sc, topic, builtQuotes.toString()); } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java index 393f0b2b..cfc40ae2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java @@ -40,8 +40,7 @@ public class ThmmyDateTimeParser { private static final Pattern pattern = Pattern.compile("\\s((1[3-9]|2[0-3]):)"); - private ThmmyDateTimeParser() { - } + private ThmmyDateTimeParser() {} public static String convertToTimestamp(String thmmyDateTime) { Timber.v("Will attempt to convert %s to timestamp.", thmmyDateTime); @@ -78,7 +77,8 @@ public class ThmmyDateTimeParser { localDateTime = formatter.withLocale(greekLocale).parseLocalDateTime(thmmyDateTime); } catch (IllegalArgumentException e2) { Timber.v("Parsing DateTime %s using Greek Locale failed too.", thmmyDateTime); - Timber.e("Couldn't convert DateTime %s to timestamp!", originalDateTime); + Timber.e("Couldn't convert DateTime to timestamp (original: \"%s\", modified: \"%s\")!", + originalDateTime, thmmyDateTime); return null; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index d8cc3bba..fd621bb2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -484,10 +484,10 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa return prepareForEditResult.getValue().getPostText(); } - public String getBuildedQuotes() { + public String getBuiltQuotes() { if (prepareForReplyResult.getValue() == null) throw new NullPointerException("Reply preparation was not found"); - return prepareForReplyResult.getValue().getBuildedQuotes(); + return prepareForReplyResult.getValue().getBuiltQuotes(); } public int postCount() { diff --git a/app/src/test/java/gr/thmmy/mthmmy/utils/UploadsCoursesJSONReadingTest.java b/app/src/test/java/gr/thmmy/mthmmy/utils/UploadsCoursesJSONReadingTest.java index 999cfe4b..16d4ebb5 100644 --- a/app/src/test/java/gr/thmmy/mthmmy/utils/UploadsCoursesJSONReadingTest.java +++ b/app/src/test/java/gr/thmmy/mthmmy/utils/UploadsCoursesJSONReadingTest.java @@ -33,7 +33,7 @@ public class UploadsCoursesJSONReadingTest { InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath); assertNotNull(is); String uploadsCoursesJSON = ResourceUtils.readJSONResourceToString(is); - assertNotNull(uploadsCoursesJSON);; + assertNotNull(uploadsCoursesJSON); JSONObject jsonObject = new JSONObject(uploadsCoursesJSON); assertTrue(jsonObject.has("categories")); HashMap coursesHashMap = generateCoursesFromJSON(jsonObject);