From 5a821c76553008b479ef07d41158621a8ce11815 Mon Sep 17 00:00:00 2001 From: MJJacobs01 Date: Mon, 16 Oct 2023 19:50:57 +0200 Subject: [PATCH 1/2] Requested focus for the text field after navigation is finished Took 6 minutes --- .../addmedication/AddMedicationRoute.kt | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt index 65567a1..e90ec49 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt @@ -35,6 +35,7 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -43,6 +44,8 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight @@ -98,6 +101,11 @@ fun AddMedicationScreen( var isAfternoonSelected by rememberSaveable { mutableStateOf(false) } var isEveningSelected by rememberSaveable { mutableStateOf(false) } var isNightSelected by rememberSaveable { mutableStateOf(false) } + val focusRequester = remember { FocusRequester() } + + LaunchedEffect(Unit) { + focusRequester.requestFocus() + } Column( modifier = Modifier @@ -132,7 +140,9 @@ fun AddMedicationScreen( style = MaterialTheme.typography.bodyLarge ) TextField( - modifier = Modifier.fillMaxWidth(), + modifier = Modifier + .fillMaxWidth() + .focusRequester(focusRequester), value = medicationName, onValueChange = { medicationName = it }, // label = { Text(text = stringResource(id = R.string.medication_name)) }, @@ -356,7 +366,10 @@ fun AddMedicationScreen( Toast.LENGTH_LONG ).show() - val event = String.format(AnalyticsEvents.ADD_MEDICATION_MEDICATION_VALUE_INVALIDATED, invalidatedValue) + val event = String.format( + AnalyticsEvents.ADD_MEDICATION_MEDICATION_VALUE_INVALIDATED, + invalidatedValue + ) analyticsHelper.logEvent(event) }, onValidate = { @@ -415,7 +428,8 @@ private fun validateMedication( if (eveningSelection) timesOfDay.add(TimesOfDay.Evening) if (nightSelection) timesOfDay.add(TimesOfDay.Night) - val medications = viewModel.createMedications(name, dosage, recurrence, Date(endDate), timesOfDay) + val medications = + viewModel.createMedications(name, dosage, recurrence, Date(endDate), timesOfDay) onValidate(medications) } From 838c2b1b8f7f3c062426d00a0785d8d0df769cbd Mon Sep 17 00:00:00 2001 From: MJJacobs01 Date: Mon, 16 Oct 2023 19:58:52 +0200 Subject: [PATCH 2/2] Fixed syntax Took 8 minutes --- .../feature/addmedication/AddMedicationRoute.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt index e90ec49..e8467f9 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/AddMedicationRoute.kt @@ -37,6 +37,8 @@ import androidx.compose.material3.TextField import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -78,7 +80,6 @@ fun DefaultPreview() { fun AddMedicationRoute( onBackClicked: () -> Unit, navigateToMedicationConfirm: (List) -> Unit, - modifier: Modifier = Modifier, viewModel: AddMedicationViewModel = hiltViewModel() ) { val analyticsHelper = AnalyticsHelper.getInstance(LocalContext.current) @@ -96,7 +97,7 @@ fun AddMedicationScreen( var medicationName by rememberSaveable { mutableStateOf("") } var numberOfDosage by rememberSaveable { mutableStateOf("1") } var recurrence by rememberSaveable { mutableStateOf(Recurrence.Daily.name) } - var endDate by rememberSaveable { mutableStateOf(Date().time) } + var endDate by rememberSaveable { mutableLongStateOf(Date().time) } var isMorningSelected by rememberSaveable { mutableStateOf(false) } var isAfternoonSelected by rememberSaveable { mutableStateOf(false) } var isEveningSelected by rememberSaveable { mutableStateOf(false) } @@ -209,7 +210,7 @@ fun AddMedicationScreen( style = MaterialTheme.typography.bodyLarge ) - var selectionCount by rememberSaveable { mutableStateOf(0) } + var selectionCount by rememberSaveable { mutableIntStateOf(0) } val scope = rememberCoroutineScope() val context = LocalContext.current @@ -533,9 +534,9 @@ fun EndDateTextField(endDate: (Long) -> Unit) { val context = LocalContext.current val calendar = Calendar.getInstance() - val year: Int = calendar.get(Calendar.YEAR) - val month: Int = calendar.get(Calendar.MONTH) - val day: Int = calendar.get(Calendar.DAY_OF_MONTH) + val year: Int = calendar[Calendar.YEAR] + val month: Int = calendar[Calendar.MONTH] + val day: Int = calendar[Calendar.DAY_OF_MONTH] calendar.time = Date() val datePickerDialog =