From 3a4bae88ca2bc744a4b471a00344c22b077a57e7 Mon Sep 17 00:00:00 2001 From: Alan Evans Date: Wed, 30 Sep 2020 13:59:39 -0300 Subject: [PATCH] Add network spinner to add members. --- .../groups/ui/AddMembersResultCallback.java | 11 --- .../ui/managegroup/ManageGroupFragment.java | 50 ++++++++----- .../ui/managegroup/ManageGroupRepository.java | 10 +-- .../ui/managegroup/ManageGroupViewModel.java | 72 ++++++------------- 4 files changed, 60 insertions(+), 83 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/groups/ui/AddMembersResultCallback.java diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/AddMembersResultCallback.java b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/AddMembersResultCallback.java deleted file mode 100644 index 75531f9e8..000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/AddMembersResultCallback.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.thoughtcrime.securesms.groups.ui; - -import androidx.annotation.NonNull; - -import org.thoughtcrime.securesms.recipients.RecipientId; - -import java.util.List; - -public interface AddMembersResultCallback { - void onMembersAdded(int numberOfMembersAdded, @NonNull List invitedMembers); -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupFragment.java b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupFragment.java index f4a7ce9f8..db337a1fa 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupFragment.java @@ -11,6 +11,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -36,6 +37,8 @@ import org.thoughtcrime.securesms.components.ThreadPhotoRailView; import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto; import org.thoughtcrime.securesms.contacts.avatars.FallbackPhoto80dp; import org.thoughtcrime.securesms.groups.GroupId; +import org.thoughtcrime.securesms.groups.ui.GroupChangeFailureReason; +import org.thoughtcrime.securesms.groups.ui.GroupErrors; import org.thoughtcrime.securesms.groups.ui.GroupMemberListView; import org.thoughtcrime.securesms.groups.ui.LeaveGroupDialog; import org.thoughtcrime.securesms.groups.ui.invitesandrequests.ManagePendingAndRequestingMembersActivity; @@ -53,10 +56,12 @@ import org.thoughtcrime.securesms.recipients.RecipientId; import org.thoughtcrime.securesms.recipients.ui.bottomsheet.RecipientBottomSheetDialogFragment; import org.thoughtcrime.securesms.recipients.ui.notifications.CustomNotificationsDialogFragment; import org.thoughtcrime.securesms.recipients.ui.sharablegrouplink.ShareableGroupLinkDialogFragment; +import org.thoughtcrime.securesms.util.AsynchronousCallback; import org.thoughtcrime.securesms.util.DateUtils; import org.thoughtcrime.securesms.util.FeatureFlags; import org.thoughtcrime.securesms.util.LifecycleCursorWrapper; import org.thoughtcrime.securesms.util.views.LearnMoreTextView; +import org.thoughtcrime.securesms.util.views.SimpleProgressDialog; import java.util.List; import java.util.Locale; @@ -365,9 +370,6 @@ public class ManageGroupFragment extends LoggingFragment { mentionsRow.setOnClickListener(v -> viewModel.handleMentionNotificationSelection()); viewModel.getMentionSetting().observe(getViewLifecycleOwner(), value -> mentionsValue.setText(value)); - viewModel.getSnackbarEvents().observe(getViewLifecycleOwner(), this::handleSnackbarEvent); - viewModel.getInvitedDialogEvents().observe(getViewLifecycleOwner(), this::handleInvitedDialogEvent); - viewModel.getCanLeaveGroup().observe(getViewLifecycleOwner(), canLeave -> leaveGroup.setVisibility(canLeave ? View.VISIBLE : View.GONE)); viewModel.getCanBlockGroup().observe(getViewLifecycleOwner(), canBlock -> { blockGroup.setVisibility(canBlock ? View.VISIBLE : View.GONE); @@ -435,28 +437,38 @@ public class ManageGroupFragment extends LoggingFragment { } } - private void handleSnackbarEvent(@NonNull ManageGroupViewModel.SnackbarEvent snackbarEvent) { - Snackbar.make(requireView(), buildSnackbarString(snackbarEvent), Snackbar.LENGTH_SHORT).setTextColor(Color.WHITE).show(); - } - - private void handleInvitedDialogEvent(@NonNull ManageGroupViewModel.InvitedDialogEvent invitedDialogEvent) { - GroupInviteSentDialog.showInvitesSent(requireContext(), invitedDialogEvent.getNewInvitedMembers()); - } - - private @NonNull String buildSnackbarString(@NonNull ManageGroupViewModel.SnackbarEvent snackbarEvent) { - return getResources().getQuantityString(R.plurals.ManageGroupActivity_added, - snackbarEvent.getNumberOfMembersAdded(), - snackbarEvent.getNumberOfMembersAdded()); - } - @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); + if (requestCode == RETURN_FROM_MEDIA) { applyMediaCursorFactory(); } else if (requestCode == PICK_CONTACT && data != null) { - List selected = data.getParcelableArrayListExtra(PushContactSelectionActivity.KEY_SELECTED_RECIPIENTS); - viewModel.onAddMembers(selected); + List selected = data.getParcelableArrayListExtra(PushContactSelectionActivity.KEY_SELECTED_RECIPIENTS); + SimpleProgressDialog.DismissibleDialog progress = SimpleProgressDialog.showDelayed(requireContext()); + + viewModel.onAddMembers(selected, new AsynchronousCallback.MainThread() { + @Override + public void onComplete(ManageGroupViewModel.AddMembersResult result) { + progress.dismiss(); + if (!result.getNewInvitedMembers().isEmpty()) { + GroupInviteSentDialog.showInvitesSent(requireContext(), result.getNewInvitedMembers()); + } + + if (result.getNumberOfMembersAdded() > 0) { + String string = getResources().getQuantityString(R.plurals.ManageGroupActivity_added, + result.getNumberOfMembersAdded(), + result.getNumberOfMembersAdded()); + Snackbar.make(requireView(), string, Snackbar.LENGTH_SHORT).setTextColor(Color.WHITE).show(); + } + } + + @Override + public void onError(@Nullable GroupChangeFailureReason error) { + progress.dismiss(); + Toast.makeText(requireContext(), GroupErrors.getUserDisplayMessage(error), Toast.LENGTH_LONG).show(); + } + }); } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupRepository.java b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupRepository.java index 4b4a586ac..6a9eea53a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupRepository.java +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupRepository.java @@ -20,13 +20,13 @@ import org.thoughtcrime.securesms.groups.GroupId; import org.thoughtcrime.securesms.groups.GroupManager; import org.thoughtcrime.securesms.groups.GroupProtoUtil; import org.thoughtcrime.securesms.groups.MembershipNotSuitableForV2Exception; -import org.thoughtcrime.securesms.groups.ui.AddMembersResultCallback; import org.thoughtcrime.securesms.groups.ui.GroupChangeErrorCallback; import org.thoughtcrime.securesms.groups.ui.GroupChangeFailureReason; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientId; import org.thoughtcrime.securesms.recipients.RecipientUtil; +import org.thoughtcrime.securesms.util.AsynchronousCallback; import org.thoughtcrime.securesms.util.FeatureFlags; import org.thoughtcrime.securesms.util.concurrent.SignalExecutors; import org.thoughtcrime.securesms.util.concurrent.SimpleTask; @@ -130,14 +130,16 @@ final class ManageGroupRepository { }); } - void addMembers(@NonNull List selected, @NonNull AddMembersResultCallback addMembersResultCallback, @NonNull GroupChangeErrorCallback error) { + void addMembers(@NonNull List selected, + @NonNull AsynchronousCallback.WorkerThread callback) + { SignalExecutors.UNBOUNDED.execute(() -> { try { GroupManager.GroupActionResult groupActionResult = GroupManager.addMembers(context, groupId.requirePush(), selected); - addMembersResultCallback.onMembersAdded(groupActionResult.getAddedMemberCount(), groupActionResult.getInvitedMembers()); + callback.onComplete(new ManageGroupViewModel.AddMembersResult(groupActionResult.getAddedMemberCount(), Recipient.resolvedList(groupActionResult.getInvitedMembers()))); } catch (GroupChangeException | MembershipNotSuitableForV2Exception | IOException e) { Log.w(TAG, e); - error.onError(GroupChangeFailureReason.fromException(e)); + callback.onError(GroupChangeFailureReason.fromException(e)); } }); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupViewModel.java b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupViewModel.java index 021e48b38..569d31295 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupViewModel.java +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/ui/managegroup/ManageGroupViewModel.java @@ -38,10 +38,10 @@ import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientId; import org.thoughtcrime.securesms.recipients.RecipientUtil; +import org.thoughtcrime.securesms.util.AsynchronousCallback; import org.thoughtcrime.securesms.util.DefaultValueLiveData; import org.thoughtcrime.securesms.util.ExpirationUtil; import org.thoughtcrime.securesms.util.FeatureFlags; -import org.thoughtcrime.securesms.util.SingleLiveEvent; import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.livedata.LiveDataUtil; import org.thoughtcrime.securesms.util.views.SimpleProgressDialog; @@ -55,8 +55,6 @@ public class ManageGroupViewModel extends ViewModel { private final Context context; private final ManageGroupRepository manageGroupRepository; - private final SingleLiveEvent snackbarEvents = new SingleLiveEvent<>(); - private final SingleLiveEvent invitedDialogEvents = new SingleLiveEvent<>(); private final LiveData title; private final LiveData isAdmin; private final LiveData canEditGroupAttributes; @@ -207,14 +205,6 @@ public class ManageGroupViewModel extends ViewModel { return hasCustomNotifications; } - SingleLiveEvent getSnackbarEvents() { - return snackbarEvents; - } - - SingleLiveEvent getInvitedDialogEvents() { - return invitedDialogEvents; - } - LiveData getCanCollapseMemberList() { return canCollapseMemberList; } @@ -266,8 +256,10 @@ public class ManageGroupViewModel extends ViewModel { () -> RecipientUtil.unblock(context, recipient))); } - void onAddMembers(List selected) { - manageGroupRepository.addMembers(selected, this::showAddSuccess, this::showErrorToast); + void onAddMembers(@NonNull List selected, + @NonNull AsynchronousCallback.MainThread callback) + { + manageGroupRepository.addMembers(selected, callback.toWorkerCallback()); } void setMuteUntil(long muteUntil) { @@ -306,17 +298,6 @@ public class ManageGroupViewModel extends ViewModel { } } - @WorkerThread - private void showAddSuccess(int numberOfMembersAdded, @NonNull List newInvitedMembers) { - if (!newInvitedMembers.isEmpty()) { - invitedDialogEvents.postValue(new InvitedDialogEvent(Recipient.resolvedList(newInvitedMembers))); - } - - if (numberOfMembersAdded > 0) { - snackbarEvents.postValue(new SnackbarEvent(numberOfMembersAdded)); - } - } - @WorkerThread private void showErrorToast(@NonNull GroupChangeFailureReason e) { Util.runOnMain(() -> Toast.makeText(context, GroupErrors.getUserDisplayMessage(e), Toast.LENGTH_LONG).show()); @@ -338,6 +319,24 @@ public class ManageGroupViewModel extends ViewModel { }); } + static final class AddMembersResult { + private final int numberOfMembersAdded; + private final List newInvitedMembers; + + AddMembersResult(int numberOfMembersAdded, @NonNull List newInvitedMembers) { + this.numberOfMembersAdded = numberOfMembersAdded; + this.newInvitedMembers = newInvitedMembers; + } + + int getNumberOfMembersAdded() { + return numberOfMembersAdded; + } + + List getNewInvitedMembers() { + return newInvitedMembers; + } + } + static final class GroupViewState { private final long threadId; @NonNull private final Recipient groupRecipient; @@ -383,31 +382,6 @@ public class ManageGroupViewModel extends ViewModel { } } - static final class SnackbarEvent { - private final int numberOfMembersAdded; - - private SnackbarEvent(int numberOfMembersAdded) { - this.numberOfMembersAdded = numberOfMembersAdded; - } - - public int getNumberOfMembersAdded() { - return numberOfMembersAdded; - } - } - - static final class InvitedDialogEvent { - - private final List newInvitedMembers; - - private InvitedDialogEvent(@NonNull List newInvitedMembers) { - this.newInvitedMembers = newInvitedMembers; - } - - public @NonNull List getNewInvitedMembers() { - return newInvitedMembers; - } - } - enum GroupInfoMessage { NONE, LEGACY_GROUP_LEARN_MORE,