in unit/BasicSample-kotlinApp/app/src/main/java/com/example/android/testing/unittesting/BasicSample/MainActivity.kt [89:114]
fun onSaveClick(@Suppress("UNUSED_PARAMETER") view: View) {
// Don't save if the fields do not validate.
if (!emailValidator.isValid) {
emailText.error = "Invalid email"
Log.w(TAG, "Not saving personal information: Invalid email")
return
}
// Get the text from the input fields.
val name = nameText.text.toString()
val dateOfBirth = Calendar.getInstance()
dateOfBirth.set(dobPicker.year, dobPicker.month, dobPicker.dayOfMonth)
val email = emailText.text.toString()
// Create a Setting model class to persist.
val sharedPreferenceEntry = SharedPreferenceEntry(name, dateOfBirth, email)
// Persist the personal information.
val isSuccess = sharedPreferencesHelper.savePersonalInfo(sharedPreferenceEntry)
if (isSuccess) {
Toast.makeText(this, "Personal information saved", Toast.LENGTH_LONG).show()
Log.i(TAG, "Personal information saved")
} else {
Log.e(TAG, "Failed to write personal information to SharedPreferences")
}
}