feat(FriendsList): Title

parent 294cff9c
......@@ -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()
}
......
......@@ -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<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
}
title = welcomeFriends
println("Welcome: $welcomeFriends")
}
}
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
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"?>
<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:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
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">
<TextView
android:id="@+id/friends_title"
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" />
<!-- This layout is a two-pane layout for the Friends
master/detail flow. -->
<LinearLayout
android:id="@+id/panes"
app:layout_constraintTop_toBottomOf="@id/friends_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
<androidx.recyclerview.widget.RecyclerView
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"
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" />
<!--
This layout is a two-pane layout for the Friends
master/detail flow.
-->
<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" />
<FrameLayout
android:id="@+id/friend_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
\ No newline at end of file
<?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:tools="http://schemas.android.com/tools"
android:id="@+id/friend_list"
android:name="fr.plnech.dunbar.FriendListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.ContactsActivity"
tools:showIn="@layout/activity_friends_list">
<TextView
android:id="@+id/friends_title"
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
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
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