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 ddf5f0d7..234edef0 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 @@ -59,9 +59,7 @@ import static gr.thmmy.mthmmy.services.NotificationService.NEW_POST_TAG; * key {@link #BUNDLE_TOPIC_TITLE} for faster title rendering. */ @SuppressWarnings("unchecked") -public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskObserver, - DeleteTask.DeleteTaskCallbacks, ReplyTask.ReplyTaskCallbacks, PrepareForEditTask.PrepareForEditCallbacks, - EditTask.EditTaskCallbacks, PrepareForReply.PrepareForReplyCallbacks, TopicAdapter.OnPostFocusChangeListener { +public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFocusChangeListener { //Activity's variables /** * The key to use when putting topic's url String to {@link TopicActivity}'s Bundle. @@ -125,14 +123,9 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_topic); - + // get TopicViewModel instance viewModel = ViewModelProviders.of(this).get(TopicViewModel.class); - viewModel.setTopicTaskObserver(this); - viewModel.setDeleteTaskCallbacks(this); - viewModel.setReplyFinishListener(this); - viewModel.setPrepareForEditCallbacks(this); - viewModel.setEditTaskCallbacks(this); - viewModel.setPrepareForReplyCallbacks(this); + subscribeUI(); Bundle extras = getIntent().getExtras(); String topicTitle = extras.getString(BUNDLE_TOPIC_TITLE); @@ -202,85 +195,6 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb paginationEnabled(false); - viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { - if (pageIndicatorIndex == null) return; - pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" + - String.valueOf(viewModel.getPageCount())); - }); - viewModel.getTopicTitle().observe(this, newTopicTitle -> { - if (newTopicTitle == null) return; - toolbarTitle.setText(newTopicTitle); - }); - viewModel.getPageTopicId().observe(this, pageTopicId -> { - if (pageTopicId == null) return; - if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - if (notificationManager != null) - notificationManager.cancel(NEW_POST_TAG, pageTopicId); - } - }); - viewModel.getReplyPageUrl().observe(this, replyPageUrl -> { - if (replyPageUrl == null) - replyFAB.hide(); - else - replyFAB.show(); - }); - viewModel.getPostsList().observe(this, postList -> { - if (postList == null) onTopicTaskStarted(); - recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug - postsList.clear(); - postsList.addAll(postList); - topicAdapter.notifyDataSetChanged(); - }); - viewModel.getFocusedPostIndex().observe(this, focusedPostIndex -> { - if (focusedPostIndex == null) return; - recyclerView.scrollToPosition(focusedPostIndex); - }); - viewModel.getTopicTaskResultCode().observe(this, resultCode -> { - if (resultCode == null) return; - switch (resultCode) { - case SUCCESS: - paginationEnabled(true); - progressBar.setVisibility(ProgressBar.GONE); - break; - case NETWORK_ERROR: - Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); - break; - case UNAUTHORIZED: - progressBar.setVisibility(ProgressBar.GONE); - Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show(); - break; - default: - //Parse failed - should never happen - Timber.d("Parse failed!"); //TODO report ParseException!!! - Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show(); - finish(); - break; - } - }); - viewModel.getPrepareForReplyResult().observe(this, prepareForReplyResult -> { - if (prepareForReplyResult != null) { - //prepare for a reply - postsList.add(Post.newQuickReply()); - topicAdapter.notifyItemInserted(postsList.size()); - recyclerView.scrollToPosition(postsList.size() - 1); - progressBar.setVisibility(ProgressBar.GONE); - replyFAB.hide(); - bottomNavBar.setVisibility(View.GONE); - } - - }); - viewModel.getPrepareForEditResult().observe(this, result -> { - if (result != null && result.isSuccessful()) { - viewModel.setEditingPost(true); - postsList.get(result.getPosition()).setPostType(Post.TYPE_EDIT); - topicAdapter.notifyItemChanged(result.getPosition()); - recyclerView.scrollToPosition(result.getPosition()); - progressBar.setVisibility(ProgressBar.GONE); - replyFAB.hide(); - bottomNavBar.setVisibility(View.GONE); - } - }); viewModel.loadUrl(topicPageUrl); } @@ -526,108 +440,201 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb //------------------------------------BOTTOM NAV BAR METHODS END------------------------------------ - @Override - public void onTopicTaskStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } - - @Override - public void onTopicTaskCancelled() { - progressBar.setVisibility(ProgressBar.GONE); - } - - @Override - public void onReplyTaskStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } - - @Override - public void onReplyTaskFinished(boolean success) { - View view = getCurrentFocus(); - if (view != null) { - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } - - postsList.remove(postsList.size() - 1); - topicAdapter.notifyItemRemoved(postsList.size()); - - progressBar.setVisibility(ProgressBar.GONE); - replyFAB.show(); - bottomNavBar.setVisibility(View.VISIBLE); - viewModel.setWritingReply(false); + /** + * Binds the UI to its data + */ + private void subscribeUI() { + // Implement async task callbacks + viewModel.setTopicTaskObserver(new TopicTask.TopicTaskObserver() { + @Override + public void onTopicTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } - if (success) { - if ((postsList.get(postsList.size() - 1).getPostNumber() + 1) % 15 == 0) { - viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + "." + 2147483647); - } else { - viewModel.reloadPage(); + @Override + public void onTopicTaskCancelled() { + progressBar.setVisibility(ProgressBar.GONE); + } + }); + viewModel.setDeleteTaskCallbacks(new DeleteTask.DeleteTaskCallbacks() { + @Override + public void onDeleteTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); } - } else { - Toast.makeText(TopicActivity.this, "Post failed!", Toast.LENGTH_SHORT).show(); - } - } - @Override - public void onPrepareForReplyStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } + @Override + public void onDeleteTaskFinished(boolean result) { + progressBar.setVisibility(ProgressBar.GONE); - @Override - public void onPrepareForReplyCancelled() { - progressBar.setVisibility(ProgressBar.GONE); - } + if (result) + viewModel.reloadPage(); + else + Toast.makeText(TopicActivity.this, "Post deleted!", Toast.LENGTH_SHORT).show(); + } + }); + viewModel.setReplyFinishListener(new ReplyTask.ReplyTaskCallbacks() { + @Override + public void onReplyTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } - @Override - public void onDeleteTaskStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } + @Override + public void onReplyTaskFinished(boolean success) { + View view = getCurrentFocus(); + if (view != null) { + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } - @Override - public void onDeleteTaskFinished(boolean result) { - progressBar.setVisibility(ProgressBar.GONE); + postsList.remove(postsList.size() - 1); + topicAdapter.notifyItemRemoved(postsList.size()); - if (result) { - viewModel.reloadPage(); - } else { - Toast.makeText(TopicActivity.this, "Post deleted!", Toast.LENGTH_SHORT).show(); - } - } + progressBar.setVisibility(ProgressBar.GONE); + replyFAB.show(); + bottomNavBar.setVisibility(View.VISIBLE); + viewModel.setWritingReply(false); + + if (success) { + if ((postsList.get(postsList.size() - 1).getPostNumber() + 1) % 15 == 0) { + viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + "." + 2147483647); + } else { + viewModel.reloadPage(); + } + } else { + Toast.makeText(TopicActivity.this, "Post failed!", Toast.LENGTH_SHORT).show(); + } + } + }); + viewModel.setPrepareForEditCallbacks(new PrepareForEditTask.PrepareForEditCallbacks() { + @Override + public void onPrepareEditStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } - @Override - public void onPrepareEditStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } + @Override + public void onPrepareEditCancelled() { + progressBar.setVisibility(ProgressBar.GONE); + } + }); + viewModel.setEditTaskCallbacks(new EditTask.EditTaskCallbacks() { + @Override + public void onEditTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } - @Override - public void onPrepareEditCancelled() { - progressBar.setVisibility(ProgressBar.GONE); - } + @Override + public void onEditTaskFinished(boolean result, int position) { + View view = getCurrentFocus(); + if (view != null) { + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } - @Override - public void onEditTaskStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } + postsList.get(position).setPostType(Post.TYPE_POST); + topicAdapter.notifyItemChanged(position); + viewModel.setEditingPost(false); + progressBar.setVisibility(ProgressBar.GONE); + replyFAB.show(); + bottomNavBar.setVisibility(View.VISIBLE); - @Override - public void onEditTaskFinished(boolean result, int position) { - View view = getCurrentFocus(); - if (view != null) { - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } + if (result) { + viewModel.reloadPage(); + } else { + Toast.makeText(TopicActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show(); + } + } + }); + viewModel.setPrepareForReplyCallbacks(new PrepareForReply.PrepareForReplyCallbacks() { + @Override + public void onPrepareForReplyStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } - postsList.get(position).setPostType(Post.TYPE_POST); - topicAdapter.notifyItemChanged(position); - viewModel.setEditingPost(false); - progressBar.setVisibility(ProgressBar.GONE); - replyFAB.show(); - bottomNavBar.setVisibility(View.VISIBLE); + @Override + public void onPrepareForReplyCancelled() { + progressBar.setVisibility(ProgressBar.GONE); + } + }); + // observe the chages in data + viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { + if (pageIndicatorIndex == null) return; + pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" + + String.valueOf(viewModel.getPageCount())); + }); + viewModel.getTopicTitle().observe(this, newTopicTitle -> { + if (newTopicTitle == null) return; + if (!TextUtils.equals(toolbarTitle.getText(), newTopicTitle)) + toolbarTitle.setText(newTopicTitle); + }); + viewModel.getPageTopicId().observe(this, pageTopicId -> { + if (pageTopicId == null) return; + if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager != null) + notificationManager.cancel(NEW_POST_TAG, pageTopicId); + } + }); + viewModel.getReplyPageUrl().observe(this, replyPageUrl -> { + if (replyPageUrl == null) + replyFAB.hide(); + else + replyFAB.show(); + }); + viewModel.getPostsList().observe(this, postList -> { + if (postList == null) progressBar.setVisibility(ProgressBar.VISIBLE); + recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug + postsList.clear(); + postsList.addAll(postList); + topicAdapter.notifyDataSetChanged(); + }); + viewModel.getFocusedPostIndex().observe(this, focusedPostIndex -> { + if (focusedPostIndex == null) return; + recyclerView.scrollToPosition(focusedPostIndex); + }); + viewModel.getTopicTaskResultCode().observe(this, resultCode -> { + if (resultCode == null) return; + switch (resultCode) { + case SUCCESS: + paginationEnabled(true); + progressBar.setVisibility(ProgressBar.GONE); + break; + case NETWORK_ERROR: + Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); + break; + case UNAUTHORIZED: + progressBar.setVisibility(ProgressBar.GONE); + Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show(); + break; + default: + //Parse failed - should never happen + Timber.d("Parse failed!"); //TODO report ParseException!!! + Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show(); + finish(); + break; + } + }); + viewModel.getPrepareForReplyResult().observe(this, prepareForReplyResult -> { + if (prepareForReplyResult != null) { + //prepare for a reply + postsList.add(Post.newQuickReply()); + topicAdapter.notifyItemInserted(postsList.size()); + recyclerView.scrollToPosition(postsList.size() - 1); + progressBar.setVisibility(ProgressBar.GONE); + replyFAB.hide(); + bottomNavBar.setVisibility(View.GONE); + } - if (result) { - viewModel.reloadPage(); - } else { - Toast.makeText(TopicActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show(); - } + }); + viewModel.getPrepareForEditResult().observe(this, result -> { + if (result != null && result.isSuccessful()) { + viewModel.setEditingPost(true); + postsList.get(result.getPosition()).setPostType(Post.TYPE_EDIT); + topicAdapter.notifyItemChanged(result.getPosition()); + recyclerView.scrollToPosition(result.getPosition()); + progressBar.setVisibility(ProgressBar.GONE); + replyFAB.hide(); + bottomNavBar.setVisibility(View.GONE); + } + }); } } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 783c741f..e2b98cfe 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -445,7 +445,7 @@ class TopicAdapter extends RecyclerView.Adapter { .transform(new CircleTransform()) .into(holder.thumbnail); holder.username.setText(getSessionManager().getUsername()); - holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle()); + holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle().getValue()); holder.quickReply.setText(viewModel.getBuildedQuotes()); @@ -459,7 +459,7 @@ class TopicAdapter extends RecyclerView.Adapter { holder.quickReply.getText().toString()); holder.quickReplySubject.getText().clear(); - holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle()); + holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle().getValue()); holder.quickReply.getText().clear(); holder.submitButton.setEnabled(true); });