Search in a separate activity
app/build.gradle
26 | 26 | testImplementation 'junit:junit:4.12' | |
27 | 27 | androidTestImplementation 'com.android.support.test:runner:1.0.2' | |
28 | 28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | |
29 | - | compile project(path: ':furiganatextview') | |
29 | + | implementation project(path: ':furiganatextview') | |
30 | 30 | } |
app/src/main/java/eu/lepiller/nani/MainActivity.java
1 | 1 | package eu.lepiller.nani; | |
2 | 2 | ||
3 | + | import android.content.Context; | |
3 | 4 | import android.content.Intent; | |
5 | + | import android.os.AsyncTask; | |
4 | 6 | import android.os.Bundle; | |
5 | 7 | import android.support.design.widget.Snackbar; | |
6 | 8 | import android.support.v7.app.AppCompatActivity; | |
… | |||
22 | 24 | import eu.lepiller.nani.result.Result; | |
23 | 25 | import se.fekete.furiganatextview.furiganaview.FuriganaTextView; | |
24 | 26 | ||
25 | - | public class MainActivity extends AppCompatActivity { | |
27 | + | public class MainActivity extends AppCompatActivity implements OnTaskCompleted<SearchResult> { | |
28 | + | private LinearLayout result_view; | |
29 | + | private Button search_button; | |
26 | 30 | ||
27 | 31 | @Override | |
28 | 32 | protected void onCreate(Bundle savedInstanceState) { | |
… | |||
31 | 35 | Toolbar toolbar = findViewById(R.id.toolbar); | |
32 | 36 | setSupportActionBar(toolbar); | |
33 | 37 | ||
34 | - | final Button search_button = findViewById(R.id.search_button); | |
38 | + | search_button = findViewById(R.id.search_button); | |
39 | + | result_view = findViewById(R.id.results_view); | |
35 | 40 | final EditText search_form = findViewById(R.id.search_form); | |
36 | - | final LinearLayout result_view = findViewById(R.id.results_view); | |
37 | 41 | ||
38 | 42 | search_button.setOnClickListener(new View.OnClickListener() { | |
39 | 43 | @Override | |
40 | 44 | public void onClick(View v) { | |
41 | 45 | String text = search_form.getText().toString(); | |
42 | - | if(text.isEmpty()) { | |
46 | + | if (text.isEmpty()) { | |
43 | 47 | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_search), Snackbar.LENGTH_LONG).show(); | |
44 | 48 | return; | |
45 | 49 | } | |
46 | 50 | ||
47 | - | ArrayList<Result> searchResult = null; | |
48 | - | try { | |
49 | - | searchResult = DictionaryFactory.search(getApplicationContext(), text); | |
50 | - | } catch (NoDictionaryException e) { | |
51 | - | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_dic), | |
52 | - | Snackbar.LENGTH_LONG).show(); | |
53 | - | return; | |
54 | - | } catch (IncompatibleFormatException e) { | |
55 | - | Snackbar.make(findViewById(R.id.search_form), String.format(getString(R.string.incompatible_format), e.getName()), | |
56 | - | Snackbar.LENGTH_LONG).show(); | |
57 | - | return; | |
58 | - | } catch (DictionaryException ignored) { | |
59 | - | return; | |
60 | - | } | |
61 | - | ||
62 | 51 | result_view.removeAllViews(); | |
63 | - | int num = 0; | |
64 | - | for(Result result: searchResult) { | |
65 | - | num++; | |
66 | - | if (num > 10) | |
67 | - | break; | |
68 | - | View child_result = getLayoutInflater().inflate(R.layout.layout_result, result_view, false); | |
69 | - | ||
70 | - | FuriganaTextView kanji_view = child_result.findViewById(R.id.kanji_view); | |
71 | - | LinearLayout senses_view = child_result.findViewById(R.id.sense_view); | |
72 | - | TextView additional_info = child_result.findViewById(R.id.additional_info_view); | |
73 | - | ||
74 | - | // Populate the data into the template view using the data object | |
75 | - | kanji_view.setFuriganaText(result.getKanjiFurigana()); | |
76 | - | ||
77 | - | StringBuilder additional = new StringBuilder(); | |
78 | - | boolean separator = false; | |
79 | - | for(String s: result.getAlternatives()) { | |
80 | - | if(separator) | |
81 | - | additional.append(getResources().getString(R.string.sense_separator)); | |
82 | - | else | |
83 | - | separator = true; | |
84 | - | additional.append(s); | |
85 | - | } | |
86 | - | additional_info.setText(String.format(getResources().getString(R.string.sense_alternatives), additional.toString())); | |
87 | - | ||
88 | - | senses_view.removeAllViews(); | |
89 | - | ||
90 | - | int sense_pos = 1; | |
91 | - | for(Result.Sense sense: result.getSenses()) { | |
92 | - | View child = getLayoutInflater().inflate(R.layout.layout_sense, senses_view, false); | |
93 | - | TextView id_view = child.findViewById(R.id.id_view); | |
94 | - | TextView sense_view = child.findViewById(R.id.definition_view); | |
95 | - | ||
96 | - | id_view.setText(String.format(getResources().getString(R.string.sense_number), sense_pos)); | |
97 | - | ||
98 | - | StringBuilder sb = new StringBuilder(); | |
99 | - | boolean separator1 = false; | |
100 | - | for(String s: sense.getGlosses()) { | |
101 | - | if(separator1) | |
102 | - | sb.append(getResources().getString(R.string.sense_separator)); | |
103 | - | else | |
104 | - | separator1 = true; | |
105 | - | sb.append(s); | |
106 | - | } | |
107 | - | sense_view.setText(sb.toString()); | |
108 | - | ||
109 | - | senses_view.addView(child); | |
110 | - | sense_pos++; | |
111 | - | } | |
112 | - | ||
113 | - | result_view.addView(child_result); | |
114 | - | } | |
52 | + | search_button.setEnabled(false); | |
53 | + | ||
54 | + | DictionaryFactory.prepare(getApplicationContext()); | |
55 | + | new SearchTask(MainActivity.this).execute(text); | |
115 | 56 | } | |
116 | 57 | }); | |
117 | 58 | } | |
118 | 59 | ||
60 | + | private static class SearchTask extends AsyncTask<String, Integer, SearchResult> { | |
61 | + | OnTaskCompleted<SearchResult> callback; | |
62 | + | SearchTask(OnTaskCompleted<SearchResult> callback) { | |
63 | + | this.callback = callback; | |
64 | + | } | |
65 | + | ||
66 | + | @Override | |
67 | + | protected SearchResult doInBackground(String... sInput) { | |
68 | + | String text = sInput[0]; | |
69 | + | ||
70 | + | ArrayList<Result> searchResult; | |
71 | + | try { | |
72 | + | searchResult = DictionaryFactory.search(text); | |
73 | + | } catch (DictionaryException e) { | |
74 | + | return new SearchResult(e); | |
75 | + | } | |
76 | + | ||
77 | + | return new SearchResult(searchResult); | |
78 | + | } | |
79 | + | ||
80 | + | @Override | |
81 | + | protected void onPostExecute(SearchResult r) { | |
82 | + | callback.onTaskCompleted(r); | |
83 | + | } | |
84 | + | } | |
85 | + | ||
86 | + | @Override | |
87 | + | public void onTaskCompleted(SearchResult r) { | |
88 | + | search_button.setEnabled(true); | |
89 | + | if(r.isException()) { | |
90 | + | DictionaryException e = r.getException(); | |
91 | + | if (e instanceof NoDictionaryException) { | |
92 | + | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_dic), | |
93 | + | Snackbar.LENGTH_LONG).show(); | |
94 | + | } else if(e instanceof IncompatibleFormatException) { | |
95 | + | Snackbar.make(findViewById(R.id.search_form), String.format(getString(R.string.incompatible_format), | |
96 | + | ((IncompatibleFormatException) e).getName()), Snackbar.LENGTH_LONG).show(); | |
97 | + | } | |
98 | + | return; | |
99 | + | } | |
100 | + | ||
101 | + | ArrayList<Result> searchResult = r.getResults(); | |
102 | + | showResults(searchResult); | |
103 | + | } | |
104 | + | ||
105 | + | void showResults(ArrayList<Result> searchResult) { | |
106 | + | int num = 0; | |
107 | + | for(Result result: searchResult) { | |
108 | + | num++; | |
109 | + | if (num > 10) | |
110 | + | break; | |
111 | + | View child_result = getLayoutInflater().inflate(R.layout.layout_result, result_view, false); | |
112 | + | ||
113 | + | FuriganaTextView kanji_view = child_result.findViewById(R.id.kanji_view); | |
114 | + | LinearLayout senses_view = child_result.findViewById(R.id.sense_view); | |
115 | + | TextView additional_info = child_result.findViewById(R.id.additional_info_view); | |
116 | + | ||
117 | + | // Populate the data into the template view using the data object | |
118 | + | kanji_view.setFuriganaText(result.getKanjiFurigana()); | |
119 | + | ||
120 | + | StringBuilder additional = new StringBuilder(); | |
121 | + | boolean separator = false; | |
122 | + | for (String s : result.getAlternatives()) { | |
123 | + | if (separator) | |
124 | + | additional.append(getResources().getString(R.string.sense_separator)); | |
125 | + | else | |
126 | + | separator = true; | |
127 | + | additional.append(s); | |
128 | + | } | |
129 | + | additional_info.setText(String.format(getResources().getString(R.string.sense_alternatives), additional.toString())); | |
130 | + | ||
131 | + | senses_view.removeAllViews(); | |
132 | + | ||
133 | + | int sense_pos = 1; | |
134 | + | for (Result.Sense sense : result.getSenses()) { | |
135 | + | View child = getLayoutInflater().inflate(R.layout.layout_sense, senses_view, false); | |
136 | + | TextView id_view = child.findViewById(R.id.id_view); | |
137 | + | TextView sense_view = child.findViewById(R.id.definition_view); | |
138 | + | ||
139 | + | id_view.setText(String.format(getResources().getString(R.string.sense_number), sense_pos)); | |
140 | + | ||
141 | + | StringBuilder sb = new StringBuilder(); | |
142 | + | boolean separator1 = false; | |
143 | + | for (String s : sense.getGlosses()) { | |
144 | + | if (separator1) | |
145 | + | sb.append(getResources().getString(R.string.sense_separator)); | |
146 | + | else | |
147 | + | separator1 = true; | |
148 | + | sb.append(s); | |
149 | + | } | |
150 | + | sense_view.setText(sb.toString()); | |
151 | + | ||
152 | + | senses_view.addView(child); | |
153 | + | sense_pos++; | |
154 | + | } | |
155 | + | ||
156 | + | result_view.addView(child_result); | |
157 | + | } | |
158 | + | } | |
159 | + | ||
119 | 160 | @Override | |
120 | 161 | public boolean onCreateOptionsMenu(Menu menu) { | |
121 | 162 | // Inflate the menu; this adds items to the action bar if it is present. |
app/src/main/java/eu/lepiller/nani/OnTaskCompleted.java unknown status 1
1 | + | package eu.lepiller.nani; | |
2 | + | ||
3 | + | public interface OnTaskCompleted<Result> { | |
4 | + | void onTaskCompleted(Result result); | |
5 | + | } |
app/src/main/java/eu/lepiller/nani/SearchResult.java unknown status 1
1 | + | package eu.lepiller.nani; | |
2 | + | ||
3 | + | import java.util.ArrayList; | |
4 | + | ||
5 | + | import eu.lepiller.nani.dictionary.DictionaryException; | |
6 | + | import eu.lepiller.nani.result.Result; | |
7 | + | ||
8 | + | public class SearchResult { | |
9 | + | private ArrayList<Result> results; | |
10 | + | private DictionaryException exception; | |
11 | + | ||
12 | + | SearchResult(ArrayList<Result> results) { | |
13 | + | this.results = results; | |
14 | + | } | |
15 | + | ||
16 | + | SearchResult(DictionaryException e) { | |
17 | + | exception = e; | |
18 | + | } | |
19 | + | ||
20 | + | boolean isException() { | |
21 | + | return exception != null; | |
22 | + | } | |
23 | + | ||
24 | + | DictionaryException getException() { | |
25 | + | return exception; | |
26 | + | } | |
27 | + | ||
28 | + | ArrayList<Result> getResults() { | |
29 | + | return results; | |
30 | + | } | |
31 | + | } |
app/src/main/java/eu/lepiller/nani/dictionary/DictionaryFactory.java
60 | 60 | "https://nani.lepiller.eu/dicos/JMdict_swe.nani")); | |
61 | 61 | } | |
62 | 62 | ||
63 | - | public static ArrayList<Result> search(Context context, String text) throws DictionaryException { | |
63 | + | public static void prepare(Context context) { | |
64 | 64 | if(instance == null) | |
65 | 65 | instance = new DictionaryFactory(context); | |
66 | + | } | |
67 | + | ||
68 | + | public static ArrayList<Result> search(String text) throws DictionaryException { | |
69 | + | if(instance == null) | |
70 | + | throw new NoDictionaryException(); | |
66 | 71 | ||
67 | 72 | int available = 0; | |
68 | 73 | ArrayList<Result> results = new ArrayList<>(); |