From 8416ccea0c80dc998248e803c7366466e3670afd Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Thu, 11 Jul 2019 22:29:59 +0300 Subject: [PATCH] finish setting up the recyclerview --- .../activities/inbox/InboxActivity.java | 31 +++++++++++++++++-- .../mthmmy/activities/inbox/InboxAdapter.java | 3 +- .../activities/inbox/tasks/InboxTask.java | 7 ++--- .../mthmmy/viewmodel/InboxViewModel.java | 9 ++++-- 4 files changed, 40 insertions(+), 10 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 fba3b5fd..a78091bc 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 @@ -1,16 +1,28 @@ package gr.thmmy.mthmmy.activities.inbox; import android.os.Bundle; +import android.view.View; +import android.widget.ProgressBar; import androidx.lifecycle.ViewModelProviders; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; +import gr.thmmy.mthmmy.utils.ExternalAsyncTask; +import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.viewmodel.InboxViewModel; +import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import timber.log.Timber; public class InboxActivity extends BaseActivity { - InboxViewModel inboxViewModel; + private InboxViewModel inboxViewModel; + + private MaterialProgressBar progressBar; + private RecyclerView pmRecyclerview; + private InboxAdapter inboxAdapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,13 +41,26 @@ public class InboxActivity extends BaseActivity { createDrawer(); drawer.setSelection(INBOX_ID); + progressBar = findViewById(R.id.progress_bar); + pmRecyclerview = findViewById(R.id.inbox_recyclerview); + LinearLayoutManager layoutManager = new LinearLayoutManager(this); + pmRecyclerview.setLayoutManager(layoutManager); + inboxAdapter = new InboxAdapter(this); + pmRecyclerview.setAdapter(inboxAdapter); + inboxViewModel = ViewModelProviders.of(this).get(InboxViewModel.class); subscribeUI(); + + inboxViewModel.loadInbox(); } private void subscribeUI() { - inboxViewModel.setOnInboxTaskFinishedListener((resultCode, data) -> { - + inboxViewModel.setOnInboxTaskStartedListener(() -> progressBar.setVisibility(View.VISIBLE)); + inboxViewModel.setOnInboxTaskFinishedListener((resultCode, inbox) -> { + progressBar.setVisibility(View.INVISIBLE); + if (resultCode == NetworkResultCodes.SUCCESSFUL) { + inboxAdapter.notifyDataSetChanged(); + } }); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java index ed5a1419..c8a9ae74 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java @@ -52,6 +52,7 @@ public class InboxAdapter extends RecyclerView.Adapter private InboxViewModel inboxViewModel; public InboxAdapter(InboxActivity context) { + this.context = context; inboxViewModel = ViewModelProviders.of(context).get(InboxViewModel.class); } @@ -93,7 +94,7 @@ public class InboxAdapter extends RecyclerView.Adapter @Override public int getItemCount() { - return pms().size(); + return inboxViewModel.getInbox() == null ? 0 : pms().size(); } private ArrayList pms() { 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 de0db82a..29408092 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 @@ -66,8 +66,7 @@ public class InboxTask extends NewParseTask { .first().parent().attr("href")); pm.setReplyUrl(pmContainer.select("img[src=https://www.thmmy.gr/smf/Themes/scribbles2_114/images/buttons/im_reply.gif]") .first().parent().attr("href")); - pm.setDeleteUrl(pmContainer.select("img[src=https://www.thmmy.gr/smf/index.php?actio" + - "n=pm;sa=pmactions;pm_actions[321639]=delete;f=inbox;start=45;;sesc=07776660021fecb42ad23f8c5dba6aff]") + pm.setDeleteUrl(pmContainer.select("img[src=https://www.thmmy.gr/smf/Themes/scribbles2_114/images/buttons/delete.gif]") .first().parent().attr("href")); // language specific parsing @@ -85,7 +84,7 @@ public class InboxTask extends NewParseTask { } else { isAuthorDeleted = false; pm.setAuthor(username.html()); - pm.setAuthor(username.attr("href")); + pm.setAuthorProfileUrl(username.attr("href")); } String date = dateContainer.text(); @@ -105,7 +104,7 @@ public class InboxTask extends NewParseTask { } else { isAuthorDeleted = false; pm.setAuthor(username.html()); - pm.setAuthor(username.attr("href")); + pm.setAuthorProfileUrl(username.attr("href")); } String date = dateContainer.text(); 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 18b33919..7214658c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java @@ -12,9 +12,11 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask private Inbox inbox; private InboxTask.OnNetworkTaskFinishedListener onInboxTaskFinishedListener; + private InboxTask.OnTaskStartedListener onInboxTaskStartedListener; - private void loadInbox() { + public void loadInbox() { currentInboxTask = new InboxTask(); + currentInboxTask.setOnTaskStartedListener(onInboxTaskStartedListener); currentInboxTask.setOnNetworkTaskFinishedListener(this); currentInboxTask.execute(INBOX_URL); } @@ -23,6 +25,10 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask this.onInboxTaskFinishedListener = onInboxTaskFinishedListener; } + public void setOnInboxTaskStartedListener(InboxTask.OnTaskStartedListener onInboxTaskStartedListener) { + this.onInboxTaskStartedListener = onInboxTaskStartedListener; + } + @Override public void onNetworkTaskFinished(int resultCode, Inbox inbox) { this.inbox = inbox; @@ -30,7 +36,6 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask } public Inbox getInbox() { - if (inbox == null) throw new NullPointerException("Inbox has not been loaded yet"); return inbox; } }