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
import android.arch.lifecycle.ViewModelProviders
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.support.design.widget.Snackbar
@ -16,7 +13,6 @@ import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import java.util.*
import java.util.regex.Pattern
@ -80,7 +76,7 @@ class MatchActivity : AppCompatActivity() {
tts?.setOnUtteranceProgressListener(SttAfterTts())
}
if (!sttEnabled){
showText(getString(R.string.button_hint))
showPopUp(getString(R.string.button_hint))
}
}
}
@ -135,7 +131,7 @@ class MatchActivity : AppCompatActivity() {
if (matchFinished) {
val (loser, winner) = players.sortedBy { it.score }
if (ttsEnabled) {
speakText(
say(
getString(
R.string.victory_speech,
winner.name,
@ -163,7 +159,7 @@ class MatchActivity : AppCompatActivity() {
)
if (matchPoint)
scoreSpeech += getString(R.string.match_point)
speakText(scoreSpeech)
say(scoreSpeech)
} else {
if (sttEnabled)
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() {
if (matchModel?.pointId == 0)
super.onBackPressed()
@ -216,4 +188,28 @@ class MatchActivity : AppCompatActivity() {
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

@ -95,11 +95,7 @@ class StarterNameActivity : AppCompatActivity() {
}
} else {
enableSttSwitch?.isChecked = false
Snackbar.make(
findViewById(R.id.coordinatorLayout),
R.string.STT_unavailable,
Snackbar.LENGTH_SHORT
).show()
showText(R.string.STT_unavailable,)
}
}
false
@ -114,11 +110,7 @@ class StarterNameActivity : AppCompatActivity() {
enableTtsSwitch?.isChecked = true
} else {
enableTtsSwitch?.isChecked = false
Snackbar.make(
findViewById(R.id.coordinatorLayout),
R.string.TTS_unavailable,
Snackbar.LENGTH_SHORT
).show()
showText(R.string.TTS_unavailable)
Intent().apply {
action = TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
startActivity(this)
@ -135,11 +127,7 @@ class StarterNameActivity : AppCompatActivity() {
enableSttSwitch?.isChecked = true
} else {
enableSttSwitch?.isChecked = false
Snackbar.make(
findViewById(R.id.coordinatorLayout),
R.string.audio_record_permission_denied,
Snackbar.LENGTH_LONG
).show()
showText(R.string.audio_record_permission_denied)
}
} 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.support.v4.app.DialogFragment
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import java.util.*
@ -105,7 +104,7 @@ class SttDialog : DialogFragment() {
} catch (e: ActivityNotFoundException) {
sttEnabled = false
dismiss()
showText(R.string.STT_unavailable)
showPopUp(R.string.STT_unavailable)
}
}
}