Clear user flair.

This commit is contained in:
Docile-Alligator 2022-08-04 14:05:34 +10:00
parent 767bfb5af6
commit 3c0e2ab2d6
4 changed files with 55 additions and 25 deletions

View File

@ -26,15 +26,17 @@ public class SelectUserFlair {
void failed(String errorMessage); 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) { String subredditName, String accountName, SelectUserFlairListener selectUserFlairListener) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); 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.NAME_KEY, accountName);
params.put(APIUtils.TEXT_KEY, userFlair.getText());
oauthRetrofit.create(RedditAPI.class).selectUserFlair(APIUtils.getOAuthHeader(accessToken), params, subredditName) oauthRetrofit.create(RedditAPI.class).selectUserFlair(APIUtils.getOAuthHeader(accessToken), params, subredditName)
.enqueue(new Callback<String>() { .enqueue(new Callback<>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {

View File

@ -11,6 +11,7 @@ import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -162,12 +163,20 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo
}) })
.show(); .show();
} else { } else {
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) if (userFlair == null) {
.setTitle(R.string.select_this_user_flair) new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setMessage(userFlair.getText()) .setTitle(R.string.clear_user_flair)
.setPositiveButton(R.string.yes, (dialogInterface, i) -> selectUserFlair(userFlair)) .setPositiveButton(R.string.yes, (dialogInterface, i) -> selectUserFlair(userFlair))
.setNegativeButton(R.string.no, null) .setNegativeButton(R.string.no, null)
.show(); .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); mLinearLayoutManager = new LinearLayoutManagerBugFixed(SelectUserFlairActivity.this);
@ -175,19 +184,27 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo
recyclerView.setAdapter(mAdapter); recyclerView.setAdapter(mAdapter);
} }
private void selectUserFlair(UserFlair userFlair) { private void selectUserFlair(@Nullable UserFlair userFlair) {
SelectUserFlair.selectUserFlair(mOauthRetrofit, mAccessToken, userFlair, mSubredditName, mAccountName, SelectUserFlair.selectUserFlair(mOauthRetrofit, mAccessToken, userFlair, mSubredditName, mAccountName,
new SelectUserFlair.SelectUserFlairListener() { new SelectUserFlair.SelectUserFlairListener() {
@Override @Override
public void success() { 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(); finish();
} }
@Override @Override
public void failed(String errorMessage) { public void failed(String errorMessage) {
if (errorMessage == null || errorMessage.equals("")) { 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 { } else {
Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show();
} }

View File

@ -46,23 +46,28 @@ public class UserFlairRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerV
@Override @Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof UserFlairViewHolder) { if (holder instanceof UserFlairViewHolder) {
UserFlair userFlair = userFlairs.get(holder.getBindingAdapterPosition()); if (position == 0) {
if (userFlair.getHtmlText() == null || userFlair.getHtmlText().equals("")) { ((UserFlairViewHolder) holder).userFlairHtmlTextView.setText(R.string.clear_user_flair);
((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); ((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 @Override
public int getItemCount() { public int getItemCount() {
return userFlairs == null ? 0 : userFlairs.size(); return userFlairs == null ? 1 : userFlairs.size() + 1;
} }
class UserFlairViewHolder extends RecyclerView.ViewHolder { class UserFlairViewHolder extends RecyclerView.ViewHolder {
@ -84,11 +89,15 @@ public class UserFlairRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerV
} }
itemView.setOnClickListener(view -> { 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 -> { editUserFlairImageView.setOnClickListener(view -> {
itemClickListener.onClick(userFlairs.get(getBindingAdapterPosition()), true); itemClickListener.onClick(userFlairs.get(getBindingAdapterPosition() - 1), true);
}); });
} }
} }

View File

@ -1053,6 +1053,8 @@
<string name="select_user_flair_success">User flair selected</string> <string name="select_user_flair_success">User flair selected</string>
<string name="select_this_user_flair">Select this user flair?</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="select_header_size">Select Header Size</string>
<string name="large">Large</string> <string name="large">Large</string>