Skip to content

Commit 55375e5

Browse files
authored
Submit task form on Enter in title field (#303)
1 parent 0ed7e5f commit 55375e5

1 file changed

Lines changed: 53 additions & 44 deletions

File tree

android/app/src/main/java/com/dkhalife/tasks/ui/screen/TaskFormScreen.kt

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import androidx.compose.material3.Switch
4141
import androidx.compose.material3.Text
4242
import androidx.compose.material3.TopAppBar
4343
import androidx.compose.material3.TopAppBarDefaults
44+
import androidx.compose.foundation.text.KeyboardActions
45+
import androidx.compose.foundation.text.KeyboardOptions
46+
import androidx.compose.ui.text.input.ImeAction
4447
import androidx.compose.runtime.Composable
4548
import androidx.compose.runtime.getValue
4649
import androidx.compose.runtime.mutableStateOf
@@ -140,6 +143,52 @@ fun TaskFormScreen(
140143
val isRecurring = frequencyType != FrequencyType.ONCE
141144
val isEditing = existingTask != null
142145

146+
val canSubmit = title.isNotBlank() && !isSaving
147+
val submitForm: () -> Unit = submit@{
148+
if (!canSubmit) return@submit
149+
val frequency = when (frequencyType) {
150+
FrequencyType.CUSTOM -> when (repeatOn) {
151+
RepeatOn.INTERVAL -> Frequency(
152+
type = FrequencyType.CUSTOM,
153+
on = RepeatOn.INTERVAL,
154+
every = intervalEvery.toIntOrNull()?.coerceAtLeast(1) ?: 1,
155+
unit = intervalUnit
156+
)
157+
RepeatOn.DAYS_OF_THE_WEEK -> Frequency(
158+
type = FrequencyType.CUSTOM,
159+
on = RepeatOn.DAYS_OF_THE_WEEK,
160+
days = selectedDays.sorted()
161+
)
162+
RepeatOn.DAY_OF_THE_MONTHS -> Frequency(
163+
type = FrequencyType.CUSTOM,
164+
on = RepeatOn.DAY_OF_THE_MONTHS,
165+
months = selectedMonths.sorted()
166+
)
167+
else -> Frequency(type = frequencyType)
168+
}
169+
else -> Frequency(type = frequencyType)
170+
}
171+
val notification = if (notificationsEnabled) {
172+
NotificationTriggerOptions(
173+
enabled = true,
174+
dueDate = notifyDueDate,
175+
preDue = notifyPreDue,
176+
overdue = notifyOverdue
177+
)
178+
} else {
179+
NotificationTriggerOptions()
180+
}
181+
onSave(
182+
title,
183+
if (hasDueDate) dueDate?.toIsoString() else null,
184+
if (hasDueDate && isRecurring && hasEndDate) endDate?.toIsoString() else null,
185+
frequency,
186+
if (hasDueDate) notification else NotificationTriggerOptions(),
187+
selectedLabelIds.toList(),
188+
isRolling
189+
)
190+
}
191+
143192
Scaffold(
144193
topBar = {
145194
TopAppBar(
@@ -163,50 +212,8 @@ fun TaskFormScreen(
163212
elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
164213
) {
165214
Button(
166-
onClick = {
167-
val frequency = when (frequencyType) {
168-
FrequencyType.CUSTOM -> when (repeatOn) {
169-
RepeatOn.INTERVAL -> Frequency(
170-
type = FrequencyType.CUSTOM,
171-
on = RepeatOn.INTERVAL,
172-
every = intervalEvery.toIntOrNull()?.coerceAtLeast(1) ?: 1,
173-
unit = intervalUnit
174-
)
175-
RepeatOn.DAYS_OF_THE_WEEK -> Frequency(
176-
type = FrequencyType.CUSTOM,
177-
on = RepeatOn.DAYS_OF_THE_WEEK,
178-
days = selectedDays.sorted()
179-
)
180-
RepeatOn.DAY_OF_THE_MONTHS -> Frequency(
181-
type = FrequencyType.CUSTOM,
182-
on = RepeatOn.DAY_OF_THE_MONTHS,
183-
months = selectedMonths.sorted()
184-
)
185-
else -> Frequency(type = frequencyType)
186-
}
187-
else -> Frequency(type = frequencyType)
188-
}
189-
val notification = if (notificationsEnabled) {
190-
NotificationTriggerOptions(
191-
enabled = true,
192-
dueDate = notifyDueDate,
193-
preDue = notifyPreDue,
194-
overdue = notifyOverdue
195-
)
196-
} else {
197-
NotificationTriggerOptions()
198-
}
199-
onSave(
200-
title,
201-
if (hasDueDate) dueDate?.toIsoString() else null,
202-
if (hasDueDate && isRecurring && hasEndDate) endDate?.toIsoString() else null,
203-
frequency,
204-
if (hasDueDate) notification else NotificationTriggerOptions(),
205-
selectedLabelIds.toList(),
206-
isRolling
207-
)
208-
},
209-
enabled = title.isNotBlank() && !isSaving,
215+
onClick = { submitForm() },
216+
enabled = canSubmit,
210217
modifier = Modifier
211218
.fillMaxWidth()
212219
.padding(16.dp)
@@ -249,6 +256,8 @@ fun TaskFormScreen(
249256
label = { Text(stringResource(R.string.label_task_title)) },
250257
singleLine = true,
251258
shape = RoundedCornerShape(12.dp),
259+
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
260+
keyboardActions = KeyboardActions(onDone = { submitForm() }),
252261
modifier = Modifier.fillMaxWidth()
253262
)
254263

0 commit comments

Comments
 (0)