Browse Source

fix: improved handling, messages of some errors

develop
Ezerous 2 years ago
parent
commit
ea400f4e65
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  4. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForEditTask.java
  5. 10
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyResult.java
  6. 16
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java
  7. 6
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java
  8. 4
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  9. 2
      app/src/test/java/gr/thmmy/mthmmy/utils/UploadsCoursesJSONReadingTest.java

2
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); parsedDownloads.add(download);
} }
} catch (Exception e) { } catch (Exception e) {
throw new ParseException("Parsing failed (DownloadsActivity)"); throw new ParseException("Parsing failed (DownloadsActivity) for url " + downloadsUrl);
} }
} }

2
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java

@ -47,7 +47,7 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) {
String timestamp = topicSummary.getLastPostTimestamp(); String timestamp = topicSummary.getLastPostTimestamp();
try { try {
holder.mDateTimeView.setReferenceTime(Long.valueOf(timestamp)); holder.mDateTimeView.setReferenceTime(Long.parseLong(timestamp));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Timber.e(e, "Invalid number format: %s", timestamp); Timber.e(e, "Invalid number format: %s", timestamp);
holder.mDateTimeView.setText(topicSummary.getLastPostSimplifiedDateTime()); holder.mDateTimeView.setText(topicSummary.getLastPostSimplifiedDateTime());

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -709,8 +709,8 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
SharedPreferences drafts = context.getSharedPreferences(context.getString(R.string.pref_topic_drafts_key), SharedPreferences drafts = context.getSharedPreferences(context.getString(R.string.pref_topic_drafts_key),
Context.MODE_PRIVATE); Context.MODE_PRIVATE);
replyText += drafts.getString(String.valueOf(viewModel.getTopicId()), ""); replyText += drafts.getString(String.valueOf(viewModel.getTopicId()), "");
if (viewModel.getBuildedQuotes() != null && !viewModel.getBuildedQuotes().isEmpty()) if (viewModel.getBuiltQuotes() != null && !viewModel.getBuiltQuotes().isEmpty())
replyText += viewModel.getBuildedQuotes(); replyText += viewModel.getBuiltQuotes();
} }
holder.replyEditor.setText(replyText); holder.replyEditor.setText(replyText);
holder.replyEditor.getEditText().setSelection(holder.replyEditor.getText().length()); holder.replyEditor.getEditText().setSelection(holder.replyEditor.getText().length());

8
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForEditTask.java

@ -61,8 +61,12 @@ public class PrepareForEditTask extends AsyncTask<String, Void, PrepareForEditRe
icon = form.select("select[name=icon]>option[selected]").first().attr("value"); icon = form.select("select[name=icon]>option[selected]").first().attr("value");
return new PrepareForEditResult(postText, commitEditURL, numReplies, seqnum, sc, topic, icon, position, true); return new PrepareForEditResult(postText, commitEditURL, numReplies, seqnum, sc, topic, icon, position, true);
} catch (IOException | Selector.SelectorParseException e) { } catch (NullPointerException | Selector.SelectorParseException e) {
Timber.e(e, "Prepare failed."); // 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); return new PrepareForEditResult(null, null, null, null, null, null, null, position, false);
} }
} }

10
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyResult.java

