Skip to content

Conversation

@Balcan
Copy link
Collaborator

@Balcan Balcan commented Dec 11, 2025

Description

Link the JIRA issue.

Please provide a clear definition of the problem and explain your solution.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where text-based input fields were not saving their values when users clicked on other inputs, addressing JIRA issue ANDROAPP-7235. The solution introduces a focus change handler that triggers a save operation when text input fields lose focus.

Key Changes

  • Added onFieldFocusChanged helper function to handle save-on-blur behavior for text inputs
  • Updated 14 text-based input components to include onFocusChanged callbacks
  • Ensures data persistence when switching between form fields

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
form/src/main/java/org/dhis2/form/ui/provider/OnFocusChangedAction.kt New helper function that triggers FormIntent.OnSave when a field loses focus
form/src/main/java/org/dhis2/form/ui/provider/inputfield/InputsForTextValueTypeProvider.kt Added onFocusChanged callbacks to QR code, default text, and barcode input fields
form/src/main/java/org/dhis2/form/ui/provider/inputfield/FieldProvider.kt Added onFocusChanged callbacks to 11 numeric and text input types (integer, number, percentage, email, phone, link, long text, letter)

Comment on lines 6 to 21
inline fun onFieldFocusChanged(
fieldUid: String,
value: String,
valueType: ValueType?,
isFocused: Boolean,
intentHandler: (FormIntent) -> Unit,
) {
if (!isFocused) {
intentHandler(
FormIntent.OnSave(
fieldUid,
value,
valueType,
),
)
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in this helper function is duplicated 14 times across InputsForTextValueTypeProvider.kt and FieldProvider.kt. This creates maintainability concerns as any change to the save-on-blur behavior would need to be applied in 14 places.

Consider refactoring this to accept a lambda or use a higher-order function pattern to eliminate duplication. For example, you could create a modifier extension function or a composable wrapper that handles the focus change logic.

Suggested change
inline fun onFieldFocusChanged(
fieldUid: String,
value: String,
valueType: ValueType?,
isFocused: Boolean,
intentHandler: (FormIntent) -> Unit,
) {
if (!isFocused) {
intentHandler(
FormIntent.OnSave(
fieldUid,
value,
valueType,
),
)
/**
* Handles save-on-blur logic for a field. Call this function when the focus state changes.
*
* @param isFocused Whether the field is currently focused.
* @param onSave Lambda to invoke when focus is lost (blur).
*/
inline fun onFieldFocusChanged(
isFocused: Boolean,
onSave: () -> Unit,
) {
if (!isFocused) {
onSave()

Copilot uses AI. Check for mistakes.
Comment on lines 6 to 23
inline fun onFieldFocusChanged(
fieldUid: String,
value: String,
valueType: ValueType?,
isFocused: Boolean,
intentHandler: (FormIntent) -> Unit,
) {
if (!isFocused) {
intentHandler(
FormIntent.OnSave(
fieldUid,
value,
valueType,
),
)
}
}
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new focus change behavior that triggers FormIntent.OnSave when focus is lost lacks test coverage. While there are comprehensive tests for FormIntent.OnSave handling in FormViewModelTest.kt and FormRepositoryImplTest.kt, there are no tests that verify the onFocusChanged callback is properly triggered and saves values when switching between text input fields.

Consider adding tests that verify:

  1. When focus changes from one text input to another, FormIntent.OnSave is called with the correct field UID and value
  2. No save is triggered when a field gains focus (only when it loses focus)
  3. The behavior works correctly for all affected input types

Copilot uses AI. Check for mistakes.
@andresmr andresmr force-pushed the ANDROAPP-7235 branch 3 times, most recently from 0ef032e to 7158b2b Compare December 16, 2025 13:32
@Balcan Balcan changed the title fix: [ANDROAPP-7235] Text base inputs not saving when clicking on other inputs fix: [ANDROAPP-7235] Text base inputs not saving when clicking on other inputs [skip size] Dec 18, 2025
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants