From 58e90f8477fafebc8e32f187eaf232443a695247 Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Sat, 2 Nov 2019 18:44:23 +0100 Subject: [PATCH] feat: Cleaner adapter/detail --- TODO.md | 2 ++ app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt | 57 ++++++++++++++++++++++++++++++++++++++++----------------- app/src/main/res/layout/contact.xml | 44 +++++++++++++++++++++++++++++--------------- 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/TODO.md b/TODO.md index 3633855..25a9ca9 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,8 @@ - List friends - Remind daily + - Select best friend to remind of + - Keep track of reminded? - Stats - Quick increment diff --git a/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt index beafb05..4af11de 100644 --- a/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt @@ -37,38 +37,61 @@ class FriendsAdapter( class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { private val name = view.contactName private val data = view.contactData + private val phone = view.contactPhone + private val callButton = view.contactCall + private val smsButton = view.contactText private val pic = view.pic fun bind(friend: Friend) { name.text = friend.name - data.text = buildString { - if (friend.lastDate != null ) { + data.text = buildDataString(friend) + bindPhone(friend) + + bindPic(friend) + bindClick(friend) + } + + private fun buildDataString(friend: Friend): String { + return buildString { + if (friend.lastDate != null) { append(friend.timesContacted) append(" interactions, last ") append(DateUtils.getRelativeTimeSpanString(friend.lastTimeStamp)) } else { append("Never interacted") } - friend.phone?.let { - view.contactCall.visibility = View.VISIBLE - view.contactCall.setOnClickListener { - view.context.startActivity(friend.callIntent()) - } - view.contactText.visibility = View.VISIBLE - view.contactText.setOnClickListener { - view.context.startActivity(friend.smsIntent()) - } - - append("\n$it") - } - friend.photo?.let { - pic.setImageBitmap(it) - } } + } + + private fun bindClick(friend: Friend) { view.setOnClickListener { Toast.makeText(view.context, friend.mapString(), Toast.LENGTH_LONG).show() println(friend.mapString()) } } + private fun bindPic(friend: Friend) { + friend.photo?.let { + pic.setImageBitmap(it) + } + } + + private fun bindPhone(friend: Friend) { + friend.phone?.let { + phone.text = it + + mapOf( + callButton to friend.callIntent(), + smsButton to friend.smsIntent() + ).entries.forEach { pair -> + pair.key.apply { + visibility = View.VISIBLE + setOnClickListener { + view.context.startActivity(pair.value) + } + } + } + } + } + } diff --git a/app/src/main/res/layout/contact.xml b/app/src/main/res/layout/contact.xml index 00741e1..0f066fd 100644 --- a/app/src/main/res/layout/contact.xml +++ b/app/src/main/res/layout/contact.xml @@ -24,7 +24,7 @@ android:elevation="2dp" android:src="@drawable/user" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintRight_toLeftOf="@id/contactName" + app:layout_constraintEnd_toStartOf="@id/contactName" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -40,8 +40,7 @@ android:textAppearance="@style/TextAppearance.AppCompat.Title" app:layout_constraintBottom_toTopOf="@id/contactData" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintLeft_toRightOf="@id/pic" - app:layout_constraintRight_toLeftOf="@id/contactName" + app:layout_constraintStart_toEndOf="@id/pic" app:layout_constraintTop_toTopOf="parent" tools:text="@tools:sample/full_names" /> @@ -50,6 +49,21 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="15dp" + android:focusable="false" + android:focusableInTouchMode="false" + android:gravity="end" + android:textAppearance="@style/TextAppearance.AppCompat.Subhead" + app:layout_constraintBottom_toTopOf="@id/contactPhone" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toEndOf="@id/pic" + app:layout_constraintTop_toBottomOf="@id/contactName" + tools:text="2 interactions, last 1 hour ago" /> + + + app:layout_constraintStart_toEndOf="@+id/pic" + app:layout_constraintTop_toBottomOf="@id/contactData" + tools:text="0213 456" /> + app:layout_constraintEnd_toStartOf="@id/contactCall" + app:layout_constraintStart_toEndOf="@id/pic" + app:layout_constraintTop_toBottomOf="@id/contactPhone" + tools:visibility="visible" /> + app:layout_constraintStart_toEndOf="@id/contactText" + app:layout_constraintTop_toBottomOf="@id/contactPhone" + tools:visibility="visible" /> -- libgit2 0.27.0