Handle sending comment fails. Fixed snackbar hidden by keyboard in some activities. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-08-20 15:01:54 +08:00
parent a340517974
commit 029bbc951b
13 changed files with 202 additions and 98 deletions

View File

@ -124,6 +124,7 @@
<activity
android:name=".CommentActivity"
android:label="@string/comment_activity_label"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize" />
<activity

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
@ -8,10 +9,11 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
@ -104,7 +106,10 @@ public class CommentActivity extends AppCompatActivity {
setSupportActionBar(toolbar);
commentEditText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
private void getCurrentAccount() {
@ -117,6 +122,15 @@ public class CommentActivity extends AppCompatActivity {
}).execute();
}
@Override
protected void onPause() {
super.onPause();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(commentEditText.getWindowToken(), 0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.comment_activity, menu);
@ -130,6 +144,11 @@ public class CommentActivity extends AppCompatActivity {
finish();
return true;
case R.id.action_send_comment_activity:
if(commentEditText.getText() == null || commentEditText.getText().toString().equals("")) {
Snackbar.make(coordinatorLayout, R.string.comment_content_required, Snackbar.LENGTH_SHORT).show();
return true;
}
item.setEnabled(false);
item.getIcon().setAlpha(130);
Snackbar sendingSnackbar = Snackbar.make(coordinatorLayout, R.string.sending_comment, Snackbar.LENGTH_INDEFINITE);
@ -152,18 +171,16 @@ public class CommentActivity extends AppCompatActivity {
}
@Override
public void sendCommentFailed() {
public void sendCommentFailed(@Nullable String errorMessage) {
sendingSnackbar.dismiss();
item.setEnabled(true);
item.getIcon().setAlpha(255);
Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show();
}
@Override
public void parseSentCommentFailed() {
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
if(errorMessage == null) {
Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show();
} else {
Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show();
}
}
});
return true;

View File

@ -13,6 +13,7 @@ class CommentData implements Parcelable {
private String id;
private String fullName;
private String author;
private String linkAuthor;
private String commentTime;
private String commentContent;
private String linkId;
@ -35,12 +36,13 @@ class CommentData implements Parcelable {
private boolean isLoadingMoreChildren;
private boolean loadMoreChildrenFailed;
CommentData(String id, String fullName, String author, String commentTime, String commentContent,
CommentData(String id, String fullName, String author, String linkAuthor, String commentTime, String commentContent,
String linkId, String subredditName, String parentId, int score, boolean isSubmitter, String permalink,
int depth, boolean collapsed, boolean hasReply, boolean scoreHidden) {
this.id = id;
this.fullName = fullName;
this.author = author;
this.linkAuthor = linkAuthor;
this.commentTime = commentTime;
this.commentContent = commentContent;
this.linkId = linkId;
@ -70,6 +72,7 @@ class CommentData implements Parcelable {
id = in.readString();
fullName = in.readString();
author = in.readString();
linkAuthor = in.readString();
commentTime = in.readString();
commentContent = in.readString();
linkId = in.readString();
@ -120,6 +123,10 @@ class CommentData implements Parcelable {
this.author = author;
}
public String getLinkAuthor() {
return linkAuthor;
}
public String getCommentTime() {
return commentTime;
}
@ -285,6 +292,7 @@ class CommentData implements Parcelable {
parcel.writeString(id);
parcel.writeString(fullName);
parcel.writeString(author);
parcel.writeString(linkAuthor);
parcel.writeString(commentTime);
parcel.writeString(commentContent);
parcel.writeString(linkId);

View File

@ -83,53 +83,54 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter<CommentData, R
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if(holder instanceof DataViewHolder) {
CommentData comment = getItem(holder.getAdapterPosition());
if(comment != null) {
if(comment.getSubredditName().substring(2).equals(comment.getLinkAuthor())) {
((DataViewHolder) holder).authorTextView.setText("u/" + comment.getLinkAuthor());
((DataViewHolder) holder).authorTextView.setTextColor(mTextColorPrimaryDark);
((DataViewHolder) holder).authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getAuthor());
mContext.startActivity(intent);
});
} else {
((DataViewHolder) holder).authorTextView.setText("r/" + comment.getSubredditName());
((DataViewHolder) holder).authorTextView.setTextColor(mColorAccent);
((DataViewHolder) holder).authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName());
mContext.startActivity(intent);
});
}
if(comment.getAuthor().equals(comment.getSubredditName().substring(2))) {
((DataViewHolder) holder).authorTextView.setText("u/" + comment.getAuthor());
((DataViewHolder) holder).authorTextView.setTextColor(mTextColorPrimaryDark);
((DataViewHolder) holder).authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getAuthor());
mContext.startActivity(intent);
});
} else {
((DataViewHolder) holder).authorTextView.setText("r/" + comment.getSubredditName());
((DataViewHolder) holder).authorTextView.setTextColor(mColorAccent);
((DataViewHolder) holder).authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName());
mContext.startActivity(intent);
});
}
((DataViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime());
((DataViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime());
((DataViewHolder) holder).commentMarkdownView.setMarkdown(comment.getCommentContent(), mContext);
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore()));
((DataViewHolder) holder).commentMarkdownView.setMarkdown(comment.getCommentContent(), mContext);
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore()));
switch (comment.getVoteType()) {
case 1:
((DataViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
break;
case 2:
((DataViewHolder) holder).downvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
break;
}
switch (comment.getVoteType()) {
case 1:
((DataViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
break;
case 2:
((DataViewHolder) holder).downvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
break;
}
if(comment.getAuthor().equals(mAccountName)) {
((DataViewHolder) holder).moreButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).moreButton.setOnClickListener(view -> {
ModifyCommentBottomSheetFragment modifyCommentBottomSheetFragment = new ModifyCommentBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_CONTENT, comment.getCommentContent());
bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_FULLNAME, comment.getFullName());
bundle.putInt(ModifyCommentBottomSheetFragment.EXTRA_POSITION, holder.getAdapterPosition() - 1);
modifyCommentBottomSheetFragment.setArguments(bundle);
modifyCommentBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag());
});
if(comment.getAuthor().equals(mAccountName)) {
((DataViewHolder) holder).moreButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).moreButton.setOnClickListener(view -> {
ModifyCommentBottomSheetFragment modifyCommentBottomSheetFragment = new ModifyCommentBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_CONTENT, comment.getCommentContent());
bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_FULLNAME, comment.getFullName());
bundle.putInt(ModifyCommentBottomSheetFragment.EXTRA_POSITION, holder.getAdapterPosition() - 1);
modifyCommentBottomSheetFragment.setArguments(bundle);
modifyCommentBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag());
});
}
}
}
}

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
@ -8,7 +9,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
@ -85,7 +86,19 @@ public class EditCommentActivity extends AppCompatActivity {
contentEditText.setText(getIntent().getExtras().getString(EXTRA_CONTENT));
contentEditText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
@Override
protected void onPause() {
super.onPause();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0);
}
}
@Override

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
@ -8,7 +9,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@ -85,7 +86,19 @@ public class EditPostActivity extends AppCompatActivity {
contentEditText.setText(getIntent().getExtras().getString(EXTRA_CONTENT));
contentEditText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
@Override
protected void onPause() {
super.onPause();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0);
}
}
@Override

