feat(FriendsList): Title

parent 294cff9c
...@@ -28,13 +28,11 @@ class FriendDetailFragment : Fragment() { ...@@ -28,13 +28,11 @@ class FriendDetailFragment : Fragment() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
arguments?.let { arguments?.let {
println("Got args!")
if (it.containsKey(ARG_FRIEND_ID)) { if (it.containsKey(ARG_FRIEND_ID)) {
// Load the dummy content specified by the fragment // Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader // arguments. In a real-world scenario, use a Loader
// to load content from a content provider. // to load content from a content provider.
item = DummyContent.ITEM_MAP[it.getInt(ARG_FRIEND_ID)] item = DummyContent.ITEM_MAP[it.getInt(ARG_FRIEND_ID)]
println("item: $item")
activity?.toolbar_layout?.title = item?.name activity?.toolbar_layout?.title = item?.name
} }
} }
...@@ -46,8 +44,6 @@ class FriendDetailFragment : Fragment() { ...@@ -46,8 +44,6 @@ class FriendDetailFragment : Fragment() {
): View? { ): View? {
val rootView = inflater.inflate(R.layout.friend_detail, container, false) val rootView = inflater.inflate(R.layout.friend_detail, container, false)
item?.let { item?.let {
println("Got friend: ${it.name}")
activity?.title = it.name
rootView.apply { rootView.apply {
friendData.text = it.mapString() friendData.text = it.mapString()
} }
......
...@@ -2,21 +2,17 @@ package fr.plnech.dunbar.ui ...@@ -2,21 +2,17 @@ package fr.plnech.dunbar.ui
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.* import android.view.Menu
import android.widget.TextView import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import fr.plnech.dunbar.R import fr.plnech.dunbar.R
import fr.plnech.dunbar.dummy.DummyContent import fr.plnech.dunbar.dummy.DummyContent
import fr.plnech.dunbar.fetchContacts
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.plural import fr.plnech.dunbar.plural
import kotlinx.android.synthetic.main.activity_friends_list.* 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.*
import kotlinx.android.synthetic.main.friend_list_content.view.*
class FriendListActivity : AppCompatActivity() { class FriendListActivity : AppCompatActivity() {
...@@ -31,12 +27,11 @@ class FriendListActivity : AppCompatActivity() { ...@@ -31,12 +27,11 @@ class FriendListActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_friends_list) setContentView(R.layout.activity_friends_list)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
toolbar.title = title toolbar.title = title
fab.setOnClickListener { view -> 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() .setAction("Action", null).show()
} }
...@@ -48,7 +43,7 @@ class FriendListActivity : AppCompatActivity() { ...@@ -48,7 +43,7 @@ class FriendListActivity : AppCompatActivity() {
twoPane = true twoPane = true
} }
setupRecyclerView(friend_list) reloadFriends()
} }
override fun onCreateOptionsMenu(menu: Menu?): Boolean { override fun onCreateOptionsMenu(menu: Menu?): Boolean {
...@@ -71,86 +66,23 @@ class FriendListActivity : AppCompatActivity() { ...@@ -71,86 +66,23 @@ class FriendListActivity : AppCompatActivity() {
} }
private fun reloadFriends() { private fun reloadFriends() {
friends = fetchContacts() println("Reload")
friends = DummyContent.ITEMS // FIXME: Persistence
displayFriends() displayFriends()
// fetchMessages() // fetchMessages()
} }
private fun displayFriends() { private fun displayFriends() {
val adapter = FriendsAdapter(friends) println("displayF")
val adapter = FriendsPanesViewAdapter(this, friends, twoPane)
val nbFriends = adapter.itemCount val nbFriends = adapter.itemCount
contactsList.layoutManager = LinearLayoutManager(this) friend_list.layoutManager = LinearLayoutManager(this)
contactsList.setHasFixedSize(true) friend_list.setHasFixedSize(true)
contactsList.adapter = adapter friend_list.adapter = adapter
friends_title.text = val welcomeFriends =
getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends)) getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends))
} title = welcomeFriends
println("Welcome: $welcomeFriends")
private fun setupRecyclerView(recyclerView: RecyclerView) {
recyclerView.adapter = SimpleItemRecyclerViewAdapter(
this,
DummyContent.ITEMS,
twoPane
)
}
class SimpleItemRecyclerViewAdapter(
private val parentActivity: FriendListActivity,
private val values: List<Friend>,
private val twoPane: Boolean
) :
RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder>() {
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
}
} }
} }
package fr.plnech.dunbar.ui package fr.plnech.dunbar.ui
import android.text.format.DateUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import fr.plnech.dunbar.R import fr.plnech.dunbar.R
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import kotlinx.android.synthetic.main.contact_listitem.view.*
class FriendsAdapter( class FriendsAdapter(
...@@ -26,63 +22,4 @@ class FriendsAdapter( ...@@ -26,63 +22,4 @@ class FriendsAdapter(
override fun onBindViewHolder(holder: FriendsViewHolder, position: Int): Unit = override fun onBindViewHolder(holder: FriendsViewHolder, position: Int): Unit =
holder.bind(friends[position]) holder.bind(friends[position])
} }
\ No newline at end of file
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")
}
}
}
}
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<Friend>,
private val twoPane: Boolean
) :
RecyclerView.Adapter<FriendsViewHolder>() {
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
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
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content"> 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">
<TextView <!-- This layout is a two-pane layout for the Friends
android:id="@+id/friends_title" master/detail flow. -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/panes"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:text="N friends on Dunbar" />
<LinearLayout <androidx.recyclerview.widget.RecyclerView
android:id="@+id/panes" android:id="@+id/friend_list"
app:layout_constraintTop_toBottomOf="@id/friends_title" android:name="fr.plnech.dunbar.FriendListFragment"
app:layout_constraintLeft_toLeftOf="parent" android:layout_width="@dimen/item_width"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
android:baselineAligned="false" app:layoutManager="LinearLayoutManager"
android:divider="?android:attr/dividerHorizontal" tools:context="fr.plnech.dunbar.ui.FriendListActivity"
android:orientation="horizontal" tools:listitem="@layout/friend_list_content" />
android:showDividers="middle"
tools:context=".ui.FriendListActivity">
<!-- <FrameLayout
This layout is a two-pane layout for the Friends android:id="@+id/friend_detail_container"
master/detail flow. android:layout_width="0dp"
android:layout_height="match_parent"
--> android:layout_weight="3" />
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/friend_list"
android:name="fr.plnech.dunbar.FriendListFragment"
android:layout_width="@dimen/item_width"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="fr.plnech.dunbar.ui.FriendListActivity"
tools:listitem="@layout/friend_list_content" />
<FrameLayout
android:id="@+id/friend_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/friend_list"
android:name="fr.plnech.dunbar.FriendListFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" android:layout_marginLeft="16dp"
tools:context=".ui.ContactsActivity" android:layout_marginRight="16dp"
tools:showIn="@layout/activity_friends_list"> app:layoutManager="LinearLayoutManager"
tools:context=".ui.FriendListActivity"
<TextView tools:listitem="@layout/contact_listitem" />
android:id="@+id/friends_title" \ No newline at end of file
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/friend_list"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:text="N friends on Dunbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/friend_list"
android:name="fr.plnech.dunbar.FriendListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.FriendListActivity"
tools:listitem="@layout/contact_listitem" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment