Use event instead of CommitAction to handle removed PendingContacts

This commit is contained in:
Torsten Grote
2019-05-09 11:43:22 -03:00
parent 19bc73ac61
commit fbe375cc4e
7 changed files with 45 additions and 16 deletions

View File

@@ -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();
}
}

View File

@@ -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) {

View File

@@ -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();
}
}

View File

@@ -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);
}
}