1
0
Fork 0

Extracting hardcoded strings into resource file

This commit is contained in:
Eoin Mcloughlin 2020-05-17 11:36:31 +01:00
parent 806f35e507
commit 297ab32f84
16 changed files with 58 additions and 134 deletions

1
TODO
View File

@ -6,7 +6,6 @@ Polish:
* Better statistics on results
* Visual styling
* Settings info text
* Extract UI strings into resource file
* Play sample sounds when settings change
* More Settings

View File

@ -26,17 +26,11 @@
android:label="@string/sounder_activity_label"
android:parentActivityName=".MainActivity"
android:windowSoftInputMode="stateAlwaysVisible">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/settings_activity_label"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".MainActivity"

View File

@ -1,10 +1,12 @@
package es.eoinrul.ecwt
import android.content.Context
import android.content.SharedPreferences
import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioTrack
import android.view.KeyEvent
import androidx.preference.PreferenceManager
import java.util.concurrent.ArrayBlockingQueue
import kotlin.concurrent.thread
import kotlin.math.PI
@ -338,11 +340,11 @@ fun KeycodeToSoundSequence(keycode : Int) : List<SoundTypes> {
class DitDahGeneratorSettings
{
fun initFromPreferences(sharedPreferences : SharedPreferences) {
//TODO Fix these hardcoded strings
toneFrequency = sharedPreferences.getInt("sender_tone", toneFrequency)
wordsPerMinute = sharedPreferences.getInt("sender_wpn", wordsPerMinute)
farnsworthWordsPerMinute = sharedPreferences.getInt("sender_effectivewpm", farnsworthWordsPerMinute)
fun initFromPreferences(ctx : Context) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx)
toneFrequency = sharedPreferences.getInt(ctx.getString(R.string.setting_tone_key), toneFrequency)
wordsPerMinute = sharedPreferences.getInt(ctx.getString(R.string.setting_wpm_key), wordsPerMinute)
farnsworthWordsPerMinute = sharedPreferences.getInt(ctx.getString(R.string.setting_effective_wpm_key), farnsworthWordsPerMinute)
}
//TODO These values are duplicated in the settings fragment

View File

@ -16,28 +16,6 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//setSupportActionBar(toolbar)
/*fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}*/
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when(item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
fun openSettings(view: View) {

View File

@ -1,5 +1,7 @@
package es.eoinrul.ecwt
import android.content.ContentProvider
import android.content.Context
import android.media.*
import android.os.Bundle
import android.view.KeyEvent
@ -59,9 +61,8 @@ class SounderActivity : AppCompatActivity() {
}
private fun initSoundPlayer() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val generatorSettings = DitDahGeneratorSettings()
generatorSettings.initFromPreferences(sharedPreferences)
generatorSettings.initFromPreferences(this)
mSoundPlayer = DitDahSoundStream(generatorSettings)
}

View File

@ -28,9 +28,8 @@ class TrainingActivity : AppCompatActivity(),
}
private fun initSoundPlayer() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val generatorSettings = DitDahGeneratorSettings()
generatorSettings.initFromPreferences(sharedPreferences)
generatorSettings.initFromPreferences(this)
mSoundPlayer = DitDahSoundStream(generatorSettings)
mSoundPlayer!!.streamNotificationListener = this

View File

@ -14,18 +14,27 @@ class TrainingResultsActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_training_results)
var userInputText = intent.getStringExtra(TRAINING_COPIED).toLowerCase()
var correctText = intent.getStringExtra(TRAINING_ANSWER).toLowerCase()
var userInputText = intent.getStringExtra(TRAINING_COPIED)?.toLowerCase()
var correctText = intent.getStringExtra(TRAINING_ANSWER)?.toLowerCase()
var editDistance = levenshtein(correctText, userInputText)
if(userInputText != null && correctText != null) {
var editDistance = levenshtein(correctText, userInputText)
var fractionCorrect : Float = (correctText.length - editDistance).toFloat() / correctText.length.toFloat()
var percentCorrect = if(editDistance == 0) { 100 } else { (fractionCorrect * 100.0f).toInt() }
var fractionCorrect = (correctText.length - editDistance).toFloat() / correctText.length.toFloat()
var percentCorrect = if (editDistance == 0) {
100
} else {
(fractionCorrect * 100.0f).toInt()
}
findViewById<TextView>(R.id.resultSummary).text = "Score: " + percentCorrect + "%\nMade " + editDistance.toString() + " mistakes"
findViewById<TextView>(R.id.resultSummary).text = getString(R.string.training_results_summary, percentCorrect, editDistance)
}
else {
findViewById<TextView>(R.id.resultSummary).text =
getString(R.string.training_results_error)
}
// TODO Could have a per-character breakdown here
// TODO Next lesson button?
}
fun onTryAgainButtonPressed(view : View) {

View File

@ -6,26 +6,14 @@
android:layout_height="match_parent"
tools:context="es.eoinrul.ecwt.SettingsActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginLeft="176dp"
android:layout_marginEnd="176dp"
android:layout_marginRight="176dp"
android:text="SETTINGS GO HERE"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="16dp" />
<fragment
android:id="@+id/fragment"
android:name="es.eoinrul.ecwt.SettingsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -18,20 +18,20 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="openSounder"
android:text="Sounder" />
android:text="@string/sounder_button_label" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="openKochLevelSelect"
android:text="Koch Training" />
android:text="@string/training_button_label" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="openSettings"
android:text="To settings" />
android:text="@string/settings_button_label" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -10,7 +10,6 @@
android:id="@+id/keyedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entered Text Will Go Here"
android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

View File

@ -23,7 +23,7 @@
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:onClick="onTryAgainButtonPressed"
android:text="Try again"
android:text="@string/retry_training_button_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resultSummary" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -18,7 +18,7 @@
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Touch to start"
android:text="@string/start_training_button_label"
android:textAlignment="textEnd"
android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"

View File

@ -1,9 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.eoinrul.ecwt.es.eoinrul.ecwt.MainActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

View File

@ -1,28 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="com.eoinrul.ecwt.FirstFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
</fragment>
<fragment
android:id="@+id/SecondFragment"
android:name="com.eoinrul.ecwt.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
</fragment>
</navigation>

View File

@ -1,32 +1,24 @@
<resources>
<string name="app_name">Ecwt</string>
<string name="action_settings">Settings</string>
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<!-- Preference Titles -->
<string name="messages_header">Messages</string>
<string name="sync_header">Sync</string>
<!-- Messages Preferences -->
<string name="signature_title">Your signature</string>
<string name="reply_title">Default reply action</string>
<!-- Sync Preferences -->
<string name="sync_title">Sync email periodically</string>
<string name="attachment_title">Download incoming attachments</string>
<string name="attachment_summary_on">Automatically download attachments for incoming emails
</string>
<string name="attachment_summary_off">Only download attachments when manually requested</string>
<string name="sounder_activity_label">Sounder</string>
<string name="level_select_activity_label">Select Lesson</string>
<string name="training_activity_label">Training Session</string>
<string name="training_results_activity_label">Lesson Results</string>
<string name="settings_activity_label">Settings</string>
<string name="sounder_button_label">Sounder</string>
<string name="training_button_label">Koch Training</string>
<string name="settings_button_label">Settings</string>
<string name="retry_training_button_label">Try Again</string>
<string name="start_training_button_label">Touch to Start</string>
<string name="setting_tone_frequency_label">Tone Frequency</string>
<string name="setting_wpm_label">Words per Minute</string>
<string name="setting_effective_wpm_label">Effective Words per Minute</string>
<string name="setting_tone_key">sender_tone</string>
<string name="setting_wpm_key">sender_wpm</string>
<string name="setting_effective_wpm_key">sender_effective_wpm</string>
<string name="training_results_summary">Score: %d%%\nMistakes: %d</string>
<string name="training_results_error">Error: No input?</string>
</resources>

View File

@ -14,29 +14,29 @@
android:defaultValue="600"
android:max="900"
app:defaultValue="650"
app:key="sender_tone"
app:key="@string/setting_tone_key"
app:min="500"
app:seekBarIncrement="50"
app:showSeekBarValue="true"
app:title="Tone Frequency" />
app:title="@string/setting_tone_frequency_label" />
<SeekBarPreference
android:defaultValue="600"
android:max="50"
app:defaultValue="20"
app:key="sender_wpm"
app:key="@string/setting_wpm_key"
app:min="5"
app:showSeekBarValue="true"
app:title="Words per Minute" />
app:title="@string/setting_wpm_label" />
<SeekBarPreference
android:defaultValue="600"
android:max="50"
app:defaultValue="20"
app:key="sender_effectivewpm"
app:key="@string/setting_effective_wpm_key"
app:min="5"
app:showSeekBarValue="true"
app:title="Effective Words per Minute" />
app:title="@string/setting_effective_wpm_label" />
</PreferenceCategory>