Skip to content

Commit cfe1781

Browse files
Show updated Giphy gif in the edit comments.
1 parent 0b5e152 commit cfe1781

File tree

9 files changed

+135
-48
lines changed

9 files changed

+135
-48
lines changed

app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.greenrobot.eventbus.EventBus;
2929
import org.greenrobot.eventbus.Subscribe;
3030
import org.json.JSONException;
31+
import org.json.JSONObject;
3132

3233
import java.io.File;
3334
import java.io.IOException;
@@ -40,23 +41,23 @@
4041
import javax.inject.Named;
4142

4243
import ml.docilealligator.infinityforreddit.Infinity;
43-
import ml.docilealligator.infinityforreddit.thing.GiphyGif;
44-
import ml.docilealligator.infinityforreddit.thing.MediaMetadata;
4544
import ml.docilealligator.infinityforreddit.R;
46-
import ml.docilealligator.infinityforreddit.thing.UploadedImage;
4745
import ml.docilealligator.infinityforreddit.adapters.MarkdownBottomBarRecyclerViewAdapter;
4846
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
4947
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UploadedImagesBottomSheetFragment;
48+
import ml.docilealligator.infinityforreddit.comment.Comment;
49+
import ml.docilealligator.infinityforreddit.comment.ParseComment;
5050
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
5151
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed;
5252
import ml.docilealligator.infinityforreddit.databinding.ActivityEditCommentBinding;
5353
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
5454
import ml.docilealligator.infinityforreddit.markdown.RichTextJSONConverter;
55+
import ml.docilealligator.infinityforreddit.thing.GiphyGif;
56+
import ml.docilealligator.infinityforreddit.thing.MediaMetadata;
57+
import ml.docilealligator.infinityforreddit.thing.UploadedImage;
5558
import ml.docilealligator.infinityforreddit.utils.APIUtils;
5659
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
5760
import ml.docilealligator.infinityforreddit.utils.Utils;
58-
import retrofit2.Call;
59-
import retrofit2.Callback;
6061
import retrofit2.Response;
6162
import retrofit2.Retrofit;
6263

@@ -67,6 +68,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab
6768
public static final String EXTRA_FULLNAME = "EF";
6869
public static final String EXTRA_MEDIA_METADATA_LIST = "EMML";
6970
public static final String EXTRA_POSITION = "EP";
71+
public static final String RETURN_EXTRA_EDITED_COMMENT = "REEC";
7072
public static final String RETURN_EXTRA_EDITED_COMMENT_CONTENT = "REECC";
7173
public static final String RETURN_EXTRA_EDITED_COMMENT_POSITION = "REECP";
7274

@@ -266,33 +268,51 @@ private void editComment() {
266268
params.put(APIUtils.TEXT_KEY, content);
267269
}
268270

269-
mOauthRetrofit.create(RedditAPI.class)
270-
.editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params)
271-
.enqueue(new Callback<>() {
272-
@Override
273-
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
271+
Handler handler = new Handler(getMainLooper());
272+
mExecutor.execute(() -> {
273+
try {
274+
Response<String> response = mOauthRetrofit.create(RedditAPI.class)
275+
.editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params).execute();
276+
if (response.isSuccessful()) {
277+
Comment comment = ParseComment.parseSingleComment(new JSONObject(response.body()), 0);
278+
handler.post(() -> {
274279
isSubmitting = false;
275-
if (response.isSuccessful()) {
276-
Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show();
277-
278-
Intent returnIntent = new Intent();
279-
returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_CONTENT, Utils.modifyMarkdown(content));
280-
returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION));
281-
setResult(RESULT_OK, returnIntent);
282-
283-
finish();
284-
} else {
285-
Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
286-
}
287-
}
288-
289-
@Override
290-
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
280+
Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show();
281+
282+
Intent returnIntent = new Intent();
283+
returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT, comment);
284+
returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION));
285+
setResult(RESULT_OK, returnIntent);
286+
287+
finish();
288+
});
289+
} else {
290+
handler.post(() -> {
291291
isSubmitting = false;
292292
Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
293-
}
293+
});
294+
}
295+
} catch (IOException e) {
296+
e.printStackTrace();
297+
handler.post(() -> {
298+
isSubmitting = false;
299+
Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
294300
});
301+
} catch (JSONException e) {
302+
e.printStackTrace();
303+
handler.post(() -> {
304+
isSubmitting = false;
305+
Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show();
295306

307+
Intent returnIntent = new Intent();
308+
returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_CONTENT, Utils.modifyMarkdown(content));
309+
returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION));
310+
setResult(RESULT_OK, returnIntent);
311+
312+
finish();
313+
});
314+
}
315+
});
296316
}
297317
}
298318

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,20 @@ public boolean isNsfwSubreddit() {
405405
return isNsfwSubreddit;
406406
}
407407

