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