diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 2bcc986..82db47a 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -30,6 +30,9 @@ import it.danieleverducci.lunatracker.repository.WebDAVLogbookRepository import kotlinx.coroutines.Runnable import okio.IOException import org.json.JSONException +import utils.DateUtils +import java.text.DateFormat +import java.util.Date class MainActivity : AppCompatActivity() { companion object { @@ -55,6 +58,11 @@ class MainActivity : AppCompatActivity() { handler = Handler(mainLooper) adapter = LunaEventRecyclerAdapter(this) + adapter.onItemClickListener = object: LunaEventRecyclerAdapter.OnItemClickListener{ + override fun onItemClick(event: LunaEvent) { + showEventDetailDialog(event) + } + } // Show view setContentView(R.layout.activity_main) @@ -251,6 +259,22 @@ class MainActivity : AppCompatActivity() { alertDialog.show() } + fun showEventDetailDialog(event: LunaEvent) { + val dateFormat = DateFormat.getDateTimeInstance(); + val d = AlertDialog.Builder(this) + d.setTitle(event.getTypeDescription(this)) + val dialogView = layoutInflater.inflate(R.layout.dialog_event_detail, null) + dialogView.findViewById(R.id.dialog_event_detail_type_emoji).setText(event.getTypeEmoji(this)) + dialogView.findViewById(R.id.dialog_event_detail_type_description).setText(event.getTypeDescription(this)) + dialogView.findViewById(R.id.dialog_event_detail_type_date).setText(dateFormat.format(Date(event.time * 1000))) + dialogView.findViewById(R.id.dialog_event_detail_type_quantity).setText(event.quantity.toString()) + dialogView.findViewById(R.id.dialog_event_detail_type_notes).setText(event.notes) + d.setView(dialogView) + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> dialogInterface.dismiss() } + val alertDialog = d.create() + alertDialog.show() + } + fun loadLogbook() { if (savingEvent) return diff --git a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt index 4058fd3..82b4212 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt @@ -3,7 +3,6 @@ package it.danieleverducci.lunatracker.adapters import android.content.Context import android.icu.util.LocaleData import android.icu.util.ULocale -import android.text.format.DateFormat import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -11,7 +10,7 @@ import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import it.danieleverducci.lunatracker.entities.LunaEvent import it.danieleverducci.lunatracker.R -import java.util.Date +import utils.DateUtils class LunaEventRecyclerAdapter: RecyclerView.Adapter { private val context: Context @@ -19,6 +18,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter item.notes @@ -67,7 +69,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter item.notes else -> item.getTypeDescription(context) } - holder.time.text = formatTimeAgo(context, item.time) + holder.time.text = DateUtils.formatTimeAgo(context, item.time) val qtyText = if ((item.quantity ?: 0) > 0) { item.quantity.toString() + " " + when (item.type) { LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base @@ -79,49 +81,18 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter 24) - return DateFormat.getDateFormat(context).format(Date(unixTime*1000)) + "\n" + - DateFormat.getTimeFormat(context).format(Date(unixTime*1000)) - - var formattedTime = StringBuilder() - if (hoursAgo > 0) { - formattedTime.append(hoursAgo).append(" ") - if (hoursAgo.toInt() == 1) - formattedTime.append(context.getString(R.string.hour_ago)) - else - formattedTime.append(context.getString(R.string.hours_ago)) - } - if (minutesAgo > 0) { - if (formattedTime.isNotEmpty()) - formattedTime.append(", ") - formattedTime.append(minutesAgo).append(" ") - if (minutesAgo.toInt() == 1) - formattedTime.append(context.getString(R.string.minute_ago)) - else - formattedTime.append(context.getString(R.string.minutes_ago)) - } - return formattedTime.toString() - } - class LunaEventVH: RecyclerView.ViewHolder { val root: View val type: TextView @@ -137,4 +108,8 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter(R.id.time) } } + + interface OnItemClickListener { + fun onItemClick(event: LunaEvent) + } } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt b/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt index 3b6ec5f..475104d 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt @@ -46,7 +46,7 @@ class LunaEvent { jo.put("quantity", value) } var notes: String - get(): String = jo.getString("notes") + get(): String = jo.optString("notes") set(value) { jo.put("notes", value) } diff --git a/app/src/main/java/utils/DateUtils.kt b/app/src/main/java/utils/DateUtils.kt new file mode 100644 index 0000000..b192f25 --- /dev/null +++ b/app/src/main/java/utils/DateUtils.kt @@ -0,0 +1,48 @@ +package utils + +import android.content.Context +import android.text.format.DateFormat +import it.danieleverducci.lunatracker.R +import java.util.Date + +class DateUtils { + companion object { + + /** + * Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago) + */ + fun formatTimeAgo(context: Context, unixTime: Long): String { + val secondsDiff = (System.currentTimeMillis() / 1000) - unixTime + val minutesDiff = secondsDiff / 60 + + if (minutesDiff < 1) + return context.getString(R.string.now) + + val hoursAgo = (secondsDiff / (60 * 60)).toInt() + val minutesAgo = (minutesDiff % 60).toInt() + + if (hoursAgo > 24) + return DateFormat.getDateFormat(context).format(Date(unixTime*1000)) + "\n" + + DateFormat.getTimeFormat(context).format(Date(unixTime*1000)) + + var formattedTime = StringBuilder() + if (hoursAgo > 0) { + formattedTime.append(hoursAgo).append(" ") + if (hoursAgo.toInt() == 1) + formattedTime.append(context.getString(R.string.hour_ago)) + else + formattedTime.append(context.getString(R.string.hours_ago)) + } + if (minutesAgo > 0) { + if (formattedTime.isNotEmpty()) + formattedTime.append(", ") + formattedTime.append(minutesAgo).append(" ") + if (minutesAgo.toInt() == 1) + formattedTime.append(context.getString(R.string.minute_ago)) + else + formattedTime.append(context.getString(R.string.minutes_ago)) + } + return formattedTime.toString() + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_event_detail.xml b/app/src/main/res/layout/dialog_event_detail.xml new file mode 100644 index 0000000..da8a468 --- /dev/null +++ b/app/src/main/res/layout/dialog_event_detail.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file