diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c0f04bd..39b01c7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,32 +18,33 @@ - + + + + + + + android:value="fr.plnech.dunbar.ui.FriendListActivity" /> - - - - - + diff --git a/app/src/main/java/fr/plnech/dunbar/Extensions.kt b/app/src/main/java/fr/plnech/dunbar/Extensions.kt index 35ae3eb..a1f9449 100644 --- a/app/src/main/java/fr/plnech/dunbar/Extensions.kt +++ b/app/src/main/java/fr/plnech/dunbar/Extensions.kt @@ -15,7 +15,7 @@ fun String.plural(count: Int = 1): String { } } -fun Context.fetchFriends(includeNot: Boolean = true): List { +fun Context.fetchContacts(includeNot: Boolean = true): List { // TODO: https://developer.android.com/training/permissions/requesting val friends = mutableListOf() diff --git a/app/src/main/java/fr/plnech/dunbar/ui/ContactsActivity.kt b/app/src/main/java/fr/plnech/dunbar/ui/ContactsActivity.kt index a3aadba..9ca5a1d 100644 --- a/app/src/main/java/fr/plnech/dunbar/ui/ContactsActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/ContactsActivity.kt @@ -1,29 +1,25 @@ package fr.plnech.dunbar.ui -import android.content.Intent import android.os.Bundle -import android.view.Menu import android.view.MenuItem -import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.NavUtils import androidx.core.app.NotificationManagerCompat import androidx.recyclerview.widget.LinearLayoutManager -import fr.plnech.dunbar.FriendListActivity import fr.plnech.dunbar.R 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.notif.FriendReminder -import fr.plnech.dunbar.plural import kotlinx.android.synthetic.main.activity_contacts.* -import kotlinx.android.synthetic.main.content_friends.* +import kotlinx.android.synthetic.main.content_contacts.* import java.util.* -class FriendsActivity : AppCompatActivity() { +class ContactsActivity : AppCompatActivity() { private lateinit var messages: Messages - private var friends = listOf() + private var contacts = listOf() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -33,20 +29,23 @@ class FriendsActivity : AppCompatActivity() { // messages = Messages(applicationContext) fab.setOnClickListener { view -> - reloadFriends() + reloadContacts() notifyFriend() } - reloadFriends() + // Show the Up button in the action bar. + supportActionBar?.setDisplayHomeAsUpEnabled(true) + + reloadContacts() notifyFriend() } private fun notifyFriend() { val idNotification = 0 - val notRecentlyTalked = friends.filter { + val notRecentlyTalked = contacts.filter { 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 } @@ -55,7 +54,7 @@ class FriendsActivity : AppCompatActivity() { val notification = FriendReminder(applicationContext).createNotification( friend, - this@FriendsActivity + this@ContactsActivity ) notification?.let { notify(idNotification, notification) @@ -64,49 +63,27 @@ class FriendsActivity : AppCompatActivity() { } } - override fun onCreateOptionsMenu(menu: Menu): Boolean { - // Inflate the menu; this adds items to the action bar if it is present. - menuInflater.inflate(R.menu.menu_main, menu) - return true - } - - 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)) + override fun onOptionsItemSelected(item: MenuItem) = + when (item.itemId) { + android.R.id.home -> { + NavUtils.navigateUpFromSameTask(this) true } else -> super.onOptionsItemSelected(item) } - } - private fun reloadFriends() { - friends = fetchFriends() - displayFriends() -// fetchMessages() + private fun reloadContacts() { + contacts = fetchContacts() + displayContacts() } - private fun fetchMessages() { - messages.fetchAll() - Toast.makeText( - this, "${messages.sent.size} messages sent, " + - "${messages.inbox.size} messages received.", Toast.LENGTH_SHORT - ).show() - } - - - private fun displayFriends() { - val adapter = FriendsAdapter(friends) + private fun displayContacts() { + val adapter = FriendsAdapter(contacts) val nbFriends = adapter.itemCount - friendsList.layoutManager = LinearLayoutManager(this) - friendsList.setHasFixedSize(true) - friendsList.adapter = adapter - welcomeTitle.text = getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends)) + contactsList.layoutManager = LinearLayoutManager(this) + contactsList.setHasFixedSize(true) + contactsList.adapter = adapter + contactsTitle.text = getString(R.string.text_contacts).format(nbFriends) } - } diff --git a/app/src/main/java/fr/plnech/dunbar/FriendDetailActivity.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailActivity.kt similarity index 96% rename from app/src/main/java/fr/plnech/dunbar/FriendDetailActivity.kt rename to app/src/main/java/fr/plnech/dunbar/ui/FriendDetailActivity.kt index 3a1f589..465c2c5 100644 --- a/app/src/main/java/fr/plnech/dunbar/FriendDetailActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailActivity.kt @@ -1,10 +1,11 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.ui import android.content.Intent import android.os.Bundle import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import com.google.android.material.snackbar.Snackbar +import fr.plnech.dunbar.R import kotlinx.android.synthetic.main.activity_friend_detail.* /** diff --git a/app/src/main/java/fr/plnech/dunbar/FriendDetailFragment.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt similarity index 97% rename from app/src/main/java/fr/plnech/dunbar/FriendDetailFragment.kt rename to app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt index 7c3f482..a2d3ca1 100644 --- a/app/src/main/java/fr/plnech/dunbar/FriendDetailFragment.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendDetailFragment.kt @@ -1,10 +1,11 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.ui import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import fr.plnech.dunbar.R import fr.plnech.dunbar.dummy.DummyContent import fr.plnech.dunbar.model.Friend import kotlinx.android.synthetic.main.activity_friend_detail.* diff --git a/app/src/main/java/fr/plnech/dunbar/FriendListActivity.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt similarity index 76% rename from app/src/main/java/fr/plnech/dunbar/FriendListActivity.kt rename to app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt index adaa4fb..66b4530 100644 --- a/app/src/main/java/fr/plnech/dunbar/FriendListActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendListActivity.kt @@ -1,19 +1,20 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.ui import android.content.Intent import android.os.Bundle -import android.view.LayoutInflater -import android.view.MenuItem -import android.view.View -import android.view.ViewGroup +import android.view.* import android.widget.TextView import androidx.appcompat.app.AppCompatActivity -import androidx.core.app.NavUtils +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.* @@ -25,6 +26,8 @@ class FriendListActivity : AppCompatActivity() { */ private var twoPane: Boolean = false + private var friends = listOf() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_friends_list) @@ -36,8 +39,6 @@ class FriendListActivity : AppCompatActivity() { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } - // Show the Up button in the action bar. - supportActionBar?.setDisplayHomeAsUpEnabled(true) if (friend_detail_container != null) { // The detail container view will be present only in the @@ -50,17 +51,48 @@ class FriendListActivity : AppCompatActivity() { setupRecyclerView(friend_list) } - override fun onOptionsItemSelected(item: MenuItem) = - when (item.itemId) { - android.R.id.home -> { - NavUtils.navigateUpFromSameTask(this) + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.menu_main, menu) + return super.onCreateOptionsMenu(menu) + } + + 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 } 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) { - recyclerView.adapter = SimpleItemRecyclerViewAdapter(this, DummyContent.ITEMS, twoPane) + recyclerView.adapter = SimpleItemRecyclerViewAdapter( + this, + DummyContent.ITEMS, + twoPane + ) } class SimpleItemRecyclerViewAdapter( diff --git a/app/src/main/res/layout-w900dp/friend_list.xml b/app/src/main/res/layout-w900dp/friend_list.xml index f5abd42..c527b19 100644 --- a/app/src/main/res/layout-w900dp/friend_list.xml +++ b/app/src/main/res/layout-w900dp/friend_list.xml @@ -10,7 +10,7 @@ android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal" android:showDividers="middle" - tools:context=".FriendListActivity"> + tools:context=".ui.FriendListActivity">