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 57952965..fba3b5fd 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 @@ -2,11 +2,16 @@ package gr.thmmy.mthmmy.activities.inbox; import android.os.Bundle; +import androidx.lifecycle.ViewModelProviders; + import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; +import gr.thmmy.mthmmy.viewmodel.InboxViewModel; public class InboxActivity extends BaseActivity { + InboxViewModel inboxViewModel; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -23,5 +28,14 @@ public class InboxActivity extends BaseActivity { createDrawer(); drawer.setSelection(INBOX_ID); + + inboxViewModel = ViewModelProviders.of(this).get(InboxViewModel.class); + subscribeUI(); + } + + private void subscribeUI() { + inboxViewModel.setOnInboxTaskFinishedListener((resultCode, data) -> { + + }); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 639ab3c7..64c04437 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/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(); } }); - // observe the chages in data + // observe the changes in data viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { if (pageIndicatorIndex == null) return; pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" + diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java new file mode 100644 index 00000000..18b33919 --- /dev/null +++ b/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 { + private static final String INBOX_URL = "https://www.thmmy.gr/smf/index.php?action=pm"; + + private InboxTask currentInboxTask; + + private Inbox inbox; + private InboxTask.OnNetworkTaskFinishedListener onInboxTaskFinishedListener; + + private void loadInbox() { + currentInboxTask = new InboxTask(); + currentInboxTask.setOnNetworkTaskFinishedListener(this); + currentInboxTask.execute(INBOX_URL); + } + + public void setOnInboxTaskFinishedListener(InboxTask.OnNetworkTaskFinishedListener 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; + } +}