@ -1,17 +1,17 @@
package gr.thmmy.mthmmy.activities.topic.tasks; package gr.thmmy.mthmmy.activities.topic.tasks;
public class PrepareForReplyResult { public class PrepareForReplyResult {
private final String numReplies, seqnum, sc, topic, buildedQuotes; private final String numReplies, seqnum, sc, topic, builtQuotes;
private boolean successful; 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.successful = successful;
this.numReplies = numReplies; this.numReplies = numReplies;
this.seqnum = seqnum; this.seqnum = seqnum;
this.sc = sc; this.sc = sc;
this.topic = topic; this.topic = topic;
this.buildedQuotes = buildedQuotes; this.builtQuotes = builtQuotes;
} }
public String getNumReplies() { public String getNumReplies() {
@ -30,8 +30,8 @@ public class PrepareForReplyResult {
return topic; return topic;
} }
public String getBuildedQuotes() { public String getBuiltQuotes() {
return buildedQuotes; return builtQuotes;
} }
public boolean isSuccessful() { public boolean isSuccessful() {

16
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java

@ -49,16 +49,16 @@ public class PrepareForReplyTask extends AsyncTask<Integer, Void, PrepareForRepl
seqnum = document.select("input[name=seqnum]").first().attr("value"); seqnum = document.select("input[name=seqnum]").first().attr("value");
sc = document.select("input[name=sc]").first().attr("value"); sc = document.select("input[name=sc]").first().attr("value");
topic = document.select("input[name=topic]").first().attr("value"); topic = document.select("input[name=topic]").first().attr("value");
} catch (NullPointerException e) { } catch (NullPointerException | Selector.SelectorParseException e) {
// TODO: Convert this task to (New)ParseTask (?) / handle parsing errors in a better way // TODO: Convert this task to (New)ParseTask (?) / handle parsing errors in a better way
Timber.e(e, "Prepare failed (1)"); Timber.e(e, "Preparing for reply failed (parsing error).");
return new PrepareForReplyResult(false, null, null, null, null, null); return new PrepareForReplyResult(false, null, null, null, null, null);
} catch (IOException | Selector.SelectorParseException e) { } catch (Exception e) {
Timber.e(e, "Prepare failed (2)"); Timber.i("Preparing for reply failed (other error).");
return new PrepareForReplyResult(false, null, null, null, null, null); return new PrepareForReplyResult(false, null, null, null, null, null);
} }
StringBuilder buildedQuotes = new StringBuilder(); StringBuilder builtQuotes = new StringBuilder();
for (Integer postIndex : postIndices) { for (Integer postIndex : postIndices) {
request = new Request.Builder() request = new Request.Builder()
.url("https://www.thmmy.gr/smf/index.php?action=quotefast;quote=" + .url("https://www.thmmy.gr/smf/index.php?action=quotefast;quote=" +
@ -68,14 +68,14 @@ public class PrepareForReplyTask extends AsyncTask<Integer, Void, PrepareForRepl
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
String body = response.body().string(); String body = response.body().string();
body = Parser.unescapeEntities(body, false); body = Parser.unescapeEntities(body, false);
buildedQuotes.append(body.substring(body.indexOf("<quote>") + 7, body.indexOf("</quote>"))); builtQuotes.append(body.substring(body.indexOf("<quote>") + 7, body.indexOf("</quote>")));
buildedQuotes.append("\n\n"); builtQuotes.append("\n\n");
} catch (IOException | Selector.SelectorParseException e) { } catch (IOException | Selector.SelectorParseException e) {
Timber.e(e, "Quote building failed."); Timber.e(e, "Quote building failed.");
return new PrepareForReplyResult(false, null, null, null, null, null); 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 @Override

6
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 static final Pattern pattern = Pattern.compile("\\s((1[3-9]|2[0-3]):)");
private ThmmyDateTimeParser() { private ThmmyDateTimeParser() {}
}
public static String convertToTimestamp(String thmmyDateTime) { public static String convertToTimestamp(String thmmyDateTime) {
Timber.v("Will attempt to convert %s to timestamp.", thmmyDateTime); Timber.v("Will attempt to convert %s to timestamp.", thmmyDateTime);
@ -78,7 +77,8 @@ public class ThmmyDateTimeParser {
localDateTime = formatter.withLocale(greekLocale).parseLocalDateTime(thmmyDateTime); localDateTime = formatter.withLocale(greekLocale).parseLocalDateTime(thmmyDateTime);
} catch (IllegalArgumentException e2) { } catch (IllegalArgumentException e2) {
Timber.v("Parsing DateTime %s using Greek Locale failed too.", thmmyDateTime); 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; return null;
} }
} }

4
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(); return prepareForEditResult.getValue().getPostText();
} }
public String getBuildedQuotes() { public String getBuiltQuotes() {
if (prepareForReplyResult.getValue() == null) if (prepareForReplyResult.getValue() == null)
throw new NullPointerException("Reply preparation was not found"); throw new NullPointerException("Reply preparation was not found");
return prepareForReplyResult.getValue().getBuildedQuotes(); return prepareForReplyResult.getValue().getBuiltQuotes();
} }
public int postCount() { public int postCount() {

2
app/src/test/java/gr/thmmy/mthmmy/utils/UploadsCoursesJSONReadingTest.java

@ -33,7 +33,7 @@ public class UploadsCoursesJSONReadingTest {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath); InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath);
assertNotNull(is); assertNotNull(is);
String uploadsCoursesJSON = ResourceUtils.readJSONResourceToString(is); String uploadsCoursesJSON = ResourceUtils.readJSONResourceToString(is);
assertNotNull(uploadsCoursesJSON);; assertNotNull(uploadsCoursesJSON);
JSONObject jsonObject = new JSONObject(uploadsCoursesJSON); JSONObject jsonObject = new JSONObject(uploadsCoursesJSON);
assertTrue(jsonObject.has("categories")); assertTrue(jsonObject.has("categories"));
HashMap<Integer, UploadsCourse> coursesHashMap = generateCoursesFromJSON(jsonObject); HashMap<Integer, UploadsCourse> coursesHashMap = generateCoursesFromJSON(jsonObject);

Loading…
Cancel
Save