diff options
9 files changed, 135 insertions, 48 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java index 1cb27b7c..b24cbf19 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java @@ -28,6 +28,7 @@ import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.json.JSONException; +import org.json.JSONObject; import java.io.File; import java.io.IOException; @@ -40,23 +41,23 @@ import javax.inject.Inject; import javax.inject.Named; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.thing.GiphyGif; -import ml.docilealligator.infinityforreddit.thing.MediaMetadata; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.thing.UploadedImage; import ml.docilealligator.infinityforreddit.adapters.MarkdownBottomBarRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.bottomsheetfragments.UploadedImagesBottomSheetFragment; +import ml.docilealligator.infinityforreddit.comment.Comment; +import ml.docilealligator.infinityforreddit.comment.ParseComment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.databinding.ActivityEditCommentBinding; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.markdown.RichTextJSONConverter; +import ml.docilealligator.infinityforreddit.thing.GiphyGif; +import ml.docilealligator.infinityforreddit.thing.MediaMetadata; +import ml.docilealligator.infinityforreddit.thing.UploadedImage; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; -import retrofit2.Call; -import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; @@ -67,6 +68,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab public static final String EXTRA_FULLNAME = "EF"; public static final String EXTRA_MEDIA_METADATA_LIST = "EMML"; public static final String EXTRA_POSITION = "EP"; + public static final String RETURN_EXTRA_EDITED_COMMENT = "REEC"; public static final String RETURN_EXTRA_EDITED_COMMENT_CONTENT = "REECC"; public static final String RETURN_EXTRA_EDITED_COMMENT_POSITION = "REECP"; @@ -266,33 +268,51 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab params.put(APIUtils.TEXT_KEY, content); } - mOauthRetrofit.create(RedditAPI.class) - .editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params) - .enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + Handler handler = new Handler(getMainLooper()); + mExecutor.execute(() -> { + try { + Response<String> response = mOauthRetrofit.create(RedditAPI.class) + .editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params).execute(); + if (response.isSuccessful()) { + Comment comment = ParseComment.parseSingleComment(new JSONObject(response.body()), 0); + handler.post(() -> { isSubmitting = false; - if (response.isSuccessful()) { - Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show(); - - Intent returnIntent = new Intent(); - returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_CONTENT, Utils.modifyMarkdown(content)); - returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION)); - setResult(RESULT_OK, returnIntent); - - finish(); - } else { - Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); - } - } - - @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show(); + + Intent returnIntent = new Intent(); + returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT, comment); + returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION)); + setResult(RESULT_OK, returnIntent); + + finish(); + }); + } else { + handler.post(() -> { isSubmitting = false; Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); - } + }); + } + } catch (IOException e) { + e.printStackTrace(); + handler.post(() -> { + isSubmitting = false; + Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); }); + } catch (JSONException e) { + e.printStackTrace(); + handler.post(() -> { + isSubmitting = false; + Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show(); + Intent returnIntent = new Intent(); + returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_CONTENT, Utils.modifyMarkdown(content)); + returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION)); + setResult(RESULT_OK, returnIntent); + + finish(); + }); + } + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 7072fff6..1e64bf89 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -405,11 +405,20 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele return isNsfwSubreddit; } - private void editComment(String commentAuthor, String commentContentMarkdown, int position) { + private void editComment(Comment comment, int position) { if (sectionsPagerAdapter != null) { ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); if (fragment != null) { - fragment.editComment(commentAuthor, commentContentMarkdown, position); + fragment.editComment(comment, position); + } + } + } + + private void editComment(String commentContentMarkdown, int position) { + if (sectionsPagerAdapter != null) { + ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); + if (fragment != null) { + fragment.editComment(commentContentMarkdown, position); } } } @@ -768,9 +777,13 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele super.onActivityResult(requestCode, resultCode, data); if (requestCode == EDIT_COMMENT_REQUEST_CODE) { if (data != null && resultCode == Activity.RESULT_OK) { - editComment(null, - data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT), - data.getExtras().getInt(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION)); + if (data.hasExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT)) { + editComment((Comment) data.getParcelableExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT), + data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1)); + } else { + editComment(data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT), + data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1)); + } } } else if (requestCode == CommentActivity.WRITE_COMMENT_REQUEST_CODE) { if (data != null && resultCode == Activity.RESULT_OK) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 009b80d1..cad5c906 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -1229,9 +1229,15 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele if (requestCode == EDIT_COMMENT_REQUEST_CODE) { if (data != null) { if (sectionsPagerAdapter != null) { - sectionsPagerAdapter.editComment( - data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT), - data.getExtras().getInt(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION)); + if (data.hasExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT)) { + sectionsPagerAdapter.editComment( + (Comment) data.getParcelableExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT), + data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1)); + } else { + sectionsPagerAdapter.editComment( + data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT), + data.getIntExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION, -1)); + } } } } @@ -1714,6 +1720,15 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } } + void editComment(Comment comment, int position) { + if (fragmentManager != null) { + Fragment fragment = fragmentManager.findFragmentByTag("f1"); + if (fragment instanceof CommentsListingFragment) { + ((CommentsListingFragment) fragment).editComment(comment, position); + } + } + } + void editComment(String commentMarkdown, int position) { if (fragmentManager != null) { Fragment fragment = fragmentManager.findFragmentByTag("f1"); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java index 184249b1..a475fe19 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java @@ -365,6 +365,15 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment } } + public void editComment(Comment comment, int position) { + Comment oldComment = getItem(position); + if (oldComment != null) { + oldComment.setCommentMarkdown(comment.getCommentMarkdown()); + oldComment.setMediaMetadataMap(comment.getMediaMetadataMap()); + notifyItemChanged(position); + } + } + public void editComment(String commentContentMarkdown, int position) { Comment comment = getItem(position); if (comment != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 3a9db964..42dd48ee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -9,6 +9,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.text.Spanned; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -1052,18 +1053,31 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi } } - public void editComment(String commentAuthor, String commentContentMarkdown, int position) { - if (commentAuthor != null) { - mVisibleComments.get(position).setAuthor(commentAuthor); - } + public void editComment(Comment comment, int position) { + if (position < mVisibleComments.size() && position >= 0) { + Comment oldComment = mVisibleComments.get(position); + if (oldComment.getId().equals(comment.getId())) { + oldComment.setCommentMarkdown(comment.getCommentMarkdown()); + oldComment.setMediaMetadataMap(comment.getMediaMetadataMap()); - mVisibleComments.get(position).setSubmittedByAuthor(mVisibleComments.get(position).isSubmitter()); + if (mIsSingleCommentThreadMode) { + notifyItemChanged(position + 1); + Log.i("asdfasdf", "asdf"); + } else { + notifyItemChanged(position); + } + } + } + } - mVisibleComments.get(position).setCommentMarkdown(commentContentMarkdown); - if (mIsSingleCommentThreadMode) { - notifyItemChanged(position + 1); - } else { - notifyItemChanged(position); + public void editComment(String commentContentMarkdown, int position) { + if (position < mVisibleComments.size() && position >= 0) { + mVisibleComments.get(position).setCommentMarkdown(commentContentMarkdown); + if (mIsSingleCommentThreadMode) { + notifyItemChanged(position + 1); + } else { + notifyItemChanged(position); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java index 140e0332..d67c5550 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java @@ -426,6 +426,10 @@ public class Comment implements Parcelable { return mediaMetadataMap; } + public void setMediaMetadataMap(Map<String, MediaMetadata> mediaMetadataMap) { + this.mediaMetadataMap = mediaMetadataMap; + } + @Override public int describeContents() { return 0; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java index 82dc1fca..451e1558 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -253,7 +253,7 @@ public class ParseComment { } } - static Comment parseSingleComment(JSONObject singleCommentData, int depth) throws JSONException { + public static Comment parseSingleComment(JSONObject singleCommentData, int depth) throws JSONException { String id = singleCommentData.getString(JSONUtils.ID_KEY); String fullName = singleCommentData.getString(JSONUtils.NAME_KEY); String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java index e4a3adbe..f8e5968b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java @@ -398,6 +398,12 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni return sortType; } + public void editComment(Comment comment, int position) { + if (mAdapter != null) { + mAdapter.editComment(comment, position); + } + } + public void editComment(String commentMarkdown, int position) { if (mAdapter != null) { mAdapter.editComment(commentMarkdown, position); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index a2e43d08..d8f8b03d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -749,9 +749,15 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); } - public void editComment(String commentAuthor, String commentContentMarkdown, int position) { + public void editComment(Comment comment, int position) { if (mCommentsAdapter != null) { - mCommentsAdapter.editComment(commentAuthor, + mCommentsAdapter.editComment(comment, position); + } + } + + public void editComment(String commentContentMarkdown, int position) { + if (mCommentsAdapter != null) { + mCommentsAdapter.editComment( commentContentMarkdown, position); } |