@@ -375,11 +375,11 @@ private void api16Invalidate() {
375375
376376 @ Override
377377 public void invalidate () {
378+ //Need to force the TextView private mEditor variable to reset as well on API 16 and up
378379 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
379380 api16Invalidate ();
380381 }
381382
382- //Need to force the TextView private mEditor variable to reset as well on API 16 and up
383383 super .invalidate ();
384384 }
385385
@@ -788,13 +788,18 @@ public void run() {
788788 if (text == null ) return ;
789789
790790 // If the object is currently hidden, remove it
791+ ArrayList <TokenImageSpan > toRemove = new ArrayList <>();
791792 for (TokenImageSpan span : hiddenSpans ) {
792793 if (span .getToken ().equals (object )) {
793- hiddenSpans .remove (span );
794- // Remove it from the state and fire the callback
795- spanWatcher .onSpanRemoved (text , span , 0 , 0 );
794+ toRemove .add (span );
796795 }
797796 }
797+ for (TokenImageSpan span : toRemove ) {
798+ hiddenSpans .remove (span );
799+ // Remove it from the state and fire the callback
800+ spanWatcher .onSpanRemoved (text , span , 0 , 0 );
801+ }
802+
798803 updateCountSpan ();
799804
800805 // If the object is currently visible, remove it
@@ -845,6 +850,10 @@ private void removeSpan(TokenImageSpan span) {
845850
846851 //Add 1 to the end because we put a " " at the end of the spans when adding them
847852 text .delete (text .getSpanStart (span ), text .getSpanEnd (span ) + 1 );
853+
854+ if (allowCollapse && !isFocused ()) {
855+ updateCountSpan ();
856+ }
848857 }
849858
850859 /**
@@ -875,15 +884,19 @@ private void insertSpan(Object object, CharSequence sourceText) {
875884
876885 // If we're not focused: collapse the view if necessary
877886 if (!isFocused () && allowCollapse ) performCollapse (false );
887+
888+ //In some cases, particularly the 1 to nth objects when not focused and restoring
889+ //onSpanAdded doesn't get called
890+ if (!objects .contains (object )) {
891+ spanWatcher .onSpanAdded (editable , tokenSpan , 0 , 0 );
892+ }
878893 } else {
879894 hiddenSpans .add (tokenSpan );
880- updateCountSpan ();
881- }
882- //In some cases, particularly the 1 to nth objects when not focused and restoring
883- //onSpanAdded doesn't get called
884- if (!objects .contains (object )) {
895+ //Need to manually call onSpanAdded here as we're not putting the span on the text
885896 spanWatcher .onSpanAdded (editable , tokenSpan , 0 , 0 );
897+ updateCountSpan ();
886898 }
899+
887900 }
888901
889902 private void insertSpan (Object object ) {
@@ -1060,8 +1073,6 @@ public void setCount(int c) {
10601073 text = "+" + count ;
10611074 ((TextView )view ).setText (text );
10621075 }
1063-
1064- public int getCount () { return count ; }
10651076 }
10661077
10671078 protected class TokenImageSpan extends ViewSpan {
@@ -1116,44 +1127,12 @@ public static interface TokenListener {
11161127 }
11171128
11181129 private class TokenSpanWatcher implements SpanWatcher {
1119- private void updateCountSpan (final int change ) {
1120- final Editable text = getText ();
1121- if (text == null || lastLayout == null ) return ;
1122-
1123- CountSpan [] counts = text .getSpans (0 , text .length (), CountSpan .class );
1124- if (counts .length == 1 ) {
1125- final CountSpan span = counts [0 ];
1126- post (new Runnable () {
1127- @ Override
1128- public void run () {
1129- int spanStart = text .getSpanStart (span );
1130- int spanEnd = text .getSpanEnd (span );
1131- span .setCount (span .getCount () + change );
1132- if (span .getCount () > 0 ) {
1133- text .replace (spanStart , spanEnd , span .text );
1134- } else {
1135- text .delete (spanStart , spanEnd );
1136- text .removeSpan (span );
1137- }
1138- }
1139- });
1140-
1141- }
1142- }
11431130
11441131 @ Override
11451132 public void onSpanAdded (Spannable text , Object what , int start , int end ) {
11461133 if (what instanceof TokenImageSpan && !savingState && !focusChanging ) {
11471134 TokenImageSpan token = (TokenImageSpan )what ;
11481135 objects .add (token .getToken ());
1149- /** wdullaer: The behaviour of calling your updateCountSpan(int) here is very similar
1150- * to my updateCountSpan in insertSpan and removeObject. However because I'm handling
1151- * the appearance of the count span differently now: it inspects the state of the hiddenSpans
1152- * so the count is always correct. It also plays nicer when adding objects through addObject
1153- *
1154- * It is up to you to chose how you want it to work though.
1155- */
1156- //updateCountSpan(1);
11571136
11581137 if (listener != null )
11591138 listener .onTokenAdded (token .getToken ());
@@ -1166,7 +1145,6 @@ public void onSpanRemoved(Spannable text, Object what, int start, int end) {
11661145 TokenImageSpan token = (TokenImageSpan )what ;
11671146 if (objects .contains (token .getToken ())) {
11681147 objects .remove (token .getToken ());
1169- //updateCountSpan(-1);
11701148 }
11711149
11721150 if (listener != null )
0 commit comments