Show a snackbar when sending comment and sending comment fails. Display the sent comment data after the comment is sent.

This commit is contained in:
Alex Ning 2019-06-14 11:58:52 +08:00
parent f69ae29bb9
commit 72d811186f
9 changed files with 151 additions and 55 deletions

Binary file not shown.

Binary file not shown.

View File

@ -46,7 +46,7 @@ dependencies {
implementation 'androidx.browser:browser:1.0.0'
implementation 'com.alexvasilkov:gesture-views:2.5.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.github.pwittchen:swipe-rx2:0.3.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
// Room components
@ -75,5 +75,4 @@ dependencies {
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
implementation 'com.github.Ferfalk:SimpleSearchView:0.1.3'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'jp.wasabeef:richeditor-android:1.2.2'
}

View File

@ -24,9 +24,10 @@ import retrofit2.Retrofit;
public class CommentActivity extends AppCompatActivity {
static final String COMMENT_PARENT_TEXT = "CPT";
static final String PARENT_FULLNAME = "PF";
static final String EXTRA_COMMENT_DATA = "ECD";
static final String EXTRA_COMMENT_PARENT_TEXT_KEY = "ECPTK";
static final String EXTRA_PARENT_FULLNAME_KEY = "EPFK";
static final String EXTRA_COMMENT_DATA_KEY = "ECDK";
static final String EXTRA_PARENT_DEPTH_KEY = "EPDK";
static final int WRITE_COMMENT_REQUEST_CODE = 1;
@BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout;
@ -35,6 +36,7 @@ public class CommentActivity extends AppCompatActivity {
@BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText;
private String parentFullname;
private int parentDepth;
@Inject
@Named("oauth")
@ -55,8 +57,10 @@ public class CommentActivity extends AppCompatActivity {
setSupportActionBar(toolbar);
commentParentTextView.setText(getIntent().getExtras().getString(COMMENT_PARENT_TEXT));
parentFullname = getIntent().getExtras().getString(PARENT_FULLNAME);
Intent intent = getIntent();
commentParentTextView.setText(intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY));
parentFullname = intent.getExtras().getString(EXTRA_PARENT_FULLNAME_KEY);
parentDepth = intent.getExtras().getInt(EXTRA_PARENT_DEPTH_KEY);
}
@Override
@ -72,22 +76,37 @@ public class CommentActivity extends AppCompatActivity {
finish();
return true;
case R.id.action_send_comment_activity:
CommentData commentData = null;
SendComment.sendComment(commentEditText.getText().toString(), parentFullname, mOauthRetrofit,
item.setEnabled(false);
item.getIcon().setAlpha(130);
Snackbar sendingSnackbar = Snackbar.make(coordinatorLayout, R.string.sending_comment, Snackbar.LENGTH_INDEFINITE);
sendingSnackbar.show();
SendComment.sendComment(commentEditText.getText().toString(), parentFullname, parentDepth,
getResources().getConfiguration().locale, mOauthRetrofit,
sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""),
new SendComment.SendCommentListener() {
@Override
public void sendCommentSuccess() {
public void sendCommentSuccess(CommentData commentData) {
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_COMMENT_DATA, commentData);
returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData);
setResult(RESULT_OK, returnIntent);
finish();
}
@Override
public void sendCommentFailed() {
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();
}
});
}
return super.onOptionsItemSelected(item);

View File

@ -387,7 +387,7 @@ public class MainActivity extends AppCompatActivity {
mInsertSuccess = false;
loadUserData();*/
Intent intent = new Intent(this, CommentActivity.class);
intent.putExtra(CommentActivity.EXTRA_COMMENT_DATA, "asdfasdfas");
intent.putExtra(CommentActivity.EXTRA_COMMENT_DATA_KEY, "asdfasdfas");
startActivity(intent);
return true;
case R.id.action_lazy_mode_main_activity:

View File

@ -24,6 +24,11 @@ class ParseComment {
void onParseMoreCommentBasicInfoFailed();
}
interface ParseSentCommentListener {
void onParseSentCommentSuccess(CommentData commentData);
void onParseSentCommentFailed();
}
static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale,
boolean isPost, int parentDepth, ParseCommentListener parseCommentListener) {
try {
@ -39,7 +44,9 @@ class ParseComment {
new ParseCommentAsyncTask(childrenArray, commentData, locale, parentDepth, parseCommentListener).execute();
} catch (JSONException e) {
e.printStackTrace();
Log.i("comment json error", e.getMessage());
if(e.getMessage() != null) {
Log.i("comment json error", e.getMessage());
}
parseCommentListener.onParseCommentFailed();
}
}
@ -55,11 +62,18 @@ class ParseComment {
new ParseCommentAsyncTask(childrenArray, commentData, locale, parentDepth, parseCommentListener).execute();
} catch (JSONException e) {
e.printStackTrace();
Log.i("comment json error", e.getMessage());
if(e.getMessage() != null) {
Log.i("comment json error", e.getMessage());
}
parseCommentListener.onParseCommentFailed();
}
}
static void parseSentComment(String response, int parentDepth, Locale locale,
ParseSentCommentListener parseSentCommentListener) {
new ParseSentCommentAsyncTask(response, parentDepth, locale, parseSentCommentListener).execute();
}
private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONArray comments;
private ArrayList<CommentData> commentData;
@ -110,38 +124,13 @@ class ParseComment {
for (int i = 0; i < actualCommentLength; i++) {
JSONObject data = comments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String id = data.getString(JSONUtils.ID_KEY);
String fullName = data.getString(JSONUtils.LINK_ID_KEY);
String author = data.getString(JSONUtils.AUTHOR_KEY);
boolean isSubmitter = data.getBoolean(JSONUtils.IS_SUBMITTER_KEY);
String commentContent = "";
if(!data.isNull(JSONUtils.BODY_HTML_KEY)) {
commentContent = data.getString(JSONUtils.BODY_HTML_KEY).trim();
}
String permalink = data.getString(JSONUtils.PERMALINK_KEY);
int score = data.getInt(JSONUtils.SCORE_KEY);
long submitTime = data.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
boolean scoreHidden = data.getBoolean(JSONUtils.SCORE_HIDDEN_KEY);
Calendar submitTimeCalendar = Calendar.getInstance();
submitTimeCalendar.setTimeInMillis(submitTime);
String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm",
locale).format(submitTimeCalendar.getTime());
int depth;
if(data.has(JSONUtils.DEPTH_KEY)) {
depth = data.getInt(JSONUtils.DEPTH_KEY) + parentDepth;
} else {
depth = parentDepth;
}
boolean collapsed = data.getBoolean(JSONUtils.COLLAPSED_KEY);
boolean hasReply = !(data.get(JSONUtils.REPLIES_KEY) instanceof String);
newcommentData.add(new CommentData(id, fullName, author, formattedSubmitTime, commentContent, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden));
newcommentData.add(parseSingleComment(data, parentDepth, locale));
}
} catch (JSONException e) {
parseFailed = true;
Log.i("parse comment error", e.getMessage());
if(e.getMessage() != null) {
Log.i("parse comment error", e.getMessage());
}
}
return null;
}
@ -201,4 +190,75 @@ class ParseComment {
}
}
}
private static class ParseSentCommentAsyncTask extends AsyncTask<Void, Void, Void> {
private String response;
private int parentDepth;
private Locale locale;
private ParseSentCommentListener parseSentCommentListener;
private boolean parseFailed;
private CommentData commentData;
ParseSentCommentAsyncTask(String response, int parentDepth, Locale locale, ParseSentCommentListener parseSentCommentListener) {
this.response = response;
this.parentDepth = parentDepth;
this.locale = locale;
this.parseSentCommentListener = parseSentCommentListener;
parseFailed = false;
}
@Override
protected Void doInBackground(Void... voids) {
try {
JSONObject sentCommentData = new JSONObject(response);
commentData = parseSingleComment(sentCommentData, parentDepth, locale);
} catch (JSONException e) {
e.printStackTrace();
parseFailed = true;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(parseFailed) {
parseSentCommentListener.onParseSentCommentFailed();
} else {
parseSentCommentListener.onParseSentCommentSuccess(commentData);
}
}
}
private static CommentData parseSingleComment(JSONObject singleCommentData, int parentDepth, Locale locale) throws JSONException {
String id = singleCommentData.getString(JSONUtils.ID_KEY);
String fullName = singleCommentData.getString(JSONUtils.LINK_ID_KEY);
String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY);
boolean isSubmitter = singleCommentData.getBoolean(JSONUtils.IS_SUBMITTER_KEY);
String commentContent = "";
if(!singleCommentData.isNull(JSONUtils.BODY_HTML_KEY)) {
commentContent = singleCommentData.getString(JSONUtils.BODY_HTML_KEY).trim();
}
String permalink = singleCommentData.getString(JSONUtils.PERMALINK_KEY);
int score = singleCommentData.getInt(JSONUtils.SCORE_KEY);
long submitTime = singleCommentData.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY);
Calendar submitTimeCalendar = Calendar.getInstance();
submitTimeCalendar.setTimeInMillis(submitTime);
String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm",
locale).format(submitTimeCalendar.getTime());
int depth;
if(singleCommentData.has(JSONUtils.DEPTH_KEY)) {
depth = singleCommentData.getInt(JSONUtils.DEPTH_KEY) + parentDepth;
} else {
depth = parentDepth;
}
boolean collapsed = singleCommentData.getBoolean(JSONUtils.COLLAPSED_KEY);
boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String);
return new CommentData(id, fullName, author, formattedSubmitTime, commentContent, score,
isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden);
}
}

