mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Combine LiveData observers, avoid redundant loads.
This commit is contained in:
@@ -129,6 +129,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
Executor cryptoExecutor;
|
Executor cryptoExecutor;
|
||||||
|
|
||||||
private final Map<MessageId, String> bodyCache = new ConcurrentHashMap<>();
|
private final Map<MessageId, String> bodyCache = new ConcurrentHashMap<>();
|
||||||
|
private final MutableLiveData<String> contactName = new MutableLiveData<>();
|
||||||
|
|
||||||
private ConversationAdapter adapter;
|
private ConversationAdapter adapter;
|
||||||
private Toolbar toolbar;
|
private Toolbar toolbar;
|
||||||
@@ -165,7 +166,6 @@ public class ConversationActivity extends BriarActivity
|
|||||||
private volatile AuthorId contactAuthorId;
|
private volatile AuthorId contactAuthorId;
|
||||||
@Nullable
|
@Nullable
|
||||||
private volatile GroupId messagingGroupId;
|
private volatile GroupId messagingGroupId;
|
||||||
private MutableLiveData<String> contactName = new MutableLiveData<>();
|
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
@Override
|
@Override
|
||||||
@@ -274,7 +274,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
runOnDbThread(() -> {
|
runOnDbThread(() -> {
|
||||||
try {
|
try {
|
||||||
long start = now();
|
long start = now();
|
||||||
if (contactName == null || contactAuthorId == null) {
|
if (contactAuthorId == null) {
|
||||||
Contact contact = contactManager.getContact(contactId);
|
Contact contact = contactManager.getContact(contactId);
|
||||||
contactName.postValue(contact.getAuthor().getName());
|
contactName.postValue(contact.getAuthor().getName());
|
||||||
contactAuthorId = contact.getAuthor().getId();
|
contactAuthorId = contact.getAuthor().getId();
|
||||||
@@ -459,36 +459,22 @@ public class ConversationActivity extends BriarActivity
|
|||||||
|
|
||||||
private void onNewPrivateMessage(PrivateMessageHeader h) {
|
private void onNewPrivateMessage(PrivateMessageHeader h) {
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
if (h instanceof PrivateRequest) {
|
if (h instanceof PrivateRequest || h instanceof PrivateResponse) {
|
||||||
contactName.observe(this, new Observer<String>() {
|
String cName = contactName.getValue();
|
||||||
@Override
|
if (cName == null) {
|
||||||
public void onChanged(@Nullable String name) {
|
// Wait for the contact name to be loaded
|
||||||
if (name == null) loadContactName();
|
contactName.observe(this, new Observer<String>() {
|
||||||
else {
|
@Override
|
||||||
PrivateRequest m = (PrivateRequest) h;
|
public void onChanged(@Nullable String cName) {
|
||||||
ConversationItem item = ConversationItem.from(
|
if (cName != null) {
|
||||||
ConversationActivity.this, name, m);
|
onNewPrivateRequestOrResponse(h, cName);
|
||||||
addConversationItem(item);
|
contactName.removeObserver(this);
|
||||||
contactName.removeObserver(this);
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
} else {
|
||||||
if (contactName.getValue() == null) loadContactName();
|
onNewPrivateRequestOrResponse(h, cName);
|
||||||
} else if (h instanceof PrivateResponse) {
|
}
|
||||||
contactName.observe(this, new Observer<String>() {
|
|
||||||
@Override
|
|
||||||
public void onChanged(@Nullable String name) {
|
|
||||||
if (name == null) loadContactName();
|
|
||||||
else {
|
|
||||||
PrivateResponse m = (PrivateResponse) h;
|
|
||||||
ConversationItem item = ConversationItem.from(
|
|
||||||
ConversationActivity.this, name, m);
|
|
||||||
addConversationItem(item);
|
|
||||||
contactName.removeObserver(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (contactName.getValue() == null) loadContactName();
|
|
||||||
} else {
|
} else {
|
||||||
addConversationItem(ConversationItem.from(h));
|
addConversationItem(ConversationItem.from(h));
|
||||||
loadMessageBody(h.getId());
|
loadMessageBody(h.getId());
|
||||||
@@ -496,6 +482,12 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void onNewPrivateRequestOrResponse(PrivateMessageHeader h,
|
||||||
|
String cName) {
|
||||||
|
addConversationItem(ConversationItem.from(this, cName, h));
|
||||||
|
}
|
||||||
|
|
||||||
private void markMessages(Collection<MessageId> messageIds, boolean sent,
|
private void markMessages(Collection<MessageId> messageIds, boolean sent,
|
||||||
boolean seen) {
|
boolean seen) {
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
@@ -550,7 +542,8 @@ public class ConversationActivity extends BriarActivity
|
|||||||
//noinspection ConstantConditions init in loadGroupId()
|
//noinspection ConstantConditions init in loadGroupId()
|
||||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
storeMessage(privateMessageFactory.createPrivateMessage(
|
||||||
messagingGroupId, timestamp, body), body);
|
messagingGroupId, timestamp, body), body);
|
||||||
} catch (FormatException e) {throw new RuntimeException(e);
|
} catch (FormatException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -780,16 +773,4 @@ public class ConversationActivity extends BriarActivity
|
|||||||
throws DbException {
|
throws DbException {
|
||||||
groupInvitationManager.respondToInvitation(contactId, id, accept);
|
groupInvitationManager.respondToInvitation(contactId, id, accept);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadContactName() {
|
|
||||||
runOnDbThread(() -> {
|
|
||||||
try {
|
|
||||||
Contact c = contactManager.getContact(contactId);
|
|
||||||
contactName.postValue(c.getAuthor().getName());
|
|
||||||
} catch (DbException e) {
|
|
||||||
handleDbException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user