From 6bd29c9dc7719653360ed27e39ade11fea424e97 Mon Sep 17 00:00:00 2001
From: Ezerous
Date: Wed, 17 Jun 2020 21:28:36 +0300
Subject: [PATCH] TopicTask crash fix
---
.../activities/profile/ProfileActivity.java | 2 --
.../activities/topic/tasks/TopicTask.java | 17 ++++++++---------
2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
index e88a22de..016009ca 100644
--- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
+++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
@@ -1,7 +1,5 @@
package gr.thmmy.mthmmy.activities.profile;
-import android.app.Activity;
-import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
index 14899fa0..c3e78d8d 100644
--- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
+++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
@@ -11,6 +11,9 @@ import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import gr.thmmy.mthmmy.activities.topic.TopicParser;
import gr.thmmy.mthmmy.base.BaseApplication;
@@ -29,6 +32,8 @@ import timber.log.Timber;
* parameter.
*/
public class TopicTask extends AsyncTask {
+ private static final Pattern msgPattern = Pattern.compile("msg(\\d+)");
+
private TopicTaskObserver topicTaskObserver;
private OnTopicTaskCompleted finishListener;
@@ -58,15 +63,9 @@ public class TopicTask extends AsyncTask {
//Finds the index of message focus if present
int postFocus = 0;
-
- //TODO: Better parseInt handling - may rarely fail
- if (newPageUrl.contains("msg")) {
- String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
- if (tmp.contains(";"))
- postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf(';')));
- else if (tmp.contains("#"))
- postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf('#')));
- }
+ Matcher matcher = msgPattern.matcher(newPageUrl);
+ if (matcher.find())
+ postFocus = Integer.parseInt(Objects.requireNonNull(matcher.group(1)));
Request request = new Request.Builder()
.url(newPageUrl)