-
Notifications
You must be signed in to change notification settings - Fork 139
[Refactor] Convert date task fragment to compose #3552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bcce406
008ae4e
96161ea
23ac03d
8a6aac5
ef8afb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.groundplatform.android.ui.datacollection.tasks.date | ||
|
|
||
| import androidx.compose.foundation.interaction.MutableInteractionSource | ||
| import androidx.compose.foundation.interaction.PressInteraction | ||
| import androidx.compose.foundation.layout.Arrangement | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.height | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.width | ||
| import androidx.compose.material3.OutlinedTextField | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.testTag | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
| import org.groundplatform.android.ui.common.ExcludeFromJacocoGeneratedReport | ||
| import org.groundplatform.android.ui.theme.AppTheme | ||
|
|
||
| // TODO: Add trailing icon (close logo) for clearing selected date. | ||
|
|
||
| @Composable | ||
| fun DateTaskScreen( | ||
| dateText: String, | ||
| hintText: String, | ||
| onDateClick: () -> Unit, | ||
| modifier: Modifier = Modifier, | ||
| ) { | ||
| 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, | ||
| ) | ||
| } | ||
|
Comment on lines
+48
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation for handling clicks on the 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() },
)
} |
||
| } | ||
|
|
||
| @Preview(showBackground = true) | ||
| @Composable | ||
| @ExcludeFromJacocoGeneratedReport | ||
| fun DateTaskScreenPreview() { | ||
| AppTheme { | ||
| Column( | ||
| modifier = Modifier.padding(16.dp), | ||
| verticalArrangement = Arrangement.spacedBy(8.dp), | ||
| horizontalAlignment = Alignment.CenterHorizontally, | ||
| ) { | ||
| DateTaskScreen(dateText = "", hintText = "DD/MM/YYYY", onDateClick = {}) | ||
| Spacer(modifier = Modifier.height(10.dp)) | ||
| DateTaskScreen(dateText = "14/02/2026", hintText = "DD/MM/YYYY", onDateClick = {}) | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,16 +19,13 @@ import android.app.DatePickerDialog | |||||||||
| import android.view.View | ||||||||||
| import android.view.ViewGroup | ||||||||||
| import android.widget.LinearLayout | ||||||||||
| import androidx.compose.ui.test.assertIsDisplayed | ||||||||||
| import androidx.compose.ui.test.assertTextContains | ||||||||||
| import androidx.compose.ui.test.isDisplayed | ||||||||||
| import androidx.compose.ui.test.isNotDisplayed | ||||||||||
| import androidx.compose.ui.test.onNodeWithTag | ||||||||||
| import androidx.compose.ui.test.onNodeWithText | ||||||||||
| import androidx.test.espresso.Espresso.onView | ||||||||||
| import androidx.test.espresso.action.ViewActions.click | ||||||||||
| import androidx.test.espresso.assertion.ViewAssertions.matches | ||||||||||
| import androidx.test.espresso.matcher.ViewMatchers.isDisplayed | ||||||||||
| import androidx.test.espresso.matcher.ViewMatchers.isEnabled | ||||||||||
| import androidx.test.espresso.matcher.ViewMatchers.withId | ||||||||||
| import androidx.test.espresso.matcher.ViewMatchers.withText | ||||||||||
| import androidx.compose.ui.test.performClick | ||||||||||
| import com.google.common.truth.Truth.assertThat | ||||||||||
| import dagger.hilt.android.testing.BindValue | ||||||||||
| import dagger.hilt.android.testing.HiltAndroidTest | ||||||||||
|
|
@@ -46,9 +43,6 @@ import org.junit.runner.RunWith | |||||||||
| import org.mockito.Mock | ||||||||||
| import org.robolectric.RobolectricTestRunner | ||||||||||
|
|
||||||||||
| // TODO: Add a test for selecting a date and verifying response. | ||||||||||
| // Issue URL: https://github.com/google/ground-android/issues/2134 | ||||||||||
|
|
||||||||||
| @HiltAndroidTest | ||||||||||
| @RunWith(RobolectricTestRunner::class) | ||||||||||
| class DateTaskFragmentTest : BaseTaskFragmentTest<DateTaskFragment, DateTaskViewModel>() { | ||||||||||
|
|
@@ -94,10 +88,7 @@ class DateTaskFragmentTest : BaseTaskFragmentTest<DateTaskFragment, DateTaskView | |||||||||
| fun `default response is empty`() { | ||||||||||
| setupTaskFragment<DateTaskFragment>(job, task) | ||||||||||
|
|
||||||||||
| onView(withId(R.id.user_date_response_text)) | ||||||||||
| .check(matches(withText(""))) | ||||||||||
| .check(matches(isDisplayed())) | ||||||||||
| .check(matches(isEnabled())) | ||||||||||
| composeTestRule.onNodeWithTag("dateInputText").assertIsDisplayed().assertTextContains("M/D/YY") | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding the expected hint text 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
Suggested change
|
||||||||||
|
|
||||||||||
| runner().assertButtonIsDisabled("Next") | ||||||||||
| } | ||||||||||
|
|
@@ -113,7 +104,7 @@ class DateTaskFragmentTest : BaseTaskFragmentTest<DateTaskFragment, DateTaskView | |||||||||
| view?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1) | ||||||||||
|
|
||||||||||
| assertThat(fragment.getDatePickerDialog()).isNull() | ||||||||||
| onView(withId(R.id.user_date_response_text)).perform(click()) | ||||||||||
| composeTestRule.onNodeWithTag("dateInputText").performClick() | ||||||||||
| assertThat(fragment.getDatePickerDialog()).isNotNull() | ||||||||||
| assertThat(fragment.getDatePickerDialog()?.isShowing).isTrue() | ||||||||||
| } | ||||||||||
|
|
@@ -124,7 +115,7 @@ class DateTaskFragmentTest : BaseTaskFragmentTest<DateTaskFragment, DateTaskView | |||||||||
|
|
||||||||||
| val view: View? = fragment.view?.findViewById(R.id.task_container) | ||||||||||
| view?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1) | ||||||||||
| onView(withId(R.id.user_date_response_text)).perform(click()) | ||||||||||
| composeTestRule.onNodeWithTag("dateInputText").performClick() | ||||||||||
| assertThat(fragment.getDatePickerDialog()?.isShowing).isTrue() | ||||||||||
|
|
||||||||||
| val hardcodedYear = 2024 | ||||||||||
|
|
@@ -146,7 +137,7 @@ class DateTaskFragmentTest : BaseTaskFragmentTest<DateTaskFragment, DateTaskView | |||||||||
|
|
||||||||||
| val view: View? = fragment.view?.findViewById(R.id.task_container) | ||||||||||
| view?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1) | ||||||||||
| onView(withId(R.id.user_date_response_text)).perform(click()) | ||||||||||
| composeTestRule.onNodeWithTag("dateInputText").performClick() | ||||||||||
| assertThat(fragment.getDatePickerDialog()?.isShowing).isTrue() | ||||||||||
|
|
||||||||||
| val hardcodedYear = 2024 | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.