Browse Source

finish setting up the recyclerview

pms
Thodoris1999 6 years ago
parent
commit
8416ccea0c
  1. 31
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java
  2. 3
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java
  3. 7
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java
  4. 9
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java

31
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();
}
});
}
}

3
app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java

@ -52,6 +52,7 @@ public class InboxAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
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<RecyclerView.ViewHolder>
@Override
public int getItemCount() {
return pms().size();
return inboxViewModel.getInbox() == null ? 0 : pms().size();
}
private ArrayList<PM> pms() {

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

@ -66,8 +66,7 @@ public class InboxTask extends NewParseTask<Inbox> {
.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<Inbox> {
} 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<Inbox> {
} else {
isAuthorDeleted = false;
pm.setAuthor(username.html());
pm.setAuthor(username.attr("href"));
pm.setAuthorProfileUrl(username.attr("href"));
}
String date = dateContainer.text();

9
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<Inbox> 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;
}
}

Loading…
Cancel
Save