View File

@ -1,10 +1,9 @@
package ml.docilealligator.infinityforreddit;
import android.util.Log;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import retrofit2.Call;
@ -15,12 +14,14 @@ import retrofit2.Retrofit;
class SendComment {
interface SendCommentListener {
void sendCommentSuccess();
void sendCommentSuccess(CommentData commentData);
void sendCommentFailed();
void parseSentCommentFailed();
}
static void sendComment(String commentMarkdown, String thingFullname, Retrofit oauthRetrofit,
String accessToken, SendCommentListener sendCommentListener) {
static void sendComment(String commentMarkdown, String thingFullname, int parentDepth,
Locale locale, Retrofit oauthRetrofit, String accessToken,
SendCommentListener sendCommentListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Map<String, String> headers = RedditUtils.getOAuthHeader(accessToken);
Map<String, String> params = new HashMap<>();
@ -35,8 +36,17 @@ class SendComment {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if(response.isSuccessful()) {
Log.i("asdfasdfaf", response.body());
sendCommentListener.sendCommentSuccess();
ParseComment.parseSentComment(response.body(), parentDepth, locale, new ParseComment.ParseSentCommentListener() {
@Override
public void onParseSentCommentSuccess(CommentData commentData) {
sendCommentListener.sendCommentSuccess(commentData);
}
@Override
public void onParseSentCommentFailed() {
}
});
} else {
sendCommentListener.sendCommentFailed();
}

View File

@ -64,6 +64,7 @@ import retrofit2.Retrofit;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.view.MarkwonView;
import static ml.docilealligator.infinityforreddit.CommentActivity.EXTRA_COMMENT_DATA_KEY;
import static ml.docilealligator.infinityforreddit.CommentActivity.WRITE_COMMENT_REQUEST_CODE;
public class ViewPostDetailActivity extends AppCompatActivity {
@ -684,8 +685,9 @@ public class ViewPostDetailActivity extends AppCompatActivity {
return true;
case R.id.action_comment_view_post_detail_activity:
Intent intent = new Intent(this, CommentActivity.class);
intent.putExtra(CommentActivity.COMMENT_PARENT_TEXT, mPost.getTitle());
intent.putExtra(CommentActivity.PARENT_FULLNAME, mPost.getFullName());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName());
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0);
startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE);
return true;
case android.R.id.home:
@ -698,9 +700,13 @@ public class ViewPostDetailActivity extends AppCompatActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == WRITE_COMMENT_REQUEST_CODE) {
/*CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA);
mAdapter.addComment(comment);*/
if(data != null && resultCode == RESULT_OK && requestCode == WRITE_COMMENT_REQUEST_CODE) {
if(data.hasExtra(EXTRA_COMMENT_DATA_KEY)) {
CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA_KEY);
mAdapter.addComment(comment);
} else {
Toast.makeText(this, R.string.send_comment_failed, Toast.LENGTH_SHORT).show();
}
}
}

View File

@ -72,6 +72,8 @@
<string name="lazy_mode_stop">Lazy Mode stopped</string>
<string name="write_comment_hint">Your interesting thoughts here</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>
</resources>