Implement optional notifications muting for account muting (#1856)

This commit is contained in:
Mélanie Chauvel 2020-07-27 10:28:59 +02:00 committed by Alibek Omarov
parent 2c0b8ac6f5
commit 928fdd8d02
16 changed files with 195 additions and 44 deletions

View File

@ -66,6 +66,7 @@ import com.keylesspalace.tusky.interfaces.LinkListener
import com.keylesspalace.tusky.interfaces.ReselectableFragment
import com.keylesspalace.tusky.pager.AccountPagerAdapter
import com.keylesspalace.tusky.util.*
import com.keylesspalace.tusky.view.showMuteAccountDialog
import com.keylesspalace.tusky.viewmodel.AccountViewModel
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from
import com.uber.autodispose.autoDispose
@ -402,7 +403,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
invalidateOptionsMenu()
accountMuteButton.setOnClickListener {
viewModel.changeMuteState()
viewModel.unmuteAccount()
updateMuteButton()
}
}
@ -841,13 +842,15 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
private fun toggleMute() {
if (viewModel.relationshipData.value?.data?.muting != true) {
AlertDialog.Builder(this)
.setMessage(getString(R.string.dialog_mute_warning, loadedAccount?.username))
.setPositiveButton(android.R.string.ok) { _, _ -> viewModel.changeMuteState() }
.setNegativeButton(android.R.string.cancel, null)
.show()
loadedAccount?.let {
showMuteAccountDialog(
this,
it.username,
{ notifications -> viewModel.muteAccount(notifications) }
)
}
} else {
viewModel.changeMuteState()
viewModel.unmuteAccount()
}
}

View File

