Split into functions
This commit is contained in:
		| @ -34,7 +34,10 @@ class MatchActivity : AppCompatActivity() { | ||||
|         override fun onDone(id: String) { | ||||
|             SttDialog().show( supportFragmentManager, "SttDialog") | ||||
|         } | ||||
|  | ||||
|         override fun onStart(id: String) {} | ||||
|  | ||||
|         @Suppress("OverridingDeprecatedMember") | ||||
|         override fun onError(id: String) {} | ||||
|     } | ||||
|  | ||||
| @ -43,7 +46,12 @@ class MatchActivity : AppCompatActivity() { | ||||
|         AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) | ||||
|         setContentView(R.layout.activity_match) | ||||
|  | ||||
|         // Init ViewModel | ||||
|         initMatchModel() | ||||
|         findViews() | ||||
|         updateUI() | ||||
|     } | ||||
|  | ||||
|     fun initMatchModel() { | ||||
|         matchModel = ViewModelProviders.of(this).get(MatchModel::class.java).apply { | ||||
|             if (!matchStarted) { | ||||
|                 intent.apply { | ||||
| @ -62,19 +70,15 @@ class MatchActivity : AppCompatActivity() { | ||||
|                     ttsEnabled = getBooleanExtra("enableTTS", false) | ||||
|                     sttEnabled = getBooleanExtra("enableSTT", false) | ||||
|                     saveState() | ||||
|                 } | ||||
|                 if (ttsEnabled) { | ||||
|                     tts = TextToSpeech(this@MatchActivity, WaitForTtsInit()) | ||||
|                     if (sttEnabled) | ||||
|                         tts?.setOnUtteranceProgressListener(SttAfterTts()) | ||||
|                 } | ||||
|                 if (!sttEnabled){ | ||||
|                     showPopUp(getString(R.string.button_hint)) | ||||
|  | ||||
|                     if (ttsEnabled) tts = TextToSpeech(this@MatchActivity, WaitForTtsInit()) | ||||
|                     if (!sttEnabled) showPopUp(getString(R.string.button_hint)) | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|         // Find views | ||||
|     fun findViews() { | ||||
|         textScore = findViewById(R.id.textScore) | ||||
|         textService = findViewById(R.id.textService) | ||||
|         buttons = arrayOf( | ||||
| @ -85,13 +89,12 @@ class MatchActivity : AppCompatActivity() { | ||||
|             findViewById(R.id.imgService0), | ||||
|             findViewById(R.id.imgService1) | ||||
|         ) | ||||
|  | ||||
|         // Set HTML text for icons credits | ||||
|         findViewById<TextView>(R.id.iconsCredit).run { | ||||
|         findViewById<TextView>(R.id.iconsCredit).apply { | ||||
|             setText(fromHtml(getString(R.string.iconCredits))) | ||||
|             movementMethod = LinkMovementMethod.getInstance() | ||||
|         } | ||||
|  | ||||
|         updateUI() | ||||
|     } | ||||
|  | ||||
|     fun updateUI() { | ||||
| @ -103,9 +106,8 @@ class MatchActivity : AppCompatActivity() { | ||||
|             ) | ||||
|             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)) | ||||
|             } | ||||
|  | ||||
|             imageViews[0].setImageResource( | ||||
|                 when(serviceSide) { | ||||
| @ -120,28 +122,9 @@ class MatchActivity : AppCompatActivity() { | ||||
|                 } | ||||
|             ) | ||||
|  | ||||
|             if (matchFinished) { | ||||
|                 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) | ||||
|                     } | ||||
|                 ) | ||||
|             } else { | ||||
|             if (matchFinished) | ||||
|                 proclaimVictory() | ||||
|             else { | ||||
|                 if (ttsEnabled) { | ||||
|                     var scoreSpeech: String = getString( | ||||
|                         R.string.update_score_speech, | ||||
| @ -149,17 +132,42 @@ class MatchActivity : AppCompatActivity() { | ||||
|                         players[relaunchSide].score, | ||||
|                         players[serviceSide].name | ||||
|                     ) | ||||
|                     if (matchPoint) | ||||
|                         scoreSpeech += getString(R.string.match_point) | ||||
|                     if (matchPoint) scoreSpeech += getString(R.string.match_point) | ||||
|                     say(scoreSpeech) | ||||
|                 } else { | ||||
|                     if (sttEnabled) | ||||
|                         SttDialog().show(supportFragmentManager, "SttDialog") | ||||
|                 } | ||||
|                 if (sttEnabled) { | ||||
|                     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) { | ||||
|         matchModel?.apply { | ||||
|             if (!matchFinished) { | ||||
|  | ||||
| @ -36,14 +36,21 @@ class StarterNameActivity : AppCompatActivity() { | ||||
|         super.onCreate(savedInstanceState) | ||||
|         setContentView(R.layout.activity_starter_name) | ||||
|  | ||||
|         // Find views | ||||
|         findViews() | ||||
|         checkTtsAvailable() | ||||
|         checkSttAvailable() | ||||
|         restorePreviousSettings() | ||||
|     } | ||||
|  | ||||
|     fun findViews() { | ||||
|         player1NameInput = findViewById(R.id.player1Name) | ||||
|         player2NameInput = findViewById(R.id.player2Name) | ||||
|         starterRadioGroup = findViewById(R.id.starterRadioGroup) | ||||
|         enableTtsSwitch = findViewById(R.id.enableTtsSwitch) | ||||
|         enableSttSwitch = findViewById(R.id.enableSttSwitch) | ||||
|     } | ||||
|  | ||||
|         // Restore previous data | ||||
|     fun restorePreviousSettings() { | ||||
|         previousMatch = getPreferences(Context.MODE_PRIVATE).apply { | ||||
|             getStringSet("previousPlayers", emptySet())?.let { previousPlayers = it.toSet() } | ||||
|             val adapter = ArrayAdapter<String>( | ||||
| @ -66,38 +73,17 @@ class StarterNameActivity : AppCompatActivity() { | ||||
|             enableTtsSwitch?.isChecked = getBoolean("enableTTS", false) | ||||
|             enableSttSwitch?.isChecked = getBoolean("enableSTT", false) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|         // Check if function is available on switch checked or swapped | ||||
|     fun checkTtsAvailable() { | ||||
|         enableTtsSwitch?.setOnCheckedChangeListener  { _, isChecked -> | ||||
|         if (isChecked)  { | ||||
|             if (isChecked)  { | ||||
|                 Intent().apply { | ||||
|                     action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA | ||||
|                     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?) { | ||||
| @ -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) { | ||||
|         when (requestCode) { | ||||
|             ASK_PERMISSIONS_RECORD_AUDIO -> { | ||||
| @ -144,7 +154,7 @@ class StarterNameActivity : AppCompatActivity() { | ||||
|         val enableTTS = enableTtsSwitch?.isChecked | ||||
|         val enableSTT = enableSttSwitch?.isChecked | ||||
|  | ||||
|         // Save | ||||
|         // Save settings | ||||
|         previousMatch?.edit()?.apply{ | ||||
|             player1Name.let { putString("previousPlayer1", it) } | ||||
|             player2Name.let { putString("previousPlayer2", it) } | ||||
|  | ||||
| @ -3,6 +3,7 @@ package adrienmalin.pingpoints | ||||
| import android.arch.lifecycle.ViewModelProviders | ||||
| import android.content.Context | ||||
| import android.content.Intent | ||||
| import android.content.SharedPreferences | ||||
| import android.os.Bundle | ||||
| import android.support.v7.app.AppCompatActivity | ||||
| import android.support.v7.app.AppCompatDelegate | ||||
| @ -15,17 +16,21 @@ import kotlin.math.min | ||||
|  | ||||
|  | ||||
| class VictoryActivity : AppCompatActivity() { | ||||
|  | ||||
|     var victoryModel: VictoryModel? = null | ||||
|     var previousMatch: SharedPreferences? = null | ||||
|  | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
|         super.onCreate(savedInstanceState) | ||||
|         AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) | ||||
|         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 { | ||||
|             if (!matchFinished) { | ||||
|                 matchFinished = true | ||||
| @ -40,24 +45,12 @@ class VictoryActivity : AppCompatActivity() { | ||||
|                         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.player1NameTextView).text = players[0].name | ||||
|             findViewById<TextView>(R.id.scoreTextView).text = getString( | ||||
| @ -70,11 +63,29 @@ class VictoryActivity : AppCompatActivity() { | ||||
|                 this@VictoryActivity, | ||||
|                 R.layout.grid_item, | ||||
|                 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) { | ||||
|         startActivity( | ||||
|             Intent(this, StarterNameActivity::class.java) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user