From 968f4206cc6db1d15f47741a3cd30def241eedb5 Mon Sep 17 00:00:00 2001 From: fgerber Date: Thu, 7 Mar 2024 12:37:17 +0100 Subject: [PATCH] Add counter for group view --- .../activity/adapter/GroupListAdapter.kt | 19 +++++++++++++++---- .../net/helcel/beendroid/helper/Visits.kt | 4 ++++ app/src/main/res/layout/item_list_group.xml | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt b/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt index b25f57d..d09178a 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt @@ -4,6 +4,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button +import android.widget.TextView import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentActivity import androidx.recyclerview.widget.RecyclerView @@ -34,18 +35,28 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec inner class GroupViewHolder(itemView: View, private val activity: FragmentActivity, private val selectDialog: DialogFragment?) : RecyclerView.ViewHolder(itemView) { private val color: Button = itemView.findViewById(R.id.group_color) + private val entries: TextView = itemView.findViewById(R.id.name) fun bind(entry: Pair) { color.text = entry.second.name - color.setBackgroundColor(entry.second.color.color) - color.setTextColor(getContrastColor(entry.second.color.color)) + val entryColor = entry.second.color.color + val contrastEntryColor = getContrastColor(entryColor) + color.setBackgroundColor(entryColor) + color.setTextColor(contrastEntryColor) + entries.setTextColor(contrastEntryColor) + entries.text = visits!!.countVisited(entry.first).toString() + color.setOnClickListener { if (selectDialog == null) { val dialogFragment = EditGroupAddFragment(entry.first) { val newEntry = groups!!.getGroupFromKey(entry.first)!! color.text = newEntry.name - color.setBackgroundColor(newEntry.color.color) - color.setTextColor(getContrastColor(newEntry.color.color)) + val newEntryColor = newEntry.color.color + val contrastNewEntryColor = getContrastColor(newEntryColor) + color.setBackgroundColor(newEntryColor) + color.setTextColor(contrastNewEntryColor) + entries.setTextColor(contrastNewEntryColor) + entries.text = "0" } dialogFragment.show( activity.supportFragmentManager, diff --git a/app/src/main/java/net/helcel/beendroid/helper/Visits.kt b/app/src/main/java/net/helcel/beendroid/helper/Visits.kt index bfcd9bb..6922181 100644 --- a/app/src/main/java/net/helcel/beendroid/helper/Visits.kt +++ b/app/src/main/java/net/helcel/beendroid/helper/Visits.kt @@ -28,6 +28,10 @@ class Visits(val id: Int, private val locs: HashMap) { return locs.getOrDefault(key.code,0) } + fun countVisited(key: Int): Int { + return locs.filter { it.value == key }.size + } + @OptIn(ExperimentalSerializationApi::class) @Serializer(Visits::class) class VisitsSerializer { diff --git a/app/src/main/res/layout/item_list_group.xml b/app/src/main/res/layout/item_list_group.xml index d6f90dc..0d62230 100644 --- a/app/src/main/res/layout/item_list_group.xml +++ b/app/src/main/res/layout/item_list_group.xml @@ -16,11 +16,25 @@ android:layout_marginTop="2dp" android:layout_marginEnd="32dp" android:layout_marginBottom="2dp" + android:textAlignment="textStart" + android:textColor="?attr/colorOnPrimary" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + +