From 3f1cce8d00fadd86e65c2137171ea72dfbb44215 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Mon, 30 Jul 2018 13:54:16 +0300 Subject: [PATCH] Unread tab fetches all pages, More fixes for vector drawables --- .../activities/main/unread/UnreadAdapter.java | 10 ++-- .../main/unread/UnreadFragment.java | 59 +++++++++++++++---- .../activities/topic/TopicActivity.java | 9 ++- .../mthmmy/activities/topic/TopicAdapter.java | 9 ++- .../activities/upload/UploadActivity.java | 4 ++ .../layout/activity_topic_overflow_menu.xml | 2 - app/src/main/res/layout/activity_upload.xml | 1 - 7 files changed, 69 insertions(+), 25 deletions(-) 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 4b7c235b..65100922 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 @@ -1,6 +1,5 @@ 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; @@ -15,7 +14,6 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; class UnreadAdapter extends RecyclerView.Adapter { - private final Context context; private final List unreadList; private final UnreadFragment.UnreadFragmentInteractionListener mListener; private final MarkReadInteractionListener markReadListener; @@ -24,10 +22,9 @@ class UnreadAdapter extends RecyclerView.Adapter { private final int VIEW_TYPE_NADA = 1; private final int VIEW_TYPE_MARK_READ = 2; - UnreadAdapter(Context context, @NonNull List topicSummaryList, + UnreadAdapter(@NonNull List topicSummaryList, BaseFragment.FragmentInteractionListener listener, MarkReadInteractionListener markReadInteractionListener) { - this.context = context; this.unreadList = topicSummaryList; mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener; markReadListener = markReadInteractionListener; @@ -39,8 +36,9 @@ class UnreadAdapter extends RecyclerView.Adapter { return unreadList.get(position).getTopicUrl() == null ? VIEW_TYPE_NADA : VIEW_TYPE_ITEM; } + @NonNull @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + 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); @@ -58,7 +56,7 @@ class UnreadAdapter extends RecyclerView.Adapter { } @Override - public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { + public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) { if (holder instanceof UnreadAdapter.EmptyViewHolder) { final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder; emptyViewHolder.text.setText(unreadList.get(holder.getAdapterPosition()).getDateTimeModified()); 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 98be97e2..33025dfa 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 @@ -2,9 +2,11 @@ package gr.thmmy.mthmmy.activities.main.unread; import android.os.AsyncTask; import android.os.Bundle; +import android.support.annotation.NonNull; 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; @@ -25,7 +27,6 @@ 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.parsing.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.Request; @@ -42,13 +43,14 @@ import timber.log.Timber; 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 topicSummaries; + private int numberOfPages = 0; + private int loadedPages = 0; private UnreadTask unreadTask; private MarkReadTask markReadTask; @@ -83,6 +85,7 @@ public class UnreadFragment extends BaseFragment { super.onActivityCreated(savedInstanceState); if (topicSummaries.isEmpty()) { unreadTask = new UnreadTask(); + assert SessionManager.unreadUrl != null; unreadTask.execute(SessionManager.unreadUrl.toString()); } markReadTask = new MarkReadTask(); @@ -91,7 +94,7 @@ public class UnreadFragment extends BaseFragment { @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View rootView = inflater.inflate(R.layout.fragment_unread, container, false); @@ -99,7 +102,7 @@ public class UnreadFragment extends BaseFragment { // Set the adapter if (rootView instanceof RelativeLayout) { progressBar = rootView.findViewById(R.id.progressBar); - unreadAdapter = new UnreadAdapter(getActivity(), topicSummaries, + unreadAdapter = new UnreadAdapter(topicSummaries, fragmentInteractionListener, new UnreadAdapter.MarkReadInteractionListener() { @Override public void onMarkReadInteraction(String markReadLinkUrl) { @@ -126,7 +129,11 @@ public class UnreadFragment extends BaseFragment { @Override public void onRefresh() { if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { + topicSummaries.clear(); + numberOfPages = 0; + loadedPages = 0; unreadTask = new UnreadTask(); + assert SessionManager.unreadUrl != null; unreadTask.execute(SessionManager.unreadUrl.toString()); } } @@ -158,10 +165,10 @@ public class UnreadFragment extends BaseFragment { } @Override - public void parse(Document document) throws ParseException { + public void parse(Document document) { Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); if (!unread.isEmpty()) { - topicSummaries.clear(); + //topicSummaries.clear(); for (Element row : unread) { Elements information = row.select("td"); String link = information.last().select("a").first().attr("href"); @@ -178,7 +185,7 @@ public class UnreadFragment extends BaseFragment { dateTime.contains(" πμ") || dateTime.contains(" μμ")) { dateTime = dateTime.replaceAll(":[0-5][0-9] ", " "); } else { - dateTime=dateTime.substring(0,dateTime.lastIndexOf(":")); + dateTime = dateTime.substring(0, dateTime.lastIndexOf(":")); } if (!dateTime.contains(",")) { dateTime = dateTime.replaceAll(".+? ([0-9])", "$1"); @@ -186,9 +193,26 @@ public class UnreadFragment extends BaseFragment { topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime)); } - Element markRead = document.select("table:not(.bordercolor):not([width])").select("a") - .first(); - if (markRead != null) + Element topBar = document.select("table:not(.bordercolor):not(#bodyarea):has(td.middletext)").first(); + + 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(); + } + + if (numberOfPages == 0 && pagesElement != null) { + Elements pages = pagesElement.select("a"); + if (!pages.isEmpty()) { + numberOfPages = Integer.parseInt(pages.last().text()); + } else { + numberOfPages = 1; + } + } + + if (markRead != null && loadedPages == numberOfPages - 1) topicSummaries.add(new TopicSummary(markRead.attr("href"), markRead.text(), null, null)); } else { @@ -205,11 +229,19 @@ public class UnreadFragment extends BaseFragment { @Override protected void postExecution(ParseTask.ResultCode result) { - if (result == ResultCode.SUCCESS) + if (result == ResultCode.SUCCESS) { unreadAdapter.notifyDataSetChanged(); - progressBar.setVisibility(ProgressBar.INVISIBLE); - swipeRefreshLayout.setRefreshing(false); + ++loadedPages; + if (loadedPages < numberOfPages) { + unreadTask = new UnreadTask(); + assert SessionManager.unreadUrl != null; + unreadTask.execute(SessionManager.unreadUrl.toString() + ";start=" + loadedPages * 20); + } + + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + } } } @@ -254,6 +286,7 @@ public class UnreadFragment extends BaseFragment { } else { if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { unreadTask = new UnreadTask(); + assert SessionManager.unreadUrl != null; unreadTask.execute(SessionManager.unreadUrl.toString()); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 3ff841f6..855f7045 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -203,11 +203,16 @@ public class TopicActivity extends BaseActivity { boolean includeAppSignaturePreference = true; + //Fix for vector drawables on android <21 + static { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - //Fix for vector drawables on android <21 - AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); setContentView(R.layout.activity_topic); Bundle extras = getIntent().getExtras(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 520da629..dcf448be 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -7,12 +7,14 @@ import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AlertDialog; +import android.support.v7.content.res.AppCompatResources; import android.support.v7.widget.AppCompatImageButton; import android.support.v7.widget.RecyclerView; import android.text.Editable; @@ -434,7 +436,10 @@ class TopicAdapter extends RecyclerView.Adapter { popUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT); popUp.setFocusable(true); - popUpContent.findViewById(R.id.post_share_button).setOnClickListener(new View.OnClickListener() { + TextView shareButton = popUpContent.findViewById(R.id.post_share_button); + Drawable shareStartDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_share_white_24dp); + shareButton.setCompoundDrawablesRelativeWithIntrinsicBounds(shareStartDrawable, null, null, null); + shareButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); @@ -450,6 +455,8 @@ class TopicAdapter extends RecyclerView.Adapter { if (currentPost.getPostDeleteURL() == null || currentPost.getPostDeleteURL().equals("")) { deletePostButton.setVisibility(View.GONE); } else { + Drawable deleteStartDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_delete_white_24dp); + deletePostButton.setCompoundDrawablesRelativeWithIntrinsicBounds(deleteStartDrawable, null, null, null); popUpContent.findViewById(R.id.delete_post).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java index a6556cc3..727b59b9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java @@ -6,6 +6,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; @@ -14,6 +15,7 @@ import android.provider.OpenableColumns; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; +import android.support.v7.content.res.AppCompatResources; import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.AppCompatButton; import android.view.View; @@ -179,6 +181,8 @@ public class UploadActivity extends BaseActivity { uploadDescription = findViewById(R.id.upload_description); selectFileButton = findViewById(R.id.upload_select_file_button); + Drawable selectStartDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_insert_drive_file_white_24dp); + selectFileButton.setCompoundDrawablesRelativeWithIntrinsicBounds(selectStartDrawable, null, null, null); selectFileButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/app/src/main/res/layout/activity_topic_overflow_menu.xml b/app/src/main/res/layout/activity_topic_overflow_menu.xml index 43a8370c..361dac72 100644 --- a/app/src/main/res/layout/activity_topic_overflow_menu.xml +++ b/app/src/main/res/layout/activity_topic_overflow_menu.xml @@ -10,7 +10,6 @@ android:layout_height="35dp" android:background="?android:attr/selectableItemBackground" android:drawablePadding="5dp" - android:drawableStart="@drawable/ic_share_white_24dp" android:gravity="center_vertical" android:paddingBottom="6dp" android:paddingEnd="12dp" @@ -25,7 +24,6 @@ android:layout_height="35dp" android:background="?android:attr/selectableItemBackground" android:drawablePadding="5dp" - android:drawableStart="@drawable/ic_delete_white_24dp" android:gravity="center_vertical" android:paddingBottom="6dp" android:paddingEnd="12dp" diff --git a/app/src/main/res/layout/activity_upload.xml b/app/src/main/res/layout/activity_upload.xml index 0afd91ae..c80c3ada 100644 --- a/app/src/main/res/layout/activity_upload.xml +++ b/app/src/main/res/layout/activity_upload.xml @@ -123,7 +123,6 @@ android:layout_height="wrap_content" android:layout_gravity="center" android:drawablePadding="5dp" - android:drawableStart="@drawable/ic_insert_drive_file_white_24dp" android:gravity="center_vertical" android:text="@string/upload_select_file" android:textColor="@color/primary_text" />