From 3c0e2ab2d6cea83f4423360da9b8713cf4fe3b5a Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 4 Aug 2022 14:05:34 +1000 Subject: [PATCH] Clear user flair. --- .../infinityforreddit/SelectUserFlair.java | 10 +++--- .../activities/SelectUserFlairActivity.java | 35 ++++++++++++++----- .../UserFlairRecyclerViewAdapter.java | 33 ++++++++++------- app/src/main/res/values/strings.xml | 2 ++ 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 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() { + .enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call call, @NonNull Response 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 { - 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 @@ User flair selected Select this user flair? + Clear Flair + User flair cleared Select Header Size Large