Browse Source

reply subject fix, cleanup

pull/43/head
Thodoris1999 6 years ago
parent
commit
299f0b5afc
  1. 365
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

365
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. * key {@link #BUNDLE_TOPIC_TITLE} for faster title rendering.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskObserver, public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFocusChangeListener {
DeleteTask.DeleteTaskCallbacks, ReplyTask.ReplyTaskCallbacks, PrepareForEditTask.PrepareForEditCallbacks,
EditTask.EditTaskCallbacks, PrepareForReply.PrepareForReplyCallbacks, TopicAdapter.OnPostFocusChangeListener {
//Activity's variables //Activity's variables
/** /**
* The key to use when putting topic's url String to {@link TopicActivity}'s Bundle. * 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) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic); setContentView(R.layout.activity_topic);
// get TopicViewModel instance
viewModel = ViewModelProviders.of(this).get(TopicViewModel.class); viewModel = ViewModelProviders.of(this).get(TopicViewModel.class);
viewModel.setTopicTaskObserver(this); subscribeUI();
viewModel.setDeleteTaskCallbacks(this);
viewModel.setReplyFinishListener(this);
viewModel.setPrepareForEditCallbacks(this);
viewModel.setEditTaskCallbacks(this);
viewModel.setPrepareForReplyCallbacks(this);
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
String topicTitle = extras.getString(BUNDLE_TOPIC_TITLE); String topicTitle = extras.getString(BUNDLE_TOPIC_TITLE);
@ -202,85 +195,6 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
paginationEnabled(false); 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); viewModel.loadUrl(topicPageUrl);
} }
@ -526,108 +440,201 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
//------------------------------------BOTTOM NAV BAR METHODS END------------------------------------ //------------------------------------BOTTOM NAV BAR METHODS END------------------------------------
@Override /**
public void onTopicTaskStarted() { * Binds the UI to its data
progressBar.setVisibility(ProgressBar.VISIBLE); */
} private void subscribeUI() {
// Implement async task callbacks
@Override viewModel.setTopicTaskObserver(new TopicTask.TopicTaskObserver() {
public void onTopicTaskCancelled() { @Override
progressBar.setVisibility(ProgressBar.GONE); public void onTopicTaskStarted() {
} progressBar.setVisibility(ProgressBar.VISIBLE);
}
@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);
if (success) { @Override
if ((postsList.get(postsList.size() - 1).getPostNumber() + 1) % 15 == 0) { public void onTopicTaskCancelled() {
viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + "." + 2147483647); progressBar.setVisibility(ProgressBar.GONE);
} else { }
viewModel.reloadPage(); });
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 @Override
public void onPrepareForReplyStarted() { public void onDeleteTaskFinished(boolean result) {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.GONE);
}
@Override if (result)
public void onPrepareForReplyCancelled() { viewModel.reloadPage();
progressBar.setVisibility(ProgressBar.GONE); 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 @Override
public void onDeleteTaskStarted() { public void onReplyTaskFinished(boolean success) {
progressBar.setVisibility(ProgressBar.VISIBLE); View view = getCurrentFocus();
} if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
@Override postsList.remove(postsList.size() - 1);
public void onDeleteTaskFinished(boolean result) { topicAdapter.notifyItemRemoved(postsList.size());
progressBar.setVisibility(ProgressBar.GONE);
if (result) { progressBar.setVisibility(ProgressBar.GONE);
viewModel.reloadPage(); replyFAB.show();
} else { bottomNavBar.setVisibility(View.VISIBLE);
Toast.makeText(TopicActivity.this, "Post deleted!", Toast.LENGTH_SHORT).show(); 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 @Override
public void onPrepareEditStarted() { public void onPrepareEditCancelled() {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.GONE);
} }
});
viewModel.setEditTaskCallbacks(new EditTask.EditTaskCallbacks() {
@Override
public void onEditTaskStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override @Override
public void onPrepareEditCancelled() { public void onEditTaskFinished(boolean result, int position) {
progressBar.setVisibility(ProgressBar.GONE); View view = getCurrentFocus();
} if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
@Override postsList.get(position).setPostType(Post.TYPE_POST);
public void onEditTaskStarted() { topicAdapter.notifyItemChanged(position);
progressBar.setVisibility(ProgressBar.VISIBLE); viewModel.setEditingPost(false);
} progressBar.setVisibility(ProgressBar.GONE);
replyFAB.show();
bottomNavBar.setVisibility(View.VISIBLE);
@Override if (result) {
public void onEditTaskFinished(boolean result, int position) { viewModel.reloadPage();
View view = getCurrentFocus(); } else {
if (view != null) { Toast.makeText(TopicActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); }
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }
} });
viewModel.setPrepareForReplyCallbacks(new PrepareForReply.PrepareForReplyCallbacks() {
@Override
public void onPrepareForReplyStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
postsList.get(position).setPostType(Post.TYPE_POST); @Override
topicAdapter.notifyItemChanged(position); public void onPrepareForReplyCancelled() {
viewModel.setEditingPost(false); progressBar.setVisibility(ProgressBar.GONE);
progressBar.setVisibility(ProgressBar.GONE); }
replyFAB.show(); });
bottomNavBar.setVisibility(View.VISIBLE); // 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(); viewModel.getPrepareForEditResult().observe(this, result -> {
} else { if (result != null && result.isSuccessful()) {
Toast.makeText(TopicActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show(); 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);
}
});
} }
} }

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -445,7 +445,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
.transform(new CircleTransform()) .transform(new CircleTransform())
.into(holder.thumbnail); .into(holder.thumbnail);
holder.username.setText(getSessionManager().getUsername()); holder.username.setText(getSessionManager().getUsername());
holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle()); holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle().getValue());
holder.quickReply.setText(viewModel.getBuildedQuotes()); holder.quickReply.setText(viewModel.getBuildedQuotes());
@ -459,7 +459,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.quickReply.getText().toString()); holder.quickReply.getText().toString());
holder.quickReplySubject.getText().clear(); holder.quickReplySubject.getText().clear();
holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle()); holder.quickReplySubject.setText("Re: " + viewModel.getTopicTitle().getValue());
holder.quickReply.getText().clear(); holder.quickReply.getText().clear();
holder.submitButton.setEnabled(true); holder.submitButton.setEnabled(true);
}); });

Loading…
Cancel
Save