@ -8,6 +8,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
@ -17,10 +18,14 @@ import com.keylesspalace.tusky.interfaces.AccountActionListener;
import com.keylesspalace.tusky.util.CustomEmojiHelper;
import com.keylesspalace.tusky.util.ImageLoadingHelper;
import java.util.HashMap;
public class MutesAdapter extends AccountAdapter {
private HashMap<String, Boolean> mutingNotificationsMap;
public MutesAdapter(AccountActionListener accountActionListener) {
super(accountActionListener);
mutingNotificationsMap = new HashMap<String, Boolean>();
}
@NonNull
@ -45,19 +50,31 @@ public class MutesAdapter extends AccountAdapter {
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (getItemViewType(position) == VIEW_TYPE_ACCOUNT) {
MutedUserViewHolder holder = (MutedUserViewHolder) viewHolder;
holder.setupWithAccount(accountList.get(position));
Account account = accountList.get(position);
holder.setupWithAccount(account, mutingNotificationsMap.get(account.getId()));
holder.setupActionListener(accountActionListener);
}
}
public void updateMutingNotifications(String id, boolean mutingNotifications, int position) {
mutingNotificationsMap.put(id, mutingNotifications);
notifyItemChanged(position);
}
public void updateMutingNotificationsMap(HashMap<String, Boolean> newMutingNotificationsMap) {
mutingNotificationsMap.putAll(newMutingNotificationsMap);
notifyDataSetChanged();
}
static class MutedUserViewHolder extends RecyclerView.ViewHolder {
private ImageView avatar;
private TextView username;
private TextView displayName;
private ImageButton unmute;
private ImageButton muteNotifications;
private String id;
private boolean animateAvatar;
private boolean notifications;
MutedUserViewHolder(View itemView) {
super(itemView);
@ -65,11 +82,12 @@ public class MutesAdapter extends AccountAdapter {
username = itemView.findViewById(R.id.muted_user_username);
displayName = itemView.findViewById(R.id.muted_user_display_name);
unmute = itemView.findViewById(R.id.muted_user_unmute);
muteNotifications = itemView.findViewById(R.id.muted_user_mute_notifications);
animateAvatar = PreferenceManager.getDefaultSharedPreferences(itemView.getContext())
.getBoolean("animateGifAvatars", false);
}
void setupWithAccount(Account account) {
void setupWithAccount(Account account, Boolean mutingNotifications) {
id = account.getId();
CharSequence emojifiedName = CustomEmojiHelper.emojify(account.getName(), account.getEmojis(), displayName);
displayName.setText(emojifiedName);
@ -79,10 +97,38 @@ public class MutesAdapter extends AccountAdapter {
int avatarRadius = avatar.getContext().getResources()
.getDimensionPixelSize(R.dimen.avatar_radius_48dp);
ImageLoadingHelper.loadAvatar(account.getAvatar(), avatar, avatarRadius, animateAvatar);
String unmuteString = unmute.getContext().getString(R.string.action_unmute_desc, formattedUsername);
unmute.setContentDescription(unmuteString);
ViewCompat.setTooltipText(unmute, unmuteString);
if (mutingNotifications == null) {
muteNotifications.setEnabled(false);
notifications = true;
} else {
muteNotifications.setEnabled(true);
notifications = mutingNotifications;
}
if (notifications) {
muteNotifications.setImageResource(R.drawable.ic_notifications_24dp);
String unmuteNotificationsString = muteNotifications.getContext()
.getString(R.string.action_unmute_notifications_desc, formattedUsername);
muteNotifications.setContentDescription(unmuteNotificationsString);
ViewCompat.setTooltipText(muteNotifications, unmuteNotificationsString);
} else {
muteNotifications.setImageResource(R.drawable.ic_notifications_off_24dp);
String muteNotificationsString = muteNotifications.getContext()
.getString(R.string.action_mute_notifications_desc, formattedUsername);
muteNotifications.setContentDescription(muteNotificationsString);
ViewCompat.setTooltipText(muteNotifications, muteNotificationsString);
}
}
void setupActionListener(final AccountActionListener listener) {
unmute.setOnClickListener(v -> listener.onMute(false, id, getAdapterPosition()));
unmute.setOnClickListener(v -> listener.onMute(false, id, getAdapterPosition(), false));
muteNotifications.setOnClickListener(
v -> listener.onMute(true, id, getAdapterPosition(), !notifications));
itemView.setOnClickListener(v -> listener.onViewAccount(id));
}
}

View File

@ -194,8 +194,8 @@ class SearchViewModel @Inject constructor(
return accountManager.getAllAccountsOrderedByActive()
}
fun muteAccount(accountId: String) {
timelineCases.mute(accountId)
fun muteAccount(accountId: String, notifications: Boolean) {
timelineCases.mute(accountId, notifications)
}
fun muteConversation(status: Status, isMute: Boolean) {

View File

@ -27,6 +27,8 @@ import android.os.Environment
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.PopupMenu
@ -56,6 +58,7 @@ import com.keylesspalace.tusky.util.CardViewMode
import com.keylesspalace.tusky.util.LinkHelper
import com.keylesspalace.tusky.util.NetworkState
import com.keylesspalace.tusky.util.StatusDisplayOptions
import com.keylesspalace.tusky.view.showMuteAccountDialog
import com.keylesspalace.tusky.viewdata.AttachmentViewData
import com.keylesspalace.tusky.viewdata.StatusViewData
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from
@ -365,11 +368,11 @@ class SearchStatusesFragment : SearchFragment<Pair<Status, StatusViewData.Concre
}
private fun onMute(accountId: String, accountUsername: String) {
AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.dialog_mute_warning, accountUsername))
.setPositiveButton(android.R.string.ok) { _, _ -> viewModel.muteAccount(accountId) }
.setNegativeButton(android.R.string.cancel, null)
.show()
showMuteAccountDialog(
this.requireActivity(),
accountUsername,
{ notifications -> viewModel.muteAccount(accountId, notifications) }
)
}
private fun accountIsInMentions(account: AccountEntity?, mentions: Array<Mention>): Boolean {

View File

@ -23,6 +23,7 @@ data class Relationship (
@SerializedName("followed_by") val followedBy: Boolean,
val blocking: Boolean,
val muting: Boolean,
@SerializedName("muting_notifications") val mutingNotifications: Boolean,
val requested: Boolean,
@SerializedName("showing_reblogs") val showingReblogs: Boolean,
val subscribing: Boolean? = null, // Pleroma extension

View File

@ -24,6 +24,7 @@ import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.AccountActivity
import com.keylesspalace.tusky.AccountListActivity.Type
@ -47,6 +48,7 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.IOException
import java.util.HashMap
import javax.inject.Inject
class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
@ -80,6 +82,7 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
recyclerView.setHasFixedSize(true)
val layoutManager = LinearLayoutManager(view.context)
recyclerView.layoutManager = layoutManager
(recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
recyclerView.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL))
@ -116,7 +119,7 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
if (!mute) {
api.unmuteAccount(id)
} else {
api.muteAccount(id)
api.muteAccount(id, notifications)
}
.autoDispose(from(this))
.subscribe({
@ -126,26 +129,31 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
})
}
private fun onMuteSuccess(muted: Boolean, id: String, position: Int) {
private fun onMuteSuccess(muted: Boolean, id: String, position: Int, notifications: Boolean) {
val mutesAdapter = adapter as MutesAdapter
if (muted) {
mutesAdapter.updateMutingNotifications(id, notifications, position)
return
}
val mutesAdapter = adapter as MutesAdapter
val unmutedUser = mutesAdapter.removeItem(position)
if (unmutedUser != null) {
Snackbar.make(recyclerView, R.string.confirmation_unmuted, Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo) {
mutesAdapter.addItem(unmutedUser, position)
onMute(true, id, position)
onMute(true, id, position, notifications)
}
.show()
}
}
private fun onMuteFailure(mute: Boolean, accountId: String) {
private fun onMuteFailure(mute: Boolean, accountId: String, notifications: Boolean) {
val verb = if (mute) {
"mute"
if (notifications) {
"mute (notifications = true)"
} else {
"mute (notifications = false)"
}
} else {
"unmute"
}
@ -329,6 +337,10 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
adapter.update(accounts)
}
if (adapter is MutesAdapter) {
fetchRelationships(accounts.map { it.id })
}
bottomId = fromId
fetching = false
@ -345,8 +357,6 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
}
}
<<<<<<< HEAD
=======
private fun fetchRelationships(ids: List<String>) {
api.relationships(ids)
.autoDispose(from(this))
@ -357,7 +367,7 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
private fun onFetchRelationshipsSuccess(relationships: List<Relationship>) {
val mutesAdapter = adapter as MutesAdapter
val mutingNotificationsMap = HashMap<String, Boolean>()
var mutingNotificationsMap = HashMap<String, Boolean>()
relationships.map { mutingNotificationsMap.put(it.id, it.mutingNotifications) }
mutesAdapter.updateMutingNotificationsMap(mutingNotificationsMap)
}
@ -366,7 +376,6 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
Log.e(TAG, "Fetch failure for relationships of accounts: $ids")
}
>>>>>>> ce973ea7... Personal account notes (#1978)
private fun onFetchAccountsFailure(throwable: Throwable) {
fetching = false
Log.e(TAG, "Fetch failure", throwable)

View File

@ -922,7 +922,7 @@ public class NotificationsFragment extends SFragment implements
}
@Override
public void onMute(boolean mute, String id, int position) {
public void onMute(boolean mute, String id, int position, boolean notifications) {
// No muting from notifications yet
}

View File

@ -20,6 +20,7 @@ import android.app.DownloadManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
@ -30,6 +31,8 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -65,6 +68,7 @@ import com.keylesspalace.tusky.network.MastodonApi;
import com.keylesspalace.tusky.network.TimelineCases;
import com.keylesspalace.tusky.settings.PrefKeys;
import com.keylesspalace.tusky.util.LinkHelper;
import com.keylesspalace.tusky.view.MuteAccountDialog;
import com.keylesspalace.tusky.viewdata.AttachmentViewData;
import com.keylesspalace.tusky.interfaces.StatusActionListener;
@ -77,6 +81,8 @@ import java.util.regex.Pattern;
import javax.inject.Inject;
import kotlin.Unit;
import io.reactivex.android.schedulers.AndroidSchedulers;
import retrofit2.Call;
import retrofit2.Callback;
@ -373,11 +379,14 @@ public abstract class SFragment extends BaseFragment implements Injectable {
}
private void onMute(String accountId, String accountUsername) {
new AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.dialog_mute_warning, accountUsername))
.setPositiveButton(android.R.string.ok, (__, ___) -> timelineCases.mute(accountId))
.setNegativeButton(android.R.string.cancel, null)
.show();
MuteAccountDialog.showMuteAccountDialog(
this.getActivity(),
accountUsername,
(notifications) -> {
timelineCases.mute(accountId, notifications);
return Unit.INSTANCE;
}
);
}
private void onBlock(String accountId, String accountUsername) {

View File

@ -17,7 +17,7 @@ package com.keylesspalace.tusky.interfaces;
public interface AccountActionListener {
void onViewAccount(String id);
void onMute(final boolean mute, final String id, final int position);
void onMute(final boolean mute, final String id, final int position, final boolean notifications);
void onBlock(final boolean block, final String id, final int position);
void onRespondToFollowRequest(final boolean accept, final String id, final int position);
}

View File

@ -327,6 +327,7 @@ interface MastodonApi {
@Path("id") accountId: String
): Single<Relationship>
@FormUrlEncoded
@POST("api/v1/accounts/{id}/mute")
fun muteAccount(
@Path("id") accountId: String,

View File

@ -35,7 +35,7 @@ interface TimelineCases {
fun favourite(status: Status, favourite: Boolean): Single<Status>
fun bookmark(status: Status, bookmark: Boolean): Single<Status>
fun muteStatus(status: Status, mute: Boolean)
fun mute(id: String)
fun mute(id: String, notifications: Boolean)
fun block(id: String)
fun delete(id: String): Single<DeletedStatus>
fun pin(status: Status, pin: Boolean)

View File

@ -0,0 +1,27 @@
@file:JvmName("MuteAccountDialog")
package com.keylesspalace.tusky.view
import android.app.Activity
import android.widget.CheckBox
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.keylesspalace.tusky.R
fun showMuteAccountDialog(
activity: Activity,
accountUsername: String,
onOk: (notifications: Boolean) -> Unit
) {
val view = activity.layoutInflater.inflate(R.layout.dialog_mute_account, null)
(view.findViewById(R.id.warning) as TextView).text =
activity.getString(R.string.dialog_mute_warning, accountUsername)
val checkbox: CheckBox = view.findViewById(R.id.checkbox)
checkbox.setChecked(true)
AlertDialog.Builder(activity)
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ -> onOk(checkbox.isChecked) }
.setNegativeButton(android.R.string.cancel, null)
.show()
}

View File

@ -119,12 +119,12 @@ class AccountViewModel @Inject constructor(
}
}
fun changeMuteState() {
if (relationshipData.value?.data?.muting == true) {
changeRelationship(RelationShipAction.UNMUTE)
} else {
changeRelationship(RelationShipAction.MUTE)
}
fun muteAccount(notifications: Boolean) {
changeRelationship(RelationShipAction.MUTE, notifications)
}
fun unmuteAccount() {
changeRelationship(RelationShipAction.UNMUTE)
}
fun changeSubscribingState() {
@ -182,7 +182,10 @@ class AccountViewModel @Inject constructor(
}
}
private fun changeRelationship(relationshipAction: RelationShipAction, showReblogs: Boolean = true) {
/**
* @param parameter showReblogs if RelationShipAction.FOLLOW, notifications if MUTE
*/
private fun changeRelationship(relationshipAction: RelationShipAction, parameter: Boolean? = null) {
val relation = relationshipData.value?.data
val account = accountData.value?.data

View File

@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M0 0h24v24H0z" />
<path
android:fillColor="#000000"
android:pathData="M20 18.69L7.84 6.14 5.27 3.49 4 4.76l2.8 2.8v0.01c-0.52 0.99 -0.8 2.16-0.8 3.42v5l-2 2v1h13.73l2 2L21 19.72l-1-1.03zM12 22c1.11 0 2-0.89 2-2h-4c0 1.11 0.89 2 2 2zm6-7.32V11c0-3.08-1.64-5.64-4.5-6.32V4c0-0.83-0.67-1.5-1.5-1.5s-1.5 0.67 -1.5 1.5v0.68c-0.15 0.03 -0.29 0.08 -0.42 0.12 -0.1 0.03 -0.2 0.07 -0.3 0.11 h-0.01c-0.01 0-0.01 0-0.02 0.01 -0.23 0.09 -0.46 0.2 -0.68 0.31 0 0-0.01 0-0.01 0.01 L18 14.68z" />
</vector>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<TextView android:id="@+id/warning"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingBottom="20dp"
tools:text="@string/dialog_mute_warning"/>
<CheckBox android:id="@+id/checkbox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/textColorTertiary"
app:buttonTint="@color/compound_button_color"
android:text="@string/dialog_mute_hide_notifications"/>
</LinearLayout>

View File

@ -22,14 +22,26 @@
style="@style/TuskyImageButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_toStartOf="@id/muted_user_mute_notifications"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="12dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="4dp"
app:srcCompat="@drawable/ic_unmute_24dp" />
<ImageButton
android:id="@+id/muted_user_mute_notifications"
style="@style/TuskyImageButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="12dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/action_unmute"
android:padding="4dp"
app:srcCompat="@drawable/ic_unmute_24dp" />
app:srcCompat="@drawable/ic_notifications_24dp" />
<LinearLayout
android:layout_width="wrap_content"