408-
private void editComment(String commentAuthor, String commentContentMarkdown, int position) {
408+
private void editComment(Comment comment, int position) {
409409
if (sectionsPagerAdapter != null) {
410410
ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment();
411411
if (fragment != null) {
412-
fragment.editComment(commentAuthor, commentContentMarkdown, position);
412+
fragment.editComment(comment, position);
413+
}
414+
}
415+
}
416+
417+
private void editComment(String commentContentMarkdown, int position) {
418+
if (sectionsPagerAdapter != null) {
419+
ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment();
420+
if (fragment != null) {
421+
fragment.editComment(commentContentMarkdown, position);
413422
}
414423
}
415424
}
@@ -768,9 +777,13 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
768777
super.onActivityResult(requestCode, resultCode, data);
769778
if (requestCode == EDIT_COMMENT_REQUEST_CODE) {
770779
if (data != null && resultCode == Activity.RESULT_OK) {
771-
editComment(null,
772-
data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT),
773-
data.getExtras().getInt(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION));
780+
if (data.hasExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT)) {
781+
editComment((Comment) data.getParcelableExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT),
782+
data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1));
783+
} else {
784+
editComment(data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT),
785+
data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1));
786+
}
774787
}
775788
} else if (requestCode == CommentActivity.WRITE_COMMENT_REQUEST_CODE) {
776789
if (data != null && resultCode == Activity.RESULT_OK) {

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,9 +1229,15 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
12291229
if (requestCode == EDIT_COMMENT_REQUEST_CODE) {
12301230
if (data != null) {
12311231
if (sectionsPagerAdapter != null) {
1232-
sectionsPagerAdapter.editComment(
1233-
data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT),
1234-
data.getExtras().getInt(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION));
1232+
if (data.hasExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT)) {
1233+
sectionsPagerAdapter.editComment(
1234+
(Comment) data.getParcelableExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT),
1235+
data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1));
1236+
} else {
1237+
sectionsPagerAdapter.editComment(
1238+
data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT),
1239+
data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1));
1240+
}
12351241
}
12361242
}
12371243
}
@@ -1714,6 +1720,15 @@ void displaySortTypeInToolbar() {
17141720
}
17151721
}
17161722

1723+
void editComment(Comment comment, int position) {
1724+
if (fragmentManager != null) {
1725+
Fragment fragment = fragmentManager.findFragmentByTag("f1");
1726+
if (fragment instanceof CommentsListingFragment) {
1727+
((CommentsListingFragment) fragment).editComment(comment, position);
1728+
}
1729+
}
1730+
}
1731+
17171732
void editComment(String commentMarkdown, int position) {
17181733
if (fragmentManager != null) {
17191734
Fragment fragment = fragmentManager.findFragmentByTag("f1");

app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ public void onItemSwipe(RecyclerView.ViewHolder viewHolder, int direction, int s
365365
}
366366
}
367367

368+
public void editComment(Comment comment, int position) {
369+
Comment oldComment = getItem(position);
370+
if (oldComment != null) {
371+
oldComment.setCommentMarkdown(comment.getCommentMarkdown());
372+
oldComment.setMediaMetadataMap(comment.getMediaMetadataMap());
373+
notifyItemChanged(position);
374+
}
375+
}
376+
368377
public void editComment(String commentContentMarkdown, int position) {
369378
Comment comment = getItem(position);
370379
if (comment != null) {

app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.os.Bundle;
1010
import android.os.Handler;
1111
import android.text.Spanned;
12+
import android.util.Log;
1213
import android.view.LayoutInflater;
1314
import android.view.View;
1415
import android.view.ViewGroup;
@@ -1052,18 +1053,31 @@ public void loadMoreCommentsFailed() {
10521053
}
10531054
}
10541055

1055-
public void editComment(String commentAuthor, String commentContentMarkdown, int position) {
1056-
if (commentAuthor != null) {
1057-
mVisibleComments.get(position).setAuthor(commentAuthor);
1058-
}
1056+
public void editComment(Comment comment, int position) {
1057+
if (position < mVisibleComments.size() && position >= 0) {
1058+
Comment oldComment = mVisibleComments.get(position);
1059+
if (oldComment.getId().equals(comment.getId())) {
1060+
oldComment.setCommentMarkdown(comment.getCommentMarkdown());
1061+
oldComment.setMediaMetadataMap(comment.getMediaMetadataMap());
10591062

1060-
mVisibleComments.get(position).setSubmittedByAuthor(mVisibleComments.get(position).isSubmitter());
1063+
if (mIsSingleCommentThreadMode) {
1064+
notifyItemChanged(position + 1);
1065+
Log.i("asdfasdf", "asdf");
1066+
} else {
1067+
notifyItemChanged(position);
1068+
}
1069+
}
1070+
}
1071+
}
10611072

1062-
mVisibleComments.get(position).setCommentMarkdown(commentContentMarkdown);
1063-
if (mIsSingleCommentThreadMode) {
1064-
notifyItemChanged(position + 1);
1065-
} else {
1066-
notifyItemChanged(position);
1073+
public void editComment(String commentContentMarkdown, int position) {
1074+
if (position < mVisibleComments.size() && position >= 0) {
1075+
mVisibleComments.get(position).setCommentMarkdown(commentContentMarkdown);
1076+
if (mIsSingleCommentThreadMode) {
1077+
notifyItemChanged(position + 1);
1078+
} else {
1079+
notifyItemChanged(position);
1080+
}
10671081
}
10681082
}
10691083

app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ public Map<String, MediaMetadata> getMediaMetadataMap() {
426426
return mediaMetadataMap;
427427
}
428428

429+
public void setMediaMetadataMap(Map<String, MediaMetadata> mediaMetadataMap) {
430+
this.mediaMetadataMap = mediaMetadataMap;
431+
}
432+
429433
@Override
430434
public int describeContents() {
431435
return 0;

app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private static void expandChildren(ArrayList<Comment> comments, ArrayList<Commen
253253
}
254254
}
255255

256-
static Comment parseSingleComment(JSONObject singleCommentData, int depth) throws JSONException {
256+
public static Comment parseSingleComment(JSONObject singleCommentData, int depth) throws JSONException {
257257
String id = singleCommentData.getString(JSONUtils.ID_KEY);
258258
String fullName = singleCommentData.getString(JSONUtils.NAME_KEY);
259259
String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY);

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ public SortType getSortType() {
398398
return sortType;
399399
}
400400

401+
public void editComment(Comment comment, int position) {
402+
if (mAdapter != null) {
403+
mAdapter.editComment(comment, position);
404+
}
405+
}
406+
401407
public void editComment(String commentMarkdown, int position) {
402408
if (mAdapter != null) {
403409
mAdapter.editComment(commentMarkdown, position);

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,15 @@ public void addChildComment(Comment comment, String parentFullname, int parentPo
749749
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
750750
}
751751

752-
public void editComment(String commentAuthor, String commentContentMarkdown, int position) {
752+
public void editComment(Comment comment, int position) {
753753
if (mCommentsAdapter != null) {
754-
mCommentsAdapter.editComment(commentAuthor,
754+
mCommentsAdapter.editComment(comment, position);
755+
}
756+
}
757+
758+
public void editComment(String commentContentMarkdown, int position) {
759+
if (mCommentsAdapter != null) {
760+
mCommentsAdapter.editComment(
755761
commentContentMarkdown,
756762
position);
757763
}

0 commit comments

Comments
 (0)