From 0b983affcba53db513f7e9e108430275c17bec02 Mon Sep 17 00:00:00 2001 From: Grishka Date: Sun, 13 Feb 2022 13:53:50 +0300 Subject: [PATCH] More account interactions --- .../requests/accounts/SetAccountBlocked.java | 11 +++ .../requests/accounts/SetAccountMuted.java | 11 +++ .../android/fragments/ProfileFragment.java | 86 +++++++++++++++++-- .../android/ui/M3AlertDialogBuilder.java | 32 ++++++- mastodon/src/main/res/values/strings.xml | 14 +++ 5 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountBlocked.java create mode 100644 mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountBlocked.java b/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountBlocked.java new file mode 100644 index 000000000..5955e53d8 --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountBlocked.java @@ -0,0 +1,11 @@ +package org.joinmastodon.android.api.requests.accounts; + +import org.joinmastodon.android.api.MastodonAPIRequest; +import org.joinmastodon.android.model.Relationship; + +public class SetAccountBlocked extends MastodonAPIRequest{ + public SetAccountBlocked(String id, boolean blocked){ + super(HttpMethod.POST, "/accounts/"+id+"/"+(blocked ? "block" : "unblock"), Relationship.class); + setRequestBody(new Object()); + } +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java b/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java new file mode 100644 index 000000000..17f0a583d --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java @@ -0,0 +1,11 @@ +package org.joinmastodon.android.api.requests.accounts; + +import org.joinmastodon.android.api.MastodonAPIRequest; +import org.joinmastodon.android.model.Relationship; + +public class SetAccountMuted extends MastodonAPIRequest{ + public SetAccountMuted(String id, boolean muted){ + super(HttpMethod.POST, "/accounts/"+id+"/"+(muted ? "mute" : "unmute"), Relationship.class); + setRequestBody(new Object()); + } +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ProfileFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ProfileFragment.java index 8c67c18e6..6e1aa6fb0 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ProfileFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ProfileFragment.java @@ -5,6 +5,7 @@ import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.app.Activity; +import android.app.AlertDialog; import android.app.Fragment; import android.content.Intent; import android.content.res.Configuration; @@ -36,12 +37,15 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.accounts.GetAccountRelationships; import org.joinmastodon.android.api.requests.accounts.GetAccountStatuses; import org.joinmastodon.android.api.requests.accounts.GetOwnAccount; +import org.joinmastodon.android.api.requests.accounts.SetAccountBlocked; import org.joinmastodon.android.api.requests.accounts.SetAccountFollowed; +import org.joinmastodon.android.api.requests.accounts.SetAccountMuted; import org.joinmastodon.android.api.requests.accounts.UpdateAccountCredentials; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.AccountField; import org.joinmastodon.android.model.Relationship; +import org.joinmastodon.android.ui.M3AlertDialogBuilder; import org.joinmastodon.android.ui.drawables.CoverOverlayGradientDrawable; import org.joinmastodon.android.ui.tabs.TabLayout; import org.joinmastodon.android.ui.tabs.TabLayoutMediator; @@ -364,10 +368,10 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList menu.findItem(R.id.mute).setTitle(getString(relationship.muting ? R.string.unmute_user : R.string.mute_user, account.displayName)); menu.findItem(R.id.block).setTitle(getString(relationship.blocking ? R.string.unblock_user : R.string.block_user, account.displayName)); menu.findItem(R.id.report).setTitle(getString(R.string.report_user, account.displayName)); - String domain=account.getDomain(); - if(domain!=null) - menu.findItem(R.id.block_domain).setTitle(getString(relationship.domainBlocking ? R.string.unblock_domain : R.string.block_domain, domain)); - else +// String domain=account.getDomain(); +// if(domain!=null) +// menu.findItem(R.id.block_domain).setTitle(getString(relationship.domainBlocking ? R.string.unblock_domain : R.string.block_domain, domain)); +// else menu.findItem(R.id.block_domain).setVisible(false); } @@ -379,6 +383,10 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, account.url); startActivity(Intent.createChooser(intent, item.getTitle())); + }else if(id==R.id.mute){ + confirmToggleMuted(); + }else if(id==R.id.block){ + confirmToggleBlocked(); } return true; } @@ -408,7 +416,13 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList private void updateRelationship(){ invalidateOptionsMenu(); actionButton.setVisibility(View.VISIBLE); - actionButton.setText(relationship.following ? R.string.button_following : R.string.button_follow); + if(relationship.blocking){ + actionButton.setText(R.string.button_blocked); + }else if(relationship.muting){ + actionButton.setText(R.string.button_muted); + }else{ + actionButton.setText(relationship.following ? R.string.button_following : R.string.button_follow); + } } private void onScrollChanged(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY){ @@ -463,7 +477,13 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList else saveAndExitEditMode(); }else{ - toggleFollowing(); + if(relationship.blocking){ + confirmToggleBlocked(); + }else if(relationship.muting){ + confirmToggleMuted(); + }else{ + toggleFollowing(); + } } } @@ -620,6 +640,60 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList .exec(accountID); } + private void confirmToggleMuted(){ + new M3AlertDialogBuilder(getActivity()) + .setTitle(relationship.muting ? R.string.confirm_unmute_title : R.string.confirm_mute_title) + .setMessage(getString(relationship.muting ? R.string.confirm_unmute : R.string.confirm_mute, account.displayName)) + .setPositiveButton(relationship.muting ? R.string.do_unmute : R.string.do_mute, (dlg, i)->toggleMuted()) + .setNegativeButton(R.string.cancel, null) + .show(); + } + + private void confirmToggleBlocked(){ + new M3AlertDialogBuilder(getActivity()) + .setTitle(relationship.blocking ? R.string.confirm_unblock_title : R.string.confirm_block_title) + .setMessage(getString(relationship.blocking ? R.string.confirm_unblock : R.string.confirm_block, account.displayName)) + .setPositiveButton(relationship.blocking ? R.string.do_block : R.string.do_unblock, (dlg, i)->toggleBlocked()) + .setNegativeButton(R.string.cancel, null) + .show(); + } + + private void toggleMuted(){ + new SetAccountMuted(account.id, !relationship.muting) + .setCallback(new Callback<>(){ + @Override + public void onSuccess(Relationship result){ + relationship=result; + updateRelationship(); + } + + @Override + public void onError(ErrorResponse error){ + error.showToast(getActivity()); + } + }) + .wrapProgress(getActivity(), R.string.loading, false) + .exec(accountID); + } + + private void toggleBlocked(){ + new SetAccountBlocked(account.id, !relationship.blocking) + .setCallback(new Callback<>(){ + @Override + public void onSuccess(Relationship result){ + relationship=result; + updateRelationship(); + } + + @Override + public void onError(ErrorResponse error){ + error.showToast(getActivity()); + } + }) + .wrapProgress(getActivity(), R.string.loading, false) + .exec(accountID); + } + @Override public boolean onBackPressed(){ if(isInEditMode){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/M3AlertDialogBuilder.java b/mastodon/src/main/java/org/joinmastodon/android/ui/M3AlertDialogBuilder.java index 5efe3c4cc..e140d58ec 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/M3AlertDialogBuilder.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/M3AlertDialogBuilder.java @@ -23,9 +23,39 @@ public class M3AlertDialogBuilder extends AlertDialog.Builder{ Button btn=alert.getButton(AlertDialog.BUTTON_POSITIVE); if(btn!=null){ View buttonBar=(View) btn.getParent(); - buttonBar.setPadding(V.dp(16), V.dp(24), V.dp(16), V.dp(24)); + buttonBar.setPadding(V.dp(16), 0, V.dp(16), V.dp(24)); ((View)buttonBar.getParent()).setPadding(0, 0, 0, 0); } + // hacc + int titleID=getContext().getResources().getIdentifier("title_template", "id", "android"); + if(titleID!=0){ + View title=alert.findViewById(titleID); + if(title!=null){ + int pad=V.dp(24); + title.setPadding(pad, pad, pad, pad); + } + } + int titleDividerID=getContext().getResources().getIdentifier("titleDividerNoCustom", "id", "android"); + if(titleDividerID!=0){ + View divider=alert.findViewById(titleDividerID); + if(divider!=null){ + divider.getLayoutParams().height=0; + } + } + int scrollViewID=getContext().getResources().getIdentifier("scrollView", "id", "android"); + if(scrollViewID!=0){ + View scrollView=alert.findViewById(scrollViewID); + if(scrollView!=null){ + scrollView.setPadding(0, 0, 0, 0); + } + } + int messageID=getContext().getResources().getIdentifier("message", "id", "android"); + if(messageID!=0){ + View message=alert.findViewById(messageID); + if(message!=null){ + message.setPadding(message.getPaddingLeft(), message.getPaddingTop(), message.getPaddingRight(), V.dp(24)); + } + } return alert; } } diff --git a/mastodon/src/main/res/values/strings.xml b/mastodon/src/main/res/values/strings.xml index b48c86971..39a0bcc72 100644 --- a/mastodon/src/main/res/values/strings.xml +++ b/mastodon/src/main/res/values/strings.xml @@ -108,4 +108,18 @@ %,d voters Closed + Mute Account + Confirm to mute %s + Mute + Unmute Account + Confirm to unmute %s + Unmute + Block Account + Confirm to block %s + Block + Unblock Account + Confirm to unblock %s + Unblock + Muted + Blocked \ No newline at end of file