From b02a6c9aadad2a705ca92b813b29664eafd98110 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Wed, 11 Dec 2024 08:43:14 +0100 Subject: [PATCH] Check on weight and red color on weight loss, fixed layout on small screen or screens with high font size --- .../adapters/LunaEventRecyclerAdapter.kt | 44 +++++++++++++- app/src/main/res/layout/activity_main.xml | 5 +- app/src/main/res/layout/row_luna_event.xml | 5 +- .../res/layout/row_luna_event_vertical.xml | 59 +++++++++++++++++++ app/src/main/res/values/colors.xml | 2 + app/src/main/res/values/themes.xml | 1 + 6 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/layout/row_luna_event_vertical.xml 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 2264660..7d1ab68 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt @@ -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() 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 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 diff --git a/app/src/main/res/layout/row_luna_event.xml b/app/src/main/res/layout/row_luna_event.xml index 77daf46..e656076 100644 --- a/app/src/main/res/layout/row_luna_event.xml +++ b/app/src/main/res/layout/row_luna_event.xml @@ -24,6 +24,7 @@ android:layout_weight="2" android:ellipsize="end" android:maxLines="2" + android:textColor="@color/accent" android:text="Description"/> - \ No newline at end of file + diff --git a/app/src/main/res/layout/row_luna_event_vertical.xml b/app/src/main/res/layout/row_luna_event_vertical.xml new file mode 100644 index 0000000..faee092 --- /dev/null +++ b/app/src/main/res/layout/row_luna_event_vertical.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index b9baeba..b21425f 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -8,4 +8,6 @@ #ccc #423B25 @color/transparent + #f00 + @color/grey \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 561038d..c68ef34 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -3,6 +3,7 @@