Skip to content

Commit

Permalink
Adds hint for date/time task fields (#2782)
Browse files Browse the repository at this point in the history
* addedhint

* nit revert

* fix with function

* locale fix
  • Loading branch information
anandwana001 authored Oct 8, 2024
1 parent ee40584 commit 85d53fc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
import com.google.android.ground.databinding.DateTaskFragBinding
import com.google.android.ground.model.submission.DateTimeTaskData
import com.google.android.ground.ui.datacollection.components.TaskView
import com.google.android.ground.ui.datacollection.components.TaskViewFactory
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskFragment
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
Expand All @@ -38,6 +40,7 @@ class DateTaskFragment : AbstractTaskFragment<DateTaskViewModel>() {
private var datePickerDialog: DatePickerDialog? = null

lateinit var dateText: LiveData<String>
lateinit var dateTextHint: LiveData<String>

override fun onTaskViewAttached() {
super.onTaskViewAttached()
Expand All @@ -54,6 +57,14 @@ class DateTaskFragment : AbstractTaskFragment<DateTaskViewModel>() {
}
}
.asLiveData()

dateTextHint =
MutableLiveData<String>().apply {
val dateFormat = DateFormat.getDateFormat(requireContext()) as SimpleDateFormat
val pattern = dateFormat.toPattern()
val hint = pattern.uppercase()
value = hint
}
}

override fun onCreateTaskView(inflater: LayoutInflater): TaskView =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
import com.google.android.ground.databinding.TimeTaskFragBinding
import com.google.android.ground.model.submission.DateTimeTaskData
import com.google.android.ground.ui.datacollection.components.TaskView
import com.google.android.ground.ui.datacollection.components.TaskViewFactory
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskFragment
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
Expand All @@ -38,6 +40,7 @@ class TimeTaskFragment : AbstractTaskFragment<TimeTaskViewModel>() {
private var timePickerDialog: TimePickerDialog? = null

lateinit var timeText: LiveData<String>
lateinit var timeTextHint: LiveData<String>

override fun onTaskViewAttached() {
super.onTaskViewAttached()
Expand All @@ -54,6 +57,18 @@ class TimeTaskFragment : AbstractTaskFragment<TimeTaskViewModel>() {
}
}
.asLiveData()

timeTextHint =
MutableLiveData<String>().apply {
val timeFormat = DateFormat.getTimeFormat(requireContext())
val hint =
if (timeFormat is SimpleDateFormat) {
timeFormat.toPattern().uppercase()
} else {
"HH:MM AM/PM" // Fallback hint if DateFormat is not SimpleDateFormat
}
value = hint
}
}

override fun onCreateTaskView(inflater: LayoutInflater): TaskView =
Expand Down
1 change: 1 addition & 0 deletions ground/src/main/res/layout/date_task_frag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
android:maxLines="1"
android:onClick="@{__ -> fragment.showDateDialog()}"
android:text="@{fragment.dateText}"
android:hint="@{fragment.dateTextHint}"
tools:text="@string/date" />
</com.google.android.material.textfield.TextInputLayout>
</layout>
1 change: 1 addition & 0 deletions ground/src/main/res/layout/time_task_frag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
android:maxLines="1"
android:onClick="@{__ -> fragment.showTimeDialog()}"
android:text="@{fragment.timeText}"
android:hint="@{fragment.timeTextHint}"
tools:text="@string/time" />
</com.google.android.material.textfield.TextInputLayout>
</layout>
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ class DateTaskFragmentTest : BaseTaskFragmentTest<DateTaskFragment, DateTaskView
assertThat(fragment.dateText.value).isEqualTo("10/10/24")
}

@Test
fun `hint text is visible`() {
setupTaskFragment<DateTaskFragment>(job, task)
assertThat(fragment.dateTextHint.value).isEqualTo("M/D/YY")
}

@Test
fun testActionButtons() {
setupTaskFragment<DateTaskFragment>(job, task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,10 @@ class TimeTaskFragmentTest : BaseTaskFragmentTest<TimeTaskFragment, TimeTaskView

runner().assertButtonIsDisabled("Next").assertButtonIsHidden("Skip")
}

@Test
fun `hint text is visible`() {
setupTaskFragment<TimeTaskFragment>(job, task)
assertThat(fragment.timeTextHint.value).isEqualTo("H:MM A")
}
}

0 comments on commit 85d53fc

Please sign in to comment.