@@ -26,7 +26,7 @@ import java.util.*
2626
2727
2828class QuestionFragment : BaseNameGameViewFragment <QuestionPresenter >(), QuestionView, MainActivity.IOnBackPressed {
29- private val speechRecognizer by lazy { SpeechRecognizer .createSpeechRecognizer(activity !! ) }
29+ private lateinit var speechRecognizer: SpeechRecognizer
3030 private val speechRecognizerIntent by lazy {
3131 val speechRecIntent = Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH )
3232 speechRecIntent.putExtra(RecognizerIntent .EXTRA_LANGUAGE_MODEL ,
@@ -37,7 +37,6 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
3737 }
3838
3939 override fun openMic () {
40-
4140 speechRecognizer.setRecognitionListener(object : RecognitionListener {
4241 override fun onReadyForSpeech (params : Bundle ? ) {
4342 }
@@ -83,12 +82,17 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
8382
8483 }
8584
85+ override fun closeMic () {
86+ speechRecognizer.stopListening()
87+ }
88+
8689 private var restoreX: Float? = null
8790 private var restoreY: Float? = null
8891 private var lastCorrectBtn: Button ? = null
8992 private var lastSelectedBtn: Button ? = null
9093
9194 override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
95+ speechRecognizer = SpeechRecognizer .createSpeechRecognizer(activity!! )
9296 return inflater.inflate(R .layout.fragment_question, container, false )
9397 }
9498
@@ -173,12 +177,10 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
173177 }
174178
175179 override fun showProfile (viewState : QuestionViewState ) {
176- activity?.runOnUiThread {
177- if (btn_next.visibility == View .VISIBLE ) {
178- fadeNextButton { setProfileAndFadeIn(viewState) }
179- } else {
180- setProfileAndFadeIn(viewState)
181- }
180+ if (btn_next.visibility == View .VISIBLE ) {
181+ fadeNextButton { setProfileAndFadeIn(viewState) }
182+ } else {
183+ setProfileAndFadeIn(viewState)
182184 }
183185 }
184186
@@ -188,9 +190,7 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
188190 }
189191
190192 override fun showWrongAnswer (viewState : QuestionViewState , isEndGame : Boolean ) {
191- activity?.runOnUiThread {
192- wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, isEndGame) }
193- }
193+ wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, isEndGame) }
194194 }
195195
196196 private val showButtonsAnimatorSet by lazy {
@@ -337,7 +337,7 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
337337 }
338338
339339 override fun setTimerText (viewState : QuestionViewState ) {
340- activity?.runOnUiThread {
340+ if (view != null ) {
341341 txt_timer.scaleX = 0f
342342 txt_timer.scaleY = 0f
343343 txt_timer.alpha = 1f
@@ -359,27 +359,24 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
359359 }
360360
361361 override fun showTimesUp (viewState : QuestionViewState , isEndGame : Boolean ) {
362- activity?.runOnUiThread {
363- txt_timer.scaleX = 0f
364- txt_timer.scaleY = 0f
365- txt_timer.text = viewState.timerText
366- val restoreColor = txt_timer.currentTextColor
367- txt_timer.setTextColor(ResourcesCompat .getColor(context?.resources!! , R .color.red, activity?.theme))
368- txt_timer.animate()
369- .scaleX(1f )
370- .scaleY(1f )
371- .setInterpolator(FastOutSlowInInterpolator ())
372- .setDuration(500 )
373- .withEndAction {
374- showWrongAnswer(viewState, isEndGame)
375- txt_timer?.animate()?.alpha(0f )
376- ?.withEndAction {
377- txt_timer?.visibility = View .VISIBLE
378- txt_timer?.setTextColor(restoreColor)
379- }
380- }
381-
382- }
362+ txt_timer.scaleX = 0f
363+ txt_timer.scaleY = 0f
364+ txt_timer.text = viewState.timerText
365+ val restoreColor = txt_timer.currentTextColor
366+ txt_timer.setTextColor(ResourcesCompat .getColor(context?.resources!! , R .color.red, activity?.theme))
367+ txt_timer.animate()
368+ .scaleX(1f )
369+ .scaleY(1f )
370+ .setInterpolator(FastOutSlowInInterpolator ())
371+ .setDuration(500 )
372+ .withEndAction {
373+ showWrongAnswer(viewState, isEndGame)
374+ txt_timer?.animate()?.alpha(0f )
375+ ?.withEndAction {
376+ txt_timer?.visibility = View .VISIBLE
377+ txt_timer?.setTextColor(restoreColor)
378+ }
379+ }
383380 }
384381
385382 private fun celebrate () {
@@ -402,53 +399,4 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
402399 4 -> button4
403400 else -> null // throw IllegalStateException("Invalid button index")
404401 }
405-
406-
407- private fun startSpeechToText () {
408-
409- val speechRecognizer = SpeechRecognizer .createSpeechRecognizer(activity!! )
410- val speechRecognizerIntent = Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH )
411- speechRecognizerIntent.putExtra(RecognizerIntent .EXTRA_LANGUAGE_MODEL , RecognizerIntent .LANGUAGE_MODEL_FREE_FORM )
412- speechRecognizerIntent.putExtra(RecognizerIntent .EXTRA_LANGUAGE , Locale .getDefault())
413-
414- speechRecognizer.setRecognitionListener(object : RecognitionListener {
415- override fun onReadyForSpeech (bundle : Bundle ) {}
416-
417- override fun onBeginningOfSpeech () {}
418-
419- override fun onRmsChanged (v : Float ) {}
420-
421- override fun onBufferReceived (bytes : ByteArray ) {}
422-
423- override fun onEndOfSpeech () {}
424-
425- override fun onError (i : Int ) {}
426-
427- override fun onResults (bundle : Bundle ) {
428- val matches = bundle.getStringArrayList(SpeechRecognizer .RESULTS_RECOGNITION )// getting all the matches
429- // displaying the first match
430- // if (matches != null)
431- }
432-
433- override fun onPartialResults (bundle : Bundle ) {}
434-
435- override fun onEvent (i : Int , bundle : Bundle ) {}
436- })
437-
438- btn_end_game.setOnTouchListener { view, motionEvent ->
439- when (motionEvent.action) {
440- MotionEvent .ACTION_UP -> {
441- speechRecognizer.stopListening()
442- // editText.hint = getString(R.string.text_hint)
443- }
444-
445- MotionEvent .ACTION_DOWN -> {
446- speechRecognizer.startListening(speechRecognizerIntent)
447- // editText.setText("")
448- // editText.hint = "Listening..."
449- }
450- }
451- false
452- }
453- }
454402}
0 commit comments