Browse Source

lighten topic task and move javadoc to topic task result

pull/34/head
Thodoris1999 7 years ago
parent
commit
d55eb6b1ea
  1. 75
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
  2. 43
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java

75
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java

@ -28,67 +28,12 @@ import timber.log.Timber;
* parameter.</p>
*/
public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
//----------------------------input data-----------------------------
private TopicTaskObserver topicTaskObserver;
private OnTopicTaskCompleted finishListener;
//-----------------------------output data----------------------------
private ResultCode resultCode;
/**
* Holds this topic's title. At first this gets the value of the topic title that came with
* bundle and is rendered in the toolbar while parsing this topic. Later, if a different topic
* title is parsed from the html source, it gets updated.
*/
private String topicTitle;
/**
* This topic's reply url
*/
private String replyPageUrl;
//Topic's info related
private String topicTreeAndMods;
private String topicViewers;
private ArrayList<Post> newPostsList;
/**
* The topicId of the loaded page
*/
private int loadedPageTopicId = -1;
/**
* The index of the post that has focus
*/
private int focusedPostIndex = 0;
/**
* A list of the requested topic's pages urls
*/
private SparseArray<String> pagesUrls = new SparseArray<>();
//-------------------------------------(possibly) update data------------------------------
/**
* Holds current page's index (starting from 1, not 0)
*/
private int currentPageIndex;
/**
* Holds the requested topic's number of pages
*/
private int pageCount;
/**
* Holds this topic's base url. For example a topic with url similar to
* "https://www.thmmy.gr/smf/index.php?topic=1.15;topicseen" or
* "https://www.thmmy.gr/smf/index.php?topic=1.msg1#msg1"
* has the base url "https://www.thmmy.gr/smf/index.php?topic=1"
*/
private String baseUrl;
/**
* The url of the last page that was attempted to be loaded
*/
private String lastPageLoadAttemptedUrl;
// first load or reload constructor
public TopicTask(TopicTaskObserver topicTaskObserver, OnTopicTaskCompleted finishListener) {
this.topicTaskObserver = topicTaskObserver;
this.finishListener = finishListener;
this.baseUrl = "";
this.currentPageIndex = 1;
this.pageCount = 1;
this.lastPageLoadAttemptedUrl = "";
}
@Override
@ -98,6 +43,18 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
@Override
protected TopicTaskResult doInBackground(String... strings) {
String topicTitle = null;
String topicTreeAndMods = "";
String topicViewers = "";
ArrayList<Post> newPostsList = null;
int loadedPageTopicId = -1;
int focusedPostIndex = 0;
SparseArray<String> pagesUrls = new SparseArray<>();
int currentPageIndex = 1;
int pageCount = 1;
String baseUrl = "";
String lastPageLoadAttemptedUrl = "";
Document topic = null;
String newPageUrl = strings[0];
@ -112,15 +69,15 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf("#")));
}
}
if (!Objects.equals(lastPageLoadAttemptedUrl, "")) topicTitle = null;
lastPageLoadAttemptedUrl = newPageUrl;
if (strings[0].substring(0, strings[0].lastIndexOf(".")).contains("topic="))
baseUrl = strings[0].substring(0, strings[0].lastIndexOf(".")); //New topic's base url
replyPageUrl = null;
String replyPageUrl = null;
Request request = new Request.Builder()
.url(newPageUrl)
.build();
ResultCode resultCode;
try {
Response response = BaseApplication.getInstance().getClient().newCall(request).execute();
topic = Jsoup.parse(response.body().string());
@ -175,11 +132,13 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
Timber.i(e, "IO Exception");
resultCode = ResultCode.NETWORK_ERROR;
} catch (Exception e) {
if (isUnauthorized(topic))
if (isUnauthorized(topic)) {
resultCode = ResultCode.UNAUTHORIZED;
} else {
Timber.e(e, "Parsing Error");
resultCode = ResultCode.PARSING_ERROR;
}
}
return new TopicTaskResult(resultCode, baseUrl, topicTitle, replyPageUrl, newPostsList,
loadedPageTopicId, currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods,
topicViewers, lastPageLoadAttemptedUrl, pagesUrls);

43
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java

@ -9,10 +9,47 @@ import gr.thmmy.mthmmy.model.Post;
public class TopicTaskResult {
private final TopicTask.ResultCode resultCode;
private final String baseUrl, topicTitle, replyPageUrl;
/**
* Holds this topic's base url. For example a topic with url similar to
* "https://www.thmmy.gr/smf/index.php?topic=1.15;topicseen" or
* "https://www.thmmy.gr/smf/index.php?topic=1.msg1#msg1"
* has the base url "https://www.thmmy.gr/smf/index.php?topic=1"
*/
private final String baseUrl;
/**
* Holds this topic's title. At first this gets the value of the topic title that came with
* bundle and is rendered in the toolbar while parsing this topic. Later, if a different topic
* title is parsed from the html source, it gets updated.
*/
private final String topicTitle;
/**
* This topic's reply url
*/
private final String replyPageUrl;
private final ArrayList<Post> newPostsList;
private final int loadedPageTopicId, currentPageIndex, pageCount, focusedPostIndex;
private final String topicTreeAndMods, topicViewers, lastPageLoadAttemptedUrl;
/**
* The topicId of the loaded page
*/
private final int loadedPageTopicId;
/**
* Holds current page's index (starting from 1, not 0)
*/
private final int currentPageIndex;
/**
* Holds the requested topic's number of pages
*/
private final int pageCount;
/**
* The index of the post that has focus
*/
private final int focusedPostIndex;
//Topic's info related
private final String topicTreeAndMods;
private final String topicViewers;
/**
* The url of the last page that was attempted to be loaded
*/
private final String lastPageLoadAttemptedUrl;
private final SparseArray<String> pagesUrls;
public TopicTaskResult(TopicTask.ResultCode resultCode, String baseUrl, String topicTitle,

Loading…
Cancel
Save