From b11bf85d9041d2c09e75aa9fabfeb370b35f54fe Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 23 Sep 2015 00:46:38 +0200 Subject: [PATCH] Changed AddContactsActivity to ConnectionsActivity. This means all neighbors are shown, not just non-contacts. Also fixed a bug where the list would not be refreshed when first opened. --- app/src/main/AndroidManifest.xml | 4 +-- ..._contacts.xml => activity_connections.xml} | 2 +- app/src/main/res/values/strings.xml | 9 ++++-- ...tivity.scala => ConnectionsActivity.scala} | 31 ++++++++++++++----- .../ensichat/fragments/ContactsFragment.scala | 4 +-- 5 files changed, 34 insertions(+), 16 deletions(-) rename app/src/main/res/layout/{activity_add_contacts.xml => activity_connections.xml} (95%) rename app/src/main/scala/com/nutomic/ensichat/activities/{AddContactsActivity.scala => ConnectionsActivity.scala} (79%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 527f577..e1122d5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,8 +34,8 @@ android:theme="@style/AppTheme.NoActionBar"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6a31118..9e0ea7b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,13 +43,13 @@ Exit - + - Add Contacts + Connections - Searching for Users\nRange: ~10m + Searching for Users\nRange: ~10m Do you want to add %1$s as contact? @@ -57,6 +57,9 @@ Contact added + + You have already added %1$s as a contact + diff --git a/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala b/app/src/main/scala/com/nutomic/ensichat/activities/ConnectionsActivity.scala similarity index 79% rename from app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala rename to app/src/main/scala/com/nutomic/ensichat/activities/ConnectionsActivity.scala index 5145ee7..90e2854 100644 --- a/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala +++ b/app/src/main/scala/com/nutomic/ensichat/activities/ConnectionsActivity.scala @@ -17,7 +17,7 @@ import com.nutomic.ensichat.views.UsersAdapter /** * Lists all nearby, connected devices and allows adding them to be added as contacts. */ -class AddContactsActivity extends EnsichatActivity with OnItemClickListener { +class ConnectionsActivity extends EnsichatActivity with OnItemClickListener { private val Tag = "AddContactsActivity" @@ -32,7 +32,7 @@ class AddContactsActivity extends EnsichatActivity with OnItemClickListener { super.onCreate(savedInstanceState) getSupportActionBar.setDisplayHomeAsUpEnabled(true) - setContentView(R.layout.activity_add_contacts) + setContentView(R.layout.activity_connections) val list = findViewById(android.R.id.list).asInstanceOf[ListView] list.setAdapter(adapter) list.setOnItemClickListener(this) @@ -45,6 +45,13 @@ class AddContactsActivity extends EnsichatActivity with OnItemClickListener { .registerReceiver(onContactsUpdatedReceiver, filter) } + override def onResume(): Unit = { + super.onResume() + runOnServiceConnected(() => { + updateConnections() + }) + } + override def onDestroy(): Unit = { super.onDestroy() LocalBroadcastManager.getInstance(this).unregisterReceiver(onContactsUpdatedReceiver) @@ -55,12 +62,18 @@ class AddContactsActivity extends EnsichatActivity with OnItemClickListener { */ override def onItemClick(parent: AdapterView[_], view: View, position: Int, id: Long): Unit = { val contact = adapter.getItem(position) + if (database.getContacts.contains(contact)) { + val text = getString(R.string.contact_already_added, contact.name) + Toast.makeText(this, text, Toast.LENGTH_SHORT).show() + return + } + new Builder(this) .setMessage(getString(R.string.dialog_add_contact, contact.name)) .setPositiveButton(android.R.string.yes, new OnClickListener { override def onClick(dialog: DialogInterface, which: Int): Unit = { database.addContact(contact) - Toast.makeText(AddContactsActivity.this, R.string.toast_contact_added, Toast.LENGTH_SHORT) + Toast.makeText(ConnectionsActivity.this, R.string.toast_contact_added, Toast.LENGTH_SHORT) .show() } }) @@ -82,13 +95,15 @@ class AddContactsActivity extends EnsichatActivity with OnItemClickListener { private val onContactsUpdatedReceiver = new BroadcastReceiver() { override def onReceive(context: Context, intent: Intent): Unit = { runOnUiThread(new Runnable { - override def run(): Unit = { - adapter.clear() - (service.get.connections().map(a => service.get.getUser(a)) -- database.getContacts) - .foreach(adapter.add) - } + override def run(): Unit = updateConnections() }) } } + private def updateConnections(): Unit = { + adapter.clear() + service.get.connections().map(a => service.get.getUser(a)) + .foreach(adapter.add) + } + } diff --git a/app/src/main/scala/com/nutomic/ensichat/fragments/ContactsFragment.scala b/app/src/main/scala/com/nutomic/ensichat/fragments/ContactsFragment.scala index 00c820a..e3f9294 100644 --- a/app/src/main/scala/com/nutomic/ensichat/fragments/ContactsFragment.scala +++ b/app/src/main/scala/com/nutomic/ensichat/fragments/ContactsFragment.scala @@ -14,7 +14,7 @@ import android.view.View.OnClickListener import android.view._ import android.widget.{ListView, TextView, Toast} import com.nutomic.ensichat.R -import com.nutomic.ensichat.activities.{AddContactsActivity, EnsichatActivity, MainActivity, SettingsActivity} +import com.nutomic.ensichat.activities.{ConnectionsActivity, EnsichatActivity, MainActivity, SettingsActivity} import com.nutomic.ensichat.protocol.{ChatService, Crypto} import com.nutomic.ensichat.util.Database import com.nutomic.ensichat.views.UsersAdapter @@ -80,7 +80,7 @@ class ContactsFragment extends ListFragment with OnClickListener { } override def onClick(v: View): Unit = - startActivity(new Intent(getActivity, classOf[AddContactsActivity])) + startActivity(new Intent(getActivity, classOf[ConnectionsActivity])) override def onOptionsItemSelected(item: MenuItem): Boolean = item.getItemId match { case R.id.share_app =>