mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Added new contact item to contact spinner, fixed spinner selection bug.
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
<string name="from">From:</string>
|
||||
<string name="to">To:</string>
|
||||
<string name="anonymous">(Anonymous)</string>
|
||||
<string name="new_contact_item">New contact\u2026</string>
|
||||
<string name="groups_title">Groups</string>
|
||||
<string name="no_posts">(No posts)</string>
|
||||
<string name="create_group_title">New Group</string>
|
||||
|
||||
@@ -24,7 +24,7 @@ import net.sf.briar.R;
|
||||
import net.sf.briar.android.BriarFragmentActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.android.SelectContactsDialog;
|
||||
import net.sf.briar.android.contact.SelectContactsDialog;
|
||||
import net.sf.briar.android.invitation.AddContactActivity;
|
||||
import net.sf.briar.android.messages.NoContactsDialog;
|
||||
import net.sf.briar.api.Contact;
|
||||
|
||||
@@ -34,7 +34,7 @@ class LocalGroupSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return list.size() + 1;
|
||||
return list.isEmpty() ? 0 : list.size() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,7 +67,7 @@ class LocalGroupSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return getCount() == 0;
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public void sort(Comparator<LocalGroupItem> comparator) {
|
||||
|
||||
@@ -99,7 +99,7 @@ implements OnItemSelectedListener, OnClickListener {
|
||||
|
||||
TextView from = new TextView(this);
|
||||
from.setTextSize(18);
|
||||
from.setPadding(10, 10, 10, 10);
|
||||
from.setPadding(10, 10, 0, 10);
|
||||
from.setText(R.string.from);
|
||||
header.addView(from);
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.sf.briar.android.contact;
|
||||
|
||||
import net.sf.briar.api.Contact;
|
||||
|
||||
public class ContactItem {
|
||||
|
||||
public static final ContactItem NEW = new ContactItem(null);
|
||||
|
||||
private final Contact contact;
|
||||
|
||||
public ContactItem(Contact contact) {
|
||||
this.contact = contact;
|
||||
}
|
||||
|
||||
public Contact getContact() {
|
||||
return contact;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.sf.briar.android.contact;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class ContactNameComparator implements Comparator<ContactItem> {
|
||||
|
||||
public static final ContactNameComparator INSTANCE =
|
||||
new ContactNameComparator();
|
||||
|
||||
public int compare(ContactItem a, ContactItem b) {
|
||||
if(a == b) return 0;
|
||||
if(a == ContactItem.NEW) return 1;
|
||||
if(b == ContactItem.NEW) return -1;
|
||||
String aName = a.getContact().getAuthor().getName();
|
||||
String bName = b.getContact().getAuthor().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package net.sf.briar.android.contact;
|
||||
|
||||
import static net.sf.briar.android.contact.ContactItem.NEW;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.SpinnerAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class ContactSpinnerAdapter extends BaseAdapter
|
||||
implements SpinnerAdapter {
|
||||
|
||||
private final Context ctx;
|
||||
private final List<ContactItem> list = new ArrayList<ContactItem>();
|
||||
|
||||
public ContactSpinnerAdapter(Context ctx) {
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
public void add(ContactItem item) {
|
||||
list.add(item);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return list.isEmpty() ? 0 : list.size() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getDropDownView(int position, View convertView,
|
||||
ViewGroup parent) {
|
||||
return getView(position, convertView, parent);
|
||||
}
|
||||
|
||||
public ContactItem getItem(int position) {
|
||||
if(position == list.size()) return NEW;
|
||||
return list.get(position);
|
||||
}
|
||||
|
||||
public long getItemId(int position) {
|
||||
return android.R.layout.simple_spinner_item;
|
||||
}
|
||||
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
TextView name = new TextView(ctx);
|
||||
name.setTextSize(18);
|
||||
name.setMaxLines(1);
|
||||
Resources res = ctx.getResources();
|
||||
int pad = res.getInteger(R.integer.spinner_padding);
|
||||
name.setPadding(pad, pad, pad, pad);
|
||||
ContactItem item = getItem(position);
|
||||
if(item == NEW) name.setText(R.string.new_contact_item);
|
||||
else name.setText(item.getContact().getAuthor().getName());
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public void sort(Comparator<ContactItem> comparator) {
|
||||
Collections.sort(list, comparator);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.sf.briar.android;
|
||||
package net.sf.briar.android.contact;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
@@ -23,7 +23,7 @@ import net.sf.briar.R;
|
||||
import net.sf.briar.android.BriarFragmentActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.android.SelectContactsDialog;
|
||||
import net.sf.briar.android.contact.SelectContactsDialog;
|
||||
import net.sf.briar.android.invitation.AddContactActivity;
|
||||
import net.sf.briar.android.messages.NoContactsDialog;
|
||||
import net.sf.briar.api.Contact;
|
||||
|
||||
@@ -34,7 +34,7 @@ class GroupSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return list.size() + 1;
|
||||
return list.isEmpty() ? 0 : list.size() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,7 +67,7 @@ class GroupSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return getCount() == 0;
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public void sort(Comparator<GroupItem> comparator) {
|
||||
|
||||
@@ -102,7 +102,7 @@ implements OnItemSelectedListener, OnClickListener {
|
||||
|
||||
TextView from = new TextView(this);
|
||||
from.setTextSize(18);
|
||||
from.setPadding(10, 10, 10, 10);
|
||||
from.setPadding(10, 10, 0, 10);
|
||||
from.setText(R.string.from);
|
||||
header.addView(from);
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ implements SpinnerAdapter {
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
if(list.isEmpty()) return 0;
|
||||
return includeAnonymous ? list.size() + 2 : list.size() + 1;
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ implements SpinnerAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return getCount() == 0;
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public void sort(Comparator<LocalAuthorItem> comparator) {
|
||||
|
||||
@@ -214,6 +214,7 @@ implements InvitationListener {
|
||||
final Collection<LocalAuthor> localAuthors) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if(localAuthors.isEmpty()) throw new IllegalStateException();
|
||||
adapter.clear();
|
||||
for(LocalAuthor a : localAuthors)
|
||||
adapter.add(new LocalAuthorItem(a));
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package net.sf.briar.android.messages;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import net.sf.briar.api.Contact;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.SpinnerAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class ContactSpinnerAdapter extends ArrayAdapter<Contact>
|
||||
implements SpinnerAdapter {
|
||||
|
||||
ContactSpinnerAdapter(Context context) {
|
||||
super(context, android.R.layout.simple_spinner_item,
|
||||
new ArrayList<Contact>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
TextView name = new TextView(getContext());
|
||||
name.setTextSize(18);
|
||||
name.setMaxLines(1);
|
||||
Resources res = getContext().getResources();
|
||||
int pad = res.getInteger(R.integer.spinner_padding);
|
||||
name.setPadding(pad, pad, pad, pad);
|
||||
name.setText(getItem(position).getAuthor().getName());
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getDropDownView(int position, View convertView,
|
||||
ViewGroup parent) {
|
||||
return getView(position, convertView, parent);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,10 @@ import net.sf.briar.R;
|
||||
import net.sf.briar.android.BriarActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.android.contact.ContactItem;
|
||||
import net.sf.briar.android.contact.ContactNameComparator;
|
||||
import net.sf.briar.android.contact.ContactSpinnerAdapter;
|
||||
import net.sf.briar.android.invitation.AddContactActivity;
|
||||
import net.sf.briar.android.widgets.HorizontalSpace;
|
||||
import net.sf.briar.api.AuthorId;
|
||||
import net.sf.briar.api.Contact;
|
||||
@@ -174,12 +178,19 @@ implements OnItemSelectedListener, OnClickListener {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if(contacts.isEmpty()) finish();
|
||||
int index = -1;
|
||||
for(Contact c : contacts) {
|
||||
if(c.getId().equals(contactId)) index = adapter.getCount();
|
||||
adapter.add(c);
|
||||
adapter.clear();
|
||||
for(Contact c : contacts) adapter.add(new ContactItem(c));
|
||||
adapter.sort(ContactNameComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
int count = adapter.getCount();
|
||||
for(int i = 0; i < count; i++) {
|
||||
ContactItem item = adapter.getItem(i);
|
||||
if(item == ContactItem.NEW) continue;
|
||||
if(item.getContact().getId().equals(contactId)) {
|
||||
spinner.setSelection(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(index != -1) spinner.setSelection(index);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -199,9 +210,14 @@ implements OnItemSelectedListener, OnClickListener {
|
||||
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
Contact c = adapter.getItem(position);
|
||||
loadLocalAuthor(c.getLocalAuthorId());
|
||||
contactId = c.getId();
|
||||
ContactItem item = adapter.getItem(position);
|
||||
if(item == ContactItem.NEW) {
|
||||
startActivity(new Intent(this, AddContactActivity.class));
|
||||
} else {
|
||||
Contact c = item.getContact();
|
||||
loadLocalAuthor(c.getLocalAuthorId());
|
||||
contactId = c.getId();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadLocalAuthor(final AuthorId a) {
|
||||
|
||||
Reference in New Issue
Block a user