Do radical search work on the background
app/src/main/java/eu/lepiller/nani/OnOtherTaskCompleted.java unknown status 1
1 | + | package eu.lepiller.nani; | |
2 | + | ||
3 | + | public interface OnOtherTaskCompleted<Result> { | |
4 | + | void onOtherTaskCompleted(Result result); | |
5 | + | } |
app/src/main/java/eu/lepiller/nani/RadicalSelectorView.java
1 | 1 | package eu.lepiller.nani; | |
2 | 2 | ||
3 | 3 | import android.content.Context; | |
4 | + | import android.os.AsyncTask; | |
4 | 5 | import android.util.AttributeSet; | |
5 | 6 | import android.util.Log; | |
7 | + | import android.util.Pair; | |
6 | 8 | import android.view.LayoutInflater; | |
7 | 9 | import android.view.View; | |
8 | 10 | import android.widget.Button; | |
… | |||
13 | 15 | import android.widget.ToggleButton; | |
14 | 16 | ||
15 | 17 | import java.util.ArrayList; | |
18 | + | import java.util.HashSet; | |
16 | 19 | import java.util.List; | |
17 | 20 | import java.util.Map; | |
18 | 21 | import java.util.Set; | |
… | |||
20 | 23 | import eu.lepiller.nani.dictionary.IncompatibleFormatException; | |
21 | 24 | import eu.lepiller.nani.dictionary.RadicalDict; | |
22 | 25 | ||
23 | - | public class RadicalSelectorView extends LinearLayout { | |
26 | + | public class RadicalSelectorView extends LinearLayout implements OnTaskCompleted<Map<Integer, List<String>>>, | |
27 | + | OnOtherTaskCompleted<Pair<Map<Integer, List<String>>, Set<String>>> { | |
24 | 28 | private Button close_button; | |
25 | 29 | private LinearLayout kanji_row1, kanji_row2; | |
26 | 30 | private TableLayout radical_table; | |
27 | - | private List<String> selected = new ArrayList<>(); | |
31 | + | final List<String> selected = new ArrayList<>(); | |
28 | 32 | View.OnClickListener kanjiSelectionListener; | |
29 | 33 | RadicalDict dictionary; | |
30 | 34 | ||
… | |||
55 | 59 | ||
56 | 60 | public void setDictionary(RadicalDict dict) { | |
57 | 61 | dictionary = dict; | |
62 | + | new DictionaryUpdateTask(this).execute(dict); | |
63 | + | } | |
58 | 64 | ||
59 | - | try { | |
60 | - | Map<Integer, List<String>> radicals = dict.getAllRadicals(); | |
61 | - | for(int stroke: radicals.keySet()) { | |
62 | - | TableRow row = new TableRow(getContext()); | |
63 | - | TextView strokeView = new TextView(getContext()); | |
64 | - | strokeView.setText(Integer.toString(stroke)); | |
65 | - | row.addView(strokeView); | |
66 | - | ||
67 | - | List<String> lst = radicals.get(stroke); | |
68 | - | for(final String radical: lst) { | |
69 | - | ToggleButton radicalButton = new ToggleButton(getContext()); | |
70 | - | radicalButton.setTextOff(radical); | |
71 | - | radicalButton.setTextOn(radical); | |
72 | - | radicalButton.setText(radical); | |
73 | - | ||
74 | - | radicalButton.setOnClickListener(new OnClickListener() { | |
75 | - | @Override | |
76 | - | public void onClick(View v) { | |
77 | - | boolean checked = ((ToggleButton) v).isChecked(); | |
78 | - | if(checked) { | |
79 | - | selected.add(radical); | |
80 | - | } else { | |
81 | - | selected.remove(radical); | |
82 | - | } | |
83 | - | ||
84 | - | updateSelection(); | |
85 | - | updateRadicals(); | |
65 | + | @Override | |
66 | + | public void onTaskCompleted(Map<Integer, List<String>> radicals) { | |
67 | + | if(radicals == null) | |
68 | + | return; | |
69 | + | ||
70 | + | for (int stroke : radicals.keySet()) { | |
71 | + | TableRow row = new TableRow(getContext()); | |
72 | + | TextView strokeView = new TextView(getContext()); | |
73 | + | strokeView.setText(Integer.toString(stroke)); | |
74 | + | row.addView(strokeView); | |
75 | + | ||
76 | + | List<String> lst = radicals.get(stroke); | |
77 | + | for (final String radical : lst) { | |
78 | + | ToggleButton radicalButton = new ToggleButton(getContext()); | |
79 | + | radicalButton.setTextOff(radical); | |
80 | + | radicalButton.setTextOn(radical); | |
81 | + | radicalButton.setText(radical); | |
82 | + | ||
83 | + | radicalButton.setOnClickListener(new OnClickListener() { | |
84 | + | @Override | |
85 | + | public void onClick(View v) { | |
86 | + | boolean checked = ((ToggleButton) v).isChecked(); | |
87 | + | if (checked) { | |
88 | + | selected.add(radical); | |
89 | + | } else { | |
90 | + | selected.remove(radical); | |
86 | 91 | } | |
87 | - | }); | |
88 | - | row.addView(radicalButton); | |
89 | - | } | |
90 | - | radical_table.addView(row); | |
92 | + | ||
93 | + | String[] set = new String[selected.size()]; | |
94 | + | for(int i=0; i<selected.size(); i++) { | |
95 | + | set[i] = selected.get(i); | |
96 | + | } | |
97 | + | new DictionarySearchTask(RadicalSelectorView.this, dictionary).execute(set); | |
98 | + | } | |
99 | + | }); | |
100 | + | row.addView(radicalButton); | |
91 | 101 | } | |
92 | - | } catch(IncompatibleFormatException e) { | |
93 | - | e.printStackTrace(); | |
102 | + | radical_table.addView(row); | |
103 | + | } | |
104 | + | } | |
105 | + | ||
106 | + | private static class DictionaryUpdateTask extends AsyncTask<RadicalDict, Integer, Map<Integer, List<String>>> { | |
107 | + | OnTaskCompleted<Map<Integer, List<String>>> callback; | |
108 | + | ||
109 | + | DictionaryUpdateTask(OnTaskCompleted<Map<Integer, List<String>>> rsv) { | |
110 | + | callback = rsv; | |
111 | + | } | |
112 | + | ||
113 | + | @Override | |
114 | + | protected Map<Integer, List<String>> doInBackground(RadicalDict... sInput) { | |
115 | + | try { | |
116 | + | return sInput[0].getAllRadicals(); | |
117 | + | } catch (IncompatibleFormatException e) { | |
118 | + | e.printStackTrace(); | |
119 | + | return null; | |
120 | + | } | |
121 | + | } | |
122 | + | ||
123 | + | @Override | |
124 | + | protected void onPostExecute(Map<Integer, List<String>> r) { | |
125 | + | callback.onTaskCompleted(r); | |
126 | + | } | |
127 | + | } | |
128 | + | ||
129 | + | @Override | |
130 | + | public void onOtherTaskCompleted(Pair<Map<Integer, List<String>>, Set<String>> result) { | |
131 | + | if(result == null) | |
132 | + | return; | |
133 | + | ||
134 | + | Map<Integer, List<String>> kanji = result.first; | |
135 | + | Set<String> availableRadicals = result.second; | |
136 | + | ||
137 | + | updateSelection(kanji); | |
138 | + | updateRadicals(availableRadicals); | |
139 | + | } | |
140 | + | ||
141 | + | private static class DictionarySearchTask extends AsyncTask<String, Integer, Pair<Map<Integer, List<String>>, Set<String>>> { | |
142 | + | OnOtherTaskCompleted<Pair<Map<Integer, List<String>>, Set<String>>> callback; | |
143 | + | RadicalDict dict; | |
144 | + | ||
145 | + | DictionarySearchTask(OnOtherTaskCompleted<Pair<Map<Integer, List<String>>, Set<String>>> rsv, | |
146 | + | RadicalDict dictionary) { | |
147 | + | callback = rsv; | |
148 | + | dict = dictionary; | |
149 | + | } | |
150 | + | ||
151 | + | @Override | |
152 | + | protected Pair<Map<Integer, List<String>>, Set<String>> doInBackground(String... sInput) { | |
153 | + | try { | |
154 | + | List<String> radicals = new ArrayList<>(); | |
155 | + | for(String s: sInput) radicals.add(s); | |
156 | + | return new Pair<>(dict.match(radicals), dict.availableRadicals(radicals)); | |
157 | + | } catch (IncompatibleFormatException e) { | |
158 | + | e.printStackTrace(); | |
159 | + | return null; | |
160 | + | } | |
161 | + | } | |
162 | + | ||
163 | + | @Override | |
164 | + | protected void onPostExecute(Pair<Map<Integer, List<String>>, Set<String>> r) { | |
165 | + | callback.onOtherTaskCompleted(r); | |
94 | 166 | } | |
95 | 167 | } | |
96 | 168 | ||
… | |||
102 | 174 | kanjiSelectionListener = listener; | |
103 | 175 | } | |
104 | 176 | ||
105 | - | public void updateSelection() { | |
106 | - | try { | |
107 | - | Map<Integer, List<String>> kanji = dictionary.match(selected); | |
108 | - | LinearLayout row = kanji_row1; | |
109 | - | kanji_row1.removeAllViews(); | |
110 | - | kanji_row2.removeAllViews(); | |
177 | + | public void updateSelection(Map<Integer, List<String>> kanji) { | |
178 | + | LinearLayout row = kanji_row1; | |
179 | + | kanji_row1.removeAllViews(); | |
180 | + | kanji_row2.removeAllViews(); | |
111 | 181 | ||
112 | - | if(kanji == null) | |
113 | - | return; | |
182 | + | if(kanji == null) | |
183 | + | return; | |
114 | 184 | ||
115 | - | int total = 0; | |
116 | - | int number = 0; | |
117 | - | for (int stroke : kanji.keySet()) { | |
118 | - | total += kanji.get(stroke).size(); | |
119 | - | } | |
120 | - | for (int stroke : kanji.keySet()) { | |
121 | - | TextView strokeView = new TextView(getContext()); | |
122 | - | strokeView.setText(Integer.toString(stroke)); | |
123 | - | row.addView(strokeView); | |
124 | - | ||
125 | - | for (String k : kanji.get(stroke)) { | |
126 | - | number++; | |
127 | - | Button kanji_button = new Button(getContext()); | |
128 | - | kanji_button.setText(k); | |
129 | - | kanji_button.setOnClickListener(kanjiSelectionListener); | |
130 | - | row.addView(kanji_button); | |
131 | - | ||
132 | - | if (number > total / 2) | |
133 | - | row = kanji_row2; | |
134 | - | } | |
185 | + | int total = 0; | |
186 | + | int number = 0; | |
187 | + | for (int stroke : kanji.keySet()) { | |
188 | + | total += kanji.get(stroke).size(); | |
189 | + | } | |
190 | + | for (int stroke : kanji.keySet()) { | |
191 | + | TextView strokeView = new TextView(getContext()); | |
192 | + | strokeView.setText(Integer.toString(stroke)); | |
193 | + | row.addView(strokeView); | |
194 | + | ||
195 | + | for (String k : kanji.get(stroke)) { | |
196 | + | number++; | |
197 | + | Button kanji_button = new Button(getContext()); | |
198 | + | kanji_button.setText(k); | |
199 | + | kanji_button.setOnClickListener(kanjiSelectionListener); | |
200 | + | row.addView(kanji_button); | |
201 | + | ||
202 | + | if (number > total / 2) | |
203 | + | row = kanji_row2; | |
135 | 204 | } | |
136 | - | } catch (IncompatibleFormatException e) { | |
137 | - | e.printStackTrace(); | |
138 | 205 | } | |
139 | 206 | } | |
140 | 207 | ||
141 | - | private void updateRadicals() { | |
142 | - | try { | |
143 | - | Set<String> available = dictionary.availableRadicals(selected); | |
144 | - | Log.v("SELECTOR", Integer.toString(radical_table.getChildCount())); | |
145 | - | for(int row = 0; row < radical_table.getChildCount(); row++) { | |
146 | - | TableRow r = (TableRow) radical_table.getChildAt(row); | |
147 | - | for(int col = 0; col < r.getChildCount(); col++) { | |
148 | - | View child = r.getChildAt(col); | |
149 | - | if(child instanceof ToggleButton) { | |
150 | - | child.setEnabled(available == null || available.contains(((ToggleButton) child).getText().toString())); | |
151 | - | } | |
208 | + | private void updateRadicals(Set<String> available) { | |
209 | + | Log.v("SELECTOR", Integer.toString(radical_table.getChildCount())); | |
210 | + | for(int row = 0; row < radical_table.getChildCount(); row++) { | |
211 | + | TableRow r = (TableRow) radical_table.getChildAt(row); | |
212 | + | for(int col = 0; col < r.getChildCount(); col++) { | |
213 | + | View child = r.getChildAt(col); | |
214 | + | if(child instanceof ToggleButton) { | |
215 | + | child.setEnabled(available == null || available.contains(((ToggleButton) child).getText().toString())); | |
152 | 216 | } | |
153 | 217 | } | |
154 | - | } catch(IncompatibleFormatException e) { | |
155 | - | e.printStackTrace(); | |
156 | 218 | } | |
157 | 219 | } | |
158 | 220 | } |