Browse Source

Unread posts init

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
1d5214136f
  1. 21
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 28
      app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java
  3. 13
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  4. 109
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java
  5. 184
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  6. 1
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  7. 38
      app/src/main/res/layout/fragment_unread.xml
  8. 19
      app/src/main/res/layout/fragment_unread_empty_row.xml
  9. 54
      app/src/main/res/layout/fragment_unread_row.xml
  10. 1
      app/src/main/res/values-w820dp/dimens.xml
  11. 1
      app/src/main/res/values/dimens.xml

21
app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java

@ -16,6 +16,7 @@ import gr.thmmy.mthmmy.activities.board.BoardActivity;
import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity;
import gr.thmmy.mthmmy.activities.main.forum.ForumFragment;
import gr.thmmy.mthmmy.activities.main.recent.RecentFragment;
import gr.thmmy.mthmmy.activities.main.unread.UnreadFragment;
import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
@ -33,7 +34,7 @@ import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL;
public class MainActivity extends BaseActivity implements RecentFragment.RecentFragmentInteractionListener, ForumFragment.ForumFragmentInteractionListener {
public class MainActivity extends BaseActivity implements RecentFragment.RecentFragmentInteractionListener, ForumFragment.ForumFragmentInteractionListener, UnreadFragment.UnreadFragmentInteractionListener {
//----------------------------------------CLASS VARIABLES-----------------------------------------
private static final int TIME_INTERVAL = 2000;
@ -115,6 +116,14 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
startActivity(i);
}
@Override
public void onUnreadFragmentInteraction(TopicSummary topicSummary){
Intent i = new Intent(MainActivity.this, TopicActivity.class);
i.putExtra(BUNDLE_TOPIC_URL, topicSummary.getTopicUrl());
i.putExtra(BUNDLE_TOPIC_TITLE, topicSummary.getSubject());
startActivity(i);
}
//---------------------------------FragmentPagerAdapter---------------------------------------------
/**
@ -123,7 +132,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
* it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
@ -136,6 +145,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
return RecentFragment.newInstance(position + 1);
case 1:
return ForumFragment.newInstance(position + 1);
case 2:
return UnreadFragment.newInstance(position + 1);
default:
return RecentFragment.newInstance(position + 1); //temp (?)
}
@ -143,8 +154,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
@Override
public int getCount() {
// Show 2 total pages.
return 2;
// Show 3 total pages.
return 3;
}
@Override
@ -154,6 +165,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
return "RECENT POSTS";
case 1:
return "FORUM";
case 2:
return "UNREAD";
}
return null;

28
app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java

@ -42,8 +42,7 @@ import timber.log.Timber;
* Use the {@link ForumFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ForumFragment extends BaseFragment
{
public class ForumFragment extends BaseFragment {
private static final String TAG = "ForumFragment";
// Fragment initialization parameters, e.g. ARG_SECTION_NUMBER
@ -56,11 +55,13 @@ public class ForumFragment extends BaseFragment
private ForumTask forumTask;
// Required empty public constructor
public ForumFragment() {}
public ForumFragment() {
}
/**
* Use ONLY this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Forum.
*/
public static ForumFragment newInstance(int sectionNumber) {
@ -81,8 +82,7 @@ public class ForumFragment extends BaseFragment
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (categories.isEmpty())
{
if (categories.isEmpty()) {
forumTask = new ForumTask();
forumTask.execute();
@ -103,8 +103,7 @@ public class ForumFragment extends BaseFragment
forumAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
@Override
public void onParentExpanded(int parentPosition) {
if(BaseActivity.getSessionManager().isLoggedIn())
{
if (BaseActivity.getSessionManager().isLoggedIn()) {
if (forumTask.getStatus() == AsyncTask.Status.RUNNING)
forumTask.cancel(true);
forumTask = new ForumTask();
@ -115,8 +114,7 @@ public class ForumFragment extends BaseFragment
@Override
public void onParentCollapsed(int parentPosition) {
if(BaseActivity.getSessionManager().isLoggedIn())
{
if (BaseActivity.getSessionManager().isLoggedIn()) {
if (forumTask.getStatus() == AsyncTask.Status.RUNNING)
forumTask.cancel(true);
forumTask = new ForumTask();
@ -190,22 +188,19 @@ public class ForumFragment extends BaseFragment
public void parse(Document document) throws ParseException {
Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]");
if (categoryBlocks.size() != 0) {
for(Element categoryBlock: categoryBlocks)
{
for (Element categoryBlock : categoryBlocks) {
Element categoryElement = categoryBlock.select("td[colspan=2]>[name]").first();
String categoryUrl = categoryElement.attr("href");
Category category = new Category(categoryElement.text(), categoryUrl);
if(categoryUrl.contains("sa=collapse")|| !BaseActivity.getSessionManager().isLoggedIn())
{
if (categoryUrl.contains("sa=collapse") || !BaseActivity.getSessionManager().isLoggedIn()) {
category.setExpanded(true);
Elements boardsElements = categoryBlock.select("b [name]");
for (Element boardElement : boardsElements) {
Board board = new Board(boardElement.attr("href"), boardElement.text(), null, null, null, null);
category.getBoards().add(board);
}
}
else
} else
category.setExpanded(false);
fetchedCategories.add(category);
@ -213,8 +208,7 @@ public class ForumFragment extends BaseFragment
categories.clear();
categories.addAll(fetchedCategories);
fetchedCategories.clear();
}
else
} else
throw new ParseException("Parsing failed");
}

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

@ -51,11 +51,13 @@ public class RecentFragment extends BaseFragment {
private RecentTask recentTask;
// Required empty public constructor
public RecentFragment() {}
public RecentFragment() {
}
/**
* Use ONLY this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Recent.
*/
public static RecentFragment newInstance(int sectionNumber) {
@ -76,8 +78,7 @@ public class RecentFragment extends BaseFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (topicSummaries.isEmpty())
{
if (topicSummaries.isEmpty()) {
recentTask = new RecentTask();
recentTask.execute(SessionManager.indexUrl.toString());
@ -142,8 +143,7 @@ public class RecentFragment extends BaseFragment {
}
@Override
public void parse(Document document) throws ParseException
{
public void parse(Document document) throws ParseException {
Elements recent = document.select("#block8 :first-child div");
if (!recent.isEmpty()) {
topicSummaries.clear();
@ -174,7 +174,6 @@ public class RecentFragment extends BaseFragment {
throw new ParseException("Parsing failed");
}
@Override
protected void postParsing(ParseTask.ResultCode result) {
if (result == ResultCode.SUCCESS)
@ -183,7 +182,5 @@ public class RecentFragment extends BaseFragment {
progressBar.setVisibility(ProgressBar.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
}
}

109
app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java

@ -0,0 +1,109 @@
package gr.thmmy.mthmmy.activities.main.unread;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary;
class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
private final List<TopicSummary> unreadList;
private final UnreadFragment.UnreadFragmentInteractionListener mListener;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_NADA = 1;
UnreadAdapter(Context context, @NonNull List<TopicSummary> topicSummaryList, BaseFragment.FragmentInteractionListener listener) {
this.context = context;
this.unreadList = topicSummaryList;
mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener;
}
@Override
public int getItemViewType(int position) {
return unreadList.get(position).getTopicUrl() == null ? VIEW_TYPE_NADA : VIEW_TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_unread_row, parent, false);
return new ViewHolder(view);
} else if (viewType == VIEW_TYPE_NADA) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_unread_empty_row, parent, false);
return new EmptyViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof UnreadAdapter.EmptyViewHolder) {
final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder;
emptyViewHolder.text.setText(unreadList.get(position).getDateTimeModified());
} else if (holder instanceof UnreadAdapter.ViewHolder) {
final UnreadAdapter.ViewHolder viewHolder = (UnreadAdapter.ViewHolder) holder;
viewHolder.mTitleView.setText(unreadList.get(position).getSubject());
viewHolder.mDateTimeView.setText(unreadList.get(position).getDateTimeModified());
viewHolder.mUserView.setText(context.getString(R.string.byUser, unreadList.get(position).getLastUser()));
viewHolder.topic = unreadList.get(position);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener.onUnreadFragmentInteraction(viewHolder.topic); //?
}
}
});
}
}
@Override
public int getItemCount() {
return unreadList.size();
}
private static class ViewHolder extends RecyclerView.ViewHolder {
final View mView;
final TextView mTitleView;
final TextView mUserView;
final TextView mDateTimeView;
public TopicSummary topic;
ViewHolder(View view) {
super(view);
mView = view;
mTitleView = (TextView) view.findViewById(R.id.title);
mUserView = (TextView) view.findViewById(R.id.lastUser);
mDateTimeView = (TextView) view.findViewById(R.id.dateTime);
}
}
private static class EmptyViewHolder extends RecyclerView.ViewHolder {
final TextView text;
EmptyViewHolder(View view) {
super(view);
text = (TextView) view.findViewById(R.id.text);
}
}
}

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

@ -0,0 +1,184 @@
package gr.thmmy.mthmmy.activities.main.unread;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import timber.log.Timber;
/**
* A {@link BaseFragment} subclass.
* Activities that contain this fragment must implement the
* {@link UnreadFragment.UnreadFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link UnreadFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class UnreadFragment extends BaseFragment {
private static final String TAG = "UnreadFragment";
// Fragment initialization parameters, e.g. ARG_SECTION_NUMBER
private MaterialProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;
private UnreadAdapter unreadAdapter;
private List<TopicSummary> topicSummaries;
private UnreadTask unreadTask;
// Required empty public constructor
public UnreadFragment() {
}
/**
* Use ONLY this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Unread.
*/
public static UnreadFragment newInstance(int sectionNumber) {
UnreadFragment fragment = new UnreadFragment();
Bundle args = new Bundle();
args.putString(ARG_TAG, TAG);
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
topicSummaries = new ArrayList<>();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (topicSummaries.isEmpty()) {
unreadTask = new UnreadTask();
unreadTask.execute(SessionManager.unreadUrl.toString());
}
Timber.d("onActivityCreated");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_unread, container, false);
// Set the adapter
if (rootView instanceof RelativeLayout) {
progressBar = (MaterialProgressBar) rootView.findViewById(R.id.progressBar);
unreadAdapter = new UnreadAdapter(getActivity(), topicSummaries, fragmentInteractionListener);
CustomRecyclerView recyclerView = (CustomRecyclerView) rootView.findViewById(R.id.list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.findViewById(R.id.list).getContext());
recyclerView.setLayoutManager(linearLayoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(unreadAdapter);
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh);
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) {
unreadTask = new UnreadTask();
unreadTask.execute(SessionManager.unreadUrl.toString());
}
}
}
);
}
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING)
unreadTask.cancel(true);
}
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener {
void onUnreadFragmentInteraction(TopicSummary topicSummary);
}
//---------------------------------------ASYNC TASK-----------------------------------
private class UnreadTask extends ParseTask {
protected void onPreExecute() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
public void parse(Document document) throws ParseException {
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)");
if (!unread.isEmpty()) {
topicSummaries.clear();
for (Element row : unread) {
Elements information = row.select("td");
String link = information.get(2).select("a").first().attr("href");
String title = information.get(2).select("a").first().text();
Element lastUserAndDate = information.get(6);
String lastUser = lastUserAndDate.select("a").text();
String dateTime = lastUserAndDate.select("span").html();
dateTime = dateTime.substring(3, dateTime.indexOf("<b"));
dateTime = dateTime.replace("</b>", "");
topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime));
}
} else {
String message = document.select("table.bordercolor[cellspacing=1]").first().text();
if (message.contains("No messages")){ //It's english
message = "No unread posts!";
}else{ //It's greek
message = "Δεν υπάρχουν μη διαβασμένα μυνήματα!";
}
topicSummaries.add(new TopicSummary(null, null, null, message));
}
}
@Override
protected void postParsing(ParseTask.ResultCode result) {
if (result == ResultCode.SUCCESS)
unreadAdapter.notifyDataSetChanged();
progressBar.setVisibility(ProgressBar.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
}
}

1
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -36,6 +36,7 @@ public class SessionManager {
public static final HttpUrl indexUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?theme=4");
public static final HttpUrl forumUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=forum;theme=4");
private static final HttpUrl loginUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=login2");
public static final HttpUrl unreadUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=unread;all;start=0;theme=4");
private static final String guestName = "Guest";
//Response Codes

38
app/src/main/res/layout/fragment_unread.xml

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<gr.thmmy.mthmmy.utils.CustomRecyclerView
android:id="@+id/list"
android:name="gr.thmmy.mthmmy.sections.unread.UnreadFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:layoutManager="LinearLayoutManager"
tools:context=".activities.main.unread.UnreadFragment"
tools:listitem="@layout/fragment_unread_row"/>
</android.support.v4.widget.SwipeRefreshLayout>
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/progressBar"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding"
android:layout_width="match_parent"
android:layout_height="@dimen/progress_bar_height"
android:layout_alignParentTop="true"
android:indeterminate="true"
android:visibility="invisible"
app:mpb_indeterminateTint="@color/accent"
app:mpb_progressStyle="horizontal"/>
</RelativeLayout>

19
app/src/main/res/layout/fragment_unread_empty_row.xml

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="6dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/accent"
android:textSize="@dimen/big_text" />
</LinearLayout>

54
app/src/main/res/layout/fragment_unread_row.xml

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_light"
android:foreground="?android:attr/selectableItemBackground"
android:paddingBottom="6dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="6dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="@color/primary_text"/>
<Space
android:id="@+id/spacer"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="@+id/title"
android:layout_below="@+id/title"
android:layout_toEndOf="@+id/dateTime"/>
<TextView
android:id="@+id/dateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/spacer"
android:textColor="@color/secondary_text"/>
<TextView
android:id="@+id/lastUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/dateTime"
android:layout_alignBottom="@+id/dateTime"
android:layout_alignParentEnd="true"
android:textColor="@color/secondary_text"
android:textStyle="italic"/>
</RelativeLayout>
</LinearLayout>

1
app/src/main/res/values-w820dp/dimens.xml

@ -9,4 +9,5 @@
<dimen name="quote_button">144dp</dimen>
<dimen name="fab_margins">64dp</dimen>
<dimen name="progress_bar_height">40dp</dimen>
<dimen name="big_text">24sp</dimen>
</resources>

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

@ -7,4 +7,5 @@
<dimen name="quote_button">36dp</dimen>
<dimen name="fab_margins">16dp</dimen>
<dimen name="progress_bar_height">10dp</dimen>
<dimen name="big_text">24sp</dimen>
</resources>

Loading…
Cancel
Save