Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Corentin Garcia committed Apr 30, 2021
1 parent c92b8d6 commit db75eed
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import android.widget.AutoCompleteTextView
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
Expand Down Expand Up @@ -104,11 +105,6 @@ class ConverterActivity : AppCompatActivity() {
}

private fun initInputFields() {
// Get list of years
val yearsList = (converter.latestYear downTo converter.firstYear).toList().map {
it.toString()
}

// Set years inputs
val yearFields = listOf(
Triple(
Expand All @@ -122,26 +118,53 @@ class ConverterActivity : AppCompatActivity() {
R.string.yearOfResult
)
)

val yearsList = (converter.latestYear downTo converter.firstYear).toList().map {
it.toString()
}
for ((yearInputField, yearInputLayout, hintStringId) in yearFields) {
yearInputField.setText(yearsList.first().toString())
yearInputLayout.hint =
getString(hintStringId, converter.firstYear, converter.latestYear)
yearInputField.onFocusChangeListener = YearInputListener(
yearInputLayout, converter.firstYear, converter.latestYear, getString(
yearInputField.onFocusChangeListener = View.OnFocusChangeListener { v, _ ->
val textContent = (v as EditText).text.toString()
val errorString = getString(
R.string.invalid_year_error
)
)
if (textContent.isBlank()) {
yearInputLayout.error = errorString
}

try {
val year = textContent.toInt()
when {
year < converter.firstYear || year > converter.latestYear -> {
yearInputLayout.error = errorString
}
else -> {
yearInputLayout.error = null
}
}
} catch (err: NumberFormatException) {
yearInputLayout.error = errorString
}

// TODO: refresh corresponding currency input to handle currency change
}
}

// Set currency inputs
refreshCurrencyInputs(converter.latestYear, converter.latestYear - 2)
}

private fun refreshCurrencyInputs(fromYear: Int, toYear: Int) {
val currencyFields = listOf(
Pair(binding.sumToConvertInput, R.string.sumToConvert),
Pair(binding.resultInput, R.string.resultText)
Triple(binding.sumToConvertInput, R.string.sumToConvert, fromYear),
Triple(binding.resultInput, R.string.resultText, toYear)
)
for ((sumInput, hintStringId) in currencyFields) {
for ((sumInput, hintStringId, year) in currencyFields) {
sumInput.hint = getString(
hintStringId,
converter.getCurrencyFromYear(converter.latestYear)
converter.getCurrencyFromYear(year)
)
}
}
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<string name="appName">Calculateur d\'inflation</string>
<string name="sumToConvert">Somme à convertir (%1$s)</string>
<string name="yearOfOrigin">De (année), entre (%1$d) et (%2$d)</string>
<string name="yearOfResult">Vers (année), entre (%1$d) et (%2$d)</string>
<string name="yearOfOrigin">De (année), entre %1$d et %2$d</string>
<string name="yearOfResult">Vers (année), entre %1$d et %2$d</string>
<string name="convertButton">Convertir</string>
<string name="resultText">Résultat (%1$s)</string>
<string name="actionAbout">À propos</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<resources>
<string name="appName">Inflation Calculator</string>
<string name="sumToConvert">Amount to convert (%1$s)</string>
<string name="yearOfOrigin">From (year), between (%1$d) and (%2$d)</string>
<string name="yearOfResult">To (year), between (%1$d) and (%2$d)</string>
<string name="yearOfOrigin">From (year), between %1$d and %2$d</string>
<string name="yearOfResult">To (year), between %1$d and %2$d</string>
<string name="convertButton">Convert</string>
<string name="resultText">Result (%1$s)</string>
<string name="actionAbout">About</string>
Expand Down

0 comments on commit db75eed

Please sign in to comment.