Renumber bullets

pull/184/head
M66B 4 years ago
parent 3ad5b9bedb
commit 3cda3a3784

@ -550,6 +550,7 @@ public class FragmentCompose extends FragmentBase {
etBody.setSelection(added); etBody.setSelection(added);
} }
boolean renum = false;
BulletSpan[] bullets = text.getSpans(added + 1, added + 1, BulletSpan.class); BulletSpan[] bullets = text.getSpans(added + 1, added + 1, BulletSpan.class);
for (BulletSpan span : bullets) { for (BulletSpan span : bullets) {
int s = text.getSpanStart(span); int s = text.getSpanStart(span);
@ -570,8 +571,12 @@ public class FragmentCompose extends FragmentBase {
Log.i("Span " + (added + 1) + "..." + e); Log.i("Span " + (added + 1) + "..." + e);
} }
renum = true;
text.removeSpan(span); text.removeSpan(span);
} }
if (renum)
renumber(text);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally { } finally {
@ -580,6 +585,7 @@ public class FragmentCompose extends FragmentBase {
if (removed != null) if (removed != null)
try { try {
boolean renum = false;
BulletSpan[] spans = text.getSpans(removed - 1, removed - 1, BulletSpan.class); BulletSpan[] spans = text.getSpans(removed - 1, removed - 1, BulletSpan.class);
if (spans.length == 1) { if (spans.length == 1) {
int end = text.getSpanEnd(spans[0]); int end = text.getSpanEnd(spans[0]);
@ -587,10 +593,15 @@ public class FragmentCompose extends FragmentBase {
for (BulletSpan b : bs) { for (BulletSpan b : bs) {
int s = text.getSpanStart(b); int s = text.getSpanStart(b);
int e = text.getSpanEnd(b); int e = text.getSpanEnd(b);
if (s == e) if (s == e) {
renum = true;
text.removeSpan(b); text.removeSpan(b);
} }
} }
}
if (renum)
renumber(text);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally { } finally {
@ -622,6 +633,39 @@ public class FragmentCompose extends FragmentBase {
} else } else
throw new IllegalArgumentException(type.getName()); throw new IllegalArgumentException(type.getName());
} }
public void renumber(Editable text) {
Context context = etBody.getContext();
int dp6 = Helper.dp2pixels(context, 6);
int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
int next;
int index = 1;
int pos = -1;
for (int i = 0; i < text.length(); i = next) {
next = text.nextSpanTransition(i, text.length(), NumberSpan.class);
NumberSpan[] spans = text.getSpans(i, next, NumberSpan.class);
if (spans.length == 1) {
int start = text.getSpanStart(spans[0]);
int end = text.getSpanEnd(spans[0]);
int flags = text.getSpanFlags(spans[0]);
if (start == pos)
index++;
else
index = 1;
if (index != spans[0].getIndex()) {
NumberSpan clone = new NumberSpan(dp6, colorAccent, spans[0].getTextSize(), index);
text.removeSpan(spans[0]);
text.setSpan(clone, start, end, flags);
}
pos = end;
}
}
}
}); });
cbSignature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { cbSignature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

Loading…
Cancel
Save