diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index 5045dc74..767068f9 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -35,9 +35,9 @@ - + - + diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 9e3da5a9..5919b4e6 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser index 7dc82bdb..af6821f2 100644 Binary files a/.idea/caches/gradle_models.ser and b/.idea/caches/gradle_models.ser differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d8853f50..3679f066 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,11 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> + mVisibleComments; private String mSubredditNamePrefixed; @@ -79,7 +82,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter(); mSubredditNamePrefixed = subredditNamePrefixed; @@ -425,6 +429,20 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + ModifyCommentBottomSheetFragment modifyCommentBottomSheetFragment = new ModifyCommentBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_CONTENT, comment.getCommentContent()); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_FULLNAME, comment.getFullName()); + bundle.putInt(ModifyCommentBottomSheetFragment.EXTRA_POSITION, holder.getAdapterPosition() - 1); + modifyCommentBottomSheetFragment.setArguments(bundle); + modifyCommentBottomSheetFragment.show(((AppCompatActivity) mActivity).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag()); + }); + } + if (comment.hasReply()) { if(comment.isExpanded()) { ((CommentViewHolder) holder).expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp); @@ -436,11 +454,11 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + upVoteButton.setOnClickListener(view -> { if(mAccessToken == null) { Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; @@ -915,18 +951,18 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + downVoteButton.setOnClickListener(view -> { if(mAccessToken == null) { Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; @@ -960,18 +996,18 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mCommentViewModel.retryLoadingMore()); + getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), + () -> mCommentViewModel.retryLoadingMore()); String username = getArguments().getString(EXTRA_USERNAME_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java index e2fd34b5..ffcf707b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java @@ -2,17 +2,18 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; import android.content.Intent; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import androidx.paging.PagedListAdapter; import androidx.recyclerview.widget.DiffUtil; @@ -27,6 +28,7 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + ModifyCommentBottomSheetFragment modifyCommentBottomSheetFragment = new ModifyCommentBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_CONTENT, comment.getCommentContent()); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_FULLNAME, comment.getFullName()); + bundle.putInt(ModifyCommentBottomSheetFragment.EXTRA_POSITION, holder.getAdapterPosition() - 1); + modifyCommentBottomSheetFragment.setArguments(bundle); + modifyCommentBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag()); + }); + } } } @@ -165,9 +182,10 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter linearLayout.callOnClick()); - RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) shareButton.getLayoutParams(); - lp.addRule(RelativeLayout.ALIGN_PARENT_END); - lp.setMarginEnd(0); - shareButton.setLayoutParams(lp); - shareButton.setOnClickListener(view -> { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java new file mode 100644 index 00000000..11e30fd8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java @@ -0,0 +1,137 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Intent; +import android.content.res.Configuration; +import android.os.Build; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.Window; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; + +import com.google.android.material.snackbar.Snackbar; + +import java.util.HashMap; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class EditCommentActivity extends AppCompatActivity { + + static final String EXTRA_CONTENT = "EC"; + static final String EXTRA_FULLNAME = "EF"; + static final String EXTRA_ACCESS_TOKEN = "EAT"; + static final String EXTRA_POSITION = "EP"; + + static final String EXTRA_EDITED_COMMENT_CONTENT = "EECC"; + static final String EXTRA_EDITED_COMMENT_POSITION = "EECP"; + + @BindView(R.id.coordinator_layout_edit_comment_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.toolbar_edit_comment_activity) Toolbar toolbar; + @BindView(R.id.post_text_content_edit_text_edit_comment_activity) EditText contentEditText; + + private String mFullName; + private String mAccessToken; + private boolean isSubmitting = false; + + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_edit_comment); + + ButterKnife.bind(this); + + ((Infinity) getApplication()).getAppComponent().inject(this); + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Window window = getWindow(); + if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + window.setNavigationBarColor(ContextCompat.getColor(this, R.color.navBarColor)); + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + mFullName = getIntent().getExtras().getString(EXTRA_FULLNAME); + mAccessToken = getIntent().getExtras().getString(EXTRA_ACCESS_TOKEN); + contentEditText.setText(getIntent().getExtras().getString(EXTRA_CONTENT)); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.edit_comment_activity, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if(item.getItemId() == R.id.action_send_edit_comment_activity) { + if(!isSubmitting) { + isSubmitting = true; + + Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_SHORT).show(); + + String content = contentEditText.getText().toString(); + + Map params = new HashMap<>(); + params.put(RedditUtils.THING_ID_KEY, mFullName); + params.put(RedditUtils.TEXT_KEY, content); + + mOauthRetrofit.create(RedditAPI.class) + .editPostOrComment(RedditUtils.getOAuthHeader(mAccessToken), params) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + isSubmitting = false; + if(response.isSuccessful()) { + Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show(); + + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_EDITED_COMMENT_CONTENT, content); + returnIntent.putExtra(EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION)); + setResult(RESULT_OK, returnIntent); + + finish(); + } else { + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + isSubmitting = false; + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } + }); + + } + return true; + } else if(item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return false; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ModifyCommentBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ModifyCommentBottomSheetFragment.java new file mode 100644 index 00000000..5b19cfaf --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ModifyCommentBottomSheetFragment.java @@ -0,0 +1,84 @@ +package ml.docilealligator.infinityforreddit; + + +import android.app.Activity; +import android.content.Intent; +import android.content.res.Configuration; +import android.os.Build; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.fragment.app.Fragment; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; + + +/** + * A simple {@link Fragment} subclass. + */ +public class ModifyCommentBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + static final String EXTRA_COMMENT_FULLNAME = "ECF"; + static final String EXTRA_COMMENT_CONTENT = "ECC"; + static final String EXTRA_ACCESS_TOKEN = "EAT"; + static final String EXTRA_POSITION = "EP"; + + public ModifyCommentBottomSheetFragment() { + // Required empty public constructor + } + + @BindView(R.id.edit_text_view_modify_comment_bottom_sheet_fragment) TextView editTextView; + @BindView(R.id.delete_text_view_modify_comment_bottom_sheet_fragment) TextView deleteTextView; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_modify_comment_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + Activity activity = getActivity(); + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + + Bundle bundle = getArguments(); + String fullName = bundle.getString(EXTRA_COMMENT_FULLNAME); + String content = bundle.getString(EXTRA_COMMENT_CONTENT); + String accessToken = bundle.getString(EXTRA_ACCESS_TOKEN); + + editTextView.setOnClickListener(view -> { + Intent intent = new Intent(getActivity(), EditCommentActivity.class); + intent.putExtra(EditCommentActivity.EXTRA_ACCESS_TOKEN, accessToken); + intent.putExtra(EditCommentActivity.EXTRA_FULLNAME, fullName); + intent.putExtra(EditCommentActivity.EXTRA_CONTENT, content); + intent.putExtra(EditCommentActivity.EXTRA_POSITION, bundle.getInt(EXTRA_POSITION)); + if(activity instanceof ViewPostDetailActivity) { + activity.startActivityForResult(intent, ViewPostDetailActivity.EDIT_COMMENT_REQUEST_CODE); + } else { + startActivity(intent); + } + + dismiss(); + }); + + deleteTextView.setOnClickListener(view -> { + dismiss(); + if(activity instanceof ViewPostDetailActivity) { + ((ViewPostDetailActivity) activity).deleteComment(fullName, bundle.getInt(EXTRA_POSITION)); + } else if(activity instanceof ViewUserDetailActivity) { + ((ViewUserDetailActivity) activity).deleteComment(fullName); + } + }); + + return rootView; + } + +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index ad65fe4f..797c1834 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -59,6 +59,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { static final String EXTRA_POST_ID = "EPI"; private static final int EDIT_POST_REQUEST_CODE = 2; + static final int EDIT_COMMENT_REQUEST_CODE = 3; private RequestManager mGlide; private Locale mLocale; @@ -224,7 +225,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); } mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, - mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mPost, + mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override @@ -290,7 +291,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { } mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, - mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mPost, + mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override @@ -481,6 +482,27 @@ public class ViewPostDetailActivity extends AppCompatActivity { } } + void deleteComment(String fullName, int position) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.delete_this_comment) + .setMessage(R.string.are_you_sure) + .setPositiveButton(R.string.delete, (dialogInterface, i) + -> DeleteThing.delete(mOauthRetrofit, fullName, mAccessToken, new DeleteThing.DeleteThingListener() { + @Override + public void deleteSuccess() { + Toast.makeText(ViewPostDetailActivity.this, R.string.delete_post_success, Toast.LENGTH_SHORT).show(); + mAdapter.deleteComment(position); + } + + @Override + public void deleteFailed() { + Toast.makeText(ViewPostDetailActivity.this, R.string.delete_post_failed, Toast.LENGTH_SHORT).show(); + } + })) + .setNegativeButton(R.string.cancel, null) + .show(); + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToDetailActivity event) { if(mPost.getId().equals(event.postId)) { @@ -578,6 +600,11 @@ public class ViewPostDetailActivity extends AppCompatActivity { if(resultCode == RESULT_OK) { refresh(true); } + } else if(requestCode == EDIT_COMMENT_REQUEST_CODE) { + if(resultCode == RESULT_OK) { + mAdapter.editComment(data.getExtras().getString(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), + data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 670547d9..ad007ae1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -31,6 +31,7 @@ import com.bumptech.glide.request.RequestOptions; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.chip.Chip; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.tabs.TabLayout; @@ -385,6 +386,26 @@ public class ViewUserDetailActivity extends AppCompatActivity { } } + void deleteComment(String fullName) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.delete_this_comment) + .setMessage(R.string.are_you_sure) + .setPositiveButton(R.string.delete, (dialogInterface, i) + -> DeleteThing.delete(mOauthRetrofit, fullName, mAccessToken, new DeleteThing.DeleteThingListener() { + @Override + public void deleteSuccess() { + Toast.makeText(ViewUserDetailActivity.this, R.string.delete_post_success, Toast.LENGTH_SHORT).show(); + } + + @Override + public void deleteFailed() { + Toast.makeText(ViewUserDetailActivity.this, R.string.delete_post_failed, Toast.LENGTH_SHORT).show(); + } + })) + .setNegativeButton(R.string.cancel, null) + .show(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.view_user_detail_activity, menu); @@ -519,6 +540,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { Bundle bundle = new Bundle(); bundle.putString(CommentsListingFragment.EXTRA_USERNAME_KEY, username); bundle.putString(CommentsListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(CommentsListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } @@ -560,5 +582,11 @@ public class ViewUserDetailActivity extends AppCompatActivity { commentsListingFragment.refresh(); } } + + public void refreshComments() { + if(commentsListingFragment != null) { + commentsListingFragment.refresh(); + } + } } } diff --git a/app/src/main/res/drawable/ic_outline_more_vert_24px.xml b/app/src/main/res/drawable/ic_outline_more_vert_24px.xml new file mode 100644 index 00000000..568cbb4d --- /dev/null +++ b/app/src/main/res/drawable/ic_outline_more_vert_24px.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_edit_comment.xml b/app/src/main/res/layout/activity_edit_comment.xml new file mode 100644 index 00000000..93504c14 --- /dev/null +++ b/app/src/main/res/layout/activity_edit_comment.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_modify_comment_bottom_sheet.xml b/app/src/main/res/layout/fragment_modify_comment_bottom_sheet.xml new file mode 100644 index 00000000..a419912e --- /dev/null +++ b/app/src/main/res/layout/fragment_modify_comment_bottom_sheet.xml @@ -0,0 +1,38 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml b/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml index 81f20963..9cdce5a0 100644 --- a/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml +++ b/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml @@ -96,7 +96,6 @@ android:id="@+id/video_type_linear_layout_post_type_bottom_sheet_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_gravity="center_vertical" android:paddingTop="16dp" android:paddingBottom="16dp" android:paddingStart="32dp" diff --git a/app/src/main/res/layout/item_comment.xml b/app/src/main/res/layout/item_comment.xml index 82281e14..1283aa5b 100644 --- a/app/src/main/res/layout/item_comment.xml +++ b/app/src/main/res/layout/item_comment.xml @@ -1,5 +1,7 @@ @@ -50,7 +52,110 @@ android:layout_marginBottom="8dp" android:textColor="@color/primaryTextColor"/> - + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/edit_comment_activity.xml b/app/src/main/res/menu/edit_comment_activity.xml new file mode 100644 index 00000000..e52e484b --- /dev/null +++ b/app/src/main/res/menu/edit_comment_activity.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 392ffc9b..9b3dd695 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -182,7 +182,9 @@ Login first Delete This Post + Delete This Comment Are you sure? + Edit Delete Cancel Delete successfully