Chip measure

This commit is contained in:
M66B 2022-02-21 20:16:43 +01:00
parent 09922a3ec2
commit 6099e26624
2 changed files with 90 additions and 75 deletions

View File

@ -79,8 +79,91 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
ContextThemeWrapper ctx = new ContextThemeWrapper(context, ContextThemeWrapper ctx = new ContextThemeWrapper(context,
dark ? R.style.Base_Theme_Material3_Dark : R.style.Base_Theme_Material3_Light); dark ? R.style.Base_Theme_Material3_Dark : R.style.Base_Theme_Material3_Light);
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
int dp3 = Helper.dp2pixels(context, 3);
DisplayMetrics dm = getResources().getDisplayMetrics(); Runnable update = new Runnable() {
@Override
public void run() {
try {
Editable edit = getText();
boolean added = false;
List<ClipImageSpan> spans = new ArrayList<>();
spans.addAll(Arrays.asList(edit.getSpans(0, edit.length(), ClipImageSpan.class)));
boolean quote = false;
int start = 0;
for (int i = 0; i < edit.length(); i++) {
char kar = edit.charAt(i);
if (kar == '"')
quote = !quote;
else if (kar == ',' && !quote) {
boolean found = false;
for (ClipImageSpan span : new ArrayList<>(spans)) {
int s = edit.getSpanStart(span);
int e = edit.getSpanEnd(span);
if (s == start && e == i + 1) {
found = true;
spans.remove(span);
break;
}
}
if (!found && start < i + 1) {
String email = edit.subSequence(start, i + 1).toString();
InternetAddress[] parsed;
try {
parsed = MessageHelper.parseAddresses(context, email);
if (parsed != null)
for (InternetAddress a : parsed)
a.validate();
} catch (AddressException ex) {
parsed = null;
}
if (parsed != null && parsed.length == 1) {
Drawable avatar = null;
Uri lookupUri = ContactInfo.getLookupUri(parsed);
if (lookupUri != null) {
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
resolver, lookupUri, false);
avatar = Drawable.createFromStream(is, email);
}
String e = parsed[0].getAddress();
String p = parsed[0].getPersonal();
String text = (TextUtils.isEmpty(p) ? e : p);
// https://github.com/material-components/material-components-android/blob/master/docs/components/Chip.md
ChipDrawable cd = ChipDrawable.createFromResource(ctx, R.xml.chip);
cd.setChipIcon(avatar);
// cd.setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
cd.setText(text);
cd.setMaxWidth(getWidth());
cd.setBounds(0, 0, cd.getIntrinsicWidth(), cd.getIntrinsicHeight());
ClipImageSpan is = new ClipImageSpan(cd);
edit.setSpan(is, start, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
added = true;
}
}
if (i + 1 < edit.length() && edit.charAt(i + 1) == ' ')
start = i + 2;
else
start = i + 1;
}
}
for (ClipImageSpan span : spans)
edit.removeSpan(span);
if (spans.size() > 0 || added)
invalidate();
} catch (Throwable ex) {
Log.e(ex);
}
}
};
addTextChangedListener(new TextWatcher() { addTextChangedListener(new TextWatcher() {
@Override @Override
@ -95,79 +178,10 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
@Override @Override
public void afterTextChanged(Editable edit) { public void afterTextChanged(Editable edit) {
boolean added = false; if (getWidth() == 0)
List<ClipImageSpan> spans = new ArrayList<>(); post(update);
spans.addAll(Arrays.asList(edit.getSpans(0, edit.length(), ClipImageSpan.class))); else
update.run();
boolean quote = false;
int start = 0;
for (int i = 0; i < edit.length(); i++) {
char kar = edit.charAt(i);
if (kar == '"')
quote = !quote;
else if (kar == ',' && !quote) {
boolean found = false;
for (ClipImageSpan span : new ArrayList<>(spans)) {
int s = edit.getSpanStart(span);
int e = edit.getSpanEnd(span);
if (s == start && e == i + 1) {
found = true;
spans.remove(span);
break;
}
}
if (!found) {
String email = edit.subSequence(start, i + 1).toString();
InternetAddress[] parsed;
try {
parsed = MessageHelper.parseAddresses(context, email);
if (parsed != null)
for (InternetAddress a : parsed)
a.validate();
} catch (AddressException ex) {
parsed = null;
}
if (parsed != null && parsed.length == 1) {
Drawable avatar = null;
Uri lookupUri = ContactInfo.getLookupUri(parsed);
if (lookupUri != null) {
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
resolver, lookupUri, false);
avatar = Drawable.createFromStream(is, email);
}
String e = parsed[0].getAddress();
String p = parsed[0].getPersonal();
String text = (TextUtils.isEmpty(p) ? e : p);
// https://github.com/material-components/material-components-android/blob/master/docs/components/Chip.md
ChipDrawable cd = ChipDrawable.createFromResource(ctx, R.xml.chip);
cd.setChipIcon(avatar);
// cd.setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
cd.setText(text);
cd.setMaxWidth(2 * dm.widthPixels / 3);
cd.setBounds(0, 0, cd.getIntrinsicWidth(), cd.getIntrinsicHeight());
ClipImageSpan is = new ClipImageSpan(cd);
edit.setSpan(is, start, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
added = true;
}
}
if (i + 1 < edit.length() && edit.charAt(i + 1) == ' ')
start = i + 2;
else
start = i + 1;
}
}
for (ClipImageSpan span : spans)
edit.removeSpan(span);
if (spans.size() > 0 || added)
invalidate();
} }
}); });
} }

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<chip xmlns:android="http://schemas.android.com/apk/res/android" <chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:ellipsize="middle"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:closeIconVisible="false" app:closeIconVisible="false"
app:textEndPadding="3dp" app:textEndPadding="3dp"