[Refactor] Convert date task fragment to compose#3552
[Refactor] Convert date task fragment to compose#3552shobhitagarwal1612 wants to merge 6 commits intomasterfrom
Conversation
* Create `DateTaskScreen` Composable using `OutlinedTextField` to replace the XML-based layout. * Remove `date_task_frag.xml` and associated data binding logic from `DateTaskFragment`. * Update `DateTaskFragment` to use `createComposeView` and `collectAsStateWithLifecycle` for state management. * Refactor `DateTaskFragmentTest` to use `composeTestRule` for UI verification and interactions.
Summary of ChangesHello @shobhitagarwal1612, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly modernizes the date task component by migrating its user interface from the legacy Android View system to Jetpack Compose. This change streamlines the UI implementation, enhances maintainability, and updates the associated test suite to align with the new Compose-based interactions, ensuring continued functionality and reliability. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request successfully refactors the DateTaskFragment from using Android Views with data binding to Jetpack Compose. The XML layout has been removed and replaced with a new DateTaskScreen composable, and the fragment now hosts this composable. The corresponding tests have also been updated to use the Compose test framework instead of Espresso. The changes are well-executed and improve the maintainability of the code by adopting modern Android development practices. I have a couple of suggestions to make the code even more idiomatic and readable.
app/src/main/java/org/groundplatform/android/ui/datacollection/tasks/date/DateTaskFragment.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/org/groundplatform/android/ui/datacollection/tasks/date/DateTaskScreen.kt
Show resolved
Hide resolved
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request successfully refactors the DateTaskFragment from using XML layouts to Jetpack Compose, which is a great improvement. The code is now cleaner and more aligned with modern Android development practices. The related XML layout and string resources have been correctly removed, and the tests have been migrated to use Compose testing APIs.
I've added a few suggestions to further improve the code:
- In
DateTaskScreen.kt, I've suggested a more idiomatic way to handle clicks on the read-only text field. - In
DateTaskFragmentTest.kt, I've pointed out that hardcoding string literals in tests can make them brittle and suggested a way to make them more robust by generating the expected values dynamically.
| val interactionSource = remember { MutableInteractionSource() } | ||
| LaunchedEffect(interactionSource) { | ||
| interactionSource.interactions.collect { interaction -> | ||
| if (interaction is PressInteraction.Release) { | ||
| onDateClick() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Column(modifier = modifier) { | ||
| // TODO: Replace with simple text field. | ||
| OutlinedTextField( | ||
| value = dateText, | ||
| onValueChange = {}, | ||
| readOnly = true, | ||
| placeholder = { Text(hintText) }, | ||
| modifier = Modifier.width(200.dp).testTag("dateInputText"), | ||
| interactionSource = interactionSource, | ||
| ) | ||
| } |
There was a problem hiding this comment.
The current implementation for handling clicks on the OutlinedTextField using MutableInteractionSource and LaunchedEffect is more complex than necessary for this use case. A more idiomatic and simpler approach in Jetpack Compose is to use the Modifier.clickable directly on the OutlinedTextField. This simplifies the code, improves readability, and removes unnecessary boilerplate.
Column(modifier = modifier) {
// TODO: Replace with simple text field.
OutlinedTextField(
value = dateText,
onValueChange = {},
readOnly = true,
placeholder = { Text(hintText) },
modifier = Modifier
.width(200.dp)
.testTag("dateInputText")
.clickable { onDateClick() },
)
}| .check(matches(withText(""))) | ||
| .check(matches(isDisplayed())) | ||
| .check(matches(isEnabled())) | ||
| composeTestRule.onNodeWithTag("dateInputText").assertIsDisplayed().assertTextContains("M/D/YY") |
There was a problem hiding this comment.
Hardcoding the expected hint text "M/D/YY" makes this test brittle as it depends on the default locale of the test environment. It's better to dynamically generate the expected hint string in the same way it's generated in the DateTaskFragment, using DateFormat.getDateFormat(context). This will make the test more robust and less likely to fail if the locale changes.
You will need to add the following imports:
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import java.text.SimpleDateFormat
import android.text.format.DateFormat| composeTestRule.onNodeWithTag("dateInputText").assertIsDisplayed().assertTextContains("M/D/YY") | |
| val context = ApplicationProvider.getApplicationContext<Context>() | |
| val expectedHint = (DateFormat.getDateFormat(context) as SimpleDateFormat).toPattern().uppercase() | |
| composeTestRule.onNodeWithTag("dateInputText").assertIsDisplayed().assertTextContains(expectedHint) |
Towards #3549
Before:
date.task.-.compose.-.before.webm
After:
date.task.-.compose.-.after.webm
@andreia-ferreira PTAL?