mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Merge branch '1871-viewmodel-for-introduction' into 'master'
Introduce ViewModel for IntroductionActivity (and ContactChooserFragment) See merge request briar/briar!1349
This commit is contained in:
@@ -32,6 +32,7 @@ import org.briarproject.briar.android.account.LockManagerImpl;
|
|||||||
import org.briarproject.briar.android.account.SetupModule;
|
import org.briarproject.briar.android.account.SetupModule;
|
||||||
import org.briarproject.briar.android.contact.ContactListModule;
|
import org.briarproject.briar.android.contact.ContactListModule;
|
||||||
import org.briarproject.briar.android.forum.ForumModule;
|
import org.briarproject.briar.android.forum.ForumModule;
|
||||||
|
import org.briarproject.briar.android.introduction.IntroductionModule;
|
||||||
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
||||||
import org.briarproject.briar.android.logging.LoggingModule;
|
import org.briarproject.briar.android.logging.LoggingModule;
|
||||||
import org.briarproject.briar.android.login.LoginModule;
|
import org.briarproject.briar.android.login.LoginModule;
|
||||||
@@ -82,6 +83,7 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
|||||||
SettingsModule.class,
|
SettingsModule.class,
|
||||||
DevReportModule.class,
|
DevReportModule.class,
|
||||||
ContactListModule.class,
|
ContactListModule.class,
|
||||||
|
IntroductionModule.class,
|
||||||
// below need to be within same scope as ViewModelProvider.Factory
|
// below need to be within same scope as ViewModelProvider.Factory
|
||||||
ForumModule.class,
|
ForumModule.class,
|
||||||
GroupListModule.class,
|
GroupListModule.class,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.briarproject.briar.android.contact;
|
package org.briarproject.briar.android.contact;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.view.View;
|
|
||||||
|
|
||||||
import org.briarproject.briar.android.util.BriarAdapter;
|
import org.briarproject.briar.android.util.BriarAdapter;
|
||||||
|
|
||||||
@@ -45,8 +44,4 @@ public abstract class BaseContactListAdapter<I extends ContactItem, VH extends C
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface OnContactClickListener<I> {
|
|
||||||
void onItemClick(View view, I item);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import android.view.ViewGroup;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.NullSafety;
|
import org.briarproject.bramble.api.nullsafety.NullSafety;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil.ItemCallback;
|
import androidx.recyclerview.widget.DiffUtil.ItemCallback;
|
||||||
import androidx.recyclerview.widget.ListAdapter;
|
import androidx.recyclerview.widget.ListAdapter;
|
||||||
@@ -16,9 +15,6 @@ import androidx.recyclerview.widget.ListAdapter;
|
|||||||
public class ContactListAdapter extends
|
public class ContactListAdapter extends
|
||||||
ListAdapter<ContactListItem, ContactListItemViewHolder> {
|
ListAdapter<ContactListItem, ContactListItemViewHolder> {
|
||||||
|
|
||||||
// TODO: using the click listener interface from BaseContactListAdapter on
|
|
||||||
// purpose here because it is entangled with ContactListItemViewHolder. At
|
|
||||||
// some point we probably want to change that.
|
|
||||||
protected final OnContactClickListener<ContactListItem> listener;
|
protected final OnContactClickListener<ContactListItem> listener;
|
||||||
|
|
||||||
public ContactListAdapter(
|
public ContactListAdapter(
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
|
||||||
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
||||||
import org.briarproject.briar.android.contact.add.remote.PendingContactListActivity;
|
import org.briarproject.briar.android.contact.add.remote.PendingContactListActivity;
|
||||||
import org.briarproject.briar.android.conversation.ConversationActivity;
|
import org.briarproject.briar.android.conversation.ConversationActivity;
|
||||||
@@ -101,8 +100,6 @@ public class ContactListFragment extends BaseFragment
|
|||||||
.observe(getViewLifecycleOwner(), result -> {
|
.observe(getViewLifecycleOwner(), result -> {
|
||||||
result.onError(this::handleException).onSuccess(items -> {
|
result.onError(this::handleException).onSuccess(items -> {
|
||||||
adapter.submitList(items);
|
adapter.submitList(items);
|
||||||
// TODO remove when BriarRecyclerView was adapted
|
|
||||||
list.showData();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
viewModel.getHasPendingContacts()
|
viewModel.getHasPendingContacts()
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
|||||||
@@ -3,70 +3,41 @@ package org.briarproject.briar.android.contact;
|
|||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
|
||||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
|
||||||
import org.briarproject.bramble.api.contact.event.PendingContactAddedEvent;
|
import org.briarproject.bramble.api.contact.event.PendingContactAddedEvent;
|
||||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
|
||||||
import org.briarproject.bramble.api.db.TransactionManager;
|
import org.briarproject.bramble.api.db.TransactionManager;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
|
||||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
|
||||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||||
import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
|
|
||||||
import org.briarproject.briar.api.client.MessageTracker;
|
|
||||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
|
||||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorManager;
|
import org.briarproject.briar.api.identity.AuthorManager;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.arch.core.util.Function;
|
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.LogUtils.now;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ContactListViewModel extends DbViewModel implements EventListener {
|
class ContactListViewModel extends ContactsViewModel {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(ContactListViewModel.class.getName());
|
getLogger(ContactListViewModel.class.getName());
|
||||||
|
|
||||||
private final ContactManager contactManager;
|
|
||||||
private final AuthorManager authorManager;
|
|
||||||
private final ConversationManager conversationManager;
|
|
||||||
private final ConnectionRegistry connectionRegistry;
|
|
||||||
private final EventBus eventBus;
|
|
||||||
private final AndroidNotificationManager notificationManager;
|
private final AndroidNotificationManager notificationManager;
|
||||||
|
|
||||||
private final MutableLiveData<LiveResult<List<ContactListItem>>>
|
|
||||||
contactListItems = new MutableLiveData<>();
|
|
||||||
|
|
||||||
private final MutableLiveData<Boolean> hasPendingContacts =
|
private final MutableLiveData<Boolean> hasPendingContacts =
|
||||||
new MutableLiveData<>();
|
new MutableLiveData<>();
|
||||||
|
|
||||||
@@ -79,99 +50,25 @@ class ContactListViewModel extends DbViewModel implements EventListener {
|
|||||||
ConversationManager conversationManager,
|
ConversationManager conversationManager,
|
||||||
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
||||||
AndroidNotificationManager notificationManager) {
|
AndroidNotificationManager notificationManager) {
|
||||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||||
this.contactManager = contactManager;
|
contactManager, authorManager, conversationManager,
|
||||||
this.authorManager = authorManager;
|
connectionRegistry, eventBus);
|
||||||
this.conversationManager = conversationManager;
|
|
||||||
this.connectionRegistry = connectionRegistry;
|
|
||||||
this.eventBus = eventBus;
|
|
||||||
this.notificationManager = notificationManager;
|
this.notificationManager = notificationManager;
|
||||||
this.eventBus.addListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCleared() {
|
|
||||||
super.onCleared();
|
|
||||||
eventBus.removeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loadContacts() {
|
|
||||||
loadList(this::loadContacts, contactListItems::setValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ContactListItem> loadContacts(Transaction txn)
|
|
||||||
throws DbException {
|
|
||||||
long start = now();
|
|
||||||
List<ContactListItem> contacts = new ArrayList<>();
|
|
||||||
for (Contact c : contactManager.getContacts(txn)) {
|
|
||||||
ContactId id = c.getId();
|
|
||||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, c);
|
|
||||||
MessageTracker.GroupCount count =
|
|
||||||
conversationManager.getGroupCount(txn, id);
|
|
||||||
boolean connected = connectionRegistry.isConnected(c.getId());
|
|
||||||
contacts.add(new ContactListItem(c, authorInfo, connected, count));
|
|
||||||
}
|
|
||||||
Collections.sort(contacts);
|
|
||||||
logDuration(LOG, "Full load", start);
|
|
||||||
return contacts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof ContactAddedEvent) {
|
super.eventOccurred(e);
|
||||||
LOG.info("Contact added, reloading");
|
if (e instanceof PendingContactAddedEvent ||
|
||||||
loadContacts();
|
|
||||||
} else if (e instanceof ContactConnectedEvent) {
|
|
||||||
updateItem(((ContactConnectedEvent) e).getContactId(),
|
|
||||||
item -> new ContactListItem(item, true), false);
|
|
||||||
} else if (e instanceof ContactDisconnectedEvent) {
|
|
||||||
updateItem(((ContactDisconnectedEvent) e).getContactId(),
|
|
||||||
item -> new ContactListItem(item, false), false);
|
|
||||||
} else if (e instanceof ContactRemovedEvent) {
|
|
||||||
LOG.info("Contact removed, removing item");
|
|
||||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
|
||||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
|
||||||
LOG.info("Conversation message received, updating item");
|
|
||||||
ConversationMessageReceivedEvent<?> p =
|
|
||||||
(ConversationMessageReceivedEvent<?>) e;
|
|
||||||
ConversationMessageHeader h = p.getMessageHeader();
|
|
||||||
updateItem(p.getContactId(), item -> new ContactListItem(item, h),
|
|
||||||
true);
|
|
||||||
} else if (e instanceof PendingContactAddedEvent ||
|
|
||||||
e instanceof PendingContactRemovedEvent) {
|
e instanceof PendingContactRemovedEvent) {
|
||||||
checkForPendingContacts();
|
checkForPendingContacts();
|
||||||
} else if (e instanceof AvatarUpdatedEvent) {
|
|
||||||
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
|
|
||||||
updateItem(a.getContactId(), item -> new ContactListItem(item,
|
|
||||||
a.getAttachmentHeader()), false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<LiveResult<List<ContactListItem>>> getContactListItems() {
|
|
||||||
return contactListItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
LiveData<Boolean> getHasPendingContacts() {
|
LiveData<Boolean> getHasPendingContacts() {
|
||||||
return hasPendingContacts;
|
return hasPendingContacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateItem(ContactId c,
|
|
||||||
Function<ContactListItem, ContactListItem> replacer, boolean sort) {
|
|
||||||
List<ContactListItem> list = updateListItems(contactListItems,
|
|
||||||
itemToTest -> itemToTest.getContact().getId().equals(c),
|
|
||||||
replacer);
|
|
||||||
if (list == null) return;
|
|
||||||
if (sort) Collections.sort(list);
|
|
||||||
contactListItems.setValue(new LiveResult<>(list));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeItem(ContactId c) {
|
|
||||||
List<ContactListItem> list = removeListItems(contactListItems,
|
|
||||||
itemToTest -> itemToTest.getContact().getId().equals(c));
|
|
||||||
if (list == null) return;
|
|
||||||
contactListItems.setValue(new LiveResult<>(list));
|
|
||||||
}
|
|
||||||
|
|
||||||
void checkForPendingContacts() {
|
void checkForPendingContacts() {
|
||||||
runOnDbThread(() -> {
|
runOnDbThread(() -> {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -0,0 +1,170 @@
|
|||||||
|
package org.briarproject.briar.android.contact;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
|
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||||
|
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
|
import org.briarproject.bramble.api.db.TransactionManager;
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
|
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||||
|
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||||
|
import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
|
||||||
|
import org.briarproject.briar.api.client.MessageTracker;
|
||||||
|
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||||
|
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||||
|
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
|
import androidx.arch.core.util.Function;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.now;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class ContactsViewModel extends DbViewModel implements EventListener {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(ContactsViewModel.class.getName());
|
||||||
|
|
||||||
|
protected final ContactManager contactManager;
|
||||||
|
private final AuthorManager authorManager;
|
||||||
|
private final ConversationManager conversationManager;
|
||||||
|
private final ConnectionRegistry connectionRegistry;
|
||||||
|
private final EventBus eventBus;
|
||||||
|
|
||||||
|
private final MutableLiveData<LiveResult<List<ContactListItem>>>
|
||||||
|
contactListItems = new MutableLiveData<>();
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ContactsViewModel(Application application,
|
||||||
|
@DatabaseExecutor Executor dbExecutor,
|
||||||
|
LifecycleManager lifecycleManager, TransactionManager db,
|
||||||
|
AndroidExecutor androidExecutor, ContactManager contactManager,
|
||||||
|
AuthorManager authorManager,
|
||||||
|
ConversationManager conversationManager,
|
||||||
|
ConnectionRegistry connectionRegistry, EventBus eventBus) {
|
||||||
|
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||||
|
this.contactManager = contactManager;
|
||||||
|
this.authorManager = authorManager;
|
||||||
|
this.conversationManager = conversationManager;
|
||||||
|
this.connectionRegistry = connectionRegistry;
|
||||||
|
this.eventBus = eventBus;
|
||||||
|
this.eventBus.addListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCleared() {
|
||||||
|
super.onCleared();
|
||||||
|
eventBus.removeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void loadContacts() {
|
||||||
|
loadList(this::loadContacts, contactListItems::setValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ContactListItem> loadContacts(Transaction txn)
|
||||||
|
throws DbException {
|
||||||
|
long start = now();
|
||||||
|
List<ContactListItem> contacts = new ArrayList<>();
|
||||||
|
for (Contact c : contactManager.getContacts(txn)) {
|
||||||
|
ContactId id = c.getId();
|
||||||
|
if (!displayContact(id)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, c);
|
||||||
|
MessageTracker.GroupCount count =
|
||||||
|
conversationManager.getGroupCount(txn, id);
|
||||||
|
boolean connected = connectionRegistry.isConnected(c.getId());
|
||||||
|
contacts.add(new ContactListItem(c, authorInfo, connected, count));
|
||||||
|
}
|
||||||
|
Collections.sort(contacts);
|
||||||
|
logDuration(LOG, "Full load", start);
|
||||||
|
return contacts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override this method to display only a subset of contacts.
|
||||||
|
*/
|
||||||
|
protected boolean displayContact(ContactId contactId) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eventOccurred(Event e) {
|
||||||
|
if (e instanceof ContactAddedEvent) {
|
||||||
|
LOG.info("Contact added, reloading");
|
||||||
|
loadContacts();
|
||||||
|
} else if (e instanceof ContactConnectedEvent) {
|
||||||
|
updateItem(((ContactConnectedEvent) e).getContactId(),
|
||||||
|
item -> new ContactListItem(item, true), false);
|
||||||
|
} else if (e instanceof ContactDisconnectedEvent) {
|
||||||
|
updateItem(((ContactDisconnectedEvent) e).getContactId(),
|
||||||
|
item -> new ContactListItem(item, false), false);
|
||||||
|
} else if (e instanceof ContactRemovedEvent) {
|
||||||
|
LOG.info("Contact removed, removing item");
|
||||||
|
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||||
|
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||||
|
LOG.info("Conversation message received, updating item");
|
||||||
|
ConversationMessageReceivedEvent<?> p =
|
||||||
|
(ConversationMessageReceivedEvent<?>) e;
|
||||||
|
ConversationMessageHeader h = p.getMessageHeader();
|
||||||
|
updateItem(p.getContactId(), item -> new ContactListItem(item, h),
|
||||||
|
true);
|
||||||
|
} else if (e instanceof AvatarUpdatedEvent) {
|
||||||
|
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
|
||||||
|
updateItem(a.getContactId(), item -> new ContactListItem(item,
|
||||||
|
a.getAttachmentHeader()), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public LiveData<LiveResult<List<ContactListItem>>> getContactListItems() {
|
||||||
|
return contactListItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void updateItem(ContactId c,
|
||||||
|
Function<ContactListItem, ContactListItem> replacer, boolean sort) {
|
||||||
|
List<ContactListItem> list = updateListItems(contactListItems,
|
||||||
|
itemToTest -> itemToTest.getContact().getId().equals(c),
|
||||||
|
replacer);
|
||||||
|
if (list == null) return;
|
||||||
|
if (sort) Collections.sort(list);
|
||||||
|
contactListItems.setValue(new LiveResult<>(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void removeItem(ContactId c) {
|
||||||
|
List<ContactListItem> list = removeListItems(contactListItems,
|
||||||
|
itemToTest -> itemToTest.getContact().getId().equals(c));
|
||||||
|
if (list == null) return;
|
||||||
|
contactListItems.setValue(new LiveResult<>(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public class LegacyContactListAdapter extends
|
|
||||||
BaseContactListAdapter<ContactListItem, ContactListItemViewHolder> {
|
|
||||||
|
|
||||||
public LegacyContactListAdapter(Context context,
|
|
||||||
OnContactClickListener<ContactListItem> listener) {
|
|
||||||
super(context, ContactListItem.class, listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ContactListItemViewHolder onCreateViewHolder(ViewGroup viewGroup,
|
|
||||||
int i) {
|
|
||||||
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
||||||
R.layout.list_item_contact, viewGroup, false);
|
|
||||||
|
|
||||||
return new ContactListItemViewHolder(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean areContentsTheSame(ContactListItem c1, ContactListItem c2) {
|
|
||||||
// check for all properties that influence visual
|
|
||||||
// representation of contact
|
|
||||||
if (c1.isEmpty() != c2.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (c1.getUnreadCount() != c2.getUnreadCount()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (c1.getTimestamp() != c2.getTimestamp()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return c1.isConnected() == c2.isConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compare(ContactListItem c1, ContactListItem c2) {
|
|
||||||
return Long.compare(c2.getTimestamp(), c1.getTimestamp());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.briarproject.briar.android.contact;
|
||||||
|
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
public interface OnContactClickListener<I> {
|
||||||
|
|
||||||
|
void onItemClick(View view, I item);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.contact.ContactId;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter;
|
||||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||||
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
|
||||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||||
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
|
||||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||||
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.view.ViewGroup;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ContactSelectorAdapter extends
|
class ContactSelectorAdapter extends
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import android.view.MenuItem;
|
|||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package org.briarproject.briar.android.contactselection;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -5,74 +5,41 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.ContactListAdapter;
|
||||||
import org.briarproject.briar.android.contact.ContactListItem;
|
import org.briarproject.briar.android.contact.ContactListItem;
|
||||||
import org.briarproject.briar.android.contact.LegacyContactListAdapter;
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
|
||||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorManager;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class ContactChooserFragment extends BaseFragment {
|
public class ContactChooserFragment extends BaseFragment
|
||||||
|
implements OnContactClickListener<ContactListItem> {
|
||||||
|
|
||||||
public static final String TAG = ContactChooserFragment.class.getName();
|
private static final String TAG = ContactChooserFragment.class.getName();
|
||||||
private static final Logger LOG = Logger.getLogger(TAG);
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
|
private IntroductionViewModel viewModel;
|
||||||
|
private final ContactListAdapter adapter = new ContactListAdapter(this);
|
||||||
private BriarRecyclerView list;
|
private BriarRecyclerView list;
|
||||||
private LegacyContactListAdapter adapter;
|
|
||||||
private ContactId contactId;
|
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
|
||||||
private volatile Contact c1;
|
|
||||||
@Inject
|
|
||||||
volatile ContactManager contactManager;
|
|
||||||
@Inject
|
|
||||||
volatile AuthorManager authorManager;
|
|
||||||
@Inject
|
|
||||||
volatile ConversationManager conversationManager;
|
|
||||||
@Inject
|
|
||||||
volatile ConnectionRegistry connectionRegistry;
|
|
||||||
|
|
||||||
public static ContactChooserFragment newInstance(ContactId id) {
|
|
||||||
Bundle args = new Bundle();
|
|
||||||
|
|
||||||
ContactChooserFragment fragment = new ContactChooserFragment();
|
|
||||||
args.putInt(CONTACT_ID, id.getInt());
|
|
||||||
fragment.setArguments(args);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectFragment(ActivityComponent component) {
|
public void injectFragment(ActivityComponent component) {
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
|
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||||
|
.get(IntroductionViewModel.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -80,23 +47,20 @@ public class ContactChooserFragment extends BaseFragment {
|
|||||||
@Nullable ViewGroup container,
|
@Nullable ViewGroup container,
|
||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
|
||||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
// change toolbar text (relevant when navigating back to this fragment)
|
||||||
|
requireActivity().setTitle(R.string.introduction_activity_title);
|
||||||
|
|
||||||
OnContactClickListener<ContactListItem> onContactClickListener =
|
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||||
(view, item) -> {
|
|
||||||
if (c1 == null) throw new IllegalStateException();
|
|
||||||
Contact c2 = item.getContact();
|
|
||||||
showMessageScreen(c1, c2);
|
|
||||||
};
|
|
||||||
adapter = new LegacyContactListAdapter(requireActivity(),
|
|
||||||
onContactClickListener);
|
|
||||||
|
|
||||||
list = contentView.findViewById(R.id.list);
|
list = contentView.findViewById(R.id.list);
|
||||||
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||||
list.setAdapter(adapter);
|
list.setAdapter(adapter);
|
||||||
list.setEmptyText(R.string.no_contacts);
|
list.setEmptyText(R.string.no_contacts);
|
||||||
|
|
||||||
contactId = new ContactId(requireArguments().getInt(CONTACT_ID));
|
viewModel.getContactListItems().observe(getViewLifecycleOwner(),
|
||||||
|
result -> result.onError(this::handleException)
|
||||||
|
.onSuccess(adapter::submitList)
|
||||||
|
);
|
||||||
|
|
||||||
return contentView;
|
return contentView;
|
||||||
}
|
}
|
||||||
@@ -104,14 +68,13 @@ public class ContactChooserFragment extends BaseFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
loadContacts();
|
list.startPeriodicUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
adapter.clear();
|
list.stopPeriodicUpdate();
|
||||||
list.showProgressBar();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -119,43 +82,9 @@ public class ContactChooserFragment extends BaseFragment {
|
|||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadContacts() {
|
@Override
|
||||||
listener.runOnDbThread(() -> {
|
public void onItemClick(View view, ContactListItem item) {
|
||||||
try {
|
viewModel.setSecondContactId(item.getContact().getId());
|
||||||
List<ContactListItem> contacts = new ArrayList<>();
|
viewModel.triggerContactSelected();
|
||||||
for (Contact c : contactManager.getContacts()) {
|
|
||||||
if (c.getId().equals(contactId)) {
|
|
||||||
c1 = c;
|
|
||||||
} else {
|
|
||||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(c);
|
|
||||||
ContactId id = c.getId();
|
|
||||||
GroupCount count =
|
|
||||||
conversationManager.getGroupCount(id);
|
|
||||||
boolean connected =
|
|
||||||
connectionRegistry.isConnected(c.getId());
|
|
||||||
contacts.add(new ContactListItem(c, authorInfo,
|
|
||||||
connected, count));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
displayContacts(contacts);
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayContacts(List<ContactListItem> contacts) {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
|
||||||
if (contacts.isEmpty()) list.showData();
|
|
||||||
else adapter.addAll(contacts);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showMessageScreen(Contact c1, Contact c2) {
|
|
||||||
IntroductionMessageFragment messageFragment =
|
|
||||||
IntroductionMessageFragment
|
|
||||||
.newInstance(c1.getId().getInt(), c2.getId().getInt());
|
|
||||||
showNextFragment(messageFragment);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,30 +9,67 @@ import org.briarproject.briar.android.activity.ActivityComponent;
|
|||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||||
|
|
||||||
public class IntroductionActivity extends BriarActivity
|
public class IntroductionActivity extends BriarActivity
|
||||||
implements BaseFragmentListener {
|
implements BaseFragmentListener {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
|
private IntroductionViewModel viewModel;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void injectActivity(ActivityComponent component) {
|
||||||
|
component.inject(this);
|
||||||
|
viewModel = new ViewModelProvider(this, viewModelFactory)
|
||||||
|
.get(IntroductionViewModel.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String BUNDLE_CONTACT2 = "contact2";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
int id = intent.getIntExtra(CONTACT_ID, -1);
|
int contactId1 = intent.getIntExtra(CONTACT_ID, -1);
|
||||||
if (id == -1) throw new IllegalStateException("No ContactId");
|
if (contactId1 == -1)
|
||||||
ContactId contactId = new ContactId(id);
|
throw new IllegalStateException("No ContactId");
|
||||||
|
ContactId firstContactId = new ContactId(contactId1);
|
||||||
|
|
||||||
|
viewModel.setFirstContactId(firstContactId);
|
||||||
|
|
||||||
setContentView(R.layout.activity_fragment_container);
|
setContentView(R.layout.activity_fragment_container);
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
showInitialFragment(ContactChooserFragment.newInstance(contactId));
|
showInitialFragment(new ContactChooserFragment());
|
||||||
|
} else {
|
||||||
|
int contactId2 = savedInstanceState.getInt(BUNDLE_CONTACT2);
|
||||||
|
ContactId secondContactId = new ContactId(contactId2);
|
||||||
|
viewModel.setSecondContactId(secondContactId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
viewModel.getSecondContactSelected().observeEvent(this, e -> {
|
||||||
|
IntroductionMessageFragment fragment =
|
||||||
|
new IntroductionMessageFragment();
|
||||||
|
showNextFragment(fragment);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectActivity(ActivityComponent component) {
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||||
component.inject(this);
|
super.onSaveInstanceState(outState);
|
||||||
|
|
||||||
|
ContactId secondContactId = viewModel.getSecondContactId();
|
||||||
|
if (secondContactId != null) {
|
||||||
|
outState.putInt(BUNDLE_CONTACT2, secondContactId.getInt());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.briarproject.briar.android.introduction;
|
||||||
|
|
||||||
|
import org.briarproject.briar.android.contact.ContactItem;
|
||||||
|
|
||||||
|
class IntroductionInfo {
|
||||||
|
private final ContactItem c1;
|
||||||
|
private final ContactItem c2;
|
||||||
|
private final boolean possible;
|
||||||
|
|
||||||
|
IntroductionInfo(ContactItem c1, ContactItem c2,
|
||||||
|
boolean possible) {
|
||||||
|
this.c1 = c1;
|
||||||
|
this.c2 = c2;
|
||||||
|
this.possible = possible;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContactItem getContact1() {
|
||||||
|
return c1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContactItem getContact2() {
|
||||||
|
return c2;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isPossible() {
|
||||||
|
return possible;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.briar.android.introduction;
|
package org.briarproject.briar.android.introduction;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@@ -8,12 +7,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
@@ -24,25 +18,19 @@ import org.briarproject.briar.android.view.TextInputView;
|
|||||||
import org.briarproject.briar.android.view.TextSendController;
|
import org.briarproject.briar.android.view.TextSendController;
|
||||||
import org.briarproject.briar.android.view.TextSendController.SendListener;
|
import org.briarproject.briar.android.view.TextSendController.SendListener;
|
||||||
import org.briarproject.briar.api.attachment.AttachmentHeader;
|
import org.briarproject.briar.api.attachment.AttachmentHeader;
|
||||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorManager;
|
|
||||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
import de.hdodenhof.circleimageview.CircleImageView;
|
import de.hdodenhof.circleimageview.CircleImageView;
|
||||||
|
|
||||||
import static android.app.Activity.RESULT_OK;
|
import static android.app.Activity.RESULT_OK;
|
||||||
import static android.view.View.GONE;
|
import static android.view.View.GONE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static android.widget.Toast.LENGTH_SHORT;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||||
import static org.briarproject.briar.android.view.AuthorView.setAvatar;
|
import static org.briarproject.briar.android.view.AuthorView.setAvatar;
|
||||||
@@ -53,45 +41,21 @@ import static org.briarproject.briar.api.introduction.IntroductionConstants.MAX_
|
|||||||
public class IntroductionMessageFragment extends BaseFragment
|
public class IntroductionMessageFragment extends BaseFragment
|
||||||
implements SendListener {
|
implements SendListener {
|
||||||
|
|
||||||
public static final String TAG =
|
private static final String TAG =
|
||||||
IntroductionMessageFragment.class.getName();
|
IntroductionMessageFragment.class.getName();
|
||||||
private static final Logger LOG = Logger.getLogger(TAG);
|
|
||||||
|
|
||||||
private final static String CONTACT_ID_1 = "contact1";
|
@Inject
|
||||||
private final static String CONTACT_ID_2 = "contact2";
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
|
private IntroductionViewModel viewModel;
|
||||||
|
|
||||||
private IntroductionActivity introductionActivity;
|
|
||||||
private ViewHolder ui;
|
private ViewHolder ui;
|
||||||
private Contact contact1, contact2;
|
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
|
||||||
@Inject
|
|
||||||
protected volatile ContactManager contactManager;
|
|
||||||
@Inject
|
|
||||||
protected volatile AuthorManager authorManager;
|
|
||||||
@Inject
|
|
||||||
protected volatile IntroductionManager introductionManager;
|
|
||||||
|
|
||||||
public static IntroductionMessageFragment newInstance(int contactId1,
|
|
||||||
int contactId2) {
|
|
||||||
Bundle args = new Bundle();
|
|
||||||
args.putInt(CONTACT_ID_1, contactId1);
|
|
||||||
args.putInt(CONTACT_ID_2, contactId2);
|
|
||||||
IntroductionMessageFragment fragment =
|
|
||||||
new IntroductionMessageFragment();
|
|
||||||
fragment.setArguments(args);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAttach(Context context) {
|
|
||||||
super.onAttach(context);
|
|
||||||
introductionActivity = (IntroductionActivity) context;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectFragment(ActivityComponent component) {
|
public void injectFragment(ActivityComponent component) {
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
|
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||||
|
.get(IntroductionViewModel.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -100,18 +64,7 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
|
||||||
// change toolbar text
|
// change toolbar text
|
||||||
ActionBar actionBar = introductionActivity.getSupportActionBar();
|
requireActivity().setTitle(R.string.introduction_message_title);
|
||||||
if (actionBar != null) {
|
|
||||||
actionBar.setTitle(R.string.introduction_message_title);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get contact IDs from fragment arguments
|
|
||||||
Bundle args = requireArguments();
|
|
||||||
int contactId1 = args.getInt(CONTACT_ID_1, -1);
|
|
||||||
int contactId2 = args.getInt(CONTACT_ID_2, -1);
|
|
||||||
if (contactId1 == -1 || contactId2 == -1) {
|
|
||||||
throw new AssertionError("Use newInstance() to instantiate");
|
|
||||||
}
|
|
||||||
|
|
||||||
// inflate view
|
// inflate view
|
||||||
View v = inflater.inflate(R.layout.introduction_message, container,
|
View v = inflater.inflate(R.layout.introduction_message, container,
|
||||||
@@ -123,69 +76,44 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
ui.message.setMaxTextLength(MAX_INTRODUCTION_TEXT_LENGTH);
|
ui.message.setMaxTextLength(MAX_INTRODUCTION_TEXT_LENGTH);
|
||||||
ui.message.setReady(false);
|
ui.message.setReady(false);
|
||||||
|
|
||||||
// get contacts and then show view
|
viewModel.getIntroductionInfo().observe(getViewLifecycleOwner(), ii -> {
|
||||||
prepareToSetUpViews(contactId1, contactId2);
|
if (ii == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setUpViews(ii.getContact1(), ii.getContact2(),
|
||||||
|
ii.isPossible());
|
||||||
|
});
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUniqueTag() {
|
public String getUniqueTag() {
|
||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareToSetUpViews(int contactId1, int contactId2) {
|
|
||||||
introductionActivity.runOnDbThread(() -> {
|
|
||||||
try {
|
|
||||||
Contact contact1 =
|
|
||||||
contactManager.getContact(new ContactId(contactId1));
|
|
||||||
Contact contact2 =
|
|
||||||
contactManager.getContact(new ContactId(contactId2));
|
|
||||||
AuthorInfo a1 = authorManager.getAuthorInfo(contact1);
|
|
||||||
AuthorInfo a2 = authorManager.getAuthorInfo(contact2);
|
|
||||||
boolean possible =
|
|
||||||
introductionManager.canIntroduce(contact1, contact2);
|
|
||||||
ContactItem c1 = new ContactItem(contact1, a1);
|
|
||||||
ContactItem c2 = new ContactItem(contact2, a2);
|
|
||||||
setUpViews(c1, c2, possible);
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setUpViews(ContactItem c1, ContactItem c2, boolean possible) {
|
private void setUpViews(ContactItem c1, ContactItem c2, boolean possible) {
|
||||||
introductionActivity.runOnUiThreadUnlessDestroyed(() -> {
|
// set avatars
|
||||||
contact1 = c1.getContact();
|
setAvatar(ui.avatar1, c1);
|
||||||
contact2 = c2.getContact();
|
setAvatar(ui.avatar2, c2);
|
||||||
|
|
||||||
// set avatars
|
// set contact names
|
||||||
setAvatar(ui.avatar1, c1);
|
ui.contactName1.setText(getContactDisplayName(c1.getContact()));
|
||||||
setAvatar(ui.avatar2, c2);
|
ui.contactName2.setText(getContactDisplayName(c2.getContact()));
|
||||||
|
|
||||||
// set contact names
|
// hide progress bar
|
||||||
ui.contactName1.setText(getContactDisplayName(c1.getContact()));
|
ui.progressBar.setVisibility(GONE);
|
||||||
ui.contactName2.setText(getContactDisplayName(c2.getContact()));
|
|
||||||
|
|
||||||
// hide progress bar
|
if (possible) {
|
||||||
ui.progressBar.setVisibility(GONE);
|
// show views
|
||||||
|
ui.notPossible.setVisibility(GONE);
|
||||||
if (possible) {
|
ui.message.setVisibility(VISIBLE);
|
||||||
// show views
|
ui.message.setReady(true);
|
||||||
ui.notPossible.setVisibility(GONE);
|
ui.message.showSoftKeyboard();
|
||||||
ui.message.setVisibility(VISIBLE);
|
} else {
|
||||||
ui.message.setReady(true);
|
ui.notPossible.setVisibility(VISIBLE);
|
||||||
ui.message.showSoftKeyboard();
|
ui.message.setVisibility(GONE);
|
||||||
} else {
|
}
|
||||||
ui.notPossible.setVisibility(VISIBLE);
|
|
||||||
ui.message.setVisibility(GONE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -193,7 +121,7 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case android.R.id.home:
|
case android.R.id.home:
|
||||||
hideSoftKeyboard(ui.message);
|
hideSoftKeyboard(ui.message);
|
||||||
introductionActivity.onBackPressed();
|
requireActivity().onBackPressed();
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
@@ -206,32 +134,13 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
// disable button to prevent accidental double invitations
|
// disable button to prevent accidental double invitations
|
||||||
ui.message.setReady(false);
|
ui.message.setReady(false);
|
||||||
|
|
||||||
makeIntroduction(contact1, contact2, text);
|
viewModel.makeIntroduction(text);
|
||||||
|
|
||||||
// don't wait for the introduction to be made before finishing activity
|
// don't wait for the introduction to be made before finishing activity
|
||||||
hideSoftKeyboard(ui.message);
|
hideSoftKeyboard(ui.message);
|
||||||
introductionActivity.setResult(RESULT_OK);
|
FragmentActivity activity = requireActivity();
|
||||||
introductionActivity.supportFinishAfterTransition();
|
activity.setResult(RESULT_OK);
|
||||||
}
|
activity.supportFinishAfterTransition();
|
||||||
|
|
||||||
private void makeIntroduction(Contact c1, Contact c2,
|
|
||||||
@Nullable String text) {
|
|
||||||
introductionActivity.runOnDbThread(() -> {
|
|
||||||
// actually make the introduction
|
|
||||||
try {
|
|
||||||
long timestamp = System.currentTimeMillis();
|
|
||||||
introductionManager.makeIntroduction(c1, c2, text, timestamp);
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
introductionError();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void introductionError() {
|
|
||||||
introductionActivity.runOnUiThreadUnlessDestroyed(
|
|
||||||
() -> Toast.makeText(introductionActivity,
|
|
||||||
R.string.introduction_error, LENGTH_SHORT).show());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ViewHolder {
|
private static class ViewHolder {
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.briarproject.briar.android.introduction;
|
||||||
|
|
||||||
|
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel;
|
||||||
|
import dagger.Binds;
|
||||||
|
import dagger.Module;
|
||||||
|
import dagger.multibindings.IntoMap;
|
||||||
|
|
||||||
|
@Module
|
||||||
|
public abstract class IntroductionModule {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@IntoMap
|
||||||
|
@ViewModelKey(IntroductionViewModel.class)
|
||||||
|
abstract ViewModel bindIntroductionViewModel(
|
||||||
|
IntroductionViewModel introductionViewModel);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
package org.briarproject.briar.android.introduction;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.db.TransactionManager;
|
||||||
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.contact.ContactItem;
|
||||||
|
import org.briarproject.briar.android.contact.ContactsViewModel;
|
||||||
|
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||||
|
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||||
|
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorManager;
|
||||||
|
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
|
import static android.widget.Toast.LENGTH_SHORT;
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
class IntroductionViewModel extends ContactsViewModel {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(IntroductionViewModel.class.getName());
|
||||||
|
|
||||||
|
private final ContactManager contactManager;
|
||||||
|
private final AuthorManager authorManager;
|
||||||
|
private final IntroductionManager introductionManager;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
IntroductionViewModel(Application application,
|
||||||
|
@DatabaseExecutor Executor dbExecutor,
|
||||||
|
LifecycleManager lifecycleManager, TransactionManager db,
|
||||||
|
AndroidExecutor androidExecutor, ContactManager contactManager,
|
||||||
|
AuthorManager authorManager,
|
||||||
|
ConversationManager conversationManager,
|
||||||
|
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
||||||
|
IntroductionManager introductionManager) {
|
||||||
|
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||||
|
contactManager, authorManager, conversationManager,
|
||||||
|
connectionRegistry, eventBus);
|
||||||
|
this.contactManager = contactManager;
|
||||||
|
this.authorManager = authorManager;
|
||||||
|
this.introductionManager = introductionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is the contact from whose conversation we started the introduction
|
||||||
|
* using the menu item.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private ContactId firstContactId;
|
||||||
|
/*
|
||||||
|
* This is the contact we selected from the list of contacts as a second
|
||||||
|
* contact for the introduction.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private ContactId secondContactId;
|
||||||
|
|
||||||
|
private final MutableLiveEvent<Boolean> secondContactSelected =
|
||||||
|
new MutableLiveEvent<>();
|
||||||
|
|
||||||
|
private final MutableLiveData<IntroductionInfo> introductionInfo =
|
||||||
|
new MutableLiveData<>();
|
||||||
|
|
||||||
|
void setFirstContactId(ContactId contactId) {
|
||||||
|
this.firstContactId = contactId;
|
||||||
|
loadContacts();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
ContactId getSecondContactId() {
|
||||||
|
return secondContactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setSecondContactId(ContactId contactId) {
|
||||||
|
secondContactId = contactId;
|
||||||
|
// Setting this to null here so that IntroductionMessageFragment can
|
||||||
|
// tell whether the correct value has been loaded from the database when
|
||||||
|
// selecting a second contact repeatedly.
|
||||||
|
introductionInfo.setValue(null);
|
||||||
|
loadIntroductionInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger the event that the second contact has been selected from the
|
||||||
|
* contact list by the user.
|
||||||
|
*/
|
||||||
|
void triggerContactSelected() {
|
||||||
|
secondContactSelected.setEvent(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event will be triggered once the second contact has been selected
|
||||||
|
* from the list of contacts displayed. It is not fired when the second
|
||||||
|
* contact gets restored from the saved instance state.
|
||||||
|
*/
|
||||||
|
LiveEvent<Boolean> getSecondContactSelected() {
|
||||||
|
return secondContactSelected;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holder for the introduction info object with data about both contacts
|
||||||
|
* and whether the introduction is possible. May wrap null if the data
|
||||||
|
* is not available yet. This happens when it is reset by selecting a
|
||||||
|
* contact with the same view model instance more than once.
|
||||||
|
*/
|
||||||
|
LiveData<IntroductionInfo> getIntroductionInfo() {
|
||||||
|
return introductionInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean displayContact(ContactId contactId) {
|
||||||
|
return !requireNonNull(firstContactId).equals(contactId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadIntroductionInfo() {
|
||||||
|
final ContactId firstContactId = requireNonNull(this.firstContactId);
|
||||||
|
final ContactId secondContactId = requireNonNull(this.secondContactId);
|
||||||
|
runOnDbThread(() -> {
|
||||||
|
try {
|
||||||
|
Contact firstContact =
|
||||||
|
contactManager.getContact(firstContactId);
|
||||||
|
Contact secondContact =
|
||||||
|
contactManager.getContact(secondContactId);
|
||||||
|
AuthorInfo a1 = authorManager.getAuthorInfo(firstContact);
|
||||||
|
AuthorInfo a2 = authorManager.getAuthorInfo(secondContact);
|
||||||
|
boolean possible = introductionManager
|
||||||
|
.canIntroduce(firstContact, secondContact);
|
||||||
|
ContactItem c1 = new ContactItem(firstContact, a1);
|
||||||
|
ContactItem c2 = new ContactItem(secondContact, a2);
|
||||||
|
introductionInfo.postValue(
|
||||||
|
new IntroductionInfo(c1, c2, possible));
|
||||||
|
} catch (DbException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void makeIntroduction(@Nullable String text) {
|
||||||
|
final IntroductionInfo info =
|
||||||
|
requireNonNull(introductionInfo.getValue());
|
||||||
|
runOnDbThread(() -> {
|
||||||
|
// actually make the introduction
|
||||||
|
try {
|
||||||
|
long timestamp = System.currentTimeMillis();
|
||||||
|
introductionManager.makeIntroduction(
|
||||||
|
info.getContact1().getContact(),
|
||||||
|
info.getContact2().getContact(), text, timestamp);
|
||||||
|
} catch (DbException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
androidExecutor.runOnUiThread(() -> Toast.makeText(
|
||||||
|
getApplication(), R.string.introduction_error,
|
||||||
|
LENGTH_SHORT).show());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contactselection.BaseContactSelectorFragment;
|
import org.briarproject.briar.android.contactselection.BaseContactSelectorFragment;
|
||||||
import org.briarproject.briar.android.contactselection.ContactSelectorController;
|
import org.briarproject.briar.android.contactselection.ContactSelectorController;
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import android.view.ViewGroup;
|
|||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contactselection.BaseContactSelectorAdapter;
|
import org.briarproject.briar.android.contactselection.BaseContactSelectorAdapter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import android.widget.ImageView;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contactselection.BaseSelectableContactHolder;
|
import org.briarproject.briar.android.contactselection.BaseSelectableContactHolder;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|||||||
@@ -52,7 +52,8 @@ public class BriarRecyclerView extends FrameLayout {
|
|||||||
R.styleable.BriarRecyclerView);
|
R.styleable.BriarRecyclerView);
|
||||||
isScrollingToEnd = attributes
|
isScrollingToEnd = attributes
|
||||||
.getBoolean(R.styleable.BriarRecyclerView_scrollToEnd, true);
|
.getBoolean(R.styleable.BriarRecyclerView_scrollToEnd, true);
|
||||||
int drawableRes = attributes.getResourceId(R.styleable.BriarRecyclerView_emptyImage, -1);
|
int drawableRes = attributes
|
||||||
|
.getResourceId(R.styleable.BriarRecyclerView_emptyImage, -1);
|
||||||
if (drawableRes != -1) setEmptyImage(drawableRes);
|
if (drawableRes != -1) setEmptyImage(drawableRes);
|
||||||
String emtpyText =
|
String emtpyText =
|
||||||
attributes.getString(R.styleable.BriarRecyclerView_emptyText);
|
attributes.getString(R.styleable.BriarRecyclerView_emptyText);
|
||||||
@@ -87,10 +88,30 @@ public class BriarRecyclerView extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
emptyObserver = new RecyclerView.AdapterDataObserver() {
|
emptyObserver = new RecyclerView.AdapterDataObserver() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged() {
|
||||||
|
super.onChanged();
|
||||||
|
showData();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onItemRangeChanged(int positionStart, int itemCount) {
|
||||||
|
super.onItemRangeChanged(positionStart, itemCount);
|
||||||
|
if (itemCount > 0) showData();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onItemRangeMoved(int fromPosition, int toPosition,
|
||||||
|
int itemCount) {
|
||||||
|
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
|
||||||
|
if (itemCount > 0) showData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onItemRangeInserted(int positionStart, int itemCount) {
|
public void onItemRangeInserted(int positionStart, int itemCount) {
|
||||||
super.onItemRangeInserted(positionStart, itemCount);
|
super.onItemRangeInserted(positionStart, itemCount);
|
||||||
if (itemCount > 0) showData();
|
showData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public abstract class DbViewModel extends AndroidViewModel {
|
|||||||
private final Executor dbExecutor;
|
private final Executor dbExecutor;
|
||||||
private final LifecycleManager lifecycleManager;
|
private final LifecycleManager lifecycleManager;
|
||||||
private final TransactionManager db;
|
private final TransactionManager db;
|
||||||
private final AndroidExecutor androidExecutor;
|
protected final AndroidExecutor androidExecutor;
|
||||||
|
|
||||||
public DbViewModel(
|
public DbViewModel(
|
||||||
@NonNull Application application,
|
@NonNull Application application,
|
||||||
|
|||||||
Reference in New Issue
Block a user