mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Use event instead of CommitAction to handle removed PendingContacts
This commit is contained in:
@@ -80,10 +80,8 @@ public interface ContactManager {
|
||||
/**
|
||||
* Removes a {@link PendingContact} that is in state
|
||||
* {@link PendingContactState FAILED}.
|
||||
* @param commitAction an action to run on the main thread after removing.
|
||||
*/
|
||||
void removePendingContact(PendingContactId pendingContact,
|
||||
Runnable commitAction) throws DbException;
|
||||
void removePendingContact(PendingContactId pendingContact) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the contact with the given ID.
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.api.contact.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.PendingContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a pending contact is removed.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class PendingContactRemovedEvent extends Event {
|
||||
|
||||
private final PendingContactId id;
|
||||
|
||||
public PendingContactRemovedEvent(PendingContactId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PendingContactId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.PendingContact;
|
||||
import org.briarproject.bramble.api.contact.PendingContactId;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedRemotelyEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
@@ -218,8 +219,7 @@ class ContactManagerImpl implements ContactManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePendingContact(PendingContactId id,
|
||||
Runnable commitAction) throws DbException {
|
||||
public void removePendingContact(PendingContactId id) throws DbException {
|
||||
// TODO replace with real implementation
|
||||
for (PendingContact pc : pendingContacts) {
|
||||
if (pc.getId().equals(id)) {
|
||||
@@ -231,7 +231,8 @@ class ContactManagerImpl implements ContactManager {
|
||||
Thread.sleep(250);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
db.transaction(true, txn -> txn.attach(commitAction));
|
||||
db.transaction(true,
|
||||
txn -> txn.attach(new PendingContactRemovedEvent(id)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedRemotelyEvent;
|
||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||
@@ -299,7 +300,8 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
if (pe.getPendingContactState() == WAITING_FOR_CONNECTION) {
|
||||
checkForPendingContacts();
|
||||
}
|
||||
} else if (e instanceof ContactAddedRemotelyEvent) {
|
||||
} else if (e instanceof PendingContactRemovedEvent ||
|
||||
e instanceof ContactAddedRemotelyEvent) {
|
||||
checkForPendingContacts();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,8 +86,7 @@ public class PendingContactListActivity extends BriarActivity
|
||||
|
||||
@Override
|
||||
public void onFailedPendingContactRemoved(PendingContact pendingContact) {
|
||||
viewModel.removePendingContact(pendingContact.getId(),
|
||||
() -> adapter.remove(pendingContact));
|
||||
viewModel.removePendingContact(pendingContact.getId());
|
||||
}
|
||||
|
||||
private void onPendingContactsChanged(Collection<PendingContact> contacts) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.PendingContact;
|
||||
import org.briarproject.bramble.api.contact.PendingContactId;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedRemotelyEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
@@ -63,7 +64,8 @@ public class PendingContactListViewModel extends AndroidViewModel
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof ContactAddedRemotelyEvent ||
|
||||
e instanceof PendingContactStateChangedEvent) {
|
||||
e instanceof PendingContactStateChangedEvent ||
|
||||
e instanceof PendingContactRemovedEvent) {
|
||||
loadPendingContacts();
|
||||
}
|
||||
}
|
||||
@@ -82,16 +84,14 @@ public class PendingContactListViewModel extends AndroidViewModel
|
||||
return pendingContacts;
|
||||
}
|
||||
|
||||
void removePendingContact(PendingContactId id, Runnable commitAction) {
|
||||
void removePendingContact(PendingContactId id) {
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
contactManager
|
||||
.removePendingContact(id, commitAction);
|
||||
contactManager.removePendingContact(id);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
loadPendingContacts();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,8 +40,10 @@ class PendingContactViewHolder extends ViewHolder {
|
||||
avatar.setBackgroundBytes(item.getId().getBytes());
|
||||
name.setText(item.getAlias());
|
||||
time.setText(formatDate(time.getContext(), item.getTimestamp()));
|
||||
removeButton.setOnClickListener(
|
||||
v -> listener.onFailedPendingContactRemoved(item));
|
||||
removeButton.setOnClickListener(v -> {
|
||||
listener.onFailedPendingContactRemoved(item);
|
||||
removeButton.setEnabled(false);
|
||||
});
|
||||
|
||||
int color = ContextCompat
|
||||
.getColor(status.getContext(), R.color.briar_green);
|
||||
@@ -69,6 +71,7 @@ class PendingContactViewHolder extends ViewHolder {
|
||||
}
|
||||
status.setTextColor(color);
|
||||
removeButton.setVisibility(buttonVisibility);
|
||||
removeButton.setEnabled(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user