forked from penguin86/luna-tracker
DateUtils: move event details formatting to DateUtils
Also do not display seconds, because it is not meaningful and is not selected in date picker.
This commit is contained in:
@@ -41,7 +41,6 @@ import okio.IOException
|
|||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import utils.DateUtils
|
import utils.DateUtils
|
||||||
import utils.NumericUtils
|
import utils.NumericUtils
|
||||||
import java.text.DateFormat
|
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
@@ -360,7 +359,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
fun showEventDetailDialog(event: LunaEvent, items: ArrayList<LunaEvent>) {
|
fun showEventDetailDialog(event: LunaEvent, items: ArrayList<LunaEvent>) {
|
||||||
// Do not update list while the detail is shown, to avoid changing the object below while it is changed by the user
|
// Do not update list while the detail is shown, to avoid changing the object below while it is changed by the user
|
||||||
pauseLogbookUpdate = true
|
pauseLogbookUpdate = true
|
||||||
val dateFormat = DateFormat.getDateTimeInstance()
|
|
||||||
val d = AlertDialog.Builder(this)
|
val d = AlertDialog.Builder(this)
|
||||||
d.setTitle(R.string.dialog_event_detail_title)
|
d.setTitle(R.string.dialog_event_detail_title)
|
||||||
val dialogView = layoutInflater.inflate(R.layout.dialog_event_detail, null)
|
val dialogView = layoutInflater.inflate(R.layout.dialog_event_detail, null)
|
||||||
@@ -372,8 +370,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val currentDateTime = Calendar.getInstance()
|
val currentDateTime = Calendar.getInstance()
|
||||||
currentDateTime.time = Date(event.time * 1000)
|
currentDateTime.time = Date(event.time * 1000)
|
||||||
|
|
||||||
val dateTextView = dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_date)
|
val dateTextView = dialogView.findViewById<TextView>(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 {
|
dateTextView.setOnClickListener {
|
||||||
// Show datetime picker
|
// Show datetime picker
|
||||||
val startYear = currentDateTime.get(Calendar.YEAR)
|
val startYear = currentDateTime.get(Calendar.YEAR)
|
||||||
@@ -386,11 +385,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
TimePickerDialog(this, { _, hour, minute ->
|
TimePickerDialog(this, { _, hour, minute ->
|
||||||
val pickedDateTime = Calendar.getInstance()
|
val pickedDateTime = Calendar.getInstance()
|
||||||
pickedDateTime.set(year, month, day, hour, minute)
|
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
|
// 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()
|
logbook?.sort()
|
||||||
recyclerView.adapter?.notifyDataSetChanged()
|
recyclerView.adapter?.notifyDataSetChanged()
|
||||||
saveLogbook()
|
saveLogbook()
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
import android.text.format.DateFormat
|
import android.text.format.DateFormat
|
||||||
import it.danieleverducci.lunatracker.R
|
import it.danieleverducci.lunatracker.R
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
class DateUtils {
|
class DateUtils {
|
||||||
companion object {
|
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 {
|
fun formatTimeDuration(context: Context, secondsDiff: Long): String {
|
||||||
var seconds = secondsDiff
|
var seconds = secondsDiff
|
||||||
|
|
||||||
@@ -65,7 +70,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 {
|
fun formatTimeAgo(context: Context, unixTime: Long): String {
|
||||||
val secondsDiff = (System.currentTimeMillis() / 1000) - unixTime
|
val secondsDiff = (System.currentTimeMillis() / 1000) - unixTime
|
||||||
@@ -100,5 +106,21 @@ class DateUtils {
|
|||||||
}
|
}
|
||||||
return formattedTime.toString()
|
return formattedTime.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format time as localized string without seconds. E.g. "Sept 18, 2025, 03:36 PM".
|
||||||
|
* Used in the event detail dialog.
|
||||||
|
*/
|
||||||
|
fun formatDateTime(unixTime: Long): String {
|
||||||
|
val date = Date(unixTime * 1000)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
val dateFormat = android.icu.text.DateFormat.getDateTimeInstance(android.icu.text.DateFormat.RELATIVE_SHORT, android.icu.text.DateFormat.SHORT)
|
||||||
|
return dateFormat.format(date)
|
||||||
|
} else {
|
||||||
|
// fallback
|
||||||
|
val dateFormat = java.text.DateFormat.getDateTimeInstance()
|
||||||
|
return dateFormat.format(date)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user