From 85d53fcb1b513c3db911c7cc81150741d756c31f Mon Sep 17 00:00:00 2001
From: Akshay Nandwana <akshaynandwana001@gmail.com>
Date: Tue, 8 Oct 2024 09:59:57 +0530
Subject: [PATCH] Adds hint for date/time task fields (#2782)

* addedhint

* nit revert

* fix with function

* locale fix
---
 .../datacollection/tasks/date/DateTaskFragment.kt | 11 +++++++++++
 .../datacollection/tasks/time/TimeTaskFragment.kt | 15 +++++++++++++++
 ground/src/main/res/layout/date_task_frag.xml     |  1 +
 ground/src/main/res/layout/time_task_frag.xml     |  1 +
 .../tasks/date/DateTaskFragmentTest.kt            |  6 ++++++
 .../tasks/time/TimeTaskFragmentTest.kt            |  6 ++++++
 6 files changed, 40 insertions(+)

diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragment.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragment.kt
index e68d15f8d6..017518614a 100644
--- a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragment.kt
+++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragment.kt
@@ -20,6 +20,7 @@ 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
@@ -27,6 +28,7 @@ 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
@@ -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()
@@ -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 =
diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragment.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragment.kt
index 0d7a211d3e..0376b36216 100644
--- a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragment.kt
+++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragment.kt
@@ -20,6 +20,7 @@ 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
@@ -27,6 +28,7 @@ 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
@@ -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()
@@ -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 =
diff --git a/ground/src/main/res/layout/date_task_frag.xml b/ground/src/main/res/layout/date_task_frag.xml
index 12b46e9b00..58f20826ab 100644
--- a/ground/src/main/res/layout/date_task_frag.xml
+++ b/ground/src/main/res/layout/date_task_frag.xml
@@ -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>
\ No newline at end of file
diff --git a/ground/src/main/res/layout/time_task_frag.xml b/ground/src/main/res/layout/time_task_frag.xml
index 03353a79f5..7efaf4afa6 100644
--- a/ground/src/main/res/layout/time_task_frag.xml
+++ b/ground/src/main/res/layout/time_task_frag.xml
@@ -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>
\ No newline at end of file
diff --git a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragmentTest.kt b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragmentTest.kt
index 83cc7a1f39..c1b9644c85 100644
--- a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragmentTest.kt
+++ b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/date/DateTaskFragmentTest.kt
@@ -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)
diff --git a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragmentTest.kt b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragmentTest.kt
index 1440d913a3..8a31b838d3 100644
--- a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragmentTest.kt
+++ b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/time/TimeTaskFragmentTest.kt
@@ -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")
+  }
 }