Browse Source

Fixes for fatal crashes

pull/63/head
Ezerous 5 years ago
parent
commit
2e0235de61
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  2. 7
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  3. 35
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/Posting.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  5. 16
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/PrepareForReplyTask.java
  6. 12
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

5
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -22,6 +22,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
@ -146,9 +147,9 @@ public class RecentFragment extends BaseFragment {
topicSummaries.addAll(fetchedRecent); topicSummaries.addAll(fetchedRecent);
recentAdapter.notifyDataSetChanged(); recentAdapter.notifyDataSetChanged();
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) {
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show();
} else { } else {
Toast.makeText(getContext(), "Unexpected error," + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Unexpected error," +
" please contact the developers with the details", Toast.LENGTH_LONG).show(); " please contact the developers with the details", Toast.LENGTH_LONG).show();
} }

7
app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java

@ -143,11 +143,12 @@ public class UnreadFragment extends BaseFragment {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (unreadTask.isRunning()) if (unreadTask!=null && unreadTask.isRunning())
unreadTask.cancel(true); unreadTask.cancel(true);
if (markReadTask.isRunning()) if (markReadTask!=null && markReadTask.isRunning())
markReadTask.cancel(true); markReadTask.cancel(true);
topicSummaries.clear(); if(topicSummaries!=null)
topicSummaries.clear();
} }
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { public interface UnreadFragmentInteractionListener extends FragmentInteractionListener {

35
app/src/main/java/gr/thmmy/mthmmy/activities/topic/Posting.java

@ -2,6 +2,7 @@ package gr.thmmy.mthmmy.activities.topic;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException; import java.io.IOException;
@ -60,22 +61,24 @@ public class Posting {
String finalUrl = response.request().url().toString(); String finalUrl = response.request().url().toString();
if (finalUrl.contains("action=post")) { if (finalUrl.contains("action=post")) {
Document postErrorPage = Jsoup.parse(response.body().string()); Document postErrorPage = Jsoup.parse(response.body().string());
String[] errors = postErrorPage.select("tr[id=errors] div[id=error_list]").first() Element errorsElement = postErrorPage.select("tr[id=errors] div[id=error_list]").first();
.toString().split("<br>"); if(errorsElement!=null){
for (int i = 0; i < errors.length; ++i) { //TODO test String[] errors = errorsElement.toString().split("<br>");
Timber.d(String.valueOf(i)); for (int i = 0; i < errors.length; ++i) { //TODO test
Timber.d(errors[i]); Timber.d(String.valueOf(i));
} Timber.d(errors[i]);
for (String error : errors) { }
if (error.contains("Your session timed out while posting") || for (String error : errors) {
error.contains("Υπερβήκατε τον μέγιστο χρόνο σύνδεσης κατά την αποστολή")) if (error.contains("Your session timed out while posting") ||
return REPLY_STATUS.SESSION_ENDED; error.contains("Υπερβήκατε τον μέγιστο χρόνο σύνδεσης κατά την αποστολή"))
if (error.contains("No subject was filled in") return REPLY_STATUS.SESSION_ENDED;
|| error.contains("Δεν δόθηκε τίτλος")) if (error.contains("No subject was filled in")
return REPLY_STATUS.NO_SUBJECT; || error.contains("Δεν δόθηκε τίτλος"))
if (error.contains("The message body was left empty") return REPLY_STATUS.NO_SUBJECT;
|| error.contains("Δεν δόθηκε κείμενο για το μήνυμα")) if (error.contains("The message body was left empty")
return REPLY_STATUS.EMPTY_BODY; || error.contains("Δεν δόθηκε κείμενο για το μήνυμα"))
return REPLY_STATUS.EMPTY_BODY;
}
} }
return REPLY_STATUS.NEW_REPLY_WHILE_POSTING; return REPLY_STATUS.NEW_REPLY_WHILE_POSTING;
} }

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

@ -42,7 +42,7 @@ import java.util.ArrayList;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.topic.tasks.EditTask; import gr.thmmy.mthmmy.activities.topic.tasks.EditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyTask;
import gr.thmmy.mthmmy.activities.topic.tasks.ReplyTask; import gr.thmmy.mthmmy.activities.topic.tasks.ReplyTask;
import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask; import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask;
import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseActivity;
@ -618,7 +618,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
} }
} }
}); });
viewModel.setPrepareForReplyCallbacks(new PrepareForReply.PrepareForReplyCallbacks() { viewModel.setPrepareForReplyCallbacks(new PrepareForReplyTask.PrepareForReplyCallbacks() {
@Override @Override
public void onPrepareForReplyStarted() { public void onPrepareForReplyStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE);

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

@ -15,13 +15,13 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
public class PrepareForReply extends AsyncTask<Integer, Void, PrepareForReplyResult> { public class PrepareForReplyTask extends AsyncTask<Integer, Void, PrepareForReplyResult> {
private PrepareForReplyCallbacks listener; private PrepareForReplyCallbacks listener;
private OnPrepareForReplyFinished finishListener; private OnPrepareForReplyFinished finishListener;
private String replyPageUrl; private String replyPageUrl;
public PrepareForReply(PrepareForReplyCallbacks listener, OnPrepareForReplyFinished finishListener, public PrepareForReplyTask(PrepareForReplyCallbacks listener, OnPrepareForReplyFinished finishListener,
String replyPageUrl) { String replyPageUrl) {
this.listener = listener; this.listener = listener;
this.finishListener = finishListener; this.finishListener = finishListener;
this.replyPageUrl = replyPageUrl; this.replyPageUrl = replyPageUrl;
@ -49,12 +49,16 @@ public class PrepareForReply extends AsyncTask<Integer, Void, PrepareForReplyRes
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 (IOException | Selector.SelectorParseException e) { } catch (NullPointerException e) {
Timber.e(e, "Prepare failed."); // TODO: Convert this task to (New)ParseTask (?) / handle parsing errors in a better way
Timber.e(e, "Prepare failed (1)");
return new PrepareForReplyResult(false, null, null, null, null, null);
} catch (IOException | Selector.SelectorParseException e){
Timber.e(e, "Prepare failed (2)");
return new PrepareForReplyResult(false, null, null, null, null, null); return new PrepareForReplyResult(false, null, null, null, null, null);
} }
StringBuilder buildedQuotes = new StringBuilder(""); StringBuilder buildedQuotes = 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=" +

12
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -17,7 +17,7 @@ import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.EditTask; import gr.thmmy.mthmmy.activities.topic.tasks.EditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyResult; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyResult;
import gr.thmmy.mthmmy.activities.topic.tasks.RemoveVoteTask; import gr.thmmy.mthmmy.activities.topic.tasks.RemoveVoteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.ReplyTask; import gr.thmmy.mthmmy.activities.topic.tasks.ReplyTask;
@ -35,7 +35,7 @@ import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber; import timber.log.Timber;
public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTaskCompleted, public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTaskCompleted,
PrepareForReply.OnPrepareForReplyFinished, PrepareForEditTask.OnPrepareEditFinished { PrepareForReplyTask.OnPrepareForReplyFinished, PrepareForEditTask.OnPrepareEditFinished {
/** /**
* topic state * topic state
*/ */
@ -56,7 +56,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
private TopicTask currentTopicTask; private TopicTask currentTopicTask;
private PrepareForEditTask currentPrepareForEditTask; private PrepareForEditTask currentPrepareForEditTask;
private PrepareForReply currentPrepareForReplyTask; private PrepareForReplyTask currentPrepareForReplyTask;
//callbacks for topic activity //callbacks for topic activity
private TopicTask.TopicTaskObserver topicTaskObserver; private TopicTask.TopicTaskObserver topicTaskObserver;
@ -65,7 +65,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
private ReplyTask.ReplyTaskCallbacks replyFinishListener; private ReplyTask.ReplyTaskCallbacks replyFinishListener;
private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks; private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks;
private EditTask.EditTaskCallbacks editTaskCallbacks; private EditTask.EditTaskCallbacks editTaskCallbacks;
private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; private PrepareForReplyTask.PrepareForReplyCallbacks prepareForReplyCallbacks;
private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener; private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener;
private NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener; private NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener;
private ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener; private ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener;
@ -177,7 +177,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
stopLoading(); stopLoading();
setPageIndicatorIndex(pageCount, true); setPageIndicatorIndex(pageCount, true);
Timber.i("Preparing for reply"); Timber.i("Preparing for reply");
currentPrepareForReplyTask = new PrepareForReply(prepareForReplyCallbacks, this, currentPrepareForReplyTask = new PrepareForReplyTask(prepareForReplyCallbacks, this,
replyPageUrl.getValue()); replyPageUrl.getValue());
currentPrepareForReplyTask.execute(toQuoteList.toArray(new Integer[0])); currentPrepareForReplyTask.execute(toQuoteList.toArray(new Integer[0]));
} }
@ -424,7 +424,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
this.editTaskCallbacks = editTaskCallbacks; this.editTaskCallbacks = editTaskCallbacks;
} }
public void setPrepareForReplyCallbacks(PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks) { public void setPrepareForReplyCallbacks(PrepareForReplyTask.PrepareForReplyCallbacks prepareForReplyCallbacks) {
this.prepareForReplyCallbacks = prepareForReplyCallbacks; this.prepareForReplyCallbacks = prepareForReplyCallbacks;
} }

Loading…
Cancel
Save