15
15
import io .flutter .plugin .common .MethodChannel ;
16
16
import io .flutter .plugin .localization .LocalizationPlugin ;
17
17
import java .util .ArrayList ;
18
+ import java .util .HashMap ;
18
19
import java .util .Locale ;
19
20
20
21
/**
@@ -34,6 +35,10 @@ public class SpellCheckPlugin
34
35
private final TextServicesManager mTextServicesManager ;
35
36
private SpellCheckerSession mSpellCheckerSession ;
36
37
38
+ public static final String START_INDEX_KEY = "startIndex" ;
39
+ public static final String END_INDEX_KEY = "endIndex" ;
40
+ public static final String SUGGESTIONS_KEY = "suggestions" ;
41
+
37
42
@ VisibleForTesting MethodChannel .Result pendingResult ;
38
43
39
44
// The maximum number of suggestions that the Android spell check service is allowed to provide
@@ -105,19 +110,28 @@ public void performSpellCheck(@NonNull String locale, @NonNull String text) {
105
110
* Callback for Android spell check API that decomposes results and send results through the
106
111
* {@link SpellCheckChannel}.
107
112
*
108
- * <p>Spell check results will be encoded as a string representing the span of that result, with
109
- * the format "start_index.end_index.suggestion_1/nsuggestion_2/nsuggestion_3", where there may be
110
- * up to 5 suggestions.
113
+ * <p>Spell check results are encoded as dictionaries with a format that looks like
114
+ *
115
+ * <pre>{@code
116
+ * {
117
+ * startIndex: 0,
118
+ * endIndex: 5,
119
+ * suggestions: [hello, ...]
120
+ * }
121
+ * }</pre>
122
+ *
123
+ * where there may be up to 5 suggestions.
111
124
*/
112
125
@ Override
113
126
public void onGetSentenceSuggestions (SentenceSuggestionsInfo [] results ) {
114
127
if (results .length == 0 ) {
115
- pendingResult .success (new ArrayList <String >());
128
+ pendingResult .success (new ArrayList <HashMap < String , Object > >());
116
129
pendingResult = null ;
117
130
return ;
118
131
}
119
132
120
- ArrayList <String > spellCheckerSuggestionSpans = new ArrayList <String >();
133
+ ArrayList <HashMap <String , Object >> spellCheckerSuggestionSpans =
134
+ new ArrayList <HashMap <String , Object >>();
121
135
SentenceSuggestionsInfo spellCheckResults = results [0 ];
122
136
123
137
for (int i = 0 ; i < spellCheckResults .getSuggestionsCount (); i ++) {
@@ -128,19 +142,20 @@ public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
128
142
continue ;
129
143
}
130
144
131
- String spellCheckerSuggestionSpan = "" ;
145
+ HashMap < String , Object > spellCheckerSuggestionSpan = new HashMap < String , Object >() ;
132
146
int start = spellCheckResults .getOffsetAt (i );
133
- int end = start + spellCheckResults .getLengthAt (i ) - 1 ;
147
+ int end = start + spellCheckResults .getLengthAt (i );
134
148
135
- spellCheckerSuggestionSpan += String . valueOf ( start ) + "." ;
136
- spellCheckerSuggestionSpan += String . valueOf ( end ) + "." ;
149
+ spellCheckerSuggestionSpan . put ( START_INDEX_KEY , start );
150
+ spellCheckerSuggestionSpan . put ( END_INDEX_KEY , end );
137
151
152
+ ArrayList <String > suggestions = new ArrayList <String >();
138
153
for (int j = 0 ; j < suggestionsCount ; j ++) {
139
- spellCheckerSuggestionSpan += suggestionsInfo .getSuggestionAt (j ) + " \n " ;
154
+ suggestions . add ( suggestionsInfo .getSuggestionAt (j )) ;
140
155
}
141
156
142
- spellCheckerSuggestionSpans . add (
143
- spellCheckerSuggestionSpan . substring ( 0 , spellCheckerSuggestionSpan . length () - 1 ) );
157
+ spellCheckerSuggestionSpan . put ( SUGGESTIONS_KEY , suggestions );
158
+ spellCheckerSuggestionSpans . add ( spellCheckerSuggestionSpan );
144
159
}
145
160
146
161
pendingResult .success (spellCheckerSuggestionSpans );
0 commit comments