2020-06-05 15:33:41 +00:00
|
|
|
package org.thoughtcrime.securesms.groups.ui.addmembers;
|
|
|
|
|
2021-06-24 16:52:54 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-06-05 15:33:41 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
2020-07-30 03:04:35 +00:00
|
|
|
import android.widget.Toast;
|
2020-06-05 15:33:41 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2022-09-13 14:16:01 +00:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2020-06-05 15:33:41 +00:00
|
|
|
|
2022-04-27 19:36:02 +00:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
import org.thoughtcrime.securesms.ContactSelectionActivity;
|
2021-06-24 16:52:54 +00:00
|
|
|
import org.thoughtcrime.securesms.ContactSelectionListFragment;
|
2020-06-05 15:33:41 +00:00
|
|
|
import org.thoughtcrime.securesms.PushContactSelectionActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.groups.GroupId;
|
2021-06-24 16:52:54 +00:00
|
|
|
import org.thoughtcrime.securesms.groups.SelectionLimits;
|
2020-06-05 15:33:41 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
2021-06-24 16:52:54 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-03-14 19:49:46 +00:00
|
|
|
import java.util.Optional;
|
2021-07-23 20:22:08 +00:00
|
|
|
import java.util.function.Consumer;
|
2021-06-24 16:52:54 +00:00
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
public class AddMembersActivity extends PushContactSelectionActivity {
|
|
|
|
|
2021-07-23 20:22:08 +00:00
|
|
|
public static final String GROUP_ID = "group_id";
|
|
|
|
public static final String ANNOUNCEMENT_GROUP = "announcement_group";
|
2020-06-05 15:33:41 +00:00
|
|
|
|
|
|
|
private View done;
|
|
|
|
private AddMembersViewModel viewModel;
|
|
|
|
|
2021-06-24 16:52:54 +00:00
|
|
|
public static @NonNull Intent createIntent(@NonNull Context context,
|
|
|
|
@NonNull GroupId groupId,
|
|
|
|
int displayModeFlags,
|
|
|
|
int selectionWarning,
|
|
|
|
int selectionLimit,
|
2021-07-23 20:22:08 +00:00
|
|
|
boolean isAnnouncementGroup,
|
2021-06-24 16:52:54 +00:00
|
|
|
@NonNull List<RecipientId> membersWithoutSelf) {
|
|
|
|
Intent intent = new Intent(context, AddMembersActivity.class);
|
|
|
|
|
2021-07-23 20:22:08 +00:00
|
|
|
intent.putExtra(GROUP_ID, groupId.toString());
|
|
|
|
intent.putExtra(ANNOUNCEMENT_GROUP, isAnnouncementGroup);
|
2021-06-24 16:52:54 +00:00
|
|
|
intent.putExtra(ContactSelectionListFragment.DISPLAY_MODE, displayModeFlags);
|
|
|
|
intent.putExtra(ContactSelectionListFragment.SELECTION_LIMITS, new SelectionLimits(selectionWarning, selectionLimit));
|
|
|
|
intent.putParcelableArrayListExtra(ContactSelectionListFragment.CURRENT_SELECTION, new ArrayList<>(membersWithoutSelf));
|
|
|
|
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle icicle, boolean ready) {
|
|
|
|
getIntent().putExtra(ContactSelectionActivity.EXTRA_LAYOUT_RES_ID, R.layout.add_members_activity);
|
|
|
|
super.onCreate(icicle, ready);
|
|
|
|
|
|
|
|
AddMembersViewModel.Factory factory = new AddMembersViewModel.Factory(getGroupId());
|
|
|
|
|
|
|
|
done = findViewById(R.id.done);
|
2022-09-13 14:16:01 +00:00
|
|
|
viewModel = new ViewModelProvider(this, factory).get(AddMembersViewModel.class);
|
2020-06-05 15:33:41 +00:00
|
|
|
|
2021-01-10 00:12:10 +00:00
|
|
|
done.setOnClickListener(v ->
|
|
|
|
viewModel.getDialogStateForSelectedContacts(contactsFragment.getSelectedContacts(), this::displayAlertMessage)
|
|
|
|
);
|
2020-06-05 15:33:41 +00:00
|
|
|
|
|
|
|
disableDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initializeToolbar() {
|
|
|
|
getToolbar().setNavigationIcon(R.drawable.ic_arrow_left_24);
|
|
|
|
getToolbar().setNavigationOnClickListener(v -> {
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-02-24 17:40:28 +00:00
|
|
|
public void onBeforeContactSelected(@NonNull Optional<RecipientId> recipientId, String number, @NonNull Consumer<Boolean> callback) {
|
2020-07-30 03:04:35 +00:00
|
|
|
if (getGroupId().isV1() && recipientId.isPresent() && !Recipient.resolved(recipientId.get()).hasE164()) {
|
|
|
|
Toast.makeText(this, R.string.AddMembersActivity__this_person_cant_be_added_to_legacy_groups, Toast.LENGTH_SHORT).show();
|
2021-07-23 20:22:08 +00:00
|
|
|
callback.accept(false);
|
|
|
|
return;
|
2020-07-30 03:04:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
if (contactsFragment.hasQueryFilter()) {
|
2021-07-12 18:44:59 +00:00
|
|
|
getContactFilterView().clear();
|
2020-06-05 15:33:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 03:04:35 +00:00
|
|
|
enableDone();
|
|
|
|
|
2021-07-23 20:22:08 +00:00
|
|
|
callback.accept(true);
|
2020-06-05 15:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-02-24 17:40:28 +00:00
|
|
|
public void onContactDeselected(@NonNull Optional<RecipientId> recipientId, String number) {
|
2020-06-05 15:33:41 +00:00
|
|
|
if (contactsFragment.hasQueryFilter()) {
|
2021-07-12 18:44:59 +00:00
|
|
|
getContactFilterView().clear();
|
2020-06-05 15:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (contactsFragment.getSelectedContactsCount() < 1) {
|
|
|
|
disableDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:44:59 +00:00
|
|
|
@Override
|
|
|
|
public void onSelectionChanged() {
|
|
|
|
int selectedContactsCount = contactsFragment.getTotalMemberCount() + 1;
|
|
|
|
if (selectedContactsCount == 0) {
|
|
|
|
getToolbar().setTitle(getString(R.string.AddMembersActivity__add_members));
|
|
|
|
} else {
|
|
|
|
getToolbar().setTitle(getResources().getQuantityString(R.plurals.CreateGroupActivity__d_members, selectedContactsCount, selectedContactsCount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
private void enableDone() {
|
|
|
|
done.setEnabled(true);
|
|
|
|
done.animate().alpha(1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disableDone() {
|
|
|
|
done.setEnabled(false);
|
|
|
|
done.animate().alpha(0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private GroupId getGroupId() {
|
|
|
|
return GroupId.parseOrThrow(getIntent().getStringExtra(GROUP_ID));
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:22:08 +00:00
|
|
|
private boolean isAnnouncementGroup() {
|
|
|
|
return getIntent().getBooleanExtra(ANNOUNCEMENT_GROUP, false);
|
|
|
|
}
|
|
|
|
|
2021-01-10 00:12:10 +00:00
|
|
|
private void displayAlertMessage(@NonNull AddMembersViewModel.AddMemberDialogMessageState state) {
|
2020-06-05 15:33:41 +00:00
|
|
|
Recipient recipient = Util.firstNonNull(state.getRecipient(), Recipient.UNKNOWN);
|
|
|
|
|
2021-01-10 00:12:10 +00:00
|
|
|
String message = getResources().getQuantityString(R.plurals.AddMembersActivity__add_d_members_to_s, state.getSelectionCount(),
|
|
|
|
recipient.getDisplayName(this), state.getGroupTitle(), state.getSelectionCount());
|
|
|
|
|
2022-04-27 19:36:02 +00:00
|
|
|
new MaterialAlertDialogBuilder(this)
|
2021-01-10 00:12:10 +00:00
|
|
|
.setMessage(message)
|
|
|
|
.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel())
|
|
|
|
.setPositiveButton(R.string.AddMembersActivity__add, (dialog, which) -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
onFinishedSelection();
|
|
|
|
})
|
|
|
|
.setCancelable(true)
|
|
|
|
.show();
|
2020-06-05 15:33:41 +00:00
|
|
|
}
|
|
|
|
}
|