|
50 | 50 |
|
51 | 51 | public class MainActivity extends BaseActivity implements SettingsEventListener { |
52 | 52 |
|
53 | | - @BindView(R.id.chip_group) |
54 | | - ChipGroup chipGroup; |
55 | | - @BindView(R.id.coordinatorLayout) |
56 | | - CoordinatorLayout coordinatorLayout; |
57 | | - @BindView(R.id.bar) |
58 | | - BottomAppBar bar; |
59 | | - @BindView(R.id.navigation_view) |
60 | | - NavigationView navigationView; |
61 | | - private BottomSheetBehavior bottomDrawerBehavior; |
62 | | - private AuthInfo authInfo; |
63 | | - private Box<NerStandardTagsItem> tagsItemBox; |
64 | | - private SkeletonScreen skeletonScreen; |
65 | | - |
66 | | - @Override |
67 | | - protected void onCreate(Bundle savedInstanceState) { |
68 | | - super.onCreate(savedInstanceState); |
69 | | - setContentView(R.layout.activity_main); |
70 | | - ButterKnife.bind(this); |
71 | | - subscribeFirebaseTopic(); |
72 | | - setSupportActionBar(bar); |
73 | | - initBottomNavigationView(); |
74 | | - initVariables(); |
75 | | - getSentence(); |
76 | | - } |
77 | | - |
78 | | - private void subscribeFirebaseTopic() { |
79 | | - FirebaseMessaging.getInstance().subscribeToTopic("update_app") |
80 | | - .addOnCompleteListener(new OnCompleteListener<Void>() { |
81 | | - @Override |
82 | | - public void onComplete(@NonNull Task<Void> task) { |
83 | | - |
84 | | - } |
85 | | - }); |
86 | | - } |
87 | | - |
88 | | - private void initBottomNavigationView() { |
89 | | - View bottomDrawer = coordinatorLayout.findViewById(R.id.bottom_drawer); |
90 | | - bottomDrawerBehavior = BottomSheetBehavior.from(bottomDrawer); |
91 | | - bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
92 | | - bar.setNavigationOnClickListener( |
93 | | - new View.OnClickListener() { |
94 | | - @Override |
95 | | - public void onClick(View v) { |
96 | | - bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED); |
97 | | - } |
| 53 | + @BindView(R.id.chip_group) |
| 54 | + ChipGroup chipGroup; |
| 55 | + @BindView(R.id.coordinatorLayout) |
| 56 | + CoordinatorLayout coordinatorLayout; |
| 57 | + @BindView(R.id.bar) |
| 58 | + BottomAppBar bar; |
| 59 | + @BindView(R.id.navigation_view) |
| 60 | + NavigationView navigationView; |
| 61 | + private BottomSheetBehavior bottomDrawerBehavior; |
| 62 | + private AuthInfo authInfo; |
| 63 | + private Box<NerStandardTagsItem> tagsItemBox; |
| 64 | + private SkeletonScreen skeletonScreen; |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void onCreate(Bundle savedInstanceState) { |
| 68 | + super.onCreate(savedInstanceState); |
| 69 | + setContentView(R.layout.activity_main); |
| 70 | + ButterKnife.bind(this); |
| 71 | + subscribeFirebaseTopic(); |
| 72 | + setSupportActionBar(bar); |
| 73 | + initBottomNavigationView(); |
| 74 | + initVariables(); |
| 75 | + getSentence(); |
| 76 | + } |
| 77 | + |
| 78 | + private void subscribeFirebaseTopic() { |
| 79 | + FirebaseMessaging.getInstance().subscribeToTopic("update_app") |
| 80 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 81 | + @Override |
| 82 | + public void onComplete(@NonNull Task<Void> task) { |
| 83 | + |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + private void initBottomNavigationView() { |
| 89 | + View bottomDrawer = coordinatorLayout.findViewById(R.id.bottom_drawer); |
| 90 | + bottomDrawerBehavior = BottomSheetBehavior.from(bottomDrawer); |
| 91 | + bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 92 | + bar.setNavigationOnClickListener( |
| 93 | + new View.OnClickListener() { |
| 94 | + @Override |
| 95 | + public void onClick(View v) { |
| 96 | + bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED); |
| 97 | + } |
| 98 | + }); |
| 99 | + bar.setNavigationIcon(R.drawable.ic_menu_black_24dp); |
| 100 | + navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { |
| 101 | + @Override |
| 102 | + public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { |
| 103 | + if (menuItem.getItemId() == R.id.about_item) { |
| 104 | + showAboutFragment(); |
| 105 | + } else if (menuItem.getItemId() == R.id.settings_item) { |
| 106 | + showSettingsFragment(); |
| 107 | + } |
| 108 | + hideBottomDrawer(); |
| 109 | + return false; |
| 110 | + } |
98 | 111 | }); |
99 | | - bar.setNavigationIcon(R.drawable.ic_menu_black_24dp); |
100 | | - navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { |
101 | | - @Override |
102 | | - public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { |
103 | | - if (menuItem.getItemId() == R.id.about_item) { |
104 | | - showAboutFragment(); |
105 | | - } else if (menuItem.getItemId() == R.id.settings_item) { |
106 | | - showSettingsFragment(); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + private void showAboutFragment() { |
| 116 | + AppUtil.showFragment(new AboutFragment(), getSupportFragmentManager()); |
| 117 | + } |
| 118 | + |
| 119 | + private void showSettingsFragment() { |
| 120 | + SettingsFragment settingsFragment = new SettingsFragment(); |
| 121 | + AppUtil.showFragment(settingsFragment, getSupportFragmentManager()); |
| 122 | + } |
| 123 | + |
| 124 | + private void hideBottomDrawer() { |
| 125 | + bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 126 | + } |
| 127 | + |
| 128 | + private void initVariables() { |
| 129 | + Prefser prefser = new Prefser(this); |
| 130 | + authInfo = prefser.get(Constant.AUTH_INFO, AuthInfo.class, null); |
| 131 | + BoxStore boxStore = MyApplication.getBoxStore(); |
| 132 | + tagsItemBox = boxStore.boxFor(NerStandardTagsItem.class); |
| 133 | + } |
| 134 | + |
| 135 | + private void getSentence() { |
| 136 | + if (NetworkUtils.isConnected()) { |
| 137 | + requestGetSentence(); |
| 138 | + } else { |
| 139 | + AppUtil.showSnackbar(coordinatorLayout, getString(R.string.no_internet_label), this, SnackbarUtils.LENGTH_LONG); |
107 | 140 | } |
108 | | - hideBottomDrawer(); |
109 | | - return false; |
110 | | - } |
111 | | - }); |
112 | | - |
113 | | - } |
114 | | - |
115 | | - private void showAboutFragment() { |
116 | | - AppUtil.showFragment(new AboutFragment(), getSupportFragmentManager()); |
117 | | - } |
118 | | - |
119 | | - private void showSettingsFragment() { |
120 | | - SettingsFragment settingsFragment = new SettingsFragment(); |
121 | | - AppUtil.showFragment(settingsFragment, getSupportFragmentManager()); |
122 | | - } |
123 | | - |
124 | | - private void hideBottomDrawer() { |
125 | | - bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
126 | | - } |
127 | | - |
128 | | - private void initVariables() { |
129 | | - Prefser prefser = new Prefser(this); |
130 | | - authInfo = prefser.get(Constant.AUTH_INFO, AuthInfo.class, null); |
131 | | - BoxStore boxStore = MyApplication.getBoxStore(); |
132 | | - tagsItemBox = boxStore.boxFor(NerStandardTagsItem.class); |
133 | | - } |
134 | | - |
135 | | - private void getSentence() { |
136 | | - if (NetworkUtils.isConnected()) { |
137 | | - requestGetSentence(); |
138 | | - } else { |
139 | | - AppUtil.showSnackbar(coordinatorLayout, getString(R.string.no_internet_label), this, SnackbarUtils.LENGTH_LONG); |
140 | | - } |
141 | | - } |
142 | | - |
143 | | - private void requestGetSentence() { |
144 | | - showSkeleton(); |
145 | | - APIService apiService = RetrofitUtil.getRetrofit(authInfo.getToken()).create(APIService.class); |
146 | | - Call<Sentence> call = apiService.randomSentence(); |
147 | | - call.enqueue(new Callback<Sentence>() { |
148 | | - @Override |
149 | | - public void onResponse(@NonNull Call<Sentence> call, @NonNull Response<Sentence> response) { |
150 | | - skeletonScreen.hide(); |
151 | | - if (response.isSuccessful()) { |
152 | | - Sentence sentence = response.body(); |
153 | | - if (sentence != null) { |
154 | | - if (sentence.getWords().size() == 0) { |
155 | | - getSentence(); |
156 | | - } else { |
157 | | - handleSentence(sentence); |
| 141 | + } |
| 142 | + |
| 143 | + private void requestGetSentence() { |
| 144 | + showSkeleton(); |
| 145 | + APIService apiService = RetrofitUtil.getRetrofit(authInfo.getToken()).create(APIService.class); |
| 146 | + Call<Sentence> call = apiService.randomSentence(); |
| 147 | + call.enqueue(new Callback<Sentence>() { |
| 148 | + @Override |
| 149 | + public void onResponse(@NonNull Call<Sentence> call, @NonNull Response<Sentence> response) { |
| 150 | + skeletonScreen.hide(); |
| 151 | + if (response.isSuccessful()) { |
| 152 | + Sentence sentence = response.body(); |
| 153 | + if (sentence != null) { |
| 154 | + if (sentence.getWords().size() == 0) { |
| 155 | + getSentence(); |
| 156 | + } else { |
| 157 | + handleSentence(sentence); |
| 158 | + } |
| 159 | + } |
| 160 | + } else if (response.code() == 401) { |
| 161 | + tokenInvalid(); |
| 162 | + } |
158 | 163 | } |
159 | | - } |
160 | | - } else if (response.code() == 401) { |
161 | | - tokenInvalid(); |
| 164 | + |
| 165 | + @Override |
| 166 | + public void onFailure(@NonNull Call<Sentence> call, @NonNull Throwable t) { |
| 167 | + skeletonScreen.hide(); |
| 168 | + t.printStackTrace(); |
| 169 | + AppUtil.showSnackbar(chipGroup, getString(R.string.retry_request_message), MainActivity.this, SnackbarUtils.LENGTH_LONG); |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + private void tokenInvalid() { |
| 175 | + Intent intent = new Intent(this, LauncherActivity.class); |
| 176 | + intent.putExtra(Constant.TOKEN_INVALID, true); |
| 177 | + startActivity(intent); |
| 178 | + finish(); |
| 179 | + } |
| 180 | + |
| 181 | + private void handleSentence(Sentence sentence) { |
| 182 | + chipGroup.removeAllViews(); |
| 183 | + for (WordsItem wordsItem : sentence.getWords()) { |
| 184 | + Chip chip = generateClip(wordsItem, sentence.getSentenceId()); |
| 185 | + chipGroup.addView(chip); |
162 | 186 | } |
163 | | - } |
164 | | - |
165 | | - @Override |
166 | | - public void onFailure(@NonNull Call<Sentence> call, @NonNull Throwable t) { |
167 | | - skeletonScreen.hide(); |
168 | | - t.printStackTrace(); |
169 | | - AppUtil.showSnackbar(chipGroup, getString(R.string.retry_request_message), MainActivity.this, SnackbarUtils.LENGTH_LONG); |
170 | | - } |
171 | | - }); |
172 | | - } |
173 | | - |
174 | | - private void tokenInvalid() { |
175 | | - Intent intent = new Intent(this, LauncherActivity.class); |
176 | | - intent.putExtra(Constant.TOKEN_INVALID, true); |
177 | | - startActivity(intent); |
178 | | - finish(); |
179 | | - } |
180 | | - |
181 | | - private void handleSentence(Sentence sentence) { |
182 | | - chipGroup.removeAllViews(); |
183 | | - for (WordsItem wordsItem : sentence.getWords()) { |
184 | | - Chip chip = generateClip(wordsItem, sentence.getSentenceId()); |
185 | | - chipGroup.addView(chip); |
186 | | - } |
187 | | - } |
188 | | - |
189 | | - private Chip generateClip(WordsItem wordsItem, String sentenceId) { |
190 | | - Chip chip = |
191 | | - (Chip) getLayoutInflater().inflate(R.layout.item_chip, chipGroup, false); |
192 | | - chip.setText(wordsItem.getWord()); |
193 | | - chip.setChipBackgroundColor(ColorStateList.valueOf(Color.parseColor(AppUtil.colorHexString(wordsItem.getTag(), tagsItemBox)))); |
194 | | - if (!wordsItem.getTag().equals("O")) { |
195 | | - chip.setTextColor(Color.WHITE); |
196 | | - } |
197 | | - chip.setOnClickListener(new View.OnClickListener() { |
198 | | - @Override |
199 | | - public void onClick(View v) { |
200 | | - selectTagFragment(chip, wordsItem, sentenceId); |
201 | | - } |
202 | | - }); |
203 | | - return chip; |
204 | | - } |
205 | | - |
206 | | - private void selectTagFragment(Chip chip, WordsItem wordsItem, String sentenceId) { |
207 | | - SelectTagFragment fragment = new SelectTagFragment(); |
208 | | - fragment.setWordData(wordsItem, sentenceId); |
209 | | - fragment.setListener(new SelectTagEventListener() { |
210 | | - @Override |
211 | | - public void tagSuccessEvent(String tagTitle) { |
212 | | - AppUtil.showSnackbar(chipGroup, getString(R.string.tag_success_message), MainActivity.this, SnackbarUtils.LENGTH_LONG); |
213 | | - wordsItem.setTag(tagTitle); |
214 | | - chip.setChipBackgroundColor(ColorStateList.valueOf(Color.parseColor(AppUtil.colorHexString(tagTitle, tagsItemBox)))); |
215 | | - if (!tagTitle.equals("O")) { |
216 | | - chip.setTextColor(Color.WHITE); |
| 187 | + } |
| 188 | + |
| 189 | + private Chip generateClip(WordsItem wordsItem, String sentenceId) { |
| 190 | + Chip chip = |
| 191 | + (Chip) getLayoutInflater().inflate(R.layout.item_chip, chipGroup, false); |
| 192 | + chip.setText(wordsItem.getWord()); |
| 193 | + chip.setChipBackgroundColor(ColorStateList.valueOf(Color.parseColor(AppUtil.colorHexString(wordsItem.getTag(), tagsItemBox)))); |
| 194 | + if (!wordsItem.getTag().equals("O")) { |
| 195 | + chip.setTextColor(Color.WHITE); |
217 | 196 | } |
218 | | - } |
| 197 | + chip.setOnClickListener(new View.OnClickListener() { |
| 198 | + @Override |
| 199 | + public void onClick(View v) { |
| 200 | + selectTagFragment(chip, wordsItem, sentenceId); |
| 201 | + } |
| 202 | + }); |
| 203 | + return chip; |
| 204 | + } |
| 205 | + |
| 206 | + private void selectTagFragment(Chip chip, WordsItem wordsItem, String sentenceId) { |
| 207 | + SelectTagFragment fragment = new SelectTagFragment(); |
| 208 | + fragment.setWordData(wordsItem, sentenceId); |
| 209 | + fragment.setListener(new SelectTagEventListener() { |
| 210 | + @Override |
| 211 | + public void tagSuccessEvent(String tagTitle) { |
| 212 | + AppUtil.showSnackbar(chipGroup, getString(R.string.tag_success_message), MainActivity.this, SnackbarUtils.LENGTH_LONG); |
| 213 | + wordsItem.setTag(tagTitle); |
| 214 | + chip.setChipBackgroundColor(ColorStateList.valueOf(Color.parseColor(AppUtil.colorHexString(tagTitle, tagsItemBox)))); |
| 215 | + if (!tagTitle.equals("O")) { |
| 216 | + chip.setTextColor(Color.WHITE); |
| 217 | + } else { |
| 218 | + chip.setTextColor(Color.BLACK); |
| 219 | + } |
| 220 | + } |
219 | 221 |
|
220 | | - @Override |
221 | | - public void tokenInvalidEvent() { |
| 222 | + @Override |
| 223 | + public void tokenInvalidEvent() { |
| 224 | + tokenInvalid(); |
| 225 | + } |
| 226 | + }); |
| 227 | + AppUtil.showFragment(fragment, getSupportFragmentManager()); |
| 228 | + } |
| 229 | + |
| 230 | + @OnClick(R.id.next_sentence_button) |
| 231 | + public void nextSentence() { |
| 232 | + getSentence(); |
| 233 | + } |
| 234 | + |
| 235 | + @OnClick(R.id.legend_button) |
| 236 | + public void showLegend() { |
| 237 | + AppUtil.showFragment(new TagLegendFragment(), getSupportFragmentManager()); |
| 238 | + } |
| 239 | + |
| 240 | + private void showSkeleton() { |
| 241 | + skeletonScreen = Skeleton.bind(chipGroup) |
| 242 | + .shimmer(true) |
| 243 | + .angle(0) |
| 244 | + .color(R.color.shimmer_color) |
| 245 | + .load(R.layout.item_skeleton) |
| 246 | + .show(); |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public void signOut() { |
222 | 251 | tokenInvalid(); |
223 | | - } |
224 | | - }); |
225 | | - AppUtil.showFragment(fragment, getSupportFragmentManager()); |
226 | | - } |
227 | | - |
228 | | - @OnClick(R.id.next_sentence_button) |
229 | | - public void nextSentence() { |
230 | | - getSentence(); |
231 | | - } |
232 | | - |
233 | | - @OnClick(R.id.legend_button) |
234 | | - public void showLegend() { |
235 | | - AppUtil.showFragment(new TagLegendFragment(), getSupportFragmentManager()); |
236 | | - } |
237 | | - |
238 | | - private void showSkeleton() { |
239 | | - skeletonScreen = Skeleton.bind(chipGroup) |
240 | | - .shimmer(true) |
241 | | - .angle(0) |
242 | | - .color(R.color.shimmer_color) |
243 | | - .load(R.layout.item_skeleton) |
244 | | - .show(); |
245 | | - } |
246 | | - |
247 | | - @Override |
248 | | - public void signOut() { |
249 | | - tokenInvalid(); |
250 | | - } |
| 252 | + } |
251 | 253 | } |
0 commit comments