diff --git a/app/src/main/java/fr/plnech/dunbar/Friend.kt b/app/src/main/java/fr/plnech/dunbar/Friend.kt index 35b547f..22c6466 100644 --- a/app/src/main/java/fr/plnech/dunbar/Friend.kt +++ b/app/src/main/java/fr/plnech/dunbar/Friend.kt @@ -2,18 +2,23 @@ package fr.plnech.dunbar import android.graphics.Bitmap import android.provider.ContactsContract.Contacts +import java.util.* data class Friend(val map: MutableMap, val photo: Bitmap?) { override fun toString(): String = "$name" + fun mapString(): String = map.entries.filter { !it.value.isNullOrEmpty() }.toString() + val name: String? get() = map[Contacts.DISPLAY_NAME] val id: Int get() = map[Contacts._ID]!!.toInt() - val lastTime: String? - get() = map[Contacts.LAST_TIME_CONTACTED] + val lastTimeStamp = map[Contacts.LAST_TIME_CONTACTED]!!.toLong() + + val lastDate: Date? + get() = if (lastTimeStamp > 0) Date(lastTimeStamp) else null val timesContacted: Int get() = map[Contacts.TIMES_CONTACTED]?.toInt() ?: 0 diff --git a/app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt b/app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt new file mode 100644 index 0000000..1e361ea --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt @@ -0,0 +1,48 @@ +package fr.plnech.dunbar + +import android.text.format.DateUtils +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Toast +import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.contact.view.* + + +class FriendsAdapter( + private var friends: List = mutableListOf() +) : RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FriendsViewHolder = + FriendsViewHolder(LayoutInflater.from(parent.context).inflate(viewType, parent, false)) + + override fun getItemCount(): Int = friends.size + + override fun getItemViewType(position: Int): Int = R.layout.contact + + override fun onBindViewHolder(holder: FriendsViewHolder, position: Int): Unit = + holder.bind(friends[position]) +} + +class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { + private val name = view.contactName + private val data = view.contactData + private val pic = view.pic + + fun bind(friend: Friend) { + name.text = friend.name + data.text = buildString { + if (friend.lastDate != null ) { + append(friend.timesContacted) + append(" interactions, last ") + append(DateUtils.getRelativeTimeSpanString(friend.lastTimeStamp)) + } else { + append("Never interacted") + } + } + view.setOnClickListener { + Toast.makeText(view.context, friend.mapString(), Toast.LENGTH_LONG).show() + } + } + +} diff --git a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt index 3f6ca05..c4becbf 100644 --- a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt @@ -8,10 +8,9 @@ import android.provider.ContactsContract import android.util.Log import android.view.Menu import android.view.MenuItem -import android.widget.ArrayAdapter import android.widget.Toast import androidx.appcompat.app.AppCompatActivity -import com.google.android.material.snackbar.Snackbar +import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.content_main.* import java.io.IOException @@ -27,11 +26,10 @@ class MainActivity : AppCompatActivity() { setSupportActionBar(toolbar) fab.setOnClickListener { view -> - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show() + reloadFriends() } - fetchFriends() + reloadFriends() } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -50,8 +48,15 @@ class MainActivity : AppCompatActivity() { } } - fun fetchFriends() { - Toast.makeText(this@MainActivity, "Fetching friends...", Toast.LENGTH_SHORT).show() + private fun reloadFriends() { + fetchFriends() + displayFriends() + + Toast.makeText(this, "Friends reloaded.", Toast.LENGTH_SHORT).show() + } + + private fun fetchFriends() { + val newFriends = mutableListOf() contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, @@ -62,41 +67,43 @@ class MainActivity : AppCompatActivity() { )?.let { while (it.moveToNext()) { mutableMapOf().also { map -> - - val indexID = it.getColumnIndex(ContactsContract.Contacts._ID) - val id = it.getLong(indexID) + val indexId = it.getColumnIndex(ContactsContract.Contacts._ID) + val id = it.getLong(indexId) val photo: Bitmap? = getPhoto(id) it.columnNames.forEach { name -> val index = it.getColumnIndex(name) map[name] = it.getString(index) } - friends.add(Friend(map, photo)) + newFriends.add(Friend(map, photo)) } } + it.close() - val adapter = ArrayAdapter( - this, R.layout.contact, R.id.name, - friends - ) - - friendsList.adapter = adapter - friendsList.setOnItemClickListener { parent, view, position, id -> - val contact: String = adapter.getItem(position)?.toString() ?: "NONE" - -// Snackbar.make(friendsList, contact, Snackbar.LENGTH_INDEFINITE).show() - Toast.makeText(this, contact, Toast.LENGTH_LONG).show() - } + friends.clear() + friends.addAll(newFriends) + } - val nbFriends = adapter.count - welcomeTitle.text = "$nbFriends ${"friend".plural(nbFriends)} on Dunbar" + friends.sortByDescending { it.timesContacted } + } - Toast.makeText(this@MainActivity, "$nbFriends ", Toast.LENGTH_LONG).show() + private fun displayFriends() { + val adapter = FriendsAdapter(friends) - it.close() - } + friendsList.layoutManager = LinearLayoutManager(this) + friendsList.setHasFixedSize(true) + friendsList.adapter = adapter +// friendsList.setOnItemClickListener { parent, view, position, id -> +// val contact: String = adapter.getItem(position)?.toString() ?: "NONE" +// +// Snackbar.make(friendsList, contact, Snackbar.LENGTH_INDEFINITE).show() +// Toast.makeText(this, contact, Toast.LENGTH_LONG).show() +// } + val nbFriends = adapter.itemCount + welcomeTitle.text = "$nbFriends ${"friend".plural(nbFriends)} on Dunbar" + Toast.makeText(this@MainActivity, "$nbFriends ", Toast.LENGTH_LONG).show() } private fun getPhoto(id: Long): Bitmap? { diff --git a/app/src/main/res/layout/contact.xml b/app/src/main/res/layout/contact.xml index 3a7ee55..669d97f 100644 --- a/app/src/main/res/layout/contact.xml +++ b/app/src/main/res/layout/contact.xml @@ -4,7 +4,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent" + android:layout_height="wrap_content" android:elevation="5dp"> diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index b6e750a..3e769d4 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -10,17 +10,19 @@ + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.0" + tools:text="N friends on Dunbar" /> -