From ff0d4df2de4af5b2ef4e6842a840892f3b175438 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Mon, 29 Sep 2025 03:29:50 +0200 Subject: [PATCH] DateUtils: move event details formatting to DateUtils Also display second as 0 since it is easier to read and does not have meaning for the user. --- .../lunatracker/MainActivity.kt | 11 ++++------- app/src/main/java/utils/DateUtils.kt | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 50d65b5..c853d40 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -41,7 +41,6 @@ import okio.IOException import org.json.JSONException import utils.DateUtils import utils.NumericUtils -import java.text.DateFormat import java.util.Calendar import java.util.Date @@ -340,7 +339,6 @@ class MainActivity : AppCompatActivity() { fun showEventDetailDialog(event: LunaEvent, items: ArrayList) { // Do not update list while the detail is shown, to avoid changing the object below while it is changed by the user pauseLogbookUpdate = true - val dateFormat = DateFormat.getDateTimeInstance() val d = AlertDialog.Builder(this) d.setTitle(R.string.dialog_event_detail_title) val dialogView = layoutInflater.inflate(R.layout.dialog_event_detail, null) @@ -352,8 +350,9 @@ class MainActivity : AppCompatActivity() { val currentDateTime = Calendar.getInstance() currentDateTime.time = Date(event.time * 1000) + val dateTextView = dialogView.findViewById(R.id.dialog_event_detail_type_date) - dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), dateFormat.format(currentDateTime.time)) + dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) dateTextView.setOnClickListener { // Show datetime picker val startYear = currentDateTime.get(Calendar.YEAR) @@ -366,11 +365,9 @@ class MainActivity : AppCompatActivity() { TimePickerDialog(this, { _, hour, minute -> val pickedDateTime = Calendar.getInstance() pickedDateTime.set(year, month, day, hour, minute) - currentDateTime.time = pickedDateTime.time - dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), dateFormat.format(currentDateTime.time)) - // Save event and move it to the right position in the logbook - event.time = currentDateTime.time.time / 1000 // Seconds since epoch + event.time = pickedDateTime.time.time / 1000 // Seconds since epoch + dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) logbook?.sort() recyclerView.adapter?.notifyDataSetChanged() saveLogbook() diff --git a/app/src/main/java/utils/DateUtils.kt b/app/src/main/java/utils/DateUtils.kt index 25803da..59a310f 100644 --- a/app/src/main/java/utils/DateUtils.kt +++ b/app/src/main/java/utils/DateUtils.kt @@ -7,6 +7,10 @@ import java.util.Date class DateUtils { companion object { + /** + * Format time duration in seconds as e.g. "2 hours, 1 min". + * Used for the duration to the next/previous event in the event details dialog. + */ fun formatTimeDuration(context: Context, secondsDiff: Long): String { var seconds = secondsDiff @@ -65,7 +69,8 @@ class DateUtils { } /** - * Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago) + * Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago". + * Used for the event list. */ fun formatTimeAgo(context: Context, unixTime: Long): String { val secondsDiff = (System.currentTimeMillis() / 1000) - unixTime @@ -100,5 +105,17 @@ class DateUtils { } return formattedTime.toString() } + + /** + * Format time as localized string. E.g. "28 Sept 03:36:00". + * The seconds are set to 0 since they are distracting and not relevant. + * Used in the event detail dialog. + */ + fun formatDateTime(unixTime: Long): String { + val roundedUnixTime = unixTime - (unixTime % 60) + val date = Date(roundedUnixTime * 1000) + val dateFormat = java.text.DateFormat.getDateTimeInstance() + return dateFormat.format(date) + } } } \ No newline at end of file