Check on weight and red color on weight loss, fixed layout on small screen or screens with high font size

This commit is contained in:
Daniele Verducci 2024-12-11 08:43:14 +01:00
parent 0d331d90ff
commit b02a6c9aad
6 changed files with 107 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import it.danieleverducci.lunatracker.entities.LunaEvent
import it.danieleverducci.lunatracker.R
@ -16,18 +17,26 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
val items = ArrayList<LunaEvent>()
val numericUtils: NumericUtils
var onItemClickListener: OnItemClickListener? = null
val layoutRes: Int
constructor(context: Context) {
this.context = context
this.numericUtils = NumericUtils(context)
val fontScale = context.resources.configuration.fontScale
val screenSize = context.resources.configuration.screenWidthDp
this.layoutRes =
if(fontScale > 1.2 || screenSize < 320 || (fontScale > 1 && screenSize < 400))
R.layout.row_luna_event_vertical
else
R.layout.row_luna_event
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): LunaEventVH {
val view = LayoutInflater.from(context).inflate(R.layout.row_luna_event, parent, false)
val view = LayoutInflater.from(context).inflate(layoutRes, parent, false)
return LunaEventVH(view)
}
@ -40,6 +49,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
holder.root.setBackgroundResource(
if (position % 2 == 0) R.color.list_background_even else R.color.list_background_odd
)
holder.quantity.setTextColor(ContextCompat.getColor(context, R.color.textColor))
// Contents
holder.type.text = item.getTypeEmoji(context)
holder.description.text = when(item.type) {
@ -49,7 +59,23 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
else -> item.getTypeDescription(context)
}
holder.time.text = DateUtils.formatTimeAgo(context, item.time)
holder.quantity.text = numericUtils.formatEventQuantity(item)
var quantityText = numericUtils.formatEventQuantity(item)
// if the event is weight, show difference with the last one
if (item.type == LunaEvent.TYPE_WEIGHT) {
val lastWeight = getPreviousWeightEvent(position)
if (lastWeight != null) {
val differenceInWeight = item.quantity - lastWeight.quantity
val sign = if (differenceInWeight > 0) "+" else ""
quantityText += "\n($sign$differenceInWeight)"
if (differenceInWeight < 0) {
holder.quantity.setTextColor(ContextCompat.getColor(context, R.color.danger))
}
}
}
holder.quantity.text = quantityText
// Listeners
if (onItemClickListener != null) {
holder.root.setOnClickListener({
@ -62,6 +88,18 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
return items.size
}
private fun getPreviousWeightEvent(startFromPosition: Int): LunaEvent? {
if (startFromPosition == items.size - 1)
return null
for (pos in startFromPosition + 1 until items.size) {
val item = items.get(pos)
if (item.type != LunaEvent.TYPE_WEIGHT)
continue
return item
}
return null
}
class LunaEventVH: RecyclerView.ViewHolder {
val root: View
val type: TextView

View File

@ -6,7 +6,6 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingTop="30dp"
android:paddingLeft="15dp"
@ -16,7 +15,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:textSize="30sp"
android:textSize="30dp"
android:gravity="center_horizontal"/>
<TextView
@ -152,7 +151,7 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diario di bordo"
android:text="@string/logbook"
android:textColor="@color/accent"
android:textStyle="bold"/>

View File

@ -24,6 +24,7 @@
android:layout_weight="2"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/accent"
android:text="Description"/>
<TextView
@ -34,8 +35,6 @@
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center_horizontal"
android:lines="1"
android:maxLines="1"
android:text="Qty"/>
<TextView
@ -49,4 +48,4 @@
android:maxLines="2"
android:text="Time"/>
</LinearLayout>
</LinearLayout>

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="90dp"
android:layout_height="wrap_content"
android:textSize="28sp"
android:lines="1"
android:maxLines="1"
android:text="@string/event_diaperchange_pee_type"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ellipsize="end"
android:maxLines="2"
android:gravity="center_horizontal"
android:textColor="@color/accent"
android:text="Description"/>
<TextView
android:id="@+id/quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center_horizontal"
android:text="Qty"/>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_horizontal"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="2"
android:text="Time"/>
</LinearLayout>
</LinearLayout>

View File

@ -8,4 +8,6 @@
<color name="grey">#ccc</color>
<color name="list_background_odd">#423B25</color>
<color name="list_background_even">@color/transparent</color>
<color name="danger">#f00</color>
<color name="textColor">@color/grey</color>
</resources>

View File

@ -3,6 +3,7 @@
<style name="Theme.LunaTracker" parent="Theme.AppCompat.NoActionBar">
<item name="colorAccent">@color/accent</item>
<item name="android:textColor">@color/textColor</item>
</style>
<style name="OverflowMenuText">