code cleanup

This commit is contained in:
adrienmalin 2018-12-07 12:18:42 +01:00
parent 3ddff42431
commit 1f624dfd51
3 changed files with 40 additions and 49 deletions

View File

@ -1,11 +1,8 @@
package adrienmalin.pingpoints package adrienmalin.pingpoints
import android.arch.lifecycle.ViewModelProviders import android.arch.lifecycle.ViewModelProviders
import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.speech.tts.TextToSpeech import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener import android.speech.tts.UtteranceProgressListener
import android.support.design.widget.Snackbar import android.support.design.widget.Snackbar
@ -16,7 +13,6 @@ import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
@ -80,7 +76,7 @@ class MatchActivity : AppCompatActivity() {
tts?.setOnUtteranceProgressListener(SttAfterTts()) tts?.setOnUtteranceProgressListener(SttAfterTts())
} }
if (!sttEnabled){ if (!sttEnabled){
showText(getString(R.string.button_hint)) showPopUp(getString(R.string.button_hint))
} }
} }
} }
@ -135,7 +131,7 @@ class MatchActivity : AppCompatActivity() {
if (matchFinished) { if (matchFinished) {
val (loser, winner) = players.sortedBy { it.score } val (loser, winner) = players.sortedBy { it.score }
if (ttsEnabled) { if (ttsEnabled) {
speakText( say(
getString( getString(
R.string.victory_speech, R.string.victory_speech,
winner.name, winner.name,
@ -163,7 +159,7 @@ class MatchActivity : AppCompatActivity() {
) )
if (matchPoint) if (matchPoint)
scoreSpeech += getString(R.string.match_point) scoreSpeech += getString(R.string.match_point)
speakText(scoreSpeech) say(scoreSpeech)
} else { } else {
if (sttEnabled) if (sttEnabled)
SttDialog().show(supportFragmentManager, "SttDialog") SttDialog().show(supportFragmentManager, "SttDialog")
@ -184,30 +180,6 @@ class MatchActivity : AppCompatActivity() {
} }
} }
fun showText(text: String, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
text,
duration
).show()
}
fun showText(textId: Int, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
textId,
duration
).show()
}
fun speakText(text: String, queueMode: Int = TextToSpeech.QUEUE_FLUSH) {
tts?.speak(
text,
queueMode,
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "TTS")
)
}
override fun onBackPressed() { override fun onBackPressed() {
if (matchModel?.pointId == 0) if (matchModel?.pointId == 0)
super.onBackPressed() super.onBackPressed()
@ -216,4 +188,28 @@ class MatchActivity : AppCompatActivity() {
updateUI() updateUI()
} }
} }
fun showPopUp(text: String, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
text,
duration
).show()
}
fun showPopUp(textId: Int, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
textId,
duration
).show()
}
fun say(text: String, queueMode: Int = TextToSpeech.QUEUE_FLUSH) {
tts?.speak(
text,
queueMode,
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "TTS")
)
}
} }

View File

@ -79,7 +79,7 @@ class StarterNameActivity : AppCompatActivity() {
} }
enableSttSwitch?.setOnCheckedChangeListener { _, isChecked -> enableSttSwitch?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) { if (isChecked) {
if (SpeechRecognizer.isRecognitionAvailable(this@StarterNameActivity)) { if (SpeechRecognizer.isRecognitionAvailable(this@StarterNameActivity)) {
// Ask for record audio permission // Ask for record audio permission
if (ContextCompat.checkSelfPermission( if (ContextCompat.checkSelfPermission(
@ -95,11 +95,7 @@ class StarterNameActivity : AppCompatActivity() {
} }
} else { } else {
enableSttSwitch?.isChecked = false enableSttSwitch?.isChecked = false
Snackbar.make( showText(R.string.STT_unavailable,)
findViewById(R.id.coordinatorLayout),
R.string.STT_unavailable,
Snackbar.LENGTH_SHORT
).show()
} }
} }
false false
@ -114,11 +110,7 @@ class StarterNameActivity : AppCompatActivity() {
enableTtsSwitch?.isChecked = true enableTtsSwitch?.isChecked = true
} else { } else {
enableTtsSwitch?.isChecked = false enableTtsSwitch?.isChecked = false
Snackbar.make( showText(R.string.TTS_unavailable)
findViewById(R.id.coordinatorLayout),
R.string.TTS_unavailable,
Snackbar.LENGTH_SHORT
).show()
Intent().apply { Intent().apply {
action = TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA action = TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
startActivity(this) startActivity(this)
@ -135,11 +127,7 @@ class StarterNameActivity : AppCompatActivity() {
enableSttSwitch?.isChecked = true enableSttSwitch?.isChecked = true
} else { } else {
enableSttSwitch?.isChecked = false enableSttSwitch?.isChecked = false
Snackbar.make( showText(R.string.audio_record_permission_denied)
findViewById(R.id.coordinatorLayout),
R.string.audio_record_permission_denied,
Snackbar.LENGTH_LONG
).show()
} }
} else -> {} } else -> {}
} }
@ -185,4 +173,12 @@ class StarterNameActivity : AppCompatActivity() {
} }
) )
} }
fun showText(textId: Int, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
textId,
duration
).show()
}
} }

View File

@ -10,7 +10,6 @@ import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer import android.speech.SpeechRecognizer
import android.support.v4.app.DialogFragment import android.support.v4.app.DialogFragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import java.util.* import java.util.*
@ -105,7 +104,7 @@ class SttDialog : DialogFragment() {
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
sttEnabled = false sttEnabled = false
dismiss() dismiss()
showText(R.string.STT_unavailable) showPopUp(R.string.STT_unavailable)
} }
} }
} }