Leave group on swiping

This commit is contained in:
Mikunj 2020-02-19 15:34:02 +11:00
parent 3a0c518eeb
commit 1ce7050f1a
4 changed files with 65 additions and 27 deletions

View File

@ -1662,10 +1662,13 @@
<string name="session_restore_banner_dismiss_button_title">Dismiss</string>
<string name="session_restore_banner_restore_button_title">Restore</string>
<!-- Loki -->
<!-- Session -->
<string name="activity_register_public_key_copied_message">Copied to clipboard</string>
<!-- Session -->
<!-- Home Activity -->
<string name="activity_home_conversation_deleted">Conversation deleted</string>
<string name="activity_home_leave_group_title">Are you sure you want to leave the group?</string>
<string name="activity_home_delete_conversation_title">Are you sure you want to delete the conversation?</string>
</resources>

View File

@ -1156,21 +1156,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
builder.setMessage(getString(R.string.ConversationActivity_are_you_sure_you_want_to_leave_this_group));
builder.setPositiveButton(R.string.yes, (dialog, which) -> {
Recipient groupRecipient = getRecipient();
long threadId = DatabaseFactory.getThreadDatabase(this).getThreadIdFor(groupRecipient);
Optional<OutgoingGroupMediaMessage> leaveMessage = GroupUtil.createGroupLeaveMessage(this, groupRecipient);
if (threadId != -1 && leaveMessage.isPresent()) {
MessageSender.send(this, leaveMessage.get(), threadId, false, null);
// We need to remove the master device from the group
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this);
String localNumber = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(this);
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(this);
String groupId = groupRecipient.getAddress().toGroupString();
groupDatabase.setActive(groupId, false);
groupDatabase.remove(groupId, Address.fromSerialized(localNumber));
if (GroupUtil.leaveGroup(this, groupRecipient)) {
initializeEnabledCheck();
} else {
Toast.makeText(this, R.string.ConversationActivity_error_leaving_group, Toast.LENGTH_LONG).show();

View File

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.loki.redesign.activities
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.arch.lifecycle.Observer
import android.content.Intent
import android.database.Cursor
@ -20,6 +21,7 @@ import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_home.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
@ -36,6 +38,7 @@ import org.thoughtcrime.securesms.loki.redesign.views.SeedReminderViewDelegate
import org.thoughtcrime.securesms.mms.GlideApp
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.notifications.MessageNotifier
import org.thoughtcrime.securesms.util.GroupUtil
import org.thoughtcrime.securesms.util.TextSecurePreferences
import kotlin.math.abs
@ -230,8 +233,8 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
@SuppressLint("StaticFieldLeak")
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val threadID = (viewHolder as HomeAdapter.ViewHolder).view.thread!!.threadId
val recipient = (viewHolder as HomeAdapter.ViewHolder).view.thread!!.recipient
val threadDatabase = DatabaseFactory.getThreadDatabase(activity)
threadDatabase.archiveConversation(threadID)
val deleteThread = object : Runnable {
override fun run() {
@ -243,22 +246,45 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
apiDatabase.removeLastDeletionServerID(publicChat.channel, publicChat.server)
ApplicationContext.getInstance(activity).lokiPublicChatAPI!!.leave(publicChat.channel, publicChat.server)
}
threadDatabase.deleteConversation(threadID)
MessageNotifier.updateNotification(activity)
}
}
}
val handler = Handler()
handler.postDelayed(deleteThread, 5000)
val snackbar = Snackbar.make(activity.contentView, "Conversation Deleted", Snackbar.LENGTH_LONG)
snackbar.setAction("Undo") {
threadDatabase.unarchiveConversation(threadID)
handler.removeCallbacks(deleteThread)
animate(viewHolder, 0.0f)
val message = if (recipient.isGroupRecipient) R.string.activity_home_leave_group_title else R.string.activity_home_delete_conversation_title
val alertDialogBuilder = AlertDialog.Builder(activity)
alertDialogBuilder.setMessage(message)
alertDialogBuilder.setPositiveButton(R.string.yes) { _, _ ->
val isGroup = recipient.isGroupRecipient
// If we are deleting a group and it's active
// We need to send a leave message
if (isGroup && DatabaseFactory.getGroupDatabase(activity).isActive(recipient.address.toGroupString())) {
if (!GroupUtil.leaveGroup(activity, recipient)) {
Toast.makeText(activity, R.string.ConversationActivity_error_leaving_group, Toast.LENGTH_LONG).show()
clearView(activity.recyclerView, viewHolder)
return@setPositiveButton
}
snackbar.setActionTextColor(activity.resources.getColorWithID(R.color.accent, activity.theme))
}
// Archive and forcefully delete the conversation in 10 seconds
threadDatabase.archiveConversation(threadID)
val handler = Handler()
handler.postDelayed(deleteThread, 10000)
// Notify the user
val snackbarText = if (isGroup) R.string.MessageRecord_left_group else R.string.activity_home_conversation_deleted
val snackbar = Snackbar.make(activity.contentView, snackbarText, Snackbar.LENGTH_LONG)
snackbar.show()
}
alertDialogBuilder.setNegativeButton(R.string.no) { _, _ ->
clearView(activity.recyclerView, viewHolder)
}
alertDialogBuilder.show()
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dx: Float, dy: Float, actionState: Int, isCurrentlyActive: Boolean) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && dx < 0) {

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import android.widget.Toast;
import com.google.protobuf.ByteString;
@ -16,6 +17,7 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
import org.thoughtcrime.securesms.sms.MessageSender;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
@ -111,6 +113,27 @@ public class GroupUtil {
return Optional.of(new OutgoingGroupMediaMessage(groupRecipient, groupContext, null, System.currentTimeMillis(), 0, null, Collections.emptyList(), Collections.emptyList()));
}
public static boolean leaveGroup(@NonNull Context context, Recipient groupRecipient) {
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipient);
Optional<OutgoingGroupMediaMessage> leaveMessage = GroupUtil.createGroupLeaveMessage(context, groupRecipient);
if (threadId < 0 || !leaveMessage.isPresent()) {
return false;
}
MessageSender.send(context, leaveMessage.get(), threadId, false, null);
// We need to remove the master device from the group
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
String localNumber = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(context);
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
String groupId = groupRecipient.getAddress().toGroupString();
groupDatabase.setActive(groupId, false);
groupDatabase.remove(groupId, Address.fromSerialized(localNumber));
return true;
}
public static @NonNull GroupDescription getDescription(@NonNull Context context, @Nullable String encodedGroup) {
if (encodedGroup == null) {