diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java index 2d415dd0..73adc1e4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadAdapter.java @@ -18,15 +18,19 @@ class UnreadAdapter extends RecyclerView.Adapter { private final Context context; private final List unreadList; private final UnreadFragment.UnreadFragmentInteractionListener mListener; + private final MarkReadInteractionListener markReadListener; private final int VIEW_TYPE_ITEM = 0; private final int VIEW_TYPE_NADA = 1; private final int VIEW_TYPE_MARK_READ = 2; - UnreadAdapter(Context context, @NonNull List topicSummaryList, BaseFragment.FragmentInteractionListener listener) { + UnreadAdapter(Context context, @NonNull List topicSummaryList, + BaseFragment.FragmentInteractionListener listener, + MarkReadInteractionListener markReadInteractionListener) { this.context = context; this.unreadList = topicSummaryList; mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener; + markReadListener = markReadInteractionListener; } @Override @@ -54,7 +58,7 @@ class UnreadAdapter extends RecyclerView.Adapter { } @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if (holder instanceof UnreadAdapter.EmptyViewHolder) { final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder; emptyViewHolder.text.setText(unreadList.get(position).getDateTimeModified()); @@ -70,14 +74,11 @@ class UnreadAdapter extends RecyclerView.Adapter { 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); //? - } - } }); } else if (holder instanceof UnreadAdapter.MarkReadViewHolder) { @@ -88,11 +89,10 @@ class UnreadAdapter extends RecyclerView.Adapter { markReadViewHolder.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(markReadViewHolder.topic); + markReadListener.onMarkReadInteraction(unreadList.get(position).getTopicUrl()); } } }); @@ -140,4 +140,8 @@ class UnreadAdapter extends RecyclerView.Adapter { text = (TextView) view.findViewById(R.id.mark_read); } } + + public interface MarkReadInteractionListener { + void onMarkReadInteraction(String markReadLinkUrl); + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 99954acd..5427fc9e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/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.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.RelativeLayout; +import android.widget.Toast; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import java.io.IOException; import java.util.ArrayList; 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.exceptions.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import okhttp3.Request; import timber.log.Timber; /** @@ -49,6 +51,7 @@ public class UnreadFragment extends BaseFragment { private List topicSummaries; private UnreadTask unreadTask; + private MarkReadTask markReadTask; // Required empty public constructor public UnreadFragment() { @@ -81,8 +84,8 @@ public class UnreadFragment extends BaseFragment { if (topicSummaries.isEmpty()) { unreadTask = new UnreadTask(); unreadTask.execute(SessionManager.unreadUrl.toString()); - } + markReadTask = new MarkReadTask(); Timber.d("onActivityCreated"); } @@ -96,7 +99,16 @@ public class UnreadFragment extends BaseFragment { // Set the adapter if (rootView instanceof RelativeLayout) { 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); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.findViewById(R.id.list).getContext()); @@ -129,9 +141,10 @@ public class UnreadFragment extends BaseFragment { super.onDestroy(); if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) unreadTask.cancel(true); + if (markReadTask != null && markReadTask.getStatus() != AsyncTask.Status.RUNNING) + markReadTask.cancel(true); } - public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { void onUnreadFragmentInteraction(TopicSummary topicSummary); } @@ -147,7 +160,6 @@ public class UnreadFragment extends BaseFragment { Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); if (!unread.isEmpty()) { topicSummaries.clear(); - Log.d("UnreadFragment", unread.html()); for (Element row : unread) { Elements information = row.select("td"); String link = information.last().select("a").first().attr("href"); @@ -180,7 +192,6 @@ public class UnreadFragment extends BaseFragment { } } - @Override protected void postParsing(ParseTask.ResultCode result) { if (result == ResultCode.SUCCESS) @@ -189,6 +200,52 @@ public class UnreadFragment extends BaseFragment { progressBar.setVisibility(ProgressBar.INVISIBLE); swipeRefreshLayout.setRefreshing(false); } + } + private class MarkReadTask extends AsyncTask { + 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()); + } + } + } } }