Browse Source

generify posts list to topic items

pull/55/head
Thodoris1999 6 years ago
parent
commit
7db9c43fd8
  1. 38
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 29
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  4. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
  5. 7
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java
  6. 4
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java
  7. 3
      app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java
  8. 13
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

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

@ -12,7 +12,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDelegate;
@ -50,6 +49,7 @@ import gr.thmmy.mthmmy.editorview.EmojiKeyboard;
import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager;
import gr.thmmy.mthmmy.utils.HTMLUtils;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
@ -85,7 +85,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
/**
* Holds a list of this topic's posts
*/
private ArrayList<Post> postsList;
private ArrayList<TopicItem> topicItems;
//Reply related
private FloatingActionButton replyFAB;
//Topic's pages related
@ -170,7 +170,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
progressBar = findViewById(R.id.progressBar);
emojiKeyboard = findViewById(R.id.emoji_keyboard);
postsList = new ArrayList<>();
topicItems = new ArrayList<>();
recyclerView = findViewById(R.id.topic_recycler_view);
recyclerView.setHasFixedSize(true);
@ -179,7 +179,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
getApplicationContext(), topicPageUrl);
recyclerView.setLayoutManager(layoutManager);
topicAdapter = new TopicAdapter(this, postsList);
topicAdapter = new TopicAdapter(this, topicItems);
recyclerView.setAdapter(topicAdapter);
replyFAB = findViewById(R.id.topic_fab);
@ -280,15 +280,15 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
}
return;
} else if (viewModel.isWritingReply()) {
postsList.remove(postsList.size() - 1);
topicAdapter.notifyItemRemoved(postsList.size());
topicItems.remove(topicItems.size() - 1);
topicAdapter.notifyItemRemoved(topicItems.size());
topicAdapter.setBackButtonHidden();
viewModel.setWritingReply(false);
replyFAB.show();
bottomNavBar.setVisibility(View.VISIBLE);
return;
} else if (viewModel.isEditingPost()) {
postsList.get(viewModel.getPostBeingEditedPosition()).setPostType(Post.TYPE_POST);
((Post) topicItems.get(viewModel.getPostBeingEditedPosition())).setPostType(Post.TYPE_POST);
topicAdapter.notifyItemChanged(viewModel.getPostBeingEditedPosition());
topicAdapter.setBackButtonHidden();
viewModel.setEditingPost(false);
@ -538,7 +538,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
replyFAB.show();
bottomNavBar.setVisibility(View.VISIBLE);
viewModel.setWritingReply(false);
if ((postsList.get(postsList.size() - 1).getPostNumber() + 1) % 15 == 0) {
if ((((Post) topicItems.get(topicItems.size() - 1)).getPostNumber() + 1) % 15 == 0) {
Timber.i("Reply was posted in new page. Switching to last page.");
viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + "." + 2147483647);
} else {
@ -547,8 +547,8 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
} else {
Timber.w("Post reply unsuccessful");
Toast.makeText(getBaseContext(), "Post failed!", Toast.LENGTH_SHORT).show();
recyclerView.getChildAt(postsList.size() - 1).setAlpha(1);
recyclerView.getChildAt(postsList.size() - 1).setEnabled(true);
recyclerView.getChildAt(topicItems.size() - 1).setAlpha(1);
recyclerView.getChildAt(topicItems.size() - 1).setEnabled(true);
}
}
});
@ -581,7 +581,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
if (result) {
Timber.i("Post edit successful");
postsList.get(position).setPostType(Post.TYPE_POST);
((Post) topicItems.get(position)).setPostType(Post.TYPE_POST);
topicAdapter.notifyItemChanged(position);
replyFAB.show();
bottomNavBar.setVisibility(View.VISIBLE);
@ -631,11 +631,11 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
else
replyFAB.show();
});
viewModel.getPostsList().observe(this, postList -> {
viewModel.getTopicItems().observe(this, postList -> {
if (postList == null) progressBar.setVisibility(ProgressBar.VISIBLE);
recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug
postsList.clear();
postsList.addAll(postList);
topicItems.clear();
topicItems.addAll(postList);
topicAdapter.notifyDataSetChanged();
});
/*viewModel.getFocusedPostIndex().observe(this, focusedPostIndex -> {
@ -652,7 +652,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
break;
case NETWORK_ERROR:
Timber.w("Network error on loaded page");
if (viewModel.getPostsList().getValue() == null) {
if (viewModel.getTopicItems().getValue() == null) {
// no page has been loaded yet. Give user the ability to refresh
recyclerView.setVisibility(View.GONE);
TextView errorTextview = findViewById(R.id.error_textview);
@ -710,9 +710,9 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
Timber.i("Prepare for reply successful");
//prepare for a reply
viewModel.setWritingReply(true);
postsList.add(Post.newQuickReply());
topicAdapter.notifyItemInserted(postsList.size());
recyclerView.scrollToPosition(postsList.size() - 1);
topicItems.add(Post.newQuickReply());
topicAdapter.notifyItemInserted(topicItems.size());
recyclerView.scrollToPosition(topicItems.size() - 1);
replyFAB.hide();
bottomNavBar.setVisibility(View.GONE);
} else {
@ -725,7 +725,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
if (result != null && result.isSuccessful()) {
Timber.i("Prepare for edit successful");
viewModel.setEditingPost(true);
postsList.get(result.getPosition()).setPostType(Post.TYPE_EDIT);
((Post) topicItems.get(result.getPosition())).setPostType(Post.TYPE_EDIT);
topicAdapter.notifyItemChanged(result.getPosition());
recyclerView.scrollToPosition(result.getPosition());
replyFAB.hide();

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

@ -46,9 +46,11 @@ import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.editorview.EditorView;
import gr.thmmy.mthmmy.editorview.EmojiKeyboard;
import gr.thmmy.mthmmy.model.Poll;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyFile;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.CircleTransform;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import gr.thmmy.mthmmy.viewmodel.TopicViewModel;
@ -76,16 +78,16 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
private final OnPostFocusChangeListener postFocusListener;
private final EmojiKeyboard.EmojiKeyboardOwner emojiKeyboardOwner;
private final List<Post> postsList;
private final List<TopicItem> topicItems;
private TopicViewModel viewModel;
/**
* @param context the context of the {@link RecyclerView}
* @param postsList List of {@link Post} objects to use
* @param topicItems List of {@link Post} objects to use
*/
TopicAdapter(TopicActivity context, List<Post> postsList) {
TopicAdapter(TopicActivity context, List<TopicItem> topicItems) {
this.context = context;
this.postsList = postsList;
this.topicItems = topicItems;
this.postFocusListener = context;
this.emojiKeyboardOwner = context;
@ -96,7 +98,8 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@Override
public int getItemViewType(int position) {
return postsList.get(position).getPostType();
if (topicItems.get(position) instanceof Poll) return Poll.TYPE_POLL;
return ((Post) topicItems.get(position)).getPostType();
}
@NonNull
@ -141,8 +144,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder currentHolder,
final int position) {
if (currentHolder.getItemViewType() == Poll.TYPE_POLL) {
} else {
Post currentPost = (Post) topicItems.get(position);
if (currentHolder instanceof PostViewHolder) {
final Post currentPost = postsList.get(position);
final PostViewHolder holder = (PostViewHolder) currentHolder;
//Post's WebView parameters
@ -398,7 +404,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
editPostButton.setVisibility(View.GONE);
} else {
editPostButton.setOnClickListener(v -> {
viewModel.prepareForEdit(position, postsList.get(position).getPostEditURL());
viewModel.prepareForEdit(position, currentPost.getPostEditURL());
popUp.dismiss();
});
}
@ -508,7 +514,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
.transform(new CircleTransform())
.into(holder.thumbnail);
holder.username.setText(getSessionManager().getUsername());
holder.editSubject.setText(postsList.get(position).getSubject());
holder.editSubject.setText(currentPost.getSubject());
holder.editSubject.setRawInputType(InputType.TYPE_CLASS_TEXT);
holder.editSubject.setImeOptions(EditorInfo.IME_ACTION_DONE);
@ -545,10 +551,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
}
}
}
}
@Override
public int getItemCount() {
return postsList.size();
return topicItems.size();
}
/**
@ -680,8 +687,8 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (tmpUrlSbstr.contains("msg"))
tmpUrlSbstr = tmpUrlSbstr.substring(0, tmpUrlSbstr.indexOf("msg") - 1);
int testAgainst = Integer.parseInt(tmpUrlSbstr);
for (int i = 0; i < postsList.size(); i++) {
if (postsList.get(i).getPostIndex() == testAgainst) {
for (int i = 0; i < topicItems.size(); i++) {
if (topicItems.get(i) instanceof Post && ((Post) topicItems.get(i)).getPostIndex() == testAgainst) {
//same page
Timber.e(Integer.toString(i));
postFocusListener.onPostFocusChange(i);

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

@ -18,6 +18,7 @@ import java.util.regex.Pattern;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyFile;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber;
@ -146,10 +147,10 @@ public class TopicParser {
* @return {@link ArrayList} of {@link Post}s
* @see org.jsoup.Jsoup Jsoup
*/
public static ArrayList<Post> parseTopic(Document topic, ParseHelpers.Language language) {
public static ArrayList<TopicItem> parseTopic(Document topic, ParseHelpers.Language language) {
//Method's variables
final int NO_INDEX = -1;
ArrayList<Post> parsedPostsList = new ArrayList<>();
ArrayList<TopicItem> parsedPostsList = new ArrayList<>();
Elements postRows;
//Each row is a post

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

@ -13,6 +13,7 @@ import gr.thmmy.mthmmy.activities.topic.TopicParser;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import okhttp3.Request;
import okhttp3.Response;
@ -92,14 +93,14 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
//Finds number of pages
int pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language);
ArrayList<Post> newPostsList = TopicParser.parseTopic(topic, language);
ArrayList<TopicItem> newPostsList = TopicParser.parseTopic(topic, language);
int loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(newPageUrl));
//Finds the position of the focused message if present
int focusedPostIndex = 0;
for (int i = 0; i < newPostsList.size(); ++i) {
if (newPostsList.get(i).getPostIndex() == postFocus) {
if (newPostsList.get(i) instanceof Post && ((Post) newPostsList.get(i)).getPostIndex() == postFocus) {
focusedPostIndex = i;
break;
}

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

@ -3,6 +3,7 @@ package gr.thmmy.mthmmy.activities.topic.tasks;
import java.util.ArrayList;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.TopicItem;
public class TopicTaskResult {
private final TopicTask.ResultCode resultCode;
@ -16,7 +17,7 @@ public class TopicTaskResult {
* This topic's reply url
*/
private final String replyPageUrl;
private final ArrayList<Post> newPostsList;
private final ArrayList<TopicItem> newPostsList;
/**
* The topicId of the loaded page
*/
@ -38,7 +39,7 @@ public class TopicTaskResult {
private final String topicViewers;
public TopicTaskResult(TopicTask.ResultCode resultCode, String topicTitle,
String replyPageUrl, ArrayList<Post> newPostsList, int loadedPageTopicId,
String replyPageUrl, ArrayList<TopicItem> newPostsList, int loadedPageTopicId,
int currentPageIndex, int pageCount, int focusedPostIndex, String topicTreeAndMods,
String topicViewers) {
this.resultCode = resultCode;
@ -65,7 +66,7 @@ public class TopicTaskResult {
return replyPageUrl;
}
public ArrayList<Post> getNewPostsList() {
public ArrayList<TopicItem> getNewPostsList() {
return newPostsList;
}

4
app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

@ -2,7 +2,9 @@ package gr.thmmy.mthmmy.model;
import java.text.DecimalFormat;
public class Poll {
public class Poll extends TopicItem {
public static int TYPE_POLL = 3;
private final String question;
private Entry[] entries;
private int availableVoteCount;

3
app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java

@ -1,4 +1,5 @@
package gr.thmmy.mthmmy.model;
public class TopicItem {
public abstract class TopicItem {
}

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

@ -20,6 +20,7 @@ import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask;
import gr.thmmy.mthmmy.activities.topic.tasks.TopicTaskResult;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber;
@ -65,7 +66,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
private MutableLiveData<String> replyPageUrl = new MutableLiveData<>();
private MutableLiveData<Integer> pageTopicId = new MutableLiveData<>();
private MutableLiveData<String> topicTitle = new MutableLiveData<>();
private MutableLiveData<ArrayList<Post>> postsList = new MutableLiveData<>();
private MutableLiveData<ArrayList<TopicItem>> topicItems = new MutableLiveData<>();
private MutableLiveData<Integer> focusedPostIndex = new MutableLiveData<>();
private MutableLiveData<TopicTask.ResultCode> topicTaskResultCode = new MutableLiveData<>();
private MutableLiveData<String> topicTreeAndMods = new MutableLiveData<>();
@ -193,7 +194,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
replyPageUrl.setValue(result.getReplyPageUrl());
topicTitle.setValue(result.getTopicTitle());
pageIndicatorIndex.setValue(result.getCurrentPageIndex());
postsList.setValue(result.getNewPostsList());
topicItems.setValue(result.getNewPostsList());
focusedPostIndex.setValue(result.getFocusedPostIndex());
isUserExtraInfoVisibile.clear();
for (int i = 0; i < result.getNewPostsList().size(); i++) {
@ -262,8 +263,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
return focusedPostIndex;
}
public MutableLiveData<ArrayList<Post>> getPostsList() {
return postsList;
public MutableLiveData<ArrayList<TopicItem>> getTopicItems() {
return topicItems;
}
public MutableLiveData<String> getReplyPageUrl() {
@ -388,8 +389,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
}
public int postCount() {
if (postsList.getValue() == null)
if (topicItems.getValue() == null)
throw new NullPointerException("No page has been loaded yet!");
return postsList.getValue().size();
return topicItems.getValue().size();
}
}

Loading…
Cancel
Save