Created
April 10, 2018 18:27
-
-
Save LAHomieJob/12205b3605ae6712444ffe9b81e02993 to your computer and use it in GitHub Desktop.
WordsActivity question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WordsActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { | |
public final static String TTS = "TTS"; | |
private HashMap<String, Locale> availableLangs; | |
private WordsAdapter adapter; | |
private RecyclerView mRecyclerView; | |
private VocaNoteViewModel mVocaNoteViewModel; | |
private String origWord; | |
private String nameGroup; | |
private String language; | |
private String translation; | |
private TextToSpeech ttsOrigLang; | |
private TextToSpeech ttsLocalLang; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_words); | |
//Text-To-Speech object to pronounce words | |
// TODO: 05.04.2018 Optimize work of TextToSpeech objects | |
ttsLocalLang = new TextToSpeech(this, this); | |
ttsOrigLang = new TextToSpeech(this, this); | |
//Creating of toolbar with title Words | |
Toolbar myToolbar = findViewById(R.id.toolbar_words); | |
setSupportActionBar(myToolbar); | |
//Enable Up Button | |
ActionBar ab = getSupportActionBar(); | |
ab.setDisplayHomeAsUpEnabled(true); | |
//Values of nameGroup and language fields are recieving from another activity within intent | |
//It's not being showed here for purpose of simplicity | |
nameGroup = "Hello"; | |
language = "English"; | |
//RecyclerView containing the list of VocaNotes with sound icons | |
mRecyclerView = initRecyclerView(); | |
//Using ViewModel to observe VocaNote data | |
mVocaNoteViewModel = ViewModelProviders.of(this).get(VocaNoteViewModel.class); | |
mVocaNoteViewModel.getByGroupVcVocaNote(nameGroup).observe(this, new Observer<List<VocaNote>>() { | |
@Override | |
public void onChanged(@Nullable List<VocaNote> vocaNotes) { | |
adapter.setVocaNotes(vocaNotes); | |
} | |
}); | |
} | |
protected RecyclerView initRecyclerView() { | |
RecyclerView recyclerView = findViewById(R.id.words_recycler_view); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(layoutManager); | |
OnItemClicked listener = (v, position) -> { | |
origWord = adapter.getFilteredVocaNotes().get(position).getOriginWord(); | |
translation = adapter.getFilteredVocaNotes().get(position).getTranslation(); | |
switch (v.getId()) { | |
case R.id.word_sound: | |
// TODO: 05.04.2018 Make icon more accssesible | |
speakOut(); | |
break; | |
default: | |
Log.i("TAG", "Pushed the item with word"); | |
break; | |
} | |
}; | |
adapter = new WordsAdapter(this, listener); | |
adapter.notifyItemInserted(adapter.getItemCount()); | |
recyclerView.setAdapter(adapter); | |
return recyclerView; | |
} | |
@Override | |
public void onInit(int status) { | |
if (status == TextToSpeech.SUCCESS) { | |
int resultOrig = ttsOrigLang.setLanguage(chooseLang(language)); | |
int resultLocal = ttsLocalLang.setLanguage(Locale.getDefault()); | |
if ((resultOrig == TextToSpeech.LANG_MISSING_DATA | |
|| resultOrig == TextToSpeech.LANG_NOT_SUPPORTED) | |
|| (resultLocal == TextToSpeech.LANG_MISSING_DATA | |
|| resultLocal == TextToSpeech.LANG_NOT_SUPPORTED)) { | |
Log.e(TTS, "This Language is not supported"); | |
} | |
} | |
} | |
private void speakOut() { | |
if (origWord != null) { | |
ttsOrigLang.speak(origWord, TextToSpeech.QUEUE_FLUSH, null); | |
ttsLocalLang.speak(translation, TextToSpeech.QUEUE_ADD, null); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
/*Shutting down TextToSpeech*/ | |
if (ttsOrigLang != null) { | |
ttsOrigLang.stop(); | |
ttsOrigLang.shutdown(); | |
ttsLocalLang.stop(); | |
ttsLocalLang.shutdown(); | |
} | |
super.onDestroy(); | |
} | |
private Locale chooseLang(String language) { | |
availableLangs = new HashMap<>(); | |
availableLangs.put(getResources().getStringArray(R.array.spinner_values)[0], Locale.UK); | |
availableLangs.put(getResources().getStringArray(R.array.spinner_values)[1], new Locale("spa", "ESP")); | |
availableLangs.put(getResources().getStringArray(R.array.spinner_values)[2], Locale.ITALY); | |
availableLangs.put(getResources().getStringArray(R.array.spinner_values)[3], Locale.FRANCE); | |
availableLangs.put(getResources().getStringArray(R.array.spinner_values)[4], Locale.GERMANY); | |
return availableLangs.get(language); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment