mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
[android] Show existing alias in alias edit text view
This commit also uses LiveData Transformations to expose contact related information
This commit is contained in:
@@ -11,8 +11,9 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.EditText;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
|
|||||||
|
|
||||||
private ConversationViewModel viewModel;
|
private ConversationViewModel viewModel;
|
||||||
private ContactId contactId;
|
private ContactId contactId;
|
||||||
private TextView aliasEditText;
|
private EditText aliasEditText;
|
||||||
|
|
||||||
public static AliasDialogFragment newInstance(ContactId id) {
|
public static AliasDialogFragment newInstance(ContactId id) {
|
||||||
AliasDialogFragment f = new AliasDialogFragment();
|
AliasDialogFragment f = new AliasDialogFragment();
|
||||||
@@ -59,6 +60,10 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
|
|||||||
false);
|
false);
|
||||||
|
|
||||||
aliasEditText = v.findViewById(R.id.aliasEditText);
|
aliasEditText = v.findViewById(R.id.aliasEditText);
|
||||||
|
Contact contact = viewModel.getContact().getValue();
|
||||||
|
String alias = contact == null ? null : contact.getAlias();
|
||||||
|
aliasEditText.setText(alias);
|
||||||
|
if (alias != null) aliasEditText.setSelection(alias.length());
|
||||||
|
|
||||||
Button setButton = v.findViewById(R.id.setButton);
|
Button setButton = v.findViewById(R.id.setButton);
|
||||||
setButton.setOnClickListener(v1 -> {
|
setButton.setOnClickListener(v1 -> {
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
viewModel.isContactDeleted().observe(this, deleted -> {
|
viewModel.isContactDeleted().observe(this, deleted -> {
|
||||||
if (deleted != null && deleted) finish();
|
if (deleted != null && deleted) finish();
|
||||||
});
|
});
|
||||||
viewModel.loadContactDetails(contactId);
|
viewModel.loadContact(contactId);
|
||||||
|
|
||||||
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
||||||
setTransitionName(toolbarStatus, getBulbTransitionName(contactId));
|
setTransitionName(toolbarStatus, getBulbTransitionName(contactId));
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.app.Application;
|
|||||||
import android.arch.lifecycle.AndroidViewModel;
|
import android.arch.lifecycle.AndroidViewModel;
|
||||||
import android.arch.lifecycle.LiveData;
|
import android.arch.lifecycle.LiveData;
|
||||||
import android.arch.lifecycle.MutableLiveData;
|
import android.arch.lifecycle.MutableLiveData;
|
||||||
|
import android.arch.lifecycle.Transformations;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
@@ -38,11 +39,13 @@ public class ConversationViewModel extends AndroidViewModel {
|
|||||||
@Inject
|
@Inject
|
||||||
ContactManager contactManager;
|
ContactManager contactManager;
|
||||||
|
|
||||||
private final MutableLiveData<AuthorId> contactAuthorId =
|
private final MutableLiveData<Contact> contact = new MutableLiveData<>();
|
||||||
new MutableLiveData<>();
|
private final LiveData<AuthorId> contactAuthorId =
|
||||||
|
Transformations.map(contact, c -> c.getAuthor().getId());
|
||||||
|
private final LiveData<String> contactName =
|
||||||
|
Transformations.map(contact, UiUtils::getContactDisplayName);
|
||||||
private final MutableLiveData<Boolean> contactDeleted =
|
private final MutableLiveData<Boolean> contactDeleted =
|
||||||
new MutableLiveData<>();
|
new MutableLiveData<>();
|
||||||
private final MutableLiveData<String> contactName = new MutableLiveData<>();
|
|
||||||
|
|
||||||
public ConversationViewModel(@NonNull Application application) {
|
public ConversationViewModel(@NonNull Application application) {
|
||||||
super(application);
|
super(application);
|
||||||
@@ -52,13 +55,11 @@ public class ConversationViewModel extends AndroidViewModel {
|
|||||||
contactDeleted.setValue(false);
|
contactDeleted.setValue(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadContactDetails(ContactId contactId) {
|
void loadContact(ContactId contactId) {
|
||||||
dbExecutor.execute(() -> {
|
dbExecutor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
long start = now();
|
long start = now();
|
||||||
Contact contact = contactManager.getContact(contactId);
|
contact.postValue(contactManager.getContact(contactId));
|
||||||
contactAuthorId.postValue(contact.getAuthor().getId());
|
|
||||||
contactName.postValue(UiUtils.getContactDisplayName(contact));
|
|
||||||
logDuration(LOG, "Loading contact", start);
|
logDuration(LOG, "Loading contact", start);
|
||||||
} catch (NoSuchContactException e) {
|
} catch (NoSuchContactException e) {
|
||||||
contactDeleted.postValue(true);
|
contactDeleted.postValue(true);
|
||||||
@@ -73,23 +74,27 @@ public class ConversationViewModel extends AndroidViewModel {
|
|||||||
try {
|
try {
|
||||||
contactManager.setContactAlias(contactId,
|
contactManager.setContactAlias(contactId,
|
||||||
alias.isEmpty() ? null : alias);
|
alias.isEmpty() ? null : alias);
|
||||||
loadContactDetails(contactId);
|
loadContact(contactId);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<AuthorId> getContactAuthorId() {
|
LiveData<Contact> getContact() {
|
||||||
return contactAuthorId;
|
return contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<Boolean> isContactDeleted() {
|
LiveData<AuthorId> getContactAuthorId() {
|
||||||
return contactDeleted;
|
return contactAuthorId;
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<String> getContactDisplayName() {
|
LiveData<String> getContactDisplayName() {
|
||||||
return contactName;
|
return contactName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LiveData<Boolean> isContactDeleted() {
|
||||||
|
return contactDeleted;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user