Fix issue where recipient observing could show stale data.

This commit is contained in:
Greyson Parrelli 2021-01-07 14:56:26 -05:00 committed by Alan Evans
parent dda68d6c95
commit fba4ae91e3
2 changed files with 23 additions and 16 deletions

View file

@ -152,19 +152,17 @@ public final class ConversationListItem extends ConstraintLayout
boolean batchMode,
@Nullable String highlightSubstring)
{
if (this.recipient != null) this.recipient.removeForeverObserver(this);
observeRecipient(thread.getRecipient().live());
observeDisplayBody(null);
setSubjectViewText(null);
this.selectedThreads = selectedThreads;
this.recipient = thread.getRecipient().live();
this.threadId = thread.getThreadId();
this.glideRequests = glideRequests;
this.unreadCount = thread.getUnreadCount();
this.lastSeen = thread.getLastSeen();
this.thread = thread;
this.recipient.observeForever(this);
if (highlightSubstring != null) {
String name = recipient.get().isSelf() ? getContext().getString(R.string.note_to_self) : recipient.get().getDisplayName(getContext());
@ -209,15 +207,13 @@ public final class ConversationListItem extends ConstraintLayout
@NonNull Locale locale,
@Nullable String highlightSubstring)
{
if (this.recipient != null) this.recipient.removeForeverObserver(this);
observeRecipient(contact.live());
observeDisplayBody(null);
setSubjectViewText(null);
this.selectedThreads = Collections.emptySet();
this.recipient = contact.live();
this.glideRequests = glideRequests;
this.recipient.observeForever(this);
fromView.setText(contact);
fromView.setText(SearchUtil.getHighlightedSpan(locale, () -> new StyleSpan(Typeface.BOLD), new SpannableString(fromView.getText()), highlightSubstring));
@ -239,16 +235,13 @@ public final class ConversationListItem extends ConstraintLayout
@NonNull Locale locale,
@Nullable String highlightSubstring)
{
if (this.recipient != null) this.recipient.removeForeverObserver(this);
observeRecipient(messageResult.conversationRecipient.live());
observeDisplayBody(null);
setSubjectViewText(null);
this.selectedThreads = Collections.emptySet();
this.recipient = messageResult.conversationRecipient.live();
this.glideRequests = glideRequests;
this.recipient.observeForever(this);
fromView.setText(recipient.get(), true);
setSubjectViewText(SearchUtil.getHighlightedSpan(locale, () -> new StyleSpan(Typeface.BOLD), messageResult.bodySnippet, highlightSubstring));
dateView.setText(DateUtils.getBriefRelativeTimeSpanString(getContext(), locale, messageResult.receivedTimestampMs));
@ -266,9 +259,7 @@ public final class ConversationListItem extends ConstraintLayout
@Override
public void unbind() {
if (this.recipient != null) {
this.recipient.removeForeverObserver(this);
this.recipient = null;
observeRecipient(null);
setBatchMode(false);
contactPhotoImage.setAvatar(glideRequests, null, !batchMode);
}
@ -317,6 +308,18 @@ public final class ConversationListItem extends ConstraintLayout
return lastSeen;
}
private void observeRecipient(@Nullable LiveRecipient newRecipient) {
if (this.recipient != null) {
this.recipient.removeForeverObserver(this);
}
this.recipient = newRecipient;
if (this.recipient != null) {
this.recipient.observeForever(this);
}
}
private void observeDisplayBody(@Nullable LiveData<SpannableString> displayBody) {
if (this.displayBody != null) {
this.displayBody.removeObserver(this);
@ -404,6 +407,11 @@ public final class ConversationListItem extends ConstraintLayout
@Override
public void onRecipientChanged(@NonNull Recipient recipient) {
if (this.recipient == null || !this.recipient.getId().equals(recipient.getId())) {
Log.w(TAG, "Bad change! Local recipient doesn't match. Ignoring. Local: " + (this.recipient == null ? "null" : this.recipient.getId()) + ", Changed: " + recipient.getId());
return;
}
fromView.setText(recipient, unreadCount == 0);
contactPhotoImage.setAvatar(glideRequests, recipient, !batchMode);
setRippleColor(recipient);

View file

@ -87,11 +87,10 @@ public final class LiveRecipient {
*/
public void observeForever(@NonNull RecipientForeverObserver observer) {
Util.postToMain(() -> {
observers.add(observer);
if (observers.size() == 1) {
if (observers.isEmpty()) {
liveData.observeForever(foreverObserver);
}
observers.add(observer);
});
}