feat: FriendsList launch, contacts sub

parent 904b0fde
...@@ -18,32 +18,33 @@ ...@@ -18,32 +18,33 @@
<!-- TODO: https://developer.android.com/studio/write/app-link-indexing --> <!-- TODO: https://developer.android.com/studio/write/app-link-indexing -->
<activity <activity
android:name=".FriendListActivity" android:name=".ui.FriendListActivity"
android:label="@string/title_friend_list" android:label="@string/title_friend_list"
android:parentActivityName=".ui.FriendsActivity" android:parentActivityName=".ui.ContactsActivity"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY" <intent-filter>
android:value="fr.plnech.dunbar.ui.FriendsActivity" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </activity>
<activity <activity
android:name=".FriendDetailActivity" android:name=".ui.FriendDetailActivity"
android:label="@string/title_friend_detail" android:label="@string/title_friend_detail"
android:parentActivityName=".FriendListActivity" android:parentActivityName=".ui.FriendListActivity"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value="fr.plnech.dunbar.FriendListActivity" /> android:value="fr.plnech.dunbar.ui.FriendListActivity" />
</activity> </activity>
<activity <activity
android:name=".ui.FriendsActivity" android:name=".ui.ContactsActivity"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<intent-filter> <meta-data
<action android:name="android.intent.action.MAIN" /> android:name="android.support.PARENT_ACTIVITY"
android:value="fr.plnech.dunbar.ui.FriendListActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </activity>
</application> </application>
......
...@@ -15,7 +15,7 @@ fun String.plural(count: Int = 1): String { ...@@ -15,7 +15,7 @@ fun String.plural(count: Int = 1): String {
} }
} }
fun Context.fetchFriends(includeNot: Boolean = true): List<Friend> { fun Context.fetchContacts(includeNot: Boolean = true): List<Friend> {
// TODO: https://developer.android.com/training/permissions/requesting // TODO: https://developer.android.com/training/permissions/requesting
val friends = mutableListOf<Friend>() val friends = mutableListOf<Friend>()
......
package fr.plnech.dunbar.ui package fr.plnech.dunbar.ui
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NavUtils
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import fr.plnech.dunbar.FriendListActivity
import fr.plnech.dunbar.R import fr.plnech.dunbar.R
import fr.plnech.dunbar.data.Messages import fr.plnech.dunbar.data.Messages
import fr.plnech.dunbar.fetchFriends import fr.plnech.dunbar.fetchContacts
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.notif.FriendReminder import fr.plnech.dunbar.notif.FriendReminder
import fr.plnech.dunbar.plural
import kotlinx.android.synthetic.main.activity_contacts.* import kotlinx.android.synthetic.main.activity_contacts.*
import kotlinx.android.synthetic.main.content_friends.* import kotlinx.android.synthetic.main.content_contacts.*
import java.util.* import java.util.*
class FriendsActivity : AppCompatActivity() { class ContactsActivity : AppCompatActivity() {
private lateinit var messages: Messages private lateinit var messages: Messages
private var friends = listOf<Friend>() private var contacts = listOf<Friend>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -33,20 +29,23 @@ class FriendsActivity : AppCompatActivity() { ...@@ -33,20 +29,23 @@ class FriendsActivity : AppCompatActivity() {
// messages = Messages(applicationContext) // messages = Messages(applicationContext)
fab.setOnClickListener { view -> fab.setOnClickListener { view ->
reloadFriends() reloadContacts()
notifyFriend() notifyFriend()
} }
reloadFriends() // Show the Up button in the action bar.
supportActionBar?.setDisplayHomeAsUpEnabled(true)
reloadContacts()
notifyFriend() notifyFriend()
} }
private fun notifyFriend() { private fun notifyFriend() {
val idNotification = 0 val idNotification = 0
val notRecentlyTalked = friends.filter { val notRecentlyTalked = contacts.filter {
it.lastDate != null && (Date().time - it.lastDate!!.time) > 1 * 60 * 60 * 1000 it.lastDate != null && (Date().time - it.lastDate!!.time) > 1 * 60 * 60 * 1000
} }
println("${notRecentlyTalked.size} good old friends I could talk to.") println("${notRecentlyTalked.size} good old contacts I could talk to.")
val friend = notRecentlyTalked.minBy { it.timesContacted } val friend = notRecentlyTalked.minBy { it.timesContacted }
...@@ -55,7 +54,7 @@ class FriendsActivity : AppCompatActivity() { ...@@ -55,7 +54,7 @@ class FriendsActivity : AppCompatActivity() {
val notification = val notification =
FriendReminder(applicationContext).createNotification( FriendReminder(applicationContext).createNotification(
friend, friend,
this@FriendsActivity this@ContactsActivity
) )
notification?.let { notification?.let {
notify(idNotification, notification) notify(idNotification, notification)
...@@ -64,49 +63,27 @@ class FriendsActivity : AppCompatActivity() { ...@@ -64,49 +63,27 @@ class FriendsActivity : AppCompatActivity() {
} }
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onOptionsItemSelected(item: MenuItem) =
// Inflate the menu; this adds items to the action bar if it is present. when (item.itemId) {
menuInflater.inflate(R.menu.menu_main, menu) android.R.id.home -> {
return true NavUtils.navigateUpFromSameTask(this)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
R.id.action_friends -> {
startActivity(Intent(this, FriendListActivity::class.java))
true true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
} }
}
private fun reloadFriends() { private fun reloadContacts() {
friends = fetchFriends() contacts = fetchContacts()
displayFriends() displayContacts()
// fetchMessages()
} }
private fun fetchMessages() { private fun displayContacts() {
messages.fetchAll() val adapter = FriendsAdapter(contacts)
Toast.makeText(
this, "${messages.sent.size} messages sent, " +
"${messages.inbox.size} messages received.", Toast.LENGTH_SHORT
).show()
}
private fun displayFriends() {
val adapter = FriendsAdapter(friends)
val nbFriends = adapter.itemCount val nbFriends = adapter.itemCount
friendsList.layoutManager = LinearLayoutManager(this) contactsList.layoutManager = LinearLayoutManager(this)
friendsList.setHasFixedSize(true) contactsList.setHasFixedSize(true)
friendsList.adapter = adapter contactsList.adapter = adapter
welcomeTitle.text = getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends)) contactsTitle.text = getString(R.string.text_contacts).format(nbFriends)
} }
} }
package fr.plnech.dunbar package fr.plnech.dunbar.ui
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import fr.plnech.dunbar.R
import kotlinx.android.synthetic.main.activity_friend_detail.* import kotlinx.android.synthetic.main.activity_friend_detail.*
/** /**
......
package fr.plnech.dunbar package fr.plnech.dunbar.ui
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import fr.plnech.dunbar.R
import fr.plnech.dunbar.dummy.DummyContent import fr.plnech.dunbar.dummy.DummyContent
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import kotlinx.android.synthetic.main.activity_friend_detail.* import kotlinx.android.synthetic.main.activity_friend_detail.*
......
package fr.plnech.dunbar package fr.plnech.dunbar.ui
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.*
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NavUtils import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView 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.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 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.* import kotlinx.android.synthetic.main.friend_list_content.view.*
...@@ -25,6 +26,8 @@ class FriendListActivity : AppCompatActivity() { ...@@ -25,6 +26,8 @@ class FriendListActivity : AppCompatActivity() {
*/ */
private var twoPane: Boolean = false private var twoPane: Boolean = false
private var friends = listOf<Friend>()
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)
...@@ -36,8 +39,6 @@ class FriendListActivity : AppCompatActivity() { ...@@ -36,8 +39,6 @@ class FriendListActivity : AppCompatActivity() {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show() .setAction("Action", null).show()
} }
// Show the Up button in the action bar.
supportActionBar?.setDisplayHomeAsUpEnabled(true)
if (friend_detail_container != null) { if (friend_detail_container != null) {
// The detail container view will be present only in the // The detail container view will be present only in the
...@@ -50,17 +51,48 @@ class FriendListActivity : AppCompatActivity() { ...@@ -50,17 +51,48 @@ class FriendListActivity : AppCompatActivity() {
setupRecyclerView(friend_list) setupRecyclerView(friend_list)
} }
override fun onOptionsItemSelected(item: MenuItem) = override fun onCreateOptionsMenu(menu: Menu?): Boolean {
when (item.itemId) { menuInflater.inflate(R.menu.menu_main, menu)
android.R.id.home -> { return super.onCreateOptionsMenu(menu)
NavUtils.navigateUpFromSameTask(this) }
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
R.id.action_contacts -> {
startActivity(Intent(this, ContactsActivity::class.java))
true true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
} }
}
private fun reloadFriends() {
friends = fetchContacts()
displayFriends()
// fetchMessages()
}
private fun displayFriends() {
val adapter = FriendsAdapter(friends)
val nbFriends = adapter.itemCount
contactsList.layoutManager = LinearLayoutManager(this)
contactsList.setHasFixedSize(true)
contactsList.adapter = adapter
`@+id/contactsTitle`.text = getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends))
}
private fun setupRecyclerView(recyclerView: RecyclerView) { private fun setupRecyclerView(recyclerView: RecyclerView) {
recyclerView.adapter = SimpleItemRecyclerViewAdapter(this, DummyContent.ITEMS, twoPane) recyclerView.adapter = SimpleItemRecyclerViewAdapter(
this,
DummyContent.ITEMS,
twoPane
)
} }
class SimpleItemRecyclerViewAdapter( class SimpleItemRecyclerViewAdapter(
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
android:divider="?android:attr/dividerHorizontal" android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal" android:orientation="horizontal"
android:showDividers="middle" android:showDividers="middle"
tools:context=".FriendListActivity"> tools:context=".ui.FriendListActivity">
<!-- <!--
This layout is a two-pane layout for the Friends This layout is a two-pane layout for the Friends
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager" app:layoutManager="LinearLayoutManager"
tools:context="fr.plnech.dunbar.FriendListActivity" tools:context="fr.plnech.dunbar.ui.FriendListActivity"
tools:listitem="@layout/friend_list_content" /> tools:listitem="@layout/friend_list_content" />
<FrameLayout <FrameLayout
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_friends" /> <include layout="@layout/content_contacts" />
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab" android:id="@+id/fab"
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
tools:context=".FriendDetailActivity" tools:context=".ui.FriendDetailActivity"
tools:ignore="MergeRootFrame"> tools:ignore="MergeRootFrame">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
tools:context=".FriendListActivity"> tools:context=".ui.FriendListActivity">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar" android:id="@+id/app_bar"
......
...@@ -5,32 +5,32 @@ ...@@ -5,32 +5,32 @@
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" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.FriendsActivity" tools:context=".ui.ContactsActivity"
tools:showIn="@layout/activity_friends"> tools:showIn="@layout/activity_contacts">
<TextView <TextView
android:id="@+id/welcomeTitle" android:id="@+id/contactsTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:textSize="24sp" android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/friendsList" app:layout_constraintBottom_toTopOf="@id/contactsList"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" app:layout_constraintVertical_bias="0.0"
tools:text="N friends on Dunbar" /> tools:text="N contacts" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/friendsList" android:id="@+id/contactsList"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:scrollbars="vertical" android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/welcomeTitle" app:layout_constraintTop_toBottomOf="@id/contactsTitle"
tools:listitem="@layout/contact" tools:listitem="@layout/contact"
tools:text="@tools:sample/last_names" /> tools:text="@tools:sample/last_names" />
......
...@@ -7,4 +7,4 @@ ...@@ -7,4 +7,4 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="16dp" android:padding="16dp"
android:textIsSelectable="true" android:textIsSelectable="true"
tools:context=".FriendDetailFragment" /> tools:context=".ui.FriendDetailFragment" />
\ No newline at end of file \ No newline at end of file
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
tools:showIn="@layout/activity_friends"> tools:showIn="@layout/activity_friends">
<TextView <TextView
android:id="@+id/welcomeTitle" android:id="@+id/contactsTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
...@@ -30,6 +30,6 @@ ...@@ -30,6 +30,6 @@
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager" app:layoutManager="LinearLayoutManager"
tools:context=".FriendListActivity" tools:context=".ui.FriendListActivity"
tools:listitem="@layout/contact" /> tools:listitem="@layout/contact" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu 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"
tools:context="fr.plnech.dunbar.ui.FriendsActivity"> tools:context="fr.plnech.dunbar.ui.ContactsActivity">
<item <item
android:id="@+id/action_friends" android:id="@+id/action_contacts"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/action_friends" android:title="@string/action_contacts"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item <item
android:id="@+id/action_settings" android:id="@+id/action_settings"
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
<string name="channel_name">Dunbar</string> <string name="channel_name">Dunbar</string>
<string name="channel_description">Updates about friends you don\'t want to forget</string> <string name="channel_description">Updates about friends you don\'t want to forget</string>
<string name="action_settings">Settings</string> <string name="action_settings">Settings</string>
<string name="action_friends">Friends</string> <string name="action_contacts">Contacts</string>
<string name="title_friend_list">Friends</string> <string name="title_friend_list">Friends</string>
<string name="title_friend_detail">Friend Detail</string> <string name="title_friend_detail">Friend Detail</string>
<string name="text_welcome">"%1$d %2$s on Dunbar"</string> <string name="text_welcome">"%1$d %2$s on Dunbar"</string>
<string name="text_contacts">"%1$d contacts"</string>
</resources> </resources>
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