Browse Source

reply subject fix, cleanup

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

241
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,6 +440,12 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
//------------------------------------BOTTOM NAV BAR METHODS END------------------------------------
/**
* 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);
@ -535,7 +455,24 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
public void onTopicTaskCancelled() {
progressBar.setVisibility(ProgressBar.GONE);
}
});
viewModel.setDeleteTaskCallbacks(new DeleteTask.DeleteTaskCallbacks() {
@Override
public void onDeleteTaskStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
public void onDeleteTaskFinished(boolean result) {
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);
@ -567,33 +504,8 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
Toast.makeText(TopicActivity.this, "Post failed!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPrepareForReplyStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
public void onPrepareForReplyCancelled() {
progressBar.setVisibility(ProgressBar.GONE);
}
@Override
public void onDeleteTaskStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
public void onDeleteTaskFinished(boolean result) {
progressBar.setVisibility(ProgressBar.GONE);
if (result) {
viewModel.reloadPage();
} else {
Toast.makeText(TopicActivity.this, "Post deleted!", Toast.LENGTH_SHORT).show();
}
}
});
viewModel.setPrepareForEditCallbacks(new PrepareForEditTask.PrepareForEditCallbacks() {
@Override
public void onPrepareEditStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
@ -603,7 +515,8 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
public void onPrepareEditCancelled() {
progressBar.setVisibility(ProgressBar.GONE);
}
});
viewModel.setEditTaskCallbacks(new EditTask.EditTaskCallbacks() {
@Override
public void onEditTaskStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
@ -630,4 +543,98 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
Toast.makeText(TopicActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show();
}
}
});
viewModel.setPrepareForReplyCallbacks(new PrepareForReply.PrepareForReplyCallbacks() {
@Override
public void onPrepareForReplyStarted() {
progressBar.setVisibility(ProgressBar.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);
}
});
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);
}
});
}
}

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())
.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<RecyclerView.ViewHolder> {
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);
});

Loading…
Cancel
Save