Browse Source

add ability to browse inbox

pms
Thodoris Tyrovouzis 5 years ago
parent
commit
2ef308b103
  1. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java
  2. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/pagination/BottomPaginationView.java
  4. 55
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java
  5. 54
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java
  6. 1
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  7. 55
      app/src/main/res/layout/activity_inbox.xml

24
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.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -14,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.pagination.BottomPaginationView;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.viewmodel.InboxViewModel; import gr.thmmy.mthmmy.viewmodel.InboxViewModel;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -26,6 +28,7 @@ public class InboxActivity extends BaseActivity {
private MaterialProgressBar progressBar; private MaterialProgressBar progressBar;
private RecyclerView pmRecyclerview; private RecyclerView pmRecyclerview;
private InboxAdapter inboxAdapter; private InboxAdapter inboxAdapter;
private BottomPaginationView bottomPagination;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -50,15 +53,20 @@ public class InboxActivity extends BaseActivity {
pmRecyclerview.setLayoutManager(layoutManager); pmRecyclerview.setLayoutManager(layoutManager);
inboxAdapter = new InboxAdapter(this); inboxAdapter = new InboxAdapter(this);
pmRecyclerview.setAdapter(inboxAdapter); 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(); subscribeUI();
inboxViewModel.loadInbox(); inboxViewModel.loadInbox();
} }
private void subscribeUI() { private void subscribeUI() {
inboxViewModel.setOnInboxTaskStartedListener(() -> progressBar.setVisibility(View.VISIBLE)); inboxViewModel.setOnInboxTaskStartedListener(() -> {
progressBar.setVisibility(View.VISIBLE);
Timber.d("inbox task started");
});
inboxViewModel.setOnInboxTaskFinishedListener((resultCode, inbox) -> { inboxViewModel.setOnInboxTaskFinishedListener((resultCode, inbox) -> {
progressBar.setVisibility(View.INVISIBLE); progressBar.setVisibility(View.INVISIBLE);
if (resultCode == NetworkResultCodes.SUCCESSFUL) { if (resultCode == NetworkResultCodes.SUCCESSFUL) {
@ -70,5 +78,17 @@ public class InboxActivity extends BaseActivity {
finish(); 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);
});
} }
} }

4
app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java

@ -27,8 +27,8 @@ public class InboxTask extends NewParseTask<Inbox> {
ParseHelpers.Language language = ParseHelpers.Language.getLanguage(document); ParseHelpers.Language language = ParseHelpers.Language.getLanguage(document);
inbox.setCurrentPageIndex(ParseHelpers.parseCurrentPageIndex(document, language)); inbox.setCurrentPageIndex(ParseHelpers.parseCurrentPageIndexInbox(document, language));
inbox.setNumberOfPages(ParseHelpers.parseNumberOfPages(document, inbox.getCurrentPageIndex(), language)); inbox.setNumberOfPages(ParseHelpers.parseNumberOfPagesInbox(document, inbox.getCurrentPageIndex(), language));
ArrayList<PM> pmList = parsePMs(document, language); ArrayList<PM> pmList = parsePMs(document, language);
inbox.setPms(pmList); inbox.setPms(pmList);

4
app/src/main/java/gr/thmmy/mthmmy/pagination/BottomPaginationView.java

@ -110,10 +110,10 @@ public class BottomPaginationView extends LinearLayout {
public void run() { public void run() {
long REPEAT_DELAY = 250; long REPEAT_DELAY = 250;
if (autoIncrement) { if (autoIncrement) {
setIndicatedPageIndex(indicatorPageIndex+step); incrementPageIndicator(step);
repeatUpdateHandler.postDelayed(new BottomPaginationView.RepetitiveUpdater(step), REPEAT_DELAY); repeatUpdateHandler.postDelayed(new BottomPaginationView.RepetitiveUpdater(step), REPEAT_DELAY);
} else if (autoDecrement) { } else if (autoDecrement) {
setIndicatedPageIndex(indicatorPageIndex-step); decrementPageIndicator(step);
repeatUpdateHandler.postDelayed(new BottomPaginationView.RepetitiveUpdater(step), REPEAT_DELAY); repeatUpdateHandler.postDelayed(new BottomPaginationView.RepetitiveUpdater(step), REPEAT_DELAY);
} }
} }

55
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java

@ -247,6 +247,34 @@ public class ParseHelpers {
return returnPages; 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. * Returns current pages's page index.
* *
@ -284,6 +312,33 @@ public class ParseHelpers {
return parsedPage; 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 * Method that replaces CloudFlare-obfuscated emails with deobfuscated ones
* Replace Jsoup.parse with this wherever needed * Replace Jsoup.parse with this wherever needed

54
app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java

@ -1,5 +1,8 @@
package gr.thmmy.mthmmy.viewmodel; package gr.thmmy.mthmmy.viewmodel;
import android.os.AsyncTask;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import java.util.ArrayList; import java.util.ArrayList;
@ -7,26 +10,52 @@ import java.util.ArrayList;
import gr.thmmy.mthmmy.activities.inbox.tasks.InboxTask; import gr.thmmy.mthmmy.activities.inbox.tasks.InboxTask;
import gr.thmmy.mthmmy.model.Inbox; import gr.thmmy.mthmmy.model.Inbox;
import gr.thmmy.mthmmy.model.PM; 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 gr.thmmy.mthmmy.utils.NetworkResultCodes;
import timber.log.Timber;
public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTaskFinishedListener<Inbox> { public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTaskFinishedListener<Inbox>,
BottomPaginationView.OnPageRequestedListener {
private static final String INBOX_URL = "https://www.thmmy.gr/smf/index.php?action=pm"; 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 * caches the expand/collapse state of the user extra info in the current page for the recyclerview
*/ */
private ArrayList<Boolean> userExtraInfoVisibile = new ArrayList<>(); private ArrayList<Boolean> userExtraInfoVisibile = new ArrayList<>();
private MutableLiveData<Integer> pageIndicatorIndex = new MutableLiveData<>();
private MutableLiveData<Integer> pageCount = new MutableLiveData<>();
private InboxTask currentInboxTask; private InboxTask currentInboxTask;
private Inbox inbox; private Inbox inbox;
private InboxTask.OnNetworkTaskFinishedListener<Inbox> onInboxTaskFinishedListener; private InboxTask.OnNetworkTaskFinishedListener<Inbox> onInboxTaskFinishedListener;
private InboxTask.OnTaskStartedListener onInboxTaskStartedListener; private InboxTask.OnTaskStartedListener onInboxTaskStartedListener;
private InboxTask.OnTaskCancelledListener onInboxTaskCancelledListener;
public void loadInbox() { 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 = new InboxTask();
currentInboxTask.setOnTaskStartedListener(onInboxTaskStartedListener); currentInboxTask.setOnTaskStartedListener(onInboxTaskStartedListener);
currentInboxTask.setOnNetworkTaskFinishedListener(this); 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<Inbox> onInboxTaskFinishedListener) { public void setOnInboxTaskFinishedListener(InboxTask.OnNetworkTaskFinishedListener<Inbox> onInboxTaskFinishedListener) {
@ -37,6 +66,12 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask
this.onInboxTaskStartedListener = onInboxTaskStartedListener; this.onInboxTaskStartedListener = onInboxTaskStartedListener;
} }
@Override
public void onPageRequested(int index) {
pageIndicatorIndex.setValue(index);
loadInboxPage(index);
}
@Override @Override
public void onNetworkTaskFinished(int resultCode, Inbox inbox) { public void onNetworkTaskFinished(int resultCode, Inbox inbox) {
this.inbox = inbox; this.inbox = inbox;
@ -45,13 +80,28 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask
userExtraInfoVisibile.clear(); userExtraInfoVisibile.clear();
for (PM pm : inbox.getPms()) for (PM pm : inbox.getPms())
userExtraInfoVisibile.add(false); userExtraInfoVisibile.add(false);
pageIndicatorIndex.setValue(inbox.getCurrentPageIndex());
pageCount.setValue(inbox.getNumberOfPages());
} }
} }
public void setOnInboxTaskCancelledListener(InboxTask.OnTaskCancelledListener onInboxTaskCancelledListener) {
this.onInboxTaskCancelledListener = onInboxTaskCancelledListener;
}
public Inbox getInbox() { public Inbox getInbox() {
return inbox; return inbox;
} }
public MutableLiveData<Integer> getPageCount() {
return pageCount;
}
public MutableLiveData<Integer> getPageIndicatorIndex() {
return pageIndicatorIndex;
}
public boolean isUserExtraInfoVisible(int position) { public boolean isUserExtraInfoVisible(int position) {
return userExtraInfoVisibile.get(position); return userExtraInfoVisibile.get(position);
} }

1
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -267,6 +267,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
//sets Data //sets Data
if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) { if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) {
currentPageIndex = result.getCurrentPageIndex(); currentPageIndex = result.getCurrentPageIndex();
pageIndicatorIndex.setValue(result.getCurrentPageIndex());
pageCount.setValue(result.getPageCount()); pageCount.setValue(result.getPageCount());
topicTreeAndMods.setValue(result.getTopicTreeAndMods()); topicTreeAndMods.setValue(result.getTopicTreeAndMods());
topicViewers.setValue(result.getTopicViewers()); topicViewers.setValue(result.getTopicViewers());

55
app/src/main/res/layout/activity_inbox.xml

@ -48,63 +48,14 @@
tools:listitem="@layout/activity_inbox_pm_row" /> tools:listitem="@layout/activity_inbox_pm_row" />
</RelativeLayout> </RelativeLayout>
<LinearLayout <gr.thmmy.mthmmy.pagination.BottomPaginationView
android:id="@+id/bottom_navigation_bar" android:id="@+id/bottom_pagination"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:background="@color/primary" android:background="@color/primary"
app:elevation="8dp" app:elevation="8dp"
app:layout_behavior="gr.thmmy.mthmmy.utils.ScrollAwareLinearBehavior"> app:layout_behavior="gr.thmmy.mthmmy.utils.ScrollAwareLinearBehavior"/>
<ImageButton
android:id="@+id/page_first_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_first"
app:srcCompat="@drawable/page_first" />
<ImageButton
android:id="@+id/page_previous_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_previous"
app:srcCompat="@drawable/page_previous" />
<TextView
android:id="@+id/page_indicator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:hint="@string/button_page"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="22sp" />
<ImageButton
android:id="@+id/page_next_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_next"
app:srcCompat="@drawable/page_next" />
<ImageButton
android:id="@+id/page_last_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_last"
app:srcCompat="@drawable/page_last" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/send_pm_fab" android:id="@+id/send_pm_fab"

Loading…
Cancel
Save