Compare commits

..

3 Commits

Author SHA1 Message Date
de5b1d4ead Better number formatting 2024-11-24 10:39:14 +01:00
de75ed584b WIP detail view 2024-11-24 10:19:47 +01:00
a4b43ffb10 Alternate colorized rows in list 2024-11-24 09:46:39 +01:00
10 changed files with 216 additions and 78 deletions

View File

@ -30,6 +30,10 @@ import it.danieleverducci.lunatracker.repository.WebDAVLogbookRepository
import kotlinx.coroutines.Runnable
import okio.IOException
import org.json.JSONException
import utils.DateUtils
import utils.NumericUtils
import java.text.DateFormat
import java.util.Date
class MainActivity : AppCompatActivity() {
companion object {
@ -55,6 +59,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 +260,24 @@ class MainActivity : AppCompatActivity() {
alertDialog.show()
}
fun showEventDetailDialog(event: LunaEvent) {
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)
dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_emoji).setText(event.getTypeEmoji(this))
dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_description).setText(event.getTypeDescription(this))
dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_date).setText(dateFormat.format(Date(event.time * 1000)))
dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_quantity).setText(
NumericUtils(this).formatEventQuantity(event)
)
dialogView.findViewById<TextView>(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

View File

@ -1,9 +1,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,37 +8,18 @@ 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 java.util.Locale
import utils.DateUtils
import utils.NumericUtils
class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.LunaEventVH> {
private val context: Context
val items = ArrayList<LunaEvent>()
val measurement_unit_liquid_base: String
val measurement_unit_weight_base: String
val measurement_unit_weight_tiny: String
val numericUtils: NumericUtils
var onItemClickListener: OnItemClickListener? = null
constructor(context: Context) {
this.context = context
val measurementSystem = LocaleData.getMeasurementSystem(ULocale.getDefault())
this.measurement_unit_liquid_base = context.getString(
if (measurementSystem == LocaleData. MeasurementSystem.SI)
R.string.measurement_unit_liquid_base_metric
else
R.string.measurement_unit_liquid_base_imperial
)
this.measurement_unit_weight_base = context.getString(
if (measurementSystem == LocaleData. MeasurementSystem.SI)
R.string.measurement_unit_weight_base_metric
else
R.string.measurement_unit_weight_base_imperial
)
this.measurement_unit_weight_tiny = context.getString(
if (measurementSystem == LocaleData. MeasurementSystem.SI)
R.string.measurement_unit_weight_tiny_metric
else
R.string.measurement_unit_weight_tiny_imperial
)
this.numericUtils = NumericUtils(context)
}
override fun onCreateViewHolder(
@ -58,6 +36,11 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
position: Int
) {
val item = items.get(position)
// Colors
holder.root.setBackgroundResource(
if (position % 2 == 0) R.color.list_background_even else R.color.list_background_odd
)
// Contents
holder.type.text = item.getTypeEmoji(context)
holder.description.text = when(item.type) {
LunaEvent.TYPE_MEDICINE -> item.notes
@ -65,72 +48,37 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
LunaEvent.TYPE_CUSTOM -> item.notes
else -> item.getTypeDescription(context)
}
holder.time.text = 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
LunaEvent.TYPE_WEIGHT -> measurement_unit_weight_base
LunaEvent.TYPE_MEDICINE -> measurement_unit_weight_tiny
else -> ""
holder.time.text = DateUtils.formatTimeAgo(context, item.time)
holder.quantity.text = numericUtils.formatEventQuantity(item)
// Listeners
if (onItemClickListener != null) {
holder.root.setOnClickListener({
onItemClickListener?.onItemClick(item)
})
}
} else {
""
}
holder.quantity.text = qtyText
}
override fun getItemCount(): Int {
return items.size
}
/**
* 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()
}
class LunaEventVH: RecyclerView.ViewHolder {
val root: View
val type: TextView
val description: TextView
val quantity: TextView
val time: TextView
constructor(v: View) : super(v) {
root = v
type = v.findViewById<TextView>(R.id.type)
description = v.findViewById<TextView>(R.id.description)
quantity = v.findViewById<TextView>(R.id.quantity)
time = v.findViewById<TextView>(R.id.time)
}
}
interface OnItemClickListener {
fun onItemClick(event: LunaEvent)
}
}

View File

@ -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)
}

View File

@ -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()
}
}
}

View File

@ -0,0 +1,51 @@
package utils
import android.content.Context
import android.icu.util.LocaleData
import android.icu.util.ULocale
import it.danieleverducci.lunatracker.R
import it.danieleverducci.lunatracker.entities.LunaEvent
import java.text.NumberFormat
class NumericUtils (val context: Context) {
val numberFormat: NumberFormat
val measurement_unit_liquid_base: String
val measurement_unit_weight_base: String
val measurement_unit_weight_tiny: String
init {
this.numberFormat = NumberFormat.getInstance()
val measurementSystem = LocaleData.getMeasurementSystem(ULocale.getDefault())
this.measurement_unit_liquid_base = context.getString(
if (measurementSystem == LocaleData. MeasurementSystem.SI)
R.string.measurement_unit_liquid_base_metric
else
R.string.measurement_unit_liquid_base_imperial
)
this.measurement_unit_weight_base = context.getString(
if (measurementSystem == LocaleData. MeasurementSystem.SI)
R.string.measurement_unit_weight_base_metric
else
R.string.measurement_unit_weight_base_imperial
)
this.measurement_unit_weight_tiny = context.getString(
if (measurementSystem == LocaleData. MeasurementSystem.SI)
R.string.measurement_unit_weight_tiny_metric
else
R.string.measurement_unit_weight_tiny_imperial
)
}
fun formatEventQuantity(item: LunaEvent): String {
return if ((item.quantity ?: 0) > 0) {
numberFormat.format(item.quantity) + " " + when (item.type) {
LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base
LunaEvent.TYPE_WEIGHT -> measurement_unit_weight_base
LunaEvent.TYPE_MEDICINE -> measurement_unit_weight_tiny
else -> ""
}
} else {
""
}
}
}

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/dialog_event_detail_type_emoji"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="60dp"
android:text="@string/event_diaperchange_pee_type"/>
<TextView
android:id="@+id/dialog_event_detail_type_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:textColor="@color/accent"
android:textSize="24dp"
android:text="@string/event_diaperchange_pee_desc"/>
<TextView
android:id="@+id/dialog_event_detail_type_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="2020"/>
<TextView
android:id="@+id/dialog_event_detail_type_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="2020"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/dialog_event_detail_type_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="Lorem ipsum"/>
</ScrollView>
</LinearLayout>

View File

@ -3,12 +3,15 @@
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/type"
android:layout_width="80dp"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="28sp"
android:lines="1"
android:maxLines="1"

View File

@ -48,7 +48,7 @@
<string name="settings_storage_local">Sul dispositivo</string>
<string name="settings_storage_local_desc">La soluzione più privacy-friendly: i dati non escono mai dal tuo dispositivo</string>
<string name="settings_storage_dav">Su un server WebDAV</string>
<string name="settings_storage_dav_desc">Puoi usare un qualunque servizio che supporti WebDAV (come ad esempio Nextcloud, Owncloud, Dropbox, Box...) per salvare i dati. In questo modo puoi sincronizzarli tra più dispositivi, ad esempio quello del papà, della mamma, dei nonni... Ti servirà l\'url WebDAV, che trovi nella documentazione del tuo provider (ad es. in Nextcloud è nelle impostazioni di Files)</string>
<string name="settings_storage_dav_desc">Puoi usare un qualunque servizio che supporti WebDAV (come ad esempio Nextcloud, Owncloud, Dropbox, Box) per salvare i dati. In questo modo puoi sincronizzarli tra più dispositivi, ad esempio quello del papà, della mamma, dei nonni Ti servirà l\'url WebDAV, che trovi nella documentazione del tuo provider (ad es. in Nextcloud è nelle impostazioni di Files)</string>
<string name="settings_storage_dav_url">Url WebDAV</string>
<string name="settings_storage_dav_url_hint">https://</string>
<string name="settings_storage_dav_user">Username</string>
@ -71,4 +71,6 @@
<string name="log_medicine_dialog_description">Nome della medicina, quantità, formato, note…:</string>
<string name="log_notes_dialog_qty_hint">Quantità (numero intero), o lascia vuoto</string>
<string name="dialog_event_detail_title">Dettaglio evento</string>
</resources>

View File

@ -6,4 +6,6 @@
<color name="translucent">#c000</color>
<color name="transparent">#0000</color>
<color name="grey">#ccc</color>
<color name="list_background_odd">#423B25</color>
<color name="list_background_even">@color/transparent</color>
</resources>

View File

@ -90,4 +90,6 @@
<string name="measurement_unit_weight_base_imperial" translatable="false">oz</string>
<string name="measurement_unit_weight_tiny_imperial" translatable="false">gr</string>
<string name="dialog_event_detail_title">Event detail</string>
</resources>