From 2ef308b103ae407a81a35892bbb7b36747ee276a Mon Sep 17 00:00:00 2001 From: Thodoris Tyrovouzis Date: Tue, 31 Mar 2020 18:01:52 +0300 Subject: [PATCH] add ability to browse inbox --- .../activities/inbox/InboxActivity.java | 24 +++++++- .../activities/inbox/tasks/InboxTask.java | 4 +- .../pagination/BottomPaginationView.java | 4 +- .../mthmmy/utils/parsing/ParseHelpers.java | 55 +++++++++++++++++++ .../mthmmy/viewmodel/InboxViewModel.java | 54 +++++++++++++++++- .../mthmmy/viewmodel/TopicViewModel.java | 1 + app/src/main/res/layout/activity_inbox.xml | 55 +------------------ 7 files changed, 137 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java index a6cea947..25b09935 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java @@ -3,6 +3,7 @@ package gr.thmmy.mthmmy.activities.inbox; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -14,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; +import gr.thmmy.mthmmy.pagination.BottomPaginationView; import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.viewmodel.InboxViewModel; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; @@ -26,6 +28,7 @@ public class InboxActivity extends BaseActivity { private MaterialProgressBar progressBar; private RecyclerView pmRecyclerview; private InboxAdapter inboxAdapter; + private BottomPaginationView bottomPagination; @Override protected void onCreate(Bundle savedInstanceState) { @@ -50,15 +53,20 @@ public class InboxActivity extends BaseActivity { pmRecyclerview.setLayoutManager(layoutManager); inboxAdapter = new InboxAdapter(this); pmRecyclerview.setAdapter(inboxAdapter); + bottomPagination = findViewById(R.id.bottom_pagination); - inboxViewModel =new ViewModelProvider(this).get(InboxViewModel.class); + inboxViewModel = new ViewModelProvider(this).get(InboxViewModel.class); + bottomPagination.setOnPageRequestedListener(inboxViewModel); subscribeUI(); inboxViewModel.loadInbox(); } private void subscribeUI() { - inboxViewModel.setOnInboxTaskStartedListener(() -> progressBar.setVisibility(View.VISIBLE)); + inboxViewModel.setOnInboxTaskStartedListener(() -> { + progressBar.setVisibility(View.VISIBLE); + Timber.d("inbox task started"); + }); inboxViewModel.setOnInboxTaskFinishedListener((resultCode, inbox) -> { progressBar.setVisibility(View.INVISIBLE); if (resultCode == NetworkResultCodes.SUCCESSFUL) { @@ -70,5 +78,17 @@ public class InboxActivity extends BaseActivity { finish(); } }); + inboxViewModel.setOnInboxTaskCancelledListener(() -> { + progressBar.setVisibility(ProgressBar.GONE); + Timber.d("inbox task cancelled"); + }); + inboxViewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { + if (pageIndicatorIndex == null) return; + bottomPagination.setIndicatedPageIndex(pageIndicatorIndex); + }); + inboxViewModel.getPageCount().observe(this, pageCount -> { + if (pageCount == null) return; + bottomPagination.setTotalPageCount(pageCount); + }); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java index 5e2f20d7..aef6623e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java @@ -27,8 +27,8 @@ public class InboxTask extends NewParseTask { ParseHelpers.Language language = ParseHelpers.Language.getLanguage(document); - inbox.setCurrentPageIndex(ParseHelpers.parseCurrentPageIndex(document, language)); - inbox.setNumberOfPages(ParseHelpers.parseNumberOfPages(document, inbox.getCurrentPageIndex(), language)); + inbox.setCurrentPageIndex(ParseHelpers.parseCurrentPageIndexInbox(document, language)); + inbox.setNumberOfPages(ParseHelpers.parseNumberOfPagesInbox(document, inbox.getCurrentPageIndex(), language)); ArrayList pmList = parsePMs(document, language); inbox.setPms(pmList); diff --git a/app/src/main/java/gr/thmmy/mthmmy/pagination/BottomPaginationView.java b/app/src/main/java/gr/thmmy/mthmmy/pagination/BottomPaginationView.java index b4ea7f66..9accfeb7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/pagination/BottomPaginationView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/pagination/BottomPaginationView.java @@ -110,10 +110,10 @@ public class BottomPaginationView extends LinearLayout { public void run() { long REPEAT_DELAY = 250; if (autoIncrement) { - setIndicatedPageIndex(indicatorPageIndex+step); + incrementPageIndicator(step); repeatUpdateHandler.postDelayed(new BottomPaginationView.RepetitiveUpdater(step), REPEAT_DELAY); } else if (autoDecrement) { - setIndicatedPageIndex(indicatorPageIndex-step); + decrementPageIndicator(step); repeatUpdateHandler.postDelayed(new BottomPaginationView.RepetitiveUpdater(step), REPEAT_DELAY); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java index e16c41e2..7f9f67d1 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java @@ -247,6 +247,34 @@ public class ParseHelpers { return returnPages; } + public static int parseNumberOfPagesInbox(Document topic, int currentPage, ParseHelpers.Language language) { + int returnPages = 1; + + if (language == ParseHelpers.Language.GREEK) { + Elements pages = topic.select("div:contains(Σελίδες:)>a.navPages"); + + if (pages.size() != 0) { + returnPages = currentPage; + for (Element item : pages) { + if (Integer.parseInt(item.text()) > returnPages) + returnPages = Integer.parseInt(item.text()); + } + } + } else { + Elements pages = topic.select("div:contains(Pages:)>a.navPages"); + + if (pages.size() != 0) { + returnPages = currentPage; + for (Element item : pages) { + if (Integer.parseInt(item.text()) > returnPages) + returnPages = Integer.parseInt(item.text()); + } + } + } + + return returnPages; + } + /** * Returns current pages's page index. * @@ -284,6 +312,33 @@ public class ParseHelpers { return parsedPage; } + public static int parseCurrentPageIndexInbox(Document topic, ParseHelpers.Language language) { + int parsedPage = 1; + + if (language == ParseHelpers.Language.GREEK) { + Elements findCurrentPage = topic.select("div:contains(Σελίδες:)>b"); + + for (Element item : findCurrentPage) { + if (!item.text().contains("...") + && !item.text().contains("Σελίδες:")) { + parsedPage = Integer.parseInt(item.text()); + break; + } + } + } else { + Elements findCurrentPage = topic.select("div:contains(Pages:)>b"); + + for (Element item : findCurrentPage) { + if (!item.text().contains("...") && !item.text().contains("Pages:")) { + parsedPage = Integer.parseInt(item.text()); + break; + } + } + } + + return parsedPage; + } + /** * Method that replaces CloudFlare-obfuscated emails with deobfuscated ones * Replace Jsoup.parse with this wherever needed diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java index 9d6cad98..bf44098a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java @@ -1,5 +1,8 @@ package gr.thmmy.mthmmy.viewmodel; +import android.os.AsyncTask; + +import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import java.util.ArrayList; @@ -7,26 +10,52 @@ import java.util.ArrayList; import gr.thmmy.mthmmy.activities.inbox.tasks.InboxTask; import gr.thmmy.mthmmy.model.Inbox; import gr.thmmy.mthmmy.model.PM; +import gr.thmmy.mthmmy.pagination.BottomPaginationView; +import gr.thmmy.mthmmy.utils.ExternalAsyncTask; import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import timber.log.Timber; -public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTaskFinishedListener { +public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTaskFinishedListener, + BottomPaginationView.OnPageRequestedListener { private static final String INBOX_URL = "https://www.thmmy.gr/smf/index.php?action=pm"; /** * caches the expand/collapse state of the user extra info in the current page for the recyclerview */ private ArrayList userExtraInfoVisibile = new ArrayList<>(); + private MutableLiveData pageIndicatorIndex = new MutableLiveData<>(); + private MutableLiveData pageCount = new MutableLiveData<>(); + private InboxTask currentInboxTask; private Inbox inbox; private InboxTask.OnNetworkTaskFinishedListener onInboxTaskFinishedListener; private InboxTask.OnTaskStartedListener onInboxTaskStartedListener; + private InboxTask.OnTaskCancelledListener onInboxTaskCancelledListener; public void loadInbox() { + loadUrl(INBOX_URL); + } + + public void loadInboxPage(int index) { + loadUrl(INBOX_URL + ";f=inbox;sort=date;start=" + 15*(index-1)); + } + + public void loadUrl(String url) { + stopLoading(); currentInboxTask = new InboxTask(); currentInboxTask.setOnTaskStartedListener(onInboxTaskStartedListener); currentInboxTask.setOnNetworkTaskFinishedListener(this); - currentInboxTask.execute(INBOX_URL); + currentInboxTask.setOnTaskCancelledListener(onInboxTaskCancelledListener); + currentInboxTask.execute(url); + } + + public void stopLoading() { + if (currentInboxTask != null && currentInboxTask.getStatus() == AsyncTask.Status.RUNNING) { + Timber.i("Canceling inbox task"); + currentInboxTask.cancel(true); + onInboxTaskCancelledListener.onTaskCanceled(); + } } public void setOnInboxTaskFinishedListener(InboxTask.OnNetworkTaskFinishedListener onInboxTaskFinishedListener) { @@ -37,6 +66,12 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask this.onInboxTaskStartedListener = onInboxTaskStartedListener; } + @Override + public void onPageRequested(int index) { + pageIndicatorIndex.setValue(index); + loadInboxPage(index); + } + @Override public void onNetworkTaskFinished(int resultCode, Inbox inbox) { this.inbox = inbox; @@ -45,13 +80,28 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask userExtraInfoVisibile.clear(); for (PM pm : inbox.getPms()) userExtraInfoVisibile.add(false); + + pageIndicatorIndex.setValue(inbox.getCurrentPageIndex()); + pageCount.setValue(inbox.getNumberOfPages()); } } + public void setOnInboxTaskCancelledListener(InboxTask.OnTaskCancelledListener onInboxTaskCancelledListener) { + this.onInboxTaskCancelledListener = onInboxTaskCancelledListener; + } + public Inbox getInbox() { return inbox; } + public MutableLiveData getPageCount() { + return pageCount; + } + + public MutableLiveData getPageIndicatorIndex() { + return pageIndicatorIndex; + } + public boolean isUserExtraInfoVisible(int position) { return userExtraInfoVisibile.get(position); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index a4196cc5..f1b43462 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -267,6 +267,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa //sets Data if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) { currentPageIndex = result.getCurrentPageIndex(); + pageIndicatorIndex.setValue(result.getCurrentPageIndex()); pageCount.setValue(result.getPageCount()); topicTreeAndMods.setValue(result.getTopicTreeAndMods()); topicViewers.setValue(result.getTopicViewers()); diff --git a/app/src/main/res/layout/activity_inbox.xml b/app/src/main/res/layout/activity_inbox.xml index 6d39dee0..860b18a2 100644 --- a/app/src/main/res/layout/activity_inbox.xml +++ b/app/src/main/res/layout/activity_inbox.xml @@ -48,63 +48,14 @@ tools:listitem="@layout/activity_inbox_pm_row" /> - - - - - - - - - - - - + app:layout_behavior="gr.thmmy.mthmmy.utils.ScrollAwareLinearBehavior"/>