Browse Source

crash fix

new_messages
Thodoris1999 6 years ago
parent
commit
a74a5bddc8
  1. 11
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  3. 20
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
  4. 15
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java
  5. 11
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  6. 4
      app/src/main/res/layout/activity_topic_new_message_separator.xml
  7. 1
      app/src/main/res/values/strings.xml

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

@ -633,21 +633,16 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
topicRecyclerviewItems.addAll(postList); topicRecyclerviewItems.addAll(postList);
topicAdapter.notifyDataSetChanged(); topicAdapter.notifyDataSetChanged();
}); });
viewModel.getFocusedPostIndex().observe(this, focusedPostIndex -> { /*viewModel.getFocusedPostIndex().observe(this, focusedPostIndex -> {
if (focusedPostIndex == null) return; if (focusedPostIndex == null) return;
if (viewModel.isFocusedPostLastSeenMessage() && focusedPostIndex != viewModel.postCount() - 1) { recyclerView.scrollToPosition(focusedPostIndex);
topicRecyclerviewItems.add(focusedPostIndex, new NewPostSeparator()); });*/
topicAdapter.notifyItemInserted(focusedPostIndex);
}
//recyclerView.scrollToPosition(focusedPostIndex);
});
viewModel.getTopicTaskResultCode().observe(this, resultCode -> { viewModel.getTopicTaskResultCode().observe(this, resultCode -> {
if (resultCode == null) return; if (resultCode == null) return;
progressBar.setVisibility(ProgressBar.GONE); progressBar.setVisibility(ProgressBar.GONE);
switch (resultCode) { switch (resultCode) {
case SUCCESS: case SUCCESS:
Timber.i("Successfully loaded topic with URL %s", viewModel.getTopicUrl()); Timber.i("Successfully loaded topic with URL %s", viewModel.getTopicUrl());
Timber.i("load " + viewModel.isFocusedPostLastSeenMessage());
paginationEnabled(true); paginationEnabled(true);
break; break;
case NETWORK_ERROR: case NETWORK_ERROR:

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

@ -146,10 +146,10 @@ public class TopicParser {
* @return {@link ArrayList} of {@link Post}s * @return {@link ArrayList} of {@link Post}s
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
public static ArrayList<Post> parseTopic(Document topic, ParseHelpers.Language language) { public static ArrayList<TopicRecyclerViewItem> parseTopic(Document topic, ParseHelpers.Language language) {
//Method's variables //Method's variables
final int NO_INDEX = -1; final int NO_INDEX = -1;
ArrayList<Post> parsedPostsList = new ArrayList<>(); ArrayList<TopicRecyclerViewItem> parsedPostsList = new ArrayList<>();
Elements postRows; Elements postRows;
//Each row is a post //Each row is a post

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

@ -9,7 +9,9 @@ import org.jsoup.nodes.Element;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import gr.thmmy.mthmmy.activities.topic.NewPostSeparator;
import gr.thmmy.mthmmy.activities.topic.TopicParser; import gr.thmmy.mthmmy.activities.topic.TopicParser;
import gr.thmmy.mthmmy.activities.topic.TopicRecyclerViewItem;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.ThmmyPage;
@ -50,11 +52,11 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
if (tmp.contains(";")) { if (tmp.contains(";")) {
postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf(";"))); postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf(";")));
if (newPageUrl.contains("topicseen")) if (newPageUrl.contains("topicseen") && !newPageUrl.contains("#new"))
focusedPostLastSeenMessage = true; focusedPostLastSeenMessage = true;
} else if (tmp.contains("#")) { } else if (tmp.contains("#")) {
postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf("#"))); postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf("#")));
if (newPageUrl.contains("topicseen")) if (newPageUrl.contains("topicseen") && !newPageUrl.contains("#new"))
focusedPostLastSeenMessage = true; focusedPostLastSeenMessage = true;
} }
} }
@ -96,31 +98,33 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
//Finds number of pages //Finds number of pages
int pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language); int pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language);
ArrayList<Post> newPostsList = TopicParser.parseTopic(topic, language); ArrayList<TopicRecyclerViewItem> newPostsList = TopicParser.parseTopic(topic, language);
int loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(newPageUrl)); int loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(newPageUrl));
//Finds the position of the focused message if present //Finds the position of the focused message if present
int focusedPostIndex = 0; int focusedPostIndex = 0;
for (int i = 0; i < newPostsList.size(); ++i) { for (int i = 0; i < newPostsList.size(); ++i) {
if (newPostsList.get(i).getPostIndex() == postFocus) { if (((Post) newPostsList.get(i)).getPostIndex() == postFocus) {
focusedPostIndex = i; focusedPostIndex = i;
break; break;
} }
} }
if (focusedPostLastSeenMessage)
newPostsList.add(focusedPostIndex, new NewPostSeparator());
return new TopicTaskResult(ResultCode.SUCCESS, topicTitle, replyPageUrl, newPostsList, loadedPageTopicId, return new TopicTaskResult(ResultCode.SUCCESS, topicTitle, replyPageUrl, newPostsList, loadedPageTopicId,
currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods, topicViewers, focusedPostLastSeenMessage); currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods, topicViewers);
} catch (IOException e) { } catch (IOException e) {
return new TopicTaskResult(ResultCode.NETWORK_ERROR, null, null, null, return new TopicTaskResult(ResultCode.NETWORK_ERROR, null, null, null,
0, 0, 0, 0, null, null, false); 0, 0, 0, 0, null, null);
} catch (Exception e) { } catch (Exception e) {
if (isUnauthorized(topic)) { if (isUnauthorized(topic)) {
return new TopicTaskResult(ResultCode.UNAUTHORIZED, null, null, null, return new TopicTaskResult(ResultCode.UNAUTHORIZED, null, null, null,
0, 0, 0, 0, null, null, false); 0, 0, 0, 0, null, null);
} else { } else {
Timber.e(e, "Topic parse failed"); Timber.e(e, "Topic parse failed");
return new TopicTaskResult(ResultCode.PARSING_ERROR, null, null, null, return new TopicTaskResult(ResultCode.PARSING_ERROR, null, null, null,
0, 0, 0, 0, null, null, false); 0, 0, 0, 0, null, null);
} }
} }
} }

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

@ -2,6 +2,7 @@ package gr.thmmy.mthmmy.activities.topic.tasks;
import java.util.ArrayList; import java.util.ArrayList;
import gr.thmmy.mthmmy.activities.topic.TopicRecyclerViewItem;
import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.Post;
public class TopicTaskResult { public class TopicTaskResult {
@ -16,7 +17,7 @@ public class TopicTaskResult {
* This topic's reply url * This topic's reply url
*/ */
private final String replyPageUrl; private final String replyPageUrl;
private final ArrayList<Post> newPostsList; private final ArrayList<TopicRecyclerViewItem> newPostsList;
/** /**
* The topicId of the loaded page * The topicId of the loaded page
*/ */
@ -36,12 +37,11 @@ public class TopicTaskResult {
//Topic's info related //Topic's info related
private final String topicTreeAndMods; private final String topicTreeAndMods;
private final String topicViewers; private final String topicViewers;
private final boolean focusedPostLastSeenMessage;
public TopicTaskResult(TopicTask.ResultCode resultCode, String topicTitle, public TopicTaskResult(TopicTask.ResultCode resultCode, String topicTitle,
String replyPageUrl, ArrayList<Post> newPostsList, int loadedPageTopicId, String replyPageUrl, ArrayList<TopicRecyclerViewItem> newPostsList, int loadedPageTopicId,
int currentPageIndex, int pageCount, int focusedPostIndex, String topicTreeAndMods, int currentPageIndex, int pageCount, int focusedPostIndex, String topicTreeAndMods,
String topicViewers, boolean focusedPostLastSeenMessage) { String topicViewers) {
this.resultCode = resultCode; this.resultCode = resultCode;
this.topicTitle = topicTitle; this.topicTitle = topicTitle;
this.replyPageUrl = replyPageUrl; this.replyPageUrl = replyPageUrl;
@ -52,7 +52,6 @@ public class TopicTaskResult {
this.focusedPostIndex = focusedPostIndex; this.focusedPostIndex = focusedPostIndex;
this.topicTreeAndMods = topicTreeAndMods; this.topicTreeAndMods = topicTreeAndMods;
this.topicViewers = topicViewers; this.topicViewers = topicViewers;
this.focusedPostLastSeenMessage = focusedPostLastSeenMessage;
} }
public TopicTask.ResultCode getResultCode() { public TopicTask.ResultCode getResultCode() {
@ -67,7 +66,7 @@ public class TopicTaskResult {
return replyPageUrl; return replyPageUrl;
} }
public ArrayList<Post> getNewPostsList() { public ArrayList<TopicRecyclerViewItem> getNewPostsList() {
return newPostsList; return newPostsList;
} }
@ -94,8 +93,4 @@ public class TopicTaskResult {
public String getTopicViewers() { public String getTopicViewers() {
return topicViewers; return topicViewers;
} }
public boolean isFocusedPostLastSeenMessage() {
return focusedPostLastSeenMessage;
}
} }

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

@ -9,6 +9,7 @@ import android.preference.PreferenceManager;
import java.util.ArrayList; import java.util.ArrayList;
import gr.thmmy.mthmmy.activities.settings.SettingsActivity; import gr.thmmy.mthmmy.activities.settings.SettingsActivity;
import gr.thmmy.mthmmy.activities.topic.TopicRecyclerViewItem;
import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask; 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;
@ -65,7 +66,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
private MutableLiveData<String> replyPageUrl = new MutableLiveData<>(); private MutableLiveData<String> replyPageUrl = new MutableLiveData<>();
private MutableLiveData<Integer> pageTopicId = new MutableLiveData<>(); private MutableLiveData<Integer> pageTopicId = new MutableLiveData<>();
private MutableLiveData<String> topicTitle = new MutableLiveData<>(); private MutableLiveData<String> topicTitle = new MutableLiveData<>();
private MutableLiveData<ArrayList<Post>> postsList = new MutableLiveData<>(); private MutableLiveData<ArrayList<TopicRecyclerViewItem>> postsList = new MutableLiveData<>();
private MutableLiveData<Integer> focusedPostIndex = new MutableLiveData<>(); private MutableLiveData<Integer> focusedPostIndex = new MutableLiveData<>();
private MutableLiveData<TopicTask.ResultCode> topicTaskResultCode = new MutableLiveData<>(); private MutableLiveData<TopicTask.ResultCode> topicTaskResultCode = new MutableLiveData<>();
private MutableLiveData<String> topicTreeAndMods = new MutableLiveData<>(); private MutableLiveData<String> topicTreeAndMods = new MutableLiveData<>();
@ -73,7 +74,6 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
private String topicUrl; private String topicUrl;
private int currentPageIndex; private int currentPageIndex;
private int pageCount; private int pageCount;
private boolean focusedPostLastSeenMessage;
private MutableLiveData<PrepareForReplyResult> prepareForReplyResult = new MutableLiveData<>(); private MutableLiveData<PrepareForReplyResult> prepareForReplyResult = new MutableLiveData<>();
private MutableLiveData<PrepareForEditResult> prepareForEditResult = new MutableLiveData<>(); private MutableLiveData<PrepareForEditResult> prepareForEditResult = new MutableLiveData<>();
@ -188,7 +188,6 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) { if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) {
currentPageIndex = result.getCurrentPageIndex(); currentPageIndex = result.getCurrentPageIndex();
pageCount = result.getPageCount(); pageCount = result.getPageCount();
focusedPostLastSeenMessage = result.isFocusedPostLastSeenMessage();
topicTreeAndMods.setValue(result.getTopicTreeAndMods()); topicTreeAndMods.setValue(result.getTopicTreeAndMods());
topicViewers.setValue(result.getTopicViewers()); topicViewers.setValue(result.getTopicViewers());
pageTopicId.setValue(result.getLoadedPageTopicId()); pageTopicId.setValue(result.getLoadedPageTopicId());
@ -264,7 +263,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
return focusedPostIndex; return focusedPostIndex;
} }
public MutableLiveData<ArrayList<Post>> getPostsList() { public MutableLiveData<ArrayList<TopicRecyclerViewItem>> getPostsList() {
return postsList; return postsList;
} }
@ -394,8 +393,4 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
throw new NullPointerException("No page has been loaded yet!"); throw new NullPointerException("No page has been loaded yet!");
return postsList.getValue().size(); return postsList.getValue().size();
} }
public boolean isFocusedPostLastSeenMessage() {
return focusedPostLastSeenMessage;
}
} }

4
app/src/main/res/layout/activity_topic_new_message_separator.xml

@ -9,12 +9,13 @@
android:layout_height="1dp" android:layout_height="1dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:background="@color/red"/> android:background="@color/red"/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="NEW MESSAGES" android:text="@string/new_messages"
android:textColor="@color/red" android:textColor="@color/red"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/> android:layout_marginEnd="8dp"/>
@ -24,5 +25,6 @@
android:layout_height="1dp" android:layout_height="1dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:background="@color/red"/> android:background="@color/red"/>
</LinearLayout> </LinearLayout>

1
app/src/main/res/values/strings.xml

@ -180,4 +180,5 @@
<string name="dialog_link_url_hint">Link URL</string> <string name="dialog_link_url_hint">Link URL</string>
<string name="dialog_link_text_hint">Link text</string> <string name="dialog_link_text_hint">Link text</string>
<string name="input_field_required">Required</string> <string name="input_field_required">Required</string>
<string name="new_messages">NEW MESSAGES</string>
</resources> </resources>

Loading…
Cancel
Save