diff --git a/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt index 1bca735..dcea39e 100644 --- a/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt @@ -28,13 +28,11 @@ class FriendDetailFragment : Fragment() { super.onCreate(savedInstanceState) arguments?.let { - println("Got args!") if (it.containsKey(ARG_FRIEND_ID)) { // Load the dummy content specified by the fragment // arguments. In a real-world scenario, use a Loader // to load content from a content provider. item = DummyContent.ITEM_MAP[it.getInt(ARG_FRIEND_ID)] - println("item: $item") activity?.toolbar_layout?.title = item?.name } } @@ -46,8 +44,6 @@ class FriendDetailFragment : Fragment() { ): View? { val rootView = inflater.inflate(R.layout.friend_detail, container, false) item?.let { - println("Got friend: ${it.name}") - activity?.title = it.name rootView.apply { friendData.text = it.mapString() } diff --git a/app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt index a775b7a..47c95c8 100644 --- a/app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt @@ -2,21 +2,17 @@ package fr.plnech.dunbar.ui import android.content.Intent import android.os.Bundle -import android.view.* -import android.widget.TextView +import android.view.Menu +import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView import com.google.android.material.snackbar.Snackbar import fr.plnech.dunbar.R import fr.plnech.dunbar.dummy.DummyContent -import fr.plnech.dunbar.fetchContacts import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.plural import kotlinx.android.synthetic.main.activity_friends_list.* -import kotlinx.android.synthetic.main.content_contacts.* import kotlinx.android.synthetic.main.friend_list.* -import kotlinx.android.synthetic.main.friend_list_content.view.* class FriendListActivity : AppCompatActivity() { @@ -31,12 +27,11 @@ class FriendListActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_friends_list) - setSupportActionBar(toolbar) toolbar.title = title fab.setOnClickListener { view -> - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) + Snackbar.make(view, "Replace with your own stuff", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } @@ -48,7 +43,7 @@ class FriendListActivity : AppCompatActivity() { twoPane = true } - setupRecyclerView(friend_list) + reloadFriends() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -71,86 +66,23 @@ class FriendListActivity : AppCompatActivity() { } private fun reloadFriends() { - friends = fetchContacts() + println("Reload") + friends = DummyContent.ITEMS // FIXME: Persistence displayFriends() // fetchMessages() } private fun displayFriends() { - val adapter = FriendsAdapter(friends) + println("displayF") + val adapter = FriendsPanesViewAdapter(this, friends, twoPane) val nbFriends = adapter.itemCount - contactsList.layoutManager = LinearLayoutManager(this) - contactsList.setHasFixedSize(true) - contactsList.adapter = adapter - friends_title.text = + friend_list.layoutManager = LinearLayoutManager(this) + friend_list.setHasFixedSize(true) + friend_list.adapter = adapter + val welcomeFriends = getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends)) - } - - - private fun setupRecyclerView(recyclerView: RecyclerView) { - recyclerView.adapter = SimpleItemRecyclerViewAdapter( - this, - DummyContent.ITEMS, - twoPane - ) - } - - class SimpleItemRecyclerViewAdapter( - private val parentActivity: FriendListActivity, - private val values: List, - private val twoPane: Boolean - ) : - RecyclerView.Adapter() { - - private val onClickListener: View.OnClickListener - - init { - onClickListener = View.OnClickListener { v -> - val friend = v.tag as Friend - if (twoPane) { - val fragment = FriendDetailFragment().apply { - arguments = Bundle().apply { - putInt(FriendDetailFragment.ARG_FRIEND_ID, friend.id) - println("Put arg: ${friend.id}") - } - } - parentActivity.supportFragmentManager - .beginTransaction() - .replace(R.id.friend_detail_container, fragment) - .commit() - } else { - val intent = Intent(v.context, FriendDetailActivity::class.java).apply { - putExtra(FriendDetailFragment.ARG_FRIEND_ID, friend.id) - println("Put arg: ${friend.id}") - } - v.context.startActivity(intent) - } - } - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context) - .inflate(R.layout.friend_list_content, parent, false) - return ViewHolder(view) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val item = values[position] - holder.idView.text = item.name - holder.contentView.text = item.mapString() - - with(holder.itemView) { - tag = item - setOnClickListener(onClickListener) - } - } - - override fun getItemCount() = values.size - - inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { - val idView: TextView = view.id_text - val contentView: TextView = view.content - } + title = welcomeFriends + println("Welcome: $welcomeFriends") } } 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 346c329..2114f02 100644 --- a/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt @@ -1,14 +1,10 @@ package fr.plnech.dunbar.ui -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 fr.plnech.dunbar.R import fr.plnech.dunbar.model.Friend -import kotlinx.android.synthetic.main.contact_listitem.view.* class FriendsAdapter( @@ -26,63 +22,4 @@ class FriendsAdapter( 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 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 = buildDataString(friend) - - bindPhone(friend) - bindPic(friend) - bindClick(friend) - } - - 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) - } - } - } - } - } - - private fun bindPic(friend: Friend) { - pic.setImageBitmap(friend.photo) - } - - private fun bindClick(friend: Friend) { - view.setOnClickListener { - Toast.makeText(view.context, friend.mapString(), Toast.LENGTH_LONG).show() - println(friend.mapString()) - } - } - - 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") - } - } - } -} +} \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/ui/FriendsPanesViewAdapter.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendsPanesViewAdapter.kt new file mode 100644 index 0000000..fdede73 --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendsPanesViewAdapter.kt @@ -0,0 +1,57 @@ +package fr.plnech.dunbar.ui + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import fr.plnech.dunbar.R +import fr.plnech.dunbar.model.Friend + +class FriendsPanesViewAdapter( + private val parentActivity: FriendListActivity, + private val friends: List, + private val twoPane: Boolean +) : + RecyclerView.Adapter() { + + private val onClickListener: View.OnClickListener + + init { + onClickListener = View.OnClickListener { v -> + val friend = v.tag as Friend + if (twoPane) { + val fragment = FriendDetailFragment().apply { + arguments = Bundle().apply { + putInt(FriendDetailFragment.ARG_FRIEND_ID, friend.id) + } + } + parentActivity.supportFragmentManager + .beginTransaction() + .replace(R.id.friend_detail_container, fragment) + .commit() + } else { + val intent = Intent( + v.context, + FriendDetailActivity::class.java + ).apply { + putExtra(FriendDetailFragment.ARG_FRIEND_ID, friend.id) + } + v.context.startActivity(intent) + } + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FriendsViewHolder { + return FriendsViewHolder( + LayoutInflater.from(parent.context).inflate(R.layout.contact_listitem, parent, false) + ) + } + + override fun onBindViewHolder(holder: FriendsViewHolder, position: Int) { + holder.bind(friends[position]) { onClickListener.onClick(it) } + } + + override fun getItemCount() = friends.size +} \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/ui/FriendsViewHolder.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendsViewHolder.kt new file mode 100644 index 0000000..f04eef9 --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendsViewHolder.kt @@ -0,0 +1,72 @@ +package fr.plnech.dunbar.ui + +import android.text.format.DateUtils +import android.view.View +import android.widget.Toast +import androidx.recyclerview.widget.RecyclerView +import fr.plnech.dunbar.model.Friend +import kotlinx.android.synthetic.main.contact_listitem.view.* + +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, onClickListener: ((View) -> Unit)? = null) { + name.text = friend.name + data.text = buildDataString(friend) + view.tag = friend + + bindPhone(friend) + bindPic(friend) + bindClick(friend, onClickListener) + } + + 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) + } + } + } + } + } + + private fun bindPic(friend: Friend) { + pic.setImageBitmap(friend.photo) + } + + private fun bindClick(friend: Friend, onClickListener: ((View) -> Unit)? = null) { + view.setOnClickListener(onClickListener ?: { + Toast.makeText( + view.context, + friend.mapString(), + Toast.LENGTH_LONG + ).show() + println(friend.mapString()) + }) + } + + 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") + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout-w900dp/friend_list.xml b/app/src/main/res/layout-w900dp/friend_list.xml index d4596d6..d40bb7b 100644 --- a/app/src/main/res/layout-w900dp/friend_list.xml +++ b/app/src/main/res/layout-w900dp/friend_list.xml @@ -1,63 +1,35 @@ - + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" + android:baselineAligned="false" + android:divider="?android:attr/dividerHorizontal" + android:orientation="horizontal" + android:showDividers="middle" + tools:context=".ui.FriendListActivity"> - + - + app:layoutManager="LinearLayoutManager" + tools:context="fr.plnech.dunbar.ui.FriendListActivity" + tools:listitem="@layout/friend_list_content" /> - - - - - + - - + \ No newline at end of file diff --git a/app/src/main/res/layout/friend_list.xml b/app/src/main/res/layout/friend_list.xml index 97f906e..67bab4e 100644 --- a/app/src/main/res/layout/friend_list.xml +++ b/app/src/main/res/layout/friend_list.xml @@ -1,35 +1,13 @@ - - - - - - \ No newline at end of file + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" + app:layoutManager="LinearLayoutManager" + tools:context=".ui.FriendListActivity" + tools:listitem="@layout/contact_listitem" /> \ No newline at end of file