Use listener instead of anonymous function for database contacts update.

This commit is contained in:
Felix Ableitner 2015-02-08 22:15:44 +01:00
parent e38e805c1b
commit 61f21c0be1
4 changed files with 31 additions and 20 deletions

View file

@ -9,6 +9,7 @@ import android.test.AndroidTestCase
import android.test.mock.MockContext
import com.nutomic.ensichat.protocol.UserTest
import com.nutomic.ensichat.protocol.messages.MessageTest._
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
import junit.framework.Assert._
class DatabaseTest extends AndroidTestCase {
@ -66,8 +67,8 @@ class DatabaseTest extends AndroidTestCase {
def testAddContactCallback(): Unit = {
val latch = new CountDownLatch(1)
database.runOnContactsUpdated(() => {
latch.countDown()
database.runOnContactsUpdated(new OnContactsUpdatedListener {
override def onContactsUpdated() = latch.countDown()
})
database.addContact(UserTest.u1)
latch.await()

View file

@ -9,13 +9,14 @@ import android.widget._
import com.nutomic.ensichat.R
import com.nutomic.ensichat.protocol.ChatService
import com.nutomic.ensichat.protocol.messages.RequestAddContact
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
import com.nutomic.ensichat.util.UsersAdapter
/**
* Lists all nearby, connected devices and allows adding them to be added as contacts.
*/
class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnectionsChangedListener
with OnItemClickListener {
with OnItemClickListener with OnContactsUpdatedListener {
private val Tag = "AddContactsActivity"
@ -36,11 +37,11 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
runOnServiceConnected(() => {
service.registerConnectionListener(AddContactsActivity.this)
service.database.runOnContactsUpdated(updateList)
service.database.runOnContactsUpdated(this)
})
}
override def onConnectionsChanged() = updateList()
override def onConnectionsChanged() = onContactsUpdated()
/**
* Initiates adding the device as contact if it hasn't been added yet.
@ -64,7 +65,7 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
/**
* Fetches connections and displays them (excluding contacts).
*/
private def updateList(): Unit ={
override def onContactsUpdated(): Unit ={
runOnUiThread(new Runnable {
override def run(): Unit = {
adapter.clear()

View file

@ -8,12 +8,13 @@ import android.widget.ListView
import com.nutomic.ensichat.R
import com.nutomic.ensichat.activities.{AddContactsActivity, EnsiChatActivity, MainActivity, SettingsActivity}
import com.nutomic.ensichat.protocol.ChatService
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
import com.nutomic.ensichat.util.UsersAdapter
/**
* Lists all nearby, connected devices.
*/
class ContactsFragment extends ListFragment {
class ContactsFragment extends ListFragment with OnContactsUpdatedListener {
private lazy val adapter = new UsersAdapter(getActivity)
@ -27,14 +28,7 @@ class ContactsFragment extends ListFragment {
getActivity.asInstanceOf[EnsiChatActivity].runOnServiceConnected(() => {
database.getContacts.foreach(adapter.add)
database.runOnContactsUpdated(() => {
getActivity.runOnUiThread(new Runnable {
override def run(): Unit = {
adapter.clear()
database.getContacts.foreach(adapter.add)
}
})
})
database.runOnContactsUpdated(this)
})
}
@ -68,4 +62,12 @@ class ContactsFragment extends ListFragment {
override def onListItemClick(l: ListView, v: View, position: Int, id: Long): Unit =
getActivity.asInstanceOf[MainActivity].openChat(adapter.getItem(position).Address)
override def onContactsUpdated(): Unit = {
getActivity.runOnUiThread(new Runnable {
override def run(): Unit = {
adapter.clear()
database.getContacts.foreach(adapter.add)
}
})
}
}

View file

@ -7,8 +7,9 @@ import android.database.sqlite.{SQLiteDatabase, SQLiteOpenHelper}
import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
import com.nutomic.ensichat.protocol._
import com.nutomic.ensichat.protocol.messages._
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
import scala.collection.SortedSet
import scala.collection.{mutable, SortedSet}
import scala.collection.immutable.TreeSet
object Database {
@ -29,6 +30,10 @@ object Database {
"address text not null," +
"name text not null)"
trait OnContactsUpdatedListener {
def onContactsUpdated()
}
}
/**
@ -38,7 +43,8 @@ class Database(context: Context)
extends SQLiteOpenHelper(context, Database.DatabaseName, null, Database.DatabaseVersion)
with OnMessageReceivedListener {
private var contactsUpdatedListeners = Set[() => Unit]()
private var contactsUpdatedListeners =
new mutable.WeakHashMap[OnContactsUpdatedListener, Unit].keySet
override def onCreate(db: SQLiteDatabase): Unit = {
db.execSQL(Database.CreateContactsTable)
@ -127,20 +133,21 @@ class Database(context: Context)
cv.put("address", contact.Address.toString)
cv.put("name", contact.Name.toString)
getWritableDatabase.insert("contacts", null, cv)
contactsUpdatedListeners.foreach(_())
contactsUpdatedListeners.foreach(_.onContactsUpdated() )
}
def changeContactName(contact: User): Unit = {
val cv = new ContentValues()
cv.put("name", contact.Name.toString)
getWritableDatabase.update("contacts", cv, "address = ?", Array(contact.Address.toString))
contactsUpdatedListeners.foreach(_())
contactsUpdatedListeners.foreach(_.onContactsUpdated())
}
/**
* Pass a callback that is called whenever a new contact is added.
*/
def runOnContactsUpdated(l: () => Unit): Unit = contactsUpdatedListeners += l
def runOnContactsUpdated(listener: OnContactsUpdatedListener) =
contactsUpdatedListeners += listener
override def onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int): Unit = {
}