From c19ff317fd5a71881641f476d0445009cca8f666 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Mon, 2 Nov 2020 21:12:18 -0500 Subject: [PATCH] comments and verified * fix comments count on profile posts * all comments should be visible now * add verified badge to comments and post title --- .../adapters/CommentsAdapter.java | 10 +- .../comments/ChildCommentViewHolder.java | 3 + .../comments/ParentCommentViewHolder.java | 3 + .../instagrabber/asyncs/CommentsFetcher.java | 2 +- .../fragments/PostViewFragment.java | 340 ------------------ .../fragments/PostViewV2Fragment.java | 1 + .../webservices/ProfileService.java | 2 +- app/src/main/res/layout/dialog_post_view.xml | 19 +- app/src/main/res/layout/item_comment.xml | 16 +- .../main/res/layout/item_comment_small.xml | 14 +- 10 files changed, 58 insertions(+), 352 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java diff --git a/app/src/main/java/awais/instagrabber/adapters/CommentsAdapter.java b/app/src/main/java/awais/instagrabber/adapters/CommentsAdapter.java index fb59c71b..e0ccb44e 100755 --- a/app/src/main/java/awais/instagrabber/adapters/CommentsAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/CommentsAdapter.java @@ -142,10 +142,12 @@ public final class CommentsAdapter extends ListAdapter children = parent.getChildCommentModels(); - for (final CommentModel child : children) { - lastCommentIndex++; - flatList.add(child); - positionTypeMap.put(lastCommentIndex, TYPE_CHILD); + if (children != null) { + for (final CommentModel child : children) { + lastCommentIndex++; + flatList.add(child); + positionTypeMap.put(lastCommentIndex, TYPE_CHILD); + } } } return flatList; diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ChildCommentViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ChildCommentViewHolder.java index 27c82580..bc5aecd8 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ChildCommentViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ChildCommentViewHolder.java @@ -1,5 +1,7 @@ package awais.instagrabber.adapters.viewholder.comments; +import android.view.View; + import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; @@ -77,6 +79,7 @@ public final class ChildCommentViewHolder extends RecyclerView.ViewHolder { if (profileModel == null) return; binding.tvUsername.setText(profileModel.getUsername()); binding.ivProfilePic.setImageURI(profileModel.getSdProfilePic()); + binding.isVerified.setVisibility(profileModel.isVerified() ? View.VISIBLE : View.GONE); } private void setLikes(final int likes) { diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ParentCommentViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ParentCommentViewHolder.java index 71ae48c2..2b595d9e 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ParentCommentViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/comments/ParentCommentViewHolder.java @@ -1,5 +1,7 @@ package awais.instagrabber.adapters.viewholder.comments; +import android.view.View; + import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; @@ -77,6 +79,7 @@ public final class ParentCommentViewHolder extends RecyclerView.ViewHolder { if (profileModel == null) return; binding.tvUsername.setText(profileModel.getUsername()); binding.ivProfilePic.setImageURI(profileModel.getSdProfilePic()); + binding.isVerified.setVisibility(profileModel.isVerified() ? View.VISIBLE : View.GONE); } private void setLikes(final int likes) { diff --git a/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java index d1736b49..7c1a5c43 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java @@ -269,8 +269,8 @@ public final class CommentsFetcher extends AsyncTask idOrCodeList; -// private boolean hasInitialResult = false; -// private PostViewAdapter adapter; -// private boolean session; -// private MediaService mediaService; -// -// private FetchListener pfl = result -> { -// if (result == null) return; -// final List viewerPostModels = viewerPostViewModel.getList().getValue(); -// final List temp = viewerPostModels == null ? new ArrayList<>(idOrCodeList.size()) -// : new ArrayList<>(viewerPostModels); -// String idOrCode = isId ? result.getPostId() : result.getShortCode(); -// if (idOrCode == null) return; -// if (isId) { -// // the post id is appended with `_` in the result -// idOrCode = idOrCode.substring(0, idOrCode.indexOf('_')); -// } -// final int index = idOrCodeList.indexOf(idOrCode); -// if (index < 0) return; -// final ViewerPostModelWrapper viewerPostModelWrapper = temp.get(index); -// viewerPostModelWrapper.setViewerPostModels(result.getSliderItems() == null ? Collections.emptyList() : result.getSliderItems()); -// temp.set(index, viewerPostModelWrapper); -// viewerPostViewModel.getList().setValue(temp); -// adapter.notifyItemChanged(index); -// if (!hasInitialResult) { -// Log.d(TAG, "setting delayed position to: " + currentPostIndex); -// binding.getRoot() -// .postDelayed(() -> binding.getRoot().setCurrentItem(currentPostIndex), 200); -// } -// hasInitialResult = true; -// }; -// private MentionClickListener mentionListener = (view, text, isHashtag, isLocation) -> { -// if (isHashtag) { -// final NavDirections action = PostViewFragmentDirections -// .actionGlobalHashTagFragment(text); -// NavHostFragment.findNavController(this).navigate(action); -// return; -// } -// if (isLocation) { -// final NavDirections action = PostViewFragmentDirections -// .actionGlobalLocationFragment(text); -// NavHostFragment.findNavController(this).navigate(action); -// return; -// } -// final NavDirections action = PostViewFragmentDirections -// .actionGlobalProfileFragment("@" + text); -// NavHostFragment.findNavController(this).navigate(action); -// }; -// private OnPostViewChildViewClickListener clickListener = (v, wrapper, postPosition, childPosition) -> { -// final ViewerPostModel postModel = wrapper.getViewerPostModels().get(0); -// final String username = postModel.getProfileModel().getUsername(); -// final int id = v.getId(); -// switch (id) { -// case R.id.viewerCaption: -// break; -// case R.id.btnComments: -// String postId = postModel.getPostId(); -// if (postId.contains("_")) postId = postId.substring(0, postId.indexOf("_")); -// final NavDirections commentsAction = PostViewFragmentDirections.actionGlobalCommentsViewerFragment( -// postModel.getShortCode(), -// postId, -// postModel.getProfileModel().getId() -// ); -// NavHostFragment.findNavController(this).navigate(commentsAction); -// break; -// case R.id.btnDownload: -// final Context context = getContext(); -// if (context == null) return; -// if (checkSelfPermission(context, -// DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) { -// showDownloadDialog(wrapper.getViewerPostModels(), -// childPosition, -// username); -// return; -// } -// requestPermissions(DownloadUtils.PERMS, 8020); -// break; -// case R.id.ivProfilePic: -// case R.id.title: -// mentionListener.onClick(null, username, false, false); -// break; -// case R.id.btnLike: -// if (mediaService != null) { -// final String userId = CookieUtils.getUserIdFromCookie(COOKIE); -// final String csrfToken = CookieUtils.getCsrfTokenFromCookie(COOKIE); -// v.setEnabled(false); -// final ServiceCallback likeCallback = new ServiceCallback() { -// @Override -// public void onSuccess(final Boolean result) { -// v.setEnabled(true); -// if (result) { -// postModel.setManualLike(!postModel.getLike()); -// adapter.notifyItemChanged(postPosition); -// return; -// } -// Log.e(TAG, "like/unlike unsuccessful!"); -// } -// -// @Override -// public void onFailure(final Throwable t) { -// v.setEnabled(true); -// Log.e(TAG, "Error during like/unlike", t); -// } -// }; -// if (!postModel.getLike()) { -// mediaService.like(postModel.getPostId(), userId, csrfToken, likeCallback); -// } else { -// mediaService.unlike(postModel.getPostId(), userId, csrfToken, likeCallback); -// } -// } -// break; -// case R.id.btnBookmark: -// if (mediaService != null) { -// final String userId = CookieUtils.getUserIdFromCookie(COOKIE); -// final String csrfToken = CookieUtils.getCsrfTokenFromCookie(COOKIE); -// v.setEnabled(false); -// final ServiceCallback saveCallback = new ServiceCallback() { -// @Override -// public void onSuccess(final Boolean result) { -// v.setEnabled(true); -// if (result) { -// // postModel.setBookmarked(!postModel.isSaved()); -// adapter.notifyItemChanged(postPosition); -// return; -// } -// Log.e(TAG, "save/unsave unsuccessful!"); -// } -// -// @Override -// public void onFailure(final Throwable t) { -// v.setEnabled(true); -// Log.e(TAG, "Error during save/unsave", t); -// } -// }; -// if (!postModel.isSaved()) { -// mediaService.save(postModel.getPostId(), userId, csrfToken, saveCallback); -// } else { -// mediaService.unsave(postModel.getPostId(), userId, csrfToken, saveCallback); -// } -// } -// break; -// } -// }; -// private PostViewAdapter.OnPostCaptionLongClickListener captionLongClickListener = text -> { -// final Context context = getContext(); -// if (context == null) return; -// Utils.copyText(context, text); -// }; -// -// @Override -// public void onCreate(@Nullable final Bundle savedInstanceState) { -// super.onCreate(savedInstanceState); -// fragmentActivity = getActivity(); -// mediaService = MediaService.getInstance(); -// } -// -// @Nullable -// @Override -// public View onCreateView(@NonNull final LayoutInflater inflater, -// @Nullable final ViewGroup container, -// @Nullable final Bundle savedInstanceState) { -// if (root != null) { -// shouldRefresh = false; -// return root; -// } -// binding = FragmentPostViewBinding.inflate(inflater, container, false); -// root = binding.getRoot(); -// setupViewPager(); -// return root; -// } -// -// @Override -// public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { -// if (!shouldRefresh) return; -// init(); -// shouldRefresh = false; -// } -// -// private void setupViewPager() { -// viewerPostViewModel = new ViewModelProvider(fragmentActivity) -// .get(ViewerPostViewModel.class); -// adapter = new PostViewAdapter(clickListener, captionLongClickListener, mentionListener); -// root.setAdapter(adapter); -// root.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { -// -// @Override -// public void onPageSelected(final int position) { -// // Log.d(TAG, "onPageSelected: " + position + ", hasInitialResult: " + hasInitialResult); -// if (!hasInitialResult) { -// return; -// } -// currentPostIndex = position; -// fetchPost(); -// } -// }); -// viewerPostViewModel.getList().observe(fragmentActivity, list -> adapter.submitList(list)); -// } -// -// private void init() { -// if (getArguments() == null) return; -// final PostViewFragmentArgs fragmentArgs = PostViewFragmentArgs.fromBundle(getArguments()); -// final String[] idOrCodeArray = fragmentArgs.getIdOrCodeArray(); -// if (idOrCodeArray.length == 0) return; -// currentPostIndex = fragmentArgs.getIndex(); -// if (currentPostIndex < 0) return; -// if (currentPostIndex >= idOrCodeArray.length) return; -// idOrCodeList = Arrays.asList(idOrCodeArray); -// viewerPostViewModel.getList().setValue(createPlaceholderModels(idOrCodeArray.length)); -// isId = fragmentArgs.getIsId(); -// fetchPost(); -// } -// -// private List createPlaceholderModels(final int size) { -// final List viewerPostModels = new ArrayList<>(size); -// for (int i = 0; i < size; i++) { -// // viewerPostModels.add(new ViewerPostModel[]{ViewerPostModel.getDefaultModel(-i, "")}); -// viewerPostModels.add(new ViewerPostModelWrapper(i, null)); -// } -// return viewerPostModels; -// } -// -// private void fetchPost() { -// // Log.d(TAG, "fetchPost, currentPostIndex: " + currentPostIndex); -// final List list = viewerPostViewModel.getList().getValue(); -// if (list != null) { -// final ViewerPostModelWrapper viewerPostModels = list.get(currentPostIndex); -// if (viewerPostModels != null && viewerPostModels -// .getViewerPostModels() != null && viewerPostModels -// .getViewerPostModels().size() > 0) { -// Log.d(TAG, "returning without fetching"); -// return; -// } -// } -// if (currentPostIndex >= idOrCodeList.size() || currentPostIndex < 0) return; -// final String idOrShortCode = idOrCodeList.get(currentPostIndex); -// if (isId) { -// new iPostFetcher(idOrShortCode, pfl).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); -// return; -// } -// new PostFetcher(idOrShortCode, pfl).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); -// } -// -// private void showDownloadDialog(final List postModels, -// final int childPosition, -// final String username) { -// final List postModelsToDownload = new ArrayList<>(); -// final Context context = getContext(); -// if (context == null) return; -// if (!session && postModels.size() > 1) { -// final DialogInterface.OnClickListener clickListener = (dialog, which) -> { -// if (which == DialogInterface.BUTTON_NEGATIVE) { -// postModelsToDownload.addAll(postModels); -// } else if (which == DialogInterface.BUTTON_POSITIVE) { -// postModelsToDownload.add(postModels.get(childPosition)); -// } else { -// session = true; -// postModelsToDownload.add(postModels.get(childPosition)); -// } -// if (postModelsToDownload.size() > 0) { -// DownloadUtils.batchDownload(context, -// username, -// DownloadMethod.DOWNLOAD_POST_VIEWER, -// postModelsToDownload); -// } -// }; -// new AlertDialog.Builder(context) -// .setTitle(R.string.post_viewer_download_dialog_title) -// .setMessage(R.string.post_viewer_download_message) -// .setNeutralButton(R.string.post_viewer_download_session, clickListener) -// .setPositiveButton(R.string.post_viewer_download_current, clickListener) -// .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); -// } else { -// DownloadUtils.batchDownload(context, -// username, -// DownloadMethod.DOWNLOAD_POST_VIEWER, -// Collections.singletonList(postModels.get(childPosition))); -// } -// } -// } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java index 2145e770..f811d1e6 100644 --- a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java @@ -764,6 +764,7 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { private void setupTitles() { binding.title.setText(feedModel.getProfileModel().getUsername()); binding.righttitle.setText(feedModel.getProfileModel().getName()); + binding.isVerified.setVisibility(feedModel.getProfileModel().isVerified() ? View.VISIBLE : View.GONE); binding.title.setOnClickListener(v -> navigateToProfile("@" + feedModel.getProfileModel().getUsername())); binding.righttitle.setOnClickListener(v -> navigateToProfile("@" + feedModel.getProfileModel().getUsername())); final String locationName = feedModel.getLocationName(); diff --git a/app/src/main/java/awais/instagrabber/webservices/ProfileService.java b/app/src/main/java/awais/instagrabber/webservices/ProfileService.java index 34567d58..a221f3fb 100644 --- a/app/src/main/java/awais/instagrabber/webservices/ProfileService.java +++ b/app/src/main/java/awais/instagrabber/webservices/ProfileService.java @@ -172,7 +172,7 @@ public class ProfileService extends BaseService { } else { resourceUrl = mediaNode.has("display_resources") ? ResponseBodyUtils.getHighQualityImage(mediaNode) : displayUrl; } - JSONObject tempJsonObject = mediaNode.optJSONObject("edge_media_preview_comment"); + JSONObject tempJsonObject = mediaNode.optJSONObject("edge_media_to_comment"); final long commentsCount = tempJsonObject != null ? tempJsonObject.optLong("count") : 0; tempJsonObject = mediaNode.optJSONObject("edge_media_preview_like"); final long likesCount = tempJsonObject != null ? tempJsonObject.optLong("count") : 0; diff --git a/app/src/main/res/layout/dialog_post_view.xml b/app/src/main/res/layout/dialog_post_view.xml index 77d924a9..3ef6c076 100644 --- a/app/src/main/res/layout/dialog_post_view.xml +++ b/app/src/main/res/layout/dialog_post_view.xml @@ -65,29 +65,42 @@ android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginEnd="4dp" android:ellipsize="marquee" android:singleLine="true" android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1" android:textColor="@color/white" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@id/subtitle" - app:layout_constraintEnd_toEndOf="@id/righttitle" + app:layout_constraintEnd_toStartOf="@id/isVerified" app:layout_constraintStart_toEndOf="@id/profile_pic" app:layout_constraintTop_toTopOf="@id/profile_pic" tools:text="Username Username Username" /> + + diff --git a/app/src/main/res/layout/item_comment.xml b/app/src/main/res/layout/item_comment.xml index cb119a13..6e222821 100755 --- a/app/src/main/res/layout/item_comment.xml +++ b/app/src/main/res/layout/item_comment.xml @@ -24,7 +24,7 @@ + + + +