From f90940891696cee288cf7f3970276928dfde6c61 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Wed, 23 Dec 2020 17:12:25 -0500 Subject: [PATCH] edit post caption (just basic design, still WIP) --- .../adapters/PostViewAdapter.java | 75 ------------------- .../fragments/PostViewV2Fragment.java | 47 +++++++++++- .../repositories/MediaRepository.java | 5 ++ .../viewmodels/BasePostViewModel.java | 8 ++ .../webservices/MediaService.java | 41 ++++++++++ app/src/main/res/layout/dialog_post_view.xml | 18 ++++- app/src/main/res/values/strings.xml | 1 + 7 files changed, 115 insertions(+), 80 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/adapters/PostViewAdapter.java diff --git a/app/src/main/java/awais/instagrabber/adapters/PostViewAdapter.java b/app/src/main/java/awais/instagrabber/adapters/PostViewAdapter.java deleted file mode 100644 index f49b8974..00000000 --- a/app/src/main/java/awais/instagrabber/adapters/PostViewAdapter.java +++ /dev/null @@ -1,75 +0,0 @@ -// package awais.instagrabber.adapters; -// -// import android.view.LayoutInflater; -// import android.view.View; -// import android.view.ViewGroup; -// -// import androidx.annotation.NonNull; -// import androidx.recyclerview.widget.DiffUtil; -// import androidx.recyclerview.widget.ListAdapter; -// -// import awais.instagrabber.adapters.viewholder.PostViewerViewHolder; -// import awais.instagrabber.databinding.ItemFullPostViewBinding; -// import awais.instagrabber.interfaces.MentionClickListener; -// import awais.instagrabber.models.ViewerPostModelWrapper; -// -// public class PostViewAdapter extends ListAdapter { -// private final OnPostViewChildViewClickListener clickListener; -// private final OnPostCaptionLongClickListener longClickListener; -// private final MentionClickListener mentionClickListener; -// -// private static final DiffUtil.ItemCallback diffCallback = new DiffUtil.ItemCallback() { -// @Override -// public boolean areItemsTheSame(@NonNull final ViewerPostModelWrapper oldItem, -// @NonNull final ViewerPostModelWrapper newItem) { -// return oldItem.getPosition() == newItem.getPosition(); -// } -// -// @Override -// public boolean areContentsTheSame(@NonNull final ViewerPostModelWrapper oldItem, -// @NonNull final ViewerPostModelWrapper newItem) { -// return oldItem.getViewerPostModels().equals(newItem.getViewerPostModels()); -// } -// }; -// -// public PostViewAdapter(final OnPostViewChildViewClickListener clickListener, -// final OnPostCaptionLongClickListener longClickListener, -// final MentionClickListener mentionClickListener) { -// super(diffCallback); -// this.clickListener = clickListener; -// this.longClickListener = longClickListener; -// this.mentionClickListener = mentionClickListener; -// } -// -// @Override -// public void onViewDetachedFromWindow(@NonNull final PostViewerViewHolder holder) { -// holder.stopPlayingVideo(); -// } -// -// @NonNull -// @Override -// public PostViewerViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, -// final int viewType) { -// final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); -// final ItemFullPostViewBinding binding = ItemFullPostViewBinding -// .inflate(layoutInflater, parent, false); -// return new PostViewerViewHolder(binding); -// } -// -// @Override -// public void onBindViewHolder(@NonNull final PostViewerViewHolder holder, final int position) { -// final ViewerPostModelWrapper item = getItem(position); -// holder.bind(item, position, clickListener, longClickListener, mentionClickListener); -// } -// -// public interface OnPostViewChildViewClickListener { -// void onClick(View v, -// ViewerPostModelWrapper viewerPostModelWrapper, -// int postPosition, -// int childPosition); -// } -// -// public interface OnPostCaptionLongClickListener { -// void onLongClick(String text); -// } -// } diff --git a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java index 4b606e0c..c37b2d1e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java @@ -26,6 +26,7 @@ import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; +import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ScrollView; import android.widget.Toast; @@ -33,6 +34,7 @@ import android.widget.ViewSwitcher; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.core.content.PermissionChecker; import androidx.core.view.ViewCompat; @@ -114,6 +116,7 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { private DialogInterface.OnShowListener onShowListener; private boolean isLoggedIn; private boolean hasBeenToggled = false; + private CharSequence postCaption = null; private final VerticalDragHelper.OnVerticalDragListener onVerticalDragListener = new VerticalDragHelper.OnVerticalDragListener() { @@ -415,8 +418,7 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { private void init() { if (feedModel == null) return; - final String cookie = settingsHelper.getString(Constants.COOKIE); - isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != null; + isLoggedIn = !TextUtils.isEmpty(COOKIE) && CookieUtils.getUserIdFromCookie(COOKIE) != null; if (!wasPaused && (sharedProfilePicElement != null || sharedMainPostElement != null)) { binding.getRoot().getBackground().mutate().setAlpha(0); } @@ -710,14 +712,51 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { } private void setupCaption() { - final CharSequence postCaption = feedModel.getPostCaption(); + if (postCaption == null) postCaption = feedModel.getPostCaption(); binding.date.setText(Utils.datetimeParser.format(new Date(feedModel.getTimestamp() * 1000L))); - if (TextUtils.isEmpty(postCaption)) { + if (!feedModel.getProfileModel().getId().equals(CookieUtils.getUserIdFromCookie(COOKIE)) && TextUtils.isEmpty(postCaption)) { binding.caption.setVisibility(View.GONE); binding.translateTitle.setVisibility(View.GONE); binding.captionToggle.setVisibility(View.GONE); return; } + if (feedModel.getProfileModel().getId().equals(CookieUtils.getUserIdFromCookie(COOKIE))) { + binding.editCaption.setVisibility(View.VISIBLE); + binding.editCaption.setOnClickListener(v -> { + final EditText input = new EditText(context); + input.setText(postCaption); + new AlertDialog.Builder(context) + .setTitle(R.string.edit_caption) + .setView(input) + .setPositiveButton(R.string.confirm, (d, w) -> { + binding.editCaption.setVisibility(View.GONE); + mediaService.editCaption( + feedModel.getPostId(), + CookieUtils.getUserIdFromCookie(COOKIE), + input.getText().toString(), + CookieUtils.getCsrfTokenFromCookie(COOKIE), + new ServiceCallback() { + @Override + public void onSuccess(final Boolean result) { + binding.editCaption.setVisibility(View.VISIBLE); + if (result) { + binding.caption.setText(input.getText().toString()); + } + else Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error editing caption", t); + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + binding.editCaption.setVisibility(View.VISIBLE); + } + }); + }) + .setNegativeButton(R.string.cancel, null) + .show(); + }); + } binding.caption.addOnHashtagListener(autoLinkItem -> { final NavController navController = NavHostFragment.findNavController(this); final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/awais/instagrabber/repositories/MediaRepository.java b/app/src/main/java/awais/instagrabber/repositories/MediaRepository.java index 525a666f..ba3db923 100644 --- a/app/src/main/java/awais/instagrabber/repositories/MediaRepository.java +++ b/app/src/main/java/awais/instagrabber/repositories/MediaRepository.java @@ -41,6 +41,11 @@ public interface MediaRepository { Call commentUnlike(@Path("commentId") final String commentId, @FieldMap final Map signedForm); + @FormUrlEncoded + @POST("/api/v1/media/{mediaId}/edit_media/") + Call editCaption(@Path("mediaId") final String mediaId, + @FieldMap final Map signedForm); + @GET("/api/v1/language/translate/") Call translate(@QueryMap final Map form); } diff --git a/app/src/main/java/awais/instagrabber/viewmodels/BasePostViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/BasePostViewModel.java index b0f59e64..eb4dc8d2 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/BasePostViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/BasePostViewModel.java @@ -16,4 +16,12 @@ public class BasePostViewModel extends ViewModel { } return list; } + + public void edit(int index, T post) { + + } + + public void delete(int index) { + + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/webservices/MediaService.java b/app/src/main/java/awais/instagrabber/webservices/MediaService.java index 3e0c8a04..ed8304f3 100644 --- a/app/src/main/java/awais/instagrabber/webservices/MediaService.java +++ b/app/src/main/java/awais/instagrabber/webservices/MediaService.java @@ -281,6 +281,47 @@ public class MediaService extends BaseService { }); } + public void editCaption(final String postId, + final String userId, + final String newCaption, + @NonNull final String csrfToken, + @NonNull final ServiceCallback callback) { + final Map form = new HashMap<>(); + form.put("_csrftoken", csrfToken); + form.put("_uid", userId); + form.put("_uuid", UUID.randomUUID().toString()); + form.put("igtv_feed_preview", "false"); + form.put("media_id", postId); + form.put("caption_text", newCaption); + final Map signedForm = Utils.sign(form); + final Call request = repository.editCaption(postId, signedForm); + request.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, @NonNull final Response response) { + final String body = response.body(); + if (body == null) { + Log.e(TAG, "Error occurred while editing caption"); + callback.onSuccess(false); + return; + } + try { + final JSONObject jsonObject = new JSONObject(body); + final String status = jsonObject.optString("status"); + callback.onSuccess(status.equals("ok")); + } catch (JSONException e) { + // Log.e(TAG, "Error parsing body", e); + callback.onFailure(e); + } + } + + @Override + public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { + Log.e(TAG, "Error editing caption", t); + callback.onFailure(t); + } + }); + } + public void fetchLikes(final String mediaId, @NonNull final ServiceCallback> callback) { final Call likesRequest = repository.fetchLikes(mediaId); diff --git a/app/src/main/res/layout/dialog_post_view.xml b/app/src/main/res/layout/dialog_post_view.xml index f1f220ad..e07afcfb 100644 --- a/app/src/main/res/layout/dialog_post_view.xml +++ b/app/src/main/res/layout/dialog_post_view.xml @@ -180,14 +180,30 @@ app:layout_constraintBottom_toTopOf="@id/translateTitle" tools:text="Text text text" /> + + Apply Save Caption + Edit caption... Translate caption... Video player timeline 1x