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