Split into functions
This commit is contained in:
parent
73f59c8dcf
commit
6c1ff4f20f
@ -34,7 +34,10 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
override fun onDone(id: String) {
|
override fun onDone(id: String) {
|
||||||
SttDialog().show( supportFragmentManager, "SttDialog")
|
SttDialog().show( supportFragmentManager, "SttDialog")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart(id: String) {}
|
override fun onStart(id: String) {}
|
||||||
|
|
||||||
|
@Suppress("OverridingDeprecatedMember")
|
||||||
override fun onError(id: String) {}
|
override fun onError(id: String) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +46,12 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
||||||
setContentView(R.layout.activity_match)
|
setContentView(R.layout.activity_match)
|
||||||
|
|
||||||
// Init ViewModel
|
initMatchModel()
|
||||||
|
findViews()
|
||||||
|
updateUI()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun initMatchModel() {
|
||||||
matchModel = ViewModelProviders.of(this).get(MatchModel::class.java).apply {
|
matchModel = ViewModelProviders.of(this).get(MatchModel::class.java).apply {
|
||||||
if (!matchStarted) {
|
if (!matchStarted) {
|
||||||
intent.apply {
|
intent.apply {
|
||||||
@ -62,19 +70,15 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
ttsEnabled = getBooleanExtra("enableTTS", false)
|
ttsEnabled = getBooleanExtra("enableTTS", false)
|
||||||
sttEnabled = getBooleanExtra("enableSTT", false)
|
sttEnabled = getBooleanExtra("enableSTT", false)
|
||||||
saveState()
|
saveState()
|
||||||
}
|
|
||||||
if (ttsEnabled) {
|
if (ttsEnabled) tts = TextToSpeech(this@MatchActivity, WaitForTtsInit())
|
||||||
tts = TextToSpeech(this@MatchActivity, WaitForTtsInit())
|
if (!sttEnabled) showPopUp(getString(R.string.button_hint))
|
||||||
if (sttEnabled)
|
|
||||||
tts?.setOnUtteranceProgressListener(SttAfterTts())
|
|
||||||
}
|
|
||||||
if (!sttEnabled){
|
|
||||||
showPopUp(getString(R.string.button_hint))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Find views
|
fun findViews() {
|
||||||
textScore = findViewById(R.id.textScore)
|
textScore = findViewById(R.id.textScore)
|
||||||
textService = findViewById(R.id.textService)
|
textService = findViewById(R.id.textService)
|
||||||
buttons = arrayOf(
|
buttons = arrayOf(
|
||||||
@ -85,13 +89,12 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
findViewById(R.id.imgService0),
|
findViewById(R.id.imgService0),
|
||||||
findViewById(R.id.imgService1)
|
findViewById(R.id.imgService1)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Set HTML text for icons credits
|
// Set HTML text for icons credits
|
||||||
findViewById<TextView>(R.id.iconsCredit).run {
|
findViewById<TextView>(R.id.iconsCredit).apply {
|
||||||
setText(fromHtml(getString(R.string.iconCredits)))
|
setText(fromHtml(getString(R.string.iconCredits)))
|
||||||
movementMethod = LinkMovementMethod.getInstance()
|
movementMethod = LinkMovementMethod.getInstance()
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUI()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateUI() {
|
fun updateUI() {
|
||||||
@ -103,9 +106,8 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
)
|
)
|
||||||
textService?.text = getString(R.string.service, players[serviceSide].name)
|
textService?.text = getString(R.string.service, players[serviceSide].name)
|
||||||
|
|
||||||
for ((button, player) in buttons.zip(players)) {
|
for ((button, player) in buttons.zip(players))
|
||||||
button.text = fromHtml(getString(R.string.button_text, player.name, player.score))
|
button.text = fromHtml(getString(R.string.button_text, player.name, player.score))
|
||||||
}
|
|
||||||
|
|
||||||
imageViews[0].setImageResource(
|
imageViews[0].setImageResource(
|
||||||
when(serviceSide) {
|
when(serviceSide) {
|
||||||
@ -120,28 +122,9 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if (matchFinished) {
|
if (matchFinished)
|
||||||
val (loser, winner) = players.sortedBy { it.score }
|
proclaimVictory()
|
||||||
if (ttsEnabled) {
|
else {
|
||||||
say(
|
|
||||||
getString(
|
|
||||||
R.string.victory_speech,
|
|
||||||
winner.name,
|
|
||||||
winner.score,
|
|
||||||
loser.score
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
startActivity(
|
|
||||||
Intent(this@MatchActivity, VictoryActivity::class.java).apply {
|
|
||||||
putExtra("winnerName", winner.name)
|
|
||||||
putExtra("player1Name", players[0].name)
|
|
||||||
putExtra("player2Name", players[1].name)
|
|
||||||
putExtra("player1Score", players[0].score)
|
|
||||||
putExtra("player2Score", players[1].score)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
if (ttsEnabled) {
|
if (ttsEnabled) {
|
||||||
var scoreSpeech: String = getString(
|
var scoreSpeech: String = getString(
|
||||||
R.string.update_score_speech,
|
R.string.update_score_speech,
|
||||||
@ -149,17 +132,42 @@ class MatchActivity : AppCompatActivity() {
|
|||||||
players[relaunchSide].score,
|
players[relaunchSide].score,
|
||||||
players[serviceSide].name
|
players[serviceSide].name
|
||||||
)
|
)
|
||||||
if (matchPoint)
|
if (matchPoint) scoreSpeech += getString(R.string.match_point)
|
||||||
scoreSpeech += getString(R.string.match_point)
|
|
||||||
say(scoreSpeech)
|
say(scoreSpeech)
|
||||||
} else {
|
}
|
||||||
if (sttEnabled)
|
if (sttEnabled) {
|
||||||
SttDialog().show(supportFragmentManager, "SttDialog")
|
if (ttsEnabled) tts?.setOnUtteranceProgressListener(SttAfterTts())
|
||||||
|
else SttDialog().show(supportFragmentManager, "SttDialog")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun proclaimVictory() {
|
||||||
|
matchModel?.apply {
|
||||||
|
val (loser, winner) = players.sortedBy { it.score }
|
||||||
|
if (ttsEnabled) {
|
||||||
|
say(
|
||||||
|
getString(
|
||||||
|
R.string.victory_speech,
|
||||||
|
winner.name,
|
||||||
|
winner.score,
|
||||||
|
loser.score
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
startActivity(
|
||||||
|
Intent(this@MatchActivity, VictoryActivity::class.java).apply {
|
||||||
|
putExtra("winnerName", winner.name)
|
||||||
|
putExtra("player1Name", players[0].name)
|
||||||
|
putExtra("player2Name", players[1].name)
|
||||||
|
putExtra("player1Score", players[0].score)
|
||||||
|
putExtra("player2Score", players[1].score)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun updateScore(view: View) {
|
fun updateScore(view: View) {
|
||||||
matchModel?.apply {
|
matchModel?.apply {
|
||||||
if (!matchFinished) {
|
if (!matchFinished) {
|
||||||
|
@ -36,14 +36,21 @@ class StarterNameActivity : AppCompatActivity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_starter_name)
|
setContentView(R.layout.activity_starter_name)
|
||||||
|
|
||||||
// Find views
|
findViews()
|
||||||
|
checkTtsAvailable()
|
||||||
|
checkSttAvailable()
|
||||||
|
restorePreviousSettings()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findViews() {
|
||||||
player1NameInput = findViewById(R.id.player1Name)
|
player1NameInput = findViewById(R.id.player1Name)
|
||||||
player2NameInput = findViewById(R.id.player2Name)
|
player2NameInput = findViewById(R.id.player2Name)
|
||||||
starterRadioGroup = findViewById(R.id.starterRadioGroup)
|
starterRadioGroup = findViewById(R.id.starterRadioGroup)
|
||||||
enableTtsSwitch = findViewById(R.id.enableTtsSwitch)
|
enableTtsSwitch = findViewById(R.id.enableTtsSwitch)
|
||||||
enableSttSwitch = findViewById(R.id.enableSttSwitch)
|
enableSttSwitch = findViewById(R.id.enableSttSwitch)
|
||||||
|
}
|
||||||
|
|
||||||
// Restore previous data
|
fun restorePreviousSettings() {
|
||||||
previousMatch = getPreferences(Context.MODE_PRIVATE).apply {
|
previousMatch = getPreferences(Context.MODE_PRIVATE).apply {
|
||||||
getStringSet("previousPlayers", emptySet())?.let { previousPlayers = it.toSet() }
|
getStringSet("previousPlayers", emptySet())?.let { previousPlayers = it.toSet() }
|
||||||
val adapter = ArrayAdapter<String>(
|
val adapter = ArrayAdapter<String>(
|
||||||
@ -66,38 +73,17 @@ class StarterNameActivity : AppCompatActivity() {
|
|||||||
enableTtsSwitch?.isChecked = getBoolean("enableTTS", false)
|
enableTtsSwitch?.isChecked = getBoolean("enableTTS", false)
|
||||||
enableSttSwitch?.isChecked = getBoolean("enableSTT", false)
|
enableSttSwitch?.isChecked = getBoolean("enableSTT", false)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if function is available on switch checked or swapped
|
fun checkTtsAvailable() {
|
||||||
enableTtsSwitch?.setOnCheckedChangeListener { _, isChecked ->
|
enableTtsSwitch?.setOnCheckedChangeListener { _, isChecked ->
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
Intent().apply {
|
Intent().apply {
|
||||||
action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA
|
action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA
|
||||||
startActivityForResult(this, CHECK_TTS)
|
startActivityForResult(this, CHECK_TTS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enableSttSwitch?.setOnCheckedChangeListener { _, isChecked ->
|
|
||||||
if (isChecked) {
|
|
||||||
if (SpeechRecognizer.isRecognitionAvailable(this@StarterNameActivity)) {
|
|
||||||
// Ask for record audio permission
|
|
||||||
if (ContextCompat.checkSelfPermission(
|
|
||||||
this@StarterNameActivity,
|
|
||||||
Manifest.permission.RECORD_AUDIO
|
|
||||||
) != PackageManager.PERMISSION_GRANTED
|
|
||||||
) {
|
|
||||||
ActivityCompat.requestPermissions(
|
|
||||||
this@StarterNameActivity,
|
|
||||||
arrayOf(Manifest.permission.RECORD_AUDIO),
|
|
||||||
ASK_PERMISSIONS_RECORD_AUDIO
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
enableSttSwitch?.isChecked = false
|
|
||||||
showText(R.string.STT_unavailable)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
@ -118,6 +104,30 @@ class StarterNameActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun checkSttAvailable() {
|
||||||
|
enableSttSwitch?.setOnCheckedChangeListener { _, isChecked ->
|
||||||
|
if (isChecked) {
|
||||||
|
if (SpeechRecognizer.isRecognitionAvailable(this@StarterNameActivity)) {
|
||||||
|
// Ask for record audio permission
|
||||||
|
if (ContextCompat.checkSelfPermission(
|
||||||
|
this@StarterNameActivity,
|
||||||
|
Manifest.permission.RECORD_AUDIO
|
||||||
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this@StarterNameActivity,
|
||||||
|
arrayOf(Manifest.permission.RECORD_AUDIO),
|
||||||
|
ASK_PERMISSIONS_RECORD_AUDIO
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
enableSttSwitch?.isChecked = false
|
||||||
|
showText(R.string.STT_unavailable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||||
when (requestCode) {
|
when (requestCode) {
|
||||||
ASK_PERMISSIONS_RECORD_AUDIO -> {
|
ASK_PERMISSIONS_RECORD_AUDIO -> {
|
||||||
@ -144,7 +154,7 @@ class StarterNameActivity : AppCompatActivity() {
|
|||||||
val enableTTS = enableTtsSwitch?.isChecked
|
val enableTTS = enableTtsSwitch?.isChecked
|
||||||
val enableSTT = enableSttSwitch?.isChecked
|
val enableSTT = enableSttSwitch?.isChecked
|
||||||
|
|
||||||
// Save
|
// Save settings
|
||||||
previousMatch?.edit()?.apply{
|
previousMatch?.edit()?.apply{
|
||||||
player1Name.let { putString("previousPlayer1", it) }
|
player1Name.let { putString("previousPlayer1", it) }
|
||||||
player2Name.let { putString("previousPlayer2", it) }
|
player2Name.let { putString("previousPlayer2", it) }
|
||||||
|
@ -3,6 +3,7 @@ package adrienmalin.pingpoints
|
|||||||
import android.arch.lifecycle.ViewModelProviders
|
import android.arch.lifecycle.ViewModelProviders
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.v7.app.AppCompatActivity
|
import android.support.v7.app.AppCompatActivity
|
||||||
import android.support.v7.app.AppCompatDelegate
|
import android.support.v7.app.AppCompatDelegate
|
||||||
@ -15,17 +16,21 @@ import kotlin.math.min
|
|||||||
|
|
||||||
|
|
||||||
class VictoryActivity : AppCompatActivity() {
|
class VictoryActivity : AppCompatActivity() {
|
||||||
|
|
||||||
var victoryModel: VictoryModel? = null
|
var victoryModel: VictoryModel? = null
|
||||||
|
var previousMatch: SharedPreferences? = null
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
||||||
setContentView(R.layout.activity_victory)
|
setContentView(R.layout.activity_victory)
|
||||||
|
|
||||||
val previousMatch = getPreferences(Context.MODE_PRIVATE)
|
initVictoryModel()
|
||||||
|
previousMatch = getPreferences(Context.MODE_PRIVATE)
|
||||||
|
updateUI()
|
||||||
|
saveScore()
|
||||||
|
}
|
||||||
|
|
||||||
// Init VictoryModel
|
fun initVictoryModel() {
|
||||||
victoryModel = ViewModelProviders.of(this).get(VictoryModel::class.java).apply {
|
victoryModel = ViewModelProviders.of(this).get(VictoryModel::class.java).apply {
|
||||||
if (!matchFinished) {
|
if (!matchFinished) {
|
||||||
matchFinished = true
|
matchFinished = true
|
||||||
@ -40,24 +45,12 @@ class VictoryActivity : AppCompatActivity() {
|
|||||||
intent.getIntExtra("player2Score", 0)
|
intent.getIntExtra("player2Score", 0)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
previousMatches = previousMatch.getString("previousMatches", "") ?: ""
|
|
||||||
previousMatch.edit().apply {
|
|
||||||
putString(
|
|
||||||
"previousMatches",
|
|
||||||
getString(
|
|
||||||
R.string.results,
|
|
||||||
players[0].name,
|
|
||||||
"%2d - %2d".format(players[0].score, players[1].score),
|
|
||||||
players[1].name,
|
|
||||||
previousMatches
|
|
||||||
)
|
|
||||||
)
|
|
||||||
commit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateUI
|
fun updateUI() {
|
||||||
|
victoryModel?.apply {
|
||||||
findViewById<TextView>(R.id.congrats).text = getString(R.string.congrats, winnerName)
|
findViewById<TextView>(R.id.congrats).text = getString(R.string.congrats, winnerName)
|
||||||
findViewById<TextView>(R.id.player1NameTextView).text = players[0].name
|
findViewById<TextView>(R.id.player1NameTextView).text = players[0].name
|
||||||
findViewById<TextView>(R.id.scoreTextView).text = getString(
|
findViewById<TextView>(R.id.scoreTextView).text = getString(
|
||||||
@ -70,11 +63,29 @@ class VictoryActivity : AppCompatActivity() {
|
|||||||
this@VictoryActivity,
|
this@VictoryActivity,
|
||||||
R.layout.grid_item,
|
R.layout.grid_item,
|
||||||
R.id.grid_item_text,
|
R.id.grid_item_text,
|
||||||
previousMatches.split("\t|\n".toRegex())
|
previousMatch?.getString("previousMatches", "")?.split("\t|\n".toRegex())?.toMutableList()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun saveScore() {
|
||||||
|
victoryModel?.apply {
|
||||||
|
previousMatch?.edit()?.apply {
|
||||||
|
putString(
|
||||||
|
"previousMatches",
|
||||||
|
getString(
|
||||||
|
R.string.results,
|
||||||
|
players[0].name,
|
||||||
|
"%2d - %2d".format(players[0].score, players[1].score),
|
||||||
|
players[1].name,
|
||||||
|
previousMatches
|
||||||
|
)
|
||||||
|
)
|
||||||
|
commit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun newMatch(view: View) {
|
fun newMatch(view: View) {
|
||||||
startActivity(
|
startActivity(
|
||||||
Intent(this, StarterNameActivity::class.java)
|
Intent(this, StarterNameActivity::class.java)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user