edit post caption (just basic design, still WIP)

This commit is contained in:
Austin Huang 2020-12-23 17:12:25 -05:00
parent 61e7e5671e
commit f909408916
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
7 changed files with 115 additions and 80 deletions

View File

@ -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<ViewerPostModelWrapper, PostViewerViewHolder> {
// private final OnPostViewChildViewClickListener clickListener;
// private final OnPostCaptionLongClickListener longClickListener;
// private final MentionClickListener mentionClickListener;
//
// private static final DiffUtil.ItemCallback<ViewerPostModelWrapper> diffCallback = new DiffUtil.ItemCallback<ViewerPostModelWrapper>() {
// @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);
// }
// }

View File

@ -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<Boolean>() {
@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();

View File

@ -41,6 +41,11 @@ public interface MediaRepository {
Call<String> commentUnlike(@Path("commentId") final String commentId,
@FieldMap final Map<String, String> signedForm);
@FormUrlEncoded
@POST("/api/v1/media/{mediaId}/edit_media/")
Call<String> editCaption(@Path("mediaId") final String mediaId,
@FieldMap final Map<String, String> signedForm);
@GET("/api/v1/language/translate/")
Call<String> translate(@QueryMap final Map<String, String> form);
}

View File

@ -16,4 +16,12 @@ public class BasePostViewModel<T extends BasePostModel> extends ViewModel {
}
return list;
}
public void edit(int index, T post) {
}
public void delete(int index) {
}
}

View File

@ -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<Boolean> callback) {
final Map<String, Object> 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<String, String> signedForm = Utils.sign(form);
final Call<String> request = repository.editCaption(postId, signedForm);
request.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> 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<String> call, @NonNull final Throwable t) {
Log.e(TAG, "Error editing caption", t);
callback.onFailure(t);
}
});
}
public void fetchLikes(final String mediaId,
@NonNull final ServiceCallback<List<ProfileModel>> callback) {
final Call<String> likesRequest = repository.fetchLikes(mediaId);

View File

@ -180,14 +180,30 @@
app:layout_constraintBottom_toTopOf="@id/translateTitle"
tools:text="Text text text" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/editCaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:background="@null"
android:gravity="center_vertical"
android:text="@string/edit_caption"
android:textColor="?android:textColorSecondary"
android:textSize="16dp"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/translatedCaption"
app:layout_constraintTop_toBottomOf="@id/caption" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/translateTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:background="@null"
android:visibility="visible"
android:gravity="center_vertical"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@id/caption"
app:layout_constraintBottom_toTopOf="@id/translatedCaption"
android:text="@string/translate_caption"

View File

@ -292,6 +292,7 @@
<string name="apply">Apply</string>
<string name="save">Save</string>
<string name="caption">Caption</string>
<string name="edit_caption">Edit caption...</string>
<string name="translate_caption">Translate caption...</string>
<string name="player_timeline_desc">Video player timeline</string>
<string name="one_x" translatable="false">1x</string>