mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
[android] Fix double loading of conversation messages when rotating screen
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.android.contact;
|
||||
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.DialogInterface;
|
||||
@@ -170,23 +171,29 @@ public class ConversationActivity extends BriarActivity
|
||||
@Nullable
|
||||
private volatile GroupId messagingGroupId;
|
||||
|
||||
private final Observer<String> contactNameObserver = name -> {
|
||||
requireNonNull(name);
|
||||
loadMessages();
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle state) {
|
||||
setSceneTransitionAnimation();
|
||||
super.onCreate(state);
|
||||
|
||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||
.get(ConversationViewModel.class);
|
||||
|
||||
Intent i = getIntent();
|
||||
int id = i.getIntExtra(CONTACT_ID, -1);
|
||||
if (id == -1) throw new IllegalStateException();
|
||||
contactId = new ContactId(id);
|
||||
|
||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||
.get(ConversationViewModel.class);
|
||||
viewModel.setContactId(contactId);
|
||||
|
||||
setContentView(R.layout.activity_conversation);
|
||||
|
||||
// Custom Toolbar
|
||||
toolbar = setUpCustomToolbar(true);
|
||||
toolbar = requireNonNull(setUpCustomToolbar(true));
|
||||
if (toolbar == null) throw new AssertionError();
|
||||
toolbarAvatar = toolbar.findViewById(R.id.contactAvatar);
|
||||
toolbarStatus = toolbar.findViewById(R.id.contactStatus);
|
||||
@@ -205,8 +212,6 @@ public class ConversationActivity extends BriarActivity
|
||||
requireNonNull(deleted);
|
||||
if (deleted) finish();
|
||||
});
|
||||
viewModel.loadContact(contactId);
|
||||
viewModel.getContactDisplayName().observe(this, name -> loadMessages());
|
||||
|
||||
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
||||
setTransitionName(toolbarStatus, getBulbTransitionName(contactId));
|
||||
@@ -247,8 +252,7 @@ public class ConversationActivity extends BriarActivity
|
||||
notificationManager.blockContactNotification(contactId);
|
||||
notificationManager.clearContactNotification(contactId);
|
||||
displayContactOnlineStatus();
|
||||
if (viewModel.getContactDisplayName().getValue() != null)
|
||||
loadMessages();
|
||||
viewModel.getContactDisplayName().observe(this, contactNameObserver);
|
||||
list.startPeriodicUpdate();
|
||||
}
|
||||
|
||||
@@ -257,6 +261,7 @@ public class ConversationActivity extends BriarActivity
|
||||
super.onStop();
|
||||
eventBus.removeListener(this);
|
||||
notificationManager.unblockContactNotification(contactId);
|
||||
viewModel.getContactDisplayName().removeObserver(contactNameObserver);
|
||||
list.stopPeriodicUpdate();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.arch.lifecycle.Transformations;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
@@ -36,6 +37,8 @@ public class ConversationViewModel extends AndroidViewModel {
|
||||
private final Executor dbExecutor;
|
||||
private final ContactManager contactManager;
|
||||
|
||||
@Nullable
|
||||
private ContactId contactId = null;
|
||||
private final MutableLiveData<Contact> contact = new MutableLiveData<>();
|
||||
private final LiveData<AuthorId> contactAuthorId =
|
||||
Transformations.map(contact, c -> c.getAuthor().getId());
|
||||
@@ -54,7 +57,19 @@ public class ConversationViewModel extends AndroidViewModel {
|
||||
contactDeleted.setValue(false);
|
||||
}
|
||||
|
||||
void loadContact(ContactId contactId) {
|
||||
void setContactId(ContactId contactId) {
|
||||
if (this.contactId == null) {
|
||||
this.contactId = contactId;
|
||||
loadContact(contactId);
|
||||
} else if (!contactId.equals(this.contactId)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadContact(ContactId contactId) {
|
||||
if (!contactId.equals(this.contactId)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
@@ -69,6 +84,9 @@ public class ConversationViewModel extends AndroidViewModel {
|
||||
}
|
||||
|
||||
void setContactAlias(ContactId contactId, String alias) {
|
||||
if (!contactId.equals(this.contactId)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
contactManager.setContactAlias(contactId,
|
||||
|
||||
Reference in New Issue
Block a user