diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-08-04 04:05:34 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-08-04 04:05:34 +0000 |
commit | 3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a (patch) | |
tree | 5e3b4733b580ff5d59d8c9a23fe62fda12bedf8a | |
parent | 767bfb5af6480e4a2fdce72d49f0bc0e0484b59b (diff) | |
download | infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.tar infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.tar.gz infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.tar.bz2 infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.tar.lz infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.tar.xz infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.tar.zst infinity-for-reddit-3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a.zip |
Clear user flair.
4 files changed, 55 insertions, 25 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SelectUserFlair.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SelectUserFlair.java index e2607976..bb4b13b5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SelectUserFlair.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SelectUserFlair.java @@ -26,15 +26,17 @@ public class SelectUserFlair { void failed(String errorMessage); } - public static void selectUserFlair(Retrofit oauthRetrofit, String accessToken, UserFlair userFlair, + public static void selectUserFlair(Retrofit oauthRetrofit, String accessToken, @Nullable UserFlair userFlair, String subredditName, String accountName, SelectUserFlairListener selectUserFlairListener) { Map<String, String> params = new HashMap<>(); params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); - params.put(APIUtils.FLAIR_TEMPLATE_ID_KEY, userFlair.getId()); + if (userFlair != null) { + params.put(APIUtils.FLAIR_TEMPLATE_ID_KEY, userFlair.getId()); + params.put(APIUtils.TEXT_KEY, userFlair.getText()); + } params.put(APIUtils.NAME_KEY, accountName); - params.put(APIUtils.TEXT_KEY, userFlair.getText()); oauthRetrofit.create(RedditAPI.class).selectUserFlair(APIUtils.getOAuthHeader(accessToken), params, subredditName) - .enqueue(new Callback<String>() { + .enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java index 244fe14c..9f875d70 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java @@ -11,6 +11,7 @@ import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.recyclerview.widget.RecyclerView; @@ -162,12 +163,20 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo }) .show(); } else { - new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) - .setTitle(R.string.select_this_user_flair) - .setMessage(userFlair.getText()) - .setPositiveButton(R.string.yes, (dialogInterface, i) -> selectUserFlair(userFlair)) - .setNegativeButton(R.string.no, null) - .show(); + if (userFlair == null) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.clear_user_flair) + .setPositiveButton(R.string.yes, (dialogInterface, i) -> selectUserFlair(userFlair)) + .setNegativeButton(R.string.no, null) + .show(); + } else { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.select_this_user_flair) + .setMessage(userFlair.getText()) + .setPositiveButton(R.string.yes, (dialogInterface, i) -> selectUserFlair(userFlair)) + .setNegativeButton(R.string.no, null) + .show(); + } } }); mLinearLayoutManager = new LinearLayoutManagerBugFixed(SelectUserFlairActivity.this); @@ -175,19 +184,27 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo recyclerView.setAdapter(mAdapter); } - private void selectUserFlair(UserFlair userFlair) { + private void selectUserFlair(@Nullable UserFlair userFlair) { SelectUserFlair.selectUserFlair(mOauthRetrofit, mAccessToken, userFlair, mSubredditName, mAccountName, new SelectUserFlair.SelectUserFlairListener() { @Override public void success() { - Toast.makeText(SelectUserFlairActivity.this, R.string.select_user_flair_success, Toast.LENGTH_SHORT).show(); + if (userFlair == null) { + Toast.makeText(SelectUserFlairActivity.this, R.string.clear_user_flair_success, Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(SelectUserFlairActivity.this, R.string.select_user_flair_success, Toast.LENGTH_SHORT).show(); + } finish(); } @Override public void failed(String errorMessage) { if (errorMessage == null || errorMessage.equals("")) { - Snackbar.make(coordinatorLayout, R.string.select_user_flair_success, Snackbar.LENGTH_SHORT).show(); + if (userFlair == null) { + Snackbar.make(coordinatorLayout, R.string.clear_user_flair_success, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, R.string.select_user_flair_success, Snackbar.LENGTH_SHORT).show(); + } } else { Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserFlairRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserFlairRecyclerViewAdapter.java index 5eec9c24..3f88fe19 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserFlairRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserFlairRecyclerViewAdapter.java @@ -46,23 +46,28 @@ public class UserFlairRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerV @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { if (holder instanceof UserFlairViewHolder) { - UserFlair userFlair = userFlairs.get(holder.getBindingAdapterPosition()); - if (userFlair.getHtmlText() == null || userFlair.getHtmlText().equals("")) { - ((UserFlairViewHolder) holder).userFlairHtmlTextView.setText(userFlair.getText()); - } else { - Utils.setHTMLWithImageToTextView(((UserFlairViewHolder) holder).userFlairHtmlTextView, userFlair.getHtmlText(), true); - } - if (userFlair.isEditable()) { - ((UserFlairViewHolder) holder).editUserFlairImageView.setVisibility(View.VISIBLE); - } else { + if (position == 0) { + ((UserFlairViewHolder) holder).userFlairHtmlTextView.setText(R.string.clear_user_flair); ((UserFlairViewHolder) holder).editUserFlairImageView.setVisibility(View.GONE); + } else { + UserFlair userFlair = userFlairs.get(holder.getBindingAdapterPosition() - 1); + if (userFlair.getHtmlText() == null || userFlair.getHtmlText().equals("")) { + ((UserFlairViewHolder) holder).userFlairHtmlTextView.setText(userFlair.getText()); + } else { + Utils.setHTMLWithImageToTextView(((UserFlairViewHolder) holder).userFlairHtmlTextView, userFlair.getHtmlText(), true); + } + if (userFlair.isEditable()) { + ((UserFlairViewHolder) holder).editUserFlairImageView.setVisibility(View.VISIBLE); + } else { + ((UserFlairViewHolder) holder).editUserFlairImageView.setVisibility(View.GONE); + } } } } @Override public int getItemCount() { - return userFlairs == null ? 0 : userFlairs.size(); + return userFlairs == null ? 1 : userFlairs.size() + 1; } class UserFlairViewHolder extends RecyclerView.ViewHolder { @@ -84,11 +89,15 @@ public class UserFlairRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerV } itemView.setOnClickListener(view -> { - itemClickListener.onClick(userFlairs.get(getBindingAdapterPosition()), false); + if (getBindingAdapterPosition() == 0) { + itemClickListener.onClick(null, false); + } else { + itemClickListener.onClick(userFlairs.get(getBindingAdapterPosition() - 1), false); + } }); editUserFlairImageView.setOnClickListener(view -> { - itemClickListener.onClick(userFlairs.get(getBindingAdapterPosition()), true); + itemClickListener.onClick(userFlairs.get(getBindingAdapterPosition() - 1), true); }); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce230fdd..805449d9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1053,6 +1053,8 @@ <string name="select_user_flair_success">User flair selected</string> <string name="select_this_user_flair">Select this user flair?</string> + <string name="clear_user_flair">Clear Flair</string> + <string name="clear_user_flair_success">User flair cleared</string> <string name="select_header_size">Select Header Size</string> <string name="large">Large</string> |