Browse Source

create inbox viewmodel and bind the inbox task to Inbox Activity

pms
Thodoris1999 6 years ago
parent
commit
fd6ada0af6
  1. 14
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  3. 36
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java

14
app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxActivity.java

@ -2,11 +2,16 @@ package gr.thmmy.mthmmy.activities.inbox;
import android.os.Bundle; import android.os.Bundle;
import androidx.lifecycle.ViewModelProviders;
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.viewmodel.InboxViewModel;
public class InboxActivity extends BaseActivity { public class InboxActivity extends BaseActivity {
InboxViewModel inboxViewModel;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -23,5 +28,14 @@ public class InboxActivity extends BaseActivity {
createDrawer(); createDrawer();
drawer.setSelection(INBOX_ID); drawer.setSelection(INBOX_ID);
inboxViewModel = ViewModelProviders.of(this).get(InboxViewModel.class);
subscribeUI();
}
private void subscribeUI() {
inboxViewModel.setOnInboxTaskFinishedListener((resultCode, data) -> {
});
} }
} }

2
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -652,7 +652,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
Toast.makeText(this, "Failed to remove vote", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Failed to remove vote", Toast.LENGTH_LONG).show();
} }
}); });
// observe the chages in data // observe the changes in data
viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> {
if (pageIndicatorIndex == null) return; if (pageIndicatorIndex == null) return;
pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" + pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" +

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

@ -0,0 +1,36 @@
package gr.thmmy.mthmmy.viewmodel;
import androidx.lifecycle.ViewModel;
import gr.thmmy.mthmmy.activities.inbox.tasks.InboxTask;
import gr.thmmy.mthmmy.model.Inbox;
public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTaskFinishedListener<Inbox> {
private static final String INBOX_URL = "https://www.thmmy.gr/smf/index.php?action=pm";
private InboxTask currentInboxTask;
private Inbox inbox;
private InboxTask.OnNetworkTaskFinishedListener<Inbox> onInboxTaskFinishedListener;
private void loadInbox() {
currentInboxTask = new InboxTask();
currentInboxTask.setOnNetworkTaskFinishedListener(this);
currentInboxTask.execute(INBOX_URL);
}
public void setOnInboxTaskFinishedListener(InboxTask.OnNetworkTaskFinishedListener<Inbox> onInboxTaskFinishedListener) {
this.onInboxTaskFinishedListener = onInboxTaskFinishedListener;
}
@Override
public void onNetworkTaskFinished(int resultCode, Inbox inbox) {
this.inbox = inbox;
onInboxTaskFinishedListener.onNetworkTaskFinished(resultCode, inbox);
}
public Inbox getInbox() {
if (inbox == null) throw new NullPointerException("Inbox has not been loaded yet");
return inbox;
}
}
Loading…
Cancel
Save