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 745469e8..c9568fb7 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 @@ -20,91 +20,50 @@ import timber.log.Timber; class UnreadAdapter extends RecyclerView.Adapter { 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(@NonNull List topicSummaryList, - BaseFragment.FragmentInteractionListener listener, - MarkReadInteractionListener markReadInteractionListener) { + BaseFragment.FragmentInteractionListener listener) { this.unreadList = topicSummaryList; mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener; - markReadListener = markReadInteractionListener; - } - - @Override - public int getItemViewType(int position) { - if (unreadList.get(position).getLastPostDateTime() == null) return VIEW_TYPE_MARK_READ; - return unreadList.get(position).getTopicUrl() == null ? VIEW_TYPE_NADA : VIEW_TYPE_ITEM; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull 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); - } else if (viewType == VIEW_TYPE_MARK_READ) { - View view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.fragment_unread_mark_read_row, parent, false); - return new MarkReadViewHolder(view); - } - return null; + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.fragment_unread_row, parent, false); + return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) { TopicSummary topicSummary = unreadList.get(holder.getAdapterPosition()); - if (holder instanceof UnreadAdapter.EmptyViewHolder) { - final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder; - emptyViewHolder.text.setText(topicSummary.getLastPostDateTime()); - } else if (holder instanceof UnreadAdapter.ViewHolder) { - final UnreadAdapter.ViewHolder viewHolder = (UnreadAdapter.ViewHolder) holder; + final UnreadAdapter.ViewHolder viewHolder = (UnreadAdapter.ViewHolder) holder; - viewHolder.mTitleView.setText(topicSummary.getSubject()); - if(BaseApplication.getInstance().isDisplayRelativeTimeEnabled()){ - String timestamp = topicSummary.getLastPostTimestamp(); - try{ - viewHolder.mDateTimeView.setReferenceTime(Long.valueOf(timestamp)); - } - catch(NumberFormatException e){ - Timber.e(e, "Invalid number format: %s", timestamp); - viewHolder.mDateTimeView.setText(topicSummary.getLastPostSimplifiedDateTime()); - } + viewHolder.mTitleView.setText(topicSummary.getSubject()); + if(BaseApplication.getInstance().isDisplayRelativeTimeEnabled()){ + String timestamp = topicSummary.getLastPostTimestamp(); + try{ + viewHolder.mDateTimeView.setReferenceTime(Long.valueOf(timestamp)); } - else + catch(NumberFormatException e){ + Timber.e(e, "Invalid number format: %s", timestamp); viewHolder.mDateTimeView.setText(topicSummary.getLastPostSimplifiedDateTime()); + } + } + else + viewHolder.mDateTimeView.setText(topicSummary.getLastPostSimplifiedDateTime()); - viewHolder.mUserView.setText(topicSummary.getLastUser()); - viewHolder.topic = topicSummary; - - viewHolder.mView.setOnClickListener(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) { - final UnreadAdapter.MarkReadViewHolder markReadViewHolder = (UnreadAdapter.MarkReadViewHolder) holder; - markReadViewHolder.text.setText(unreadList.get(holder.getAdapterPosition()).getSubject()); - markReadViewHolder.topic = unreadList.get(holder.getAdapterPosition()); + viewHolder.mUserView.setText(topicSummary.getLastUser()); + viewHolder.topic = topicSummary; - markReadViewHolder.mView.setOnClickListener(v -> { - if (null != mListener) { - // Notify the active callbacks interface (the activity, if the - // fragment is attached to one) that an item has been selected. - markReadListener.onMarkReadInteraction(unreadList.get(holder.getAdapterPosition()).getTopicUrl()); - } - }); - } + viewHolder.mView.setOnClickListener(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 @@ -127,29 +86,4 @@ class UnreadAdapter extends RecyclerView.Adapter { mDateTimeView = view.findViewById(R.id.dateTime); } } - - private static class EmptyViewHolder extends RecyclerView.ViewHolder { - final TextView text; - - EmptyViewHolder(View view) { - super(view); - text = view.findViewById(R.id.text); - } - } - - private static class MarkReadViewHolder extends RecyclerView.ViewHolder { - final View mView; - final TextView text; - public TopicSummary topic; - - MarkReadViewHolder(View view) { - super(view); - mView = view; - text = view.findViewById(R.id.mark_read); - } - } - - 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 cafbb996..ffb0ede4 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 @@ -7,14 +7,18 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; -import android.widget.RelativeLayout; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; +import com.google.android.material.floatingactionbutton.FloatingActionButton; + import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; @@ -50,9 +54,13 @@ public class UnreadFragment extends BaseFragment { private MaterialProgressBar progressBar; private SwipeRefreshLayout swipeRefreshLayout; + private FloatingActionButton markAsReadFAB; + private TextView noUnreadTopicsTextView; + private UnreadAdapter unreadAdapter; private List topicSummaries; + private String markAsReadUrl = ""; private int numberOfPages = 0; private int loadedPages = 0; @@ -103,15 +111,21 @@ public class UnreadFragment extends BaseFragment { final View rootView = inflater.inflate(R.layout.fragment_unread, container, false); // Set the adapter - if (rootView instanceof RelativeLayout) { + if (rootView instanceof CoordinatorLayout) { progressBar = rootView.findViewById(R.id.progressBar); - unreadAdapter = new UnreadAdapter(topicSummaries, - fragmentInteractionListener, markReadLinkUrl -> { - if (!markReadTask.isRunning() && !unreadTask.isRunning()) { - markReadTask = new MarkReadTask(); - markReadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, markReadLinkUrl); - } - }); + noUnreadTopicsTextView = rootView.findViewById(R.id.no_unread_topics); + markAsReadFAB = rootView.findViewById(R.id.unread_fab); + + if(topicSummaries.isEmpty()){ + hideMarkAsReadFAB(); + noUnreadTopicsTextView.setVisibility(View.VISIBLE); + } + else{ + noUnreadTopicsTextView.setVisibility(View.INVISIBLE); + showMarkAsReadFAB(); + } + + unreadAdapter = new UnreadAdapter(topicSummaries, fragmentInteractionListener); CustomRecyclerView recyclerView = rootView.findViewById(R.id.list); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext()); @@ -155,20 +169,56 @@ public class UnreadFragment extends BaseFragment { void onUnreadFragmentInteraction(TopicSummary topicSummary); } + private void showMarkAsReadFAB() { + markAsReadFAB.setOnClickListener(v -> { + if (!markReadTask.isRunning() && !unreadTask.isRunning()) + showMarkAsReadConfirmationDialog(); + }); + markAsReadFAB.show(); + markAsReadFAB.setTag(true); + } + + private void hideMarkAsReadFAB() { + markAsReadFAB.setOnClickListener(null); + markAsReadFAB.hide(); + markAsReadFAB.setTag(false); + } + + private void showMarkAsReadConfirmationDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle); + builder.setTitle("Mark all as read"); + builder.setMessage("Are you sure that you want to mark ALL topics as read?"); + builder.setPositiveButton("Yep", (dialogInterface, i) -> { + markReadTask = new MarkReadTask(); + markReadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, markAsReadUrl); + }); + builder.setNegativeButton("Nope", (dialogInterface, i) -> {}); + builder.create().show(); + } + //---------------------------------------ASYNC TASK----------------------------------- private void onUnreadTaskStarted() { progressBar.setVisibility(ProgressBar.VISIBLE); } - private void onUnreadTaskFinished(int resultCode, ArrayList fetchedUnread) { + private void onUnreadTaskFinished(int resultCode, UnreadTaskData unreadTaskData) { if (resultCode == NetworkResultCodes.SUCCESSFUL) { - if(fetchedUnread!=null && !fetchedUnread.isEmpty()){ + ArrayList fetchedUnread = unreadTaskData.getTopicSummaries(); + if(!fetchedUnread.isEmpty()){ if(loadedPages==0) topicSummaries.clear(); topicSummaries.addAll(fetchedUnread); - unreadAdapter.notifyDataSetChanged(); + markAsReadUrl = unreadTaskData.getMarkAsReadUrl(); + noUnreadTopicsTextView.setVisibility(View.INVISIBLE); + showMarkAsReadFAB(); + } + else { + topicSummaries.clear(); + hideMarkAsReadFAB(); + noUnreadTopicsTextView.setVisibility(View.VISIBLE); } + unreadAdapter.notifyDataSetChanged(); loadedPages++; if (loadedPages < numberOfPages) { unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); @@ -191,18 +241,17 @@ public class UnreadFragment extends BaseFragment { } } - private class UnreadTask extends NewParseTask> { - - UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener> onParseTaskFinishedListener) { + private class UnreadTask extends NewParseTask { + UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onParseTaskFinishedListener) { super(onTaskStartedListener, onParseTaskFinishedListener); } @Override - protected ArrayList parse(Document document, Response response) throws ParseException { + protected UnreadTaskData parse(Document document, Response response) throws ParseException { Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); ArrayList fetchedTopicSummaries = new ArrayList<>(); + String markAsReadUrl=""; if (!unread.isEmpty()) { - //topicSummaries.clear(); for (Element row : unread) { Elements information = row.select("td"); String link = information.last().select("a").first().attr("href"); @@ -222,7 +271,6 @@ public class UnreadFragment extends BaseFragment { Element pagesElement = null, markRead = null; if (topBar != null) { pagesElement = topBar.select("td.middletext").first(); - markRead = document.select("table:not(.bordercolor):not([width])").select("a") .first(); } @@ -236,26 +284,38 @@ public class UnreadFragment extends BaseFragment { } if (markRead != null && loadedPages == numberOfPages - 1) - fetchedTopicSummaries.add(new TopicSummary(markRead.attr("href"), markRead.text(), null, - null)); - } 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 = "Δεν υπάρχουν μη αναγνωσμένα μηνύματα!"; - } - fetchedTopicSummaries.add(new TopicSummary(null, null, null, message)); + markAsReadUrl = markRead.attr("href"); + + return new UnreadTaskData(fetchedTopicSummaries, markAsReadUrl); } - return fetchedTopicSummaries; + + return new UnreadTaskData(new ArrayList<>(), markAsReadUrl); } @Override - protected int getResultCode(Response response, ArrayList data) { + protected int getResultCode(Response response, UnreadTaskData data) { return NetworkResultCodes.SUCCESSFUL; } } + private class UnreadTaskData { + ArrayList topicSummaries; + String markAsReadUrl; + + UnreadTaskData(ArrayList topicSummaries, String markAsReadUrl){ + this.topicSummaries = topicSummaries; + this.markAsReadUrl = markAsReadUrl; + } + + ArrayList getTopicSummaries() { + return topicSummaries; + } + + String getMarkAsReadUrl() { + return markAsReadUrl; + } + } + private class MarkReadTask extends AsyncTask { private static final int SUCCESS = 0; private static final int NETWORK_ERROR = 1; diff --git a/app/src/main/res/drawable/ic_mark_as_read.xml b/app/src/main/res/drawable/ic_mark_as_read.xml new file mode 100644 index 00000000..04b4b785 --- /dev/null +++ b/app/src/main/res/drawable/ic_mark_as_read.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/fragment_unread.xml b/app/src/main/res/layout/fragment_unread.xml index fc412190..41d018c5 100644 --- a/app/src/main/res/layout/fragment_unread.xml +++ b/app/src/main/res/layout/fragment_unread.xml @@ -1,28 +1,45 @@ - + android:layout_height="match_parent" + android:background="@color/background"> - - - + android:layout_height="match_parent"> + + + + + + - \ No newline at end of file + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_unread_empty_row.xml b/app/src/main/res/layout/fragment_unread_empty_row.xml deleted file mode 100644 index d5394258..00000000 --- a/app/src/main/res/layout/fragment_unread_empty_row.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_unread_mark_read_row.xml b/app/src/main/res/layout/fragment_unread_mark_read_row.xml deleted file mode 100644 index c825118c..00000000 --- a/app/src/main/res/layout/fragment_unread_mark_read_row.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c87f02e9..b0cae84b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,6 +23,7 @@ Unread Shoutbox Refresh + No unread topics! thmmy.gr