Browse Source

Mark read functionality

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
7816c2d032
  1. 18
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java
  2. 69
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java

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

@ -18,15 +18,19 @@ class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context; private final Context context;
private final List<TopicSummary> unreadList; private final List<TopicSummary> unreadList;
private final UnreadFragment.UnreadFragmentInteractionListener mListener; private final UnreadFragment.UnreadFragmentInteractionListener mListener;
private final MarkReadInteractionListener markReadListener;
private final int VIEW_TYPE_ITEM = 0; private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_NADA = 1; private final int VIEW_TYPE_NADA = 1;
private final int VIEW_TYPE_MARK_READ = 2; private final int VIEW_TYPE_MARK_READ = 2;
UnreadAdapter(Context context, @NonNull List<TopicSummary> topicSummaryList, BaseFragment.FragmentInteractionListener listener) { UnreadAdapter(Context context, @NonNull List<TopicSummary> topicSummaryList,
BaseFragment.FragmentInteractionListener listener,
MarkReadInteractionListener markReadInteractionListener) {
this.context = context; this.context = context;
this.unreadList = topicSummaryList; this.unreadList = topicSummaryList;
mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener; mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener;
markReadListener = markReadInteractionListener;
} }
@Override @Override
@ -54,7 +58,7 @@ class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} }
@Override @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof UnreadAdapter.EmptyViewHolder) { if (holder instanceof UnreadAdapter.EmptyViewHolder) {
final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder; final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder;
emptyViewHolder.text.setText(unreadList.get(position).getDateTimeModified()); emptyViewHolder.text.setText(unreadList.get(position).getDateTimeModified());
@ -70,14 +74,11 @@ class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
viewHolder.mView.setOnClickListener(new View.OnClickListener() { viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (null != mListener) { if (null != mListener) {
// Notify the active callbacks interface (the activity, if the // Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected. // fragment is attached to one) that an item has been selected.
mListener.onUnreadFragmentInteraction(viewHolder.topic); //? mListener.onUnreadFragmentInteraction(viewHolder.topic); //?
} }
} }
}); });
} else if (holder instanceof UnreadAdapter.MarkReadViewHolder) { } else if (holder instanceof UnreadAdapter.MarkReadViewHolder) {
@ -88,11 +89,10 @@ class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
markReadViewHolder.mView.setOnClickListener(new View.OnClickListener() { markReadViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (null != mListener) { if (null != mListener) {
// Notify the active callbacks interface (the activity, if the // Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected. // fragment is attached to one) that an item has been selected.
mListener.onUnreadFragmentInteraction(markReadViewHolder.topic); markReadListener.onMarkReadInteraction(unreadList.get(position).getTopicUrl());
} }
} }
}); });
@ -140,4 +140,8 @@ class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
text = (TextView) view.findViewById(R.id.mark_read); text = (TextView) view.findViewById(R.id.mark_read);
} }
} }
public interface MarkReadInteractionListener {
void onMarkReadInteraction(String markReadLinkUrl);
}
} }

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

@ -5,17 +5,18 @@ import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -27,6 +28,7 @@ import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.ParseTask; import gr.thmmy.mthmmy.utils.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.exceptions.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.Request;
import timber.log.Timber; import timber.log.Timber;
/** /**
@ -49,6 +51,7 @@ public class UnreadFragment extends BaseFragment {
private List<TopicSummary> topicSummaries; private List<TopicSummary> topicSummaries;
private UnreadTask unreadTask; private UnreadTask unreadTask;
private MarkReadTask markReadTask;
// Required empty public constructor // Required empty public constructor
public UnreadFragment() { public UnreadFragment() {
@ -81,8 +84,8 @@ public class UnreadFragment extends BaseFragment {
if (topicSummaries.isEmpty()) { if (topicSummaries.isEmpty()) {
unreadTask = new UnreadTask(); unreadTask = new UnreadTask();
unreadTask.execute(SessionManager.unreadUrl.toString()); unreadTask.execute(SessionManager.unreadUrl.toString());
} }
markReadTask = new MarkReadTask();
Timber.d("onActivityCreated"); Timber.d("onActivityCreated");
} }
@ -96,7 +99,16 @@ public class UnreadFragment extends BaseFragment {
// Set the adapter // Set the adapter
if (rootView instanceof RelativeLayout) { if (rootView instanceof RelativeLayout) {
progressBar = (MaterialProgressBar) rootView.findViewById(R.id.progressBar); progressBar = (MaterialProgressBar) rootView.findViewById(R.id.progressBar);
unreadAdapter = new UnreadAdapter(getActivity(), topicSummaries, fragmentInteractionListener); unreadAdapter = new UnreadAdapter(getActivity(), topicSummaries,
fragmentInteractionListener, new UnreadAdapter.MarkReadInteractionListener() {
@Override
public void onMarkReadInteraction(String markReadLinkUrl) {
if (markReadTask != null && markReadTask.getStatus() != AsyncTask.Status.RUNNING) {
markReadTask = new MarkReadTask();
markReadTask.execute(markReadLinkUrl);
}
}
});
CustomRecyclerView recyclerView = (CustomRecyclerView) rootView.findViewById(R.id.list); CustomRecyclerView recyclerView = (CustomRecyclerView) rootView.findViewById(R.id.list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.findViewById(R.id.list).getContext()); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.findViewById(R.id.list).getContext());
@ -129,9 +141,10 @@ public class UnreadFragment extends BaseFragment {
super.onDestroy(); super.onDestroy();
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING)
unreadTask.cancel(true); unreadTask.cancel(true);
if (markReadTask != null && markReadTask.getStatus() != AsyncTask.Status.RUNNING)
markReadTask.cancel(true);
} }
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { public interface UnreadFragmentInteractionListener extends FragmentInteractionListener {
void onUnreadFragmentInteraction(TopicSummary topicSummary); void onUnreadFragmentInteraction(TopicSummary topicSummary);
} }
@ -147,7 +160,6 @@ public class UnreadFragment extends BaseFragment {
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)");
if (!unread.isEmpty()) { if (!unread.isEmpty()) {
topicSummaries.clear(); topicSummaries.clear();
Log.d("UnreadFragment", unread.html());
for (Element row : unread) { for (Element row : unread) {
Elements information = row.select("td"); Elements information = row.select("td");
String link = information.last().select("a").first().attr("href"); String link = information.last().select("a").first().attr("href");
@ -180,7 +192,6 @@ public class UnreadFragment extends BaseFragment {
} }
} }
@Override @Override
protected void postParsing(ParseTask.ResultCode result) { protected void postParsing(ParseTask.ResultCode result) {
if (result == ResultCode.SUCCESS) if (result == ResultCode.SUCCESS)
@ -189,6 +200,52 @@ public class UnreadFragment extends BaseFragment {
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
} }
}
private class MarkReadTask extends AsyncTask<String, Void, Integer> {
private static final int SUCCESS = 0;
private static final int NETWORK_ERROR = 1;
private static final int OTHER_ERROR = 2;
@Override
protected void onPreExecute() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
protected Integer doInBackground(String... strings) {
Request request = new Request.Builder()
.url(strings[0])
.build();
try {
client.newCall(request).execute();
return SUCCESS;
} catch (IOException e) {
Timber.i(e, "IO Exception");
return NETWORK_ERROR;
} catch (Exception e) {
Timber.e(e, "Exception");
return OTHER_ERROR;
}
}
@Override
protected void onPostExecute(Integer result) {
progressBar.setVisibility(ProgressBar.GONE);
if (result == NETWORK_ERROR) {
Toast.makeText(getContext()
, "Task was unsuccessful!\n Please check your internet conneciton.",
Toast.LENGTH_LONG).show();
} else if (result == OTHER_ERROR) {
Toast.makeText(getContext()
, "Fatal error!\n Task aborted...", Toast.LENGTH_LONG).show();
} else {
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) {
unreadTask = new UnreadTask();
unreadTask.execute(SessionManager.unreadUrl.toString());
}
}
}
} }
} }

Loading…
Cancel
Save