View File

@ -15,6 +15,7 @@ public class JSONUtils {
static final String SUBREDDIT_NAME_PREFIX_KEY = "subreddit_name_prefixed";
static final String SELFTEXT_KEY = "selftext";
static final String AUTHOR_KEY = "author";
static final String LINK_AUTHOR_KEY = "link_author";
static final String LINK_FLAIR_TEXT_KEY = "link_flair_text";
static final String SCORE_KEY = "score";
static final String LIKES_KEY = "likes";

View File

@ -24,7 +24,7 @@ class ParseComment {
interface ParseSentCommentListener {
void onParseSentCommentSuccess(CommentData commentData);
void onParseSentCommentFailed();
void onParseSentCommentFailed(@Nullable String errorMessage);
}
static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale,
@ -179,6 +179,7 @@ class ParseComment {
private Locale locale;
private ParseSentCommentListener parseSentCommentListener;
private boolean parseFailed;
private String errorMessage;
private CommentData commentData;
ParseSentCommentAsyncTask(String response, int depth, Locale locale, ParseSentCommentListener parseSentCommentListener) {
@ -196,6 +197,7 @@ class ParseComment {
commentData = parseSingleComment(sentCommentData, depth, locale);
} catch (JSONException e) {
e.printStackTrace();
errorMessage = parseSentCommentErrorMessage(response);
parseFailed = true;
}
return null;
@ -205,7 +207,7 @@ class ParseComment {
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(parseFailed) {
parseSentCommentListener.onParseSentCommentFailed();
parseSentCommentListener.onParseSentCommentFailed(errorMessage);
} else {
parseSentCommentListener.onParseSentCommentSuccess(commentData);
}
@ -213,10 +215,10 @@ class ParseComment {
}
static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException {
Log.i("adfasdf", singleCommentData.toString());
String id = singleCommentData.getString(JSONUtils.ID_KEY);
String fullName = singleCommentData.getString(JSONUtils.NAME_KEY);
String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY);
String linkAuthor = singleCommentData.has(JSONUtils.LINK_AUTHOR_KEY) ? singleCommentData.getString(JSONUtils.LINK_AUTHOR_KEY) : null;
String linkId = singleCommentData.getString(JSONUtils.LINK_ID_KEY).substring(3);
String subredditName = singleCommentData.getString(JSONUtils.SUBREDDIT_KEY);
String parentId = singleCommentData.getString(JSONUtils.PARENT_ID_KEY);
@ -242,7 +244,37 @@ class ParseComment {
boolean collapsed = singleCommentData.getBoolean(JSONUtils.COLLAPSED_KEY);
boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String);
return new CommentData(id, fullName, author, formattedSubmitTime, commentContent, linkId,
subredditName, parentId, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden);
return new CommentData(id, fullName, author, linkAuthor, formattedSubmitTime, commentContent,
linkId, subredditName, parentId, score, isSubmitter, permalink, depth, collapsed,
hasReply, scoreHidden);
}
@Nullable
private static String parseSentCommentErrorMessage(String response) {
try {
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
if(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
.getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1);
if(error.length() != 0) {
String errorString;
if(error.length() >= 2) {
errorString = error.getString(1);
} else {
errorString = error.getString(0);
}
return errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
} else {
return null;
}
} else {
return null;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -1,6 +1,7 @@
package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
@ -9,7 +10,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
@ -171,7 +172,19 @@ public class SearchActivity extends AppCompatActivity {
query = null;
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
@Override
protected void onPause() {
super.onPause();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(simpleSearchView.getSearchEditText().getWindowToken(), 0);
}
}
@Override

View File

@ -1,6 +1,7 @@
package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.Locale;
@ -15,8 +16,7 @@ class SendComment {
interface SendCommentListener {
void sendCommentSuccess(CommentData commentData);
void sendCommentFailed();
void parseSentCommentFailed();
void sendCommentFailed(String errorMessage);
}
static void sendComment(String commentMarkdown, String thingFullname, int parentDepth,
@ -43,18 +43,18 @@ class SendComment {
}
@Override
public void onParseSentCommentFailed() {
public void onParseSentCommentFailed(@Nullable String errorMessage) {
sendCommentListener.sendCommentFailed(errorMessage);
}
});
} else {
sendCommentListener.sendCommentFailed();
sendCommentListener.sendCommentFailed(response.message());
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
sendCommentListener.sendCommentFailed();
sendCommentListener.sendCommentFailed(t.getMessage());
}
});
}

View File

@ -204,6 +204,7 @@ class SubmitPost {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
Log.i("afasdfadsfasdfasdfasdf", "a " + response.body());
try {
getSubmittedPost(response.body(), kind, oauthRetrofit, accessToken,
locale, submitPostListener);
@ -219,8 +220,7 @@ class SubmitPost {
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
Log.i("call_failed", call.request().url().toString());
submitPostListener.submitFailed(null);
submitPostListener.submitFailed(t.getMessage());
}
});
}
@ -414,13 +414,11 @@ class SubmitPost {
String errorString;
if(error.length() >= 2) {
errorString = error.getString(1);
errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
submitPostListener.submitFailed(errorString);
} else {
errorString = error.getString(0);
errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
submitPostListener.submitFailed(errorString);
}
errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
submitPostListener.submitFailed(errorString);
} else {
submitPostListener.submitFailed(null);
}

View File

@ -21,31 +21,37 @@
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ru.noties.markwon.view.MarkwonView
android:id="@+id/comment_parent_markwon_view_comment_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textSize="16sp" />
<EditText
android:id="@+id/comment_edit_text_comment_activity"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:hint="@string/write_comment_hint"
android:inputType="textCapSentences|textMultiLine"
android:textSize="18sp"
android:background="#00000000"
android:textColor="@color/primaryTextColor" />
android:orientation="vertical"
android:padding="16dp">
</LinearLayout>
<ru.noties.markwon.view.MarkwonView
android:id="@+id/comment_parent_markwon_view_comment_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textSize="16sp" />
<EditText
android:id="@+id/comment_edit_text_comment_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:hint="@string/write_comment_hint"
android:inputType="textCapSentences|textMultiLine"
android:textSize="18sp"
android:background="#00000000"
android:textColor="@color/primaryTextColor" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -2,7 +2,7 @@
<string name="app_name">Infinity</string>
<string name="login_activity_label">Login</string>
<string name="search_activity_label">Search</string>
<string name="comment_activity_label">Add Comment</string>
<string name="comment_activity_label">Send Comment</string>
<string name="comment_activity_label_is_replying">Reply</string>
<string name="post_text_activity_label">Text Post</string>
<string name="subreddit_selection_activity_label">Select a Subreddit</string>
@ -104,7 +104,8 @@
<string name="lazy_mode_start">Lazy Mode starts in %1$.1fs</string>
<string name="lazy_mode_stop">Lazy Mode stopped</string>
<string name="write_comment_hint">Your interesting thoughts here</string>
<string name="write_comment_hint">Your interesting thought here</string>
<string name="comment_content_required">Where is your interesting thought</string>
<string name="sending_comment">Sending</string>
<string name="send_comment_failed">Could not send this comment</string>
<string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string>