Move contacts updated listeners to database object so we don't need a singleton.
This commit is contained in:
parent
879c80e11c
commit
61f123c309
7 changed files with 27 additions and 20 deletions
|
@ -10,7 +10,7 @@ import com.nutomic.ensichat.R
|
|||
import com.nutomic.ensichat.protocol.{User, ChatService}
|
||||
import com.nutomic.ensichat.protocol.messages.RequestAddContact
|
||||
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
|
||||
import com.nutomic.ensichat.util.UsersAdapter
|
||||
import com.nutomic.ensichat.util.{Database, UsersAdapter}
|
||||
|
||||
/**
|
||||
* Lists all nearby, connected devices and allows adding them to be added as contacts.
|
||||
|
@ -20,6 +20,8 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
|
|||
|
||||
private val Tag = "AddContactsActivity"
|
||||
|
||||
private lazy val database = new Database(this)
|
||||
|
||||
private lazy val adapter = new UsersAdapter(this)
|
||||
|
||||
/**
|
||||
|
@ -37,7 +39,7 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
|
|||
|
||||
runOnServiceConnected(() => {
|
||||
service.registerConnectionListener(AddContactsActivity.this)
|
||||
service.database.runOnContactsUpdated(this)
|
||||
database.runOnContactsUpdated(this)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -69,7 +71,7 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
|
|||
runOnUiThread(new Runnable {
|
||||
override def run(): Unit = {
|
||||
adapter.clear()
|
||||
(service.connections().map(a => service.getUser(a)) -- service.database.getContacts)
|
||||
(service.connections().map(a => service.getUser(a)) -- database.getContacts)
|
||||
.foreach(adapter.add)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -11,7 +11,7 @@ import com.nutomic.ensichat.R
|
|||
import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
|
||||
import com.nutomic.ensichat.protocol.messages.{Message, RequestAddContact, ResultAddContact}
|
||||
import com.nutomic.ensichat.protocol.{Address, Crypto}
|
||||
import com.nutomic.ensichat.util.IdenticonGenerator
|
||||
import com.nutomic.ensichat.util.{Database, IdenticonGenerator}
|
||||
|
||||
object ConfirmAddContactDialog {
|
||||
|
||||
|
@ -27,6 +27,8 @@ class ConfirmAddContactDialog extends EnsiChatActivity with OnMessageReceivedLis
|
|||
|
||||
private val Tag = "ConfirmAddContactDialog"
|
||||
|
||||
private lazy val database = new Database(this)
|
||||
|
||||
private lazy val user = service.getUser(
|
||||
new Address(getIntent.getStringExtra(ConfirmAddContactDialog.ExtraContactAddress)))
|
||||
|
||||
|
@ -90,7 +92,7 @@ class ConfirmAddContactDialog extends EnsiChatActivity with OnMessageReceivedLis
|
|||
if (localConfirmed && remoteConfirmed) {
|
||||
Log.i(Tag, "Adding new contact " + user.toString)
|
||||
// Get the user again, in case it was updated in the mean time.
|
||||
service.database.addContact(service.getUser(user.address))
|
||||
database.addContact(service.getUser(user.address))
|
||||
Toast.makeText(this, getString(R.string.contact_added, user.name), Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
finish()
|
||||
|
|
|
@ -12,7 +12,7 @@ import com.nutomic.ensichat.activities.EnsiChatActivity
|
|||
import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
|
||||
import com.nutomic.ensichat.protocol.messages.{Message, Text}
|
||||
import com.nutomic.ensichat.protocol.{Address, ChatService}
|
||||
import com.nutomic.ensichat.util.MessagesAdapter
|
||||
import com.nutomic.ensichat.util.{Database, MessagesAdapter}
|
||||
|
||||
/**
|
||||
* Represents a single chat with another specific device.
|
||||
|
@ -28,6 +28,8 @@ class ChatFragment extends ListFragment with OnClickListener
|
|||
this.address = address
|
||||
}
|
||||
|
||||
private val database = new Database(getActivity)
|
||||
|
||||
private var address: Address = _
|
||||
|
||||
private var chatService: ChatService = _
|
||||
|
@ -47,13 +49,12 @@ class ChatFragment extends ListFragment with OnClickListener
|
|||
activity.runOnServiceConnected(() => {
|
||||
chatService = activity.service
|
||||
|
||||
chatService.database.getContact(address)
|
||||
.foreach(c => getActivity.setTitle(c.name))
|
||||
database.getContact(address).foreach(c => getActivity.setTitle(c.name))
|
||||
|
||||
// Read local device ID from service,
|
||||
adapter = new MessagesAdapter(getActivity, address)
|
||||
chatService.registerMessageListener(ChatFragment.this)
|
||||
chatService.database.getMessages(address, 15).foreach(adapter.add)
|
||||
database.getMessages(address, 15).foreach(adapter.add)
|
||||
|
||||
if (listView != null) {
|
||||
listView.setAdapter(adapter)
|
||||
|
|
|
@ -9,7 +9,7 @@ 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
|
||||
import com.nutomic.ensichat.util.{Database, UsersAdapter}
|
||||
|
||||
/**
|
||||
* Lists all nearby, connected devices.
|
||||
|
@ -18,7 +18,7 @@ class ContactsFragment extends ListFragment with OnContactsUpdatedListener {
|
|||
|
||||
private lazy val adapter = new UsersAdapter(getActivity)
|
||||
|
||||
private lazy val database = getActivity.asInstanceOf[EnsiChatActivity].service.database
|
||||
private lazy val database = new Database(getActivity)
|
||||
|
||||
override def onCreate(savedInstanceState: Bundle): Unit = {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.nutomic.ensichat.R
|
|||
import com.nutomic.ensichat.activities.EnsiChatActivity
|
||||
import com.nutomic.ensichat.fragments.SettingsFragment._
|
||||
import com.nutomic.ensichat.protocol.messages.UserName
|
||||
import com.nutomic.ensichat.util.Database
|
||||
|
||||
object SettingsFragment {
|
||||
|
||||
|
@ -25,6 +26,8 @@ object SettingsFragment {
|
|||
*/
|
||||
class SettingsFragment extends PreferenceFragment with OnPreferenceChangeListener {
|
||||
|
||||
private lazy val database = new Database(getActivity)
|
||||
|
||||
override def onCreate(savedInstanceState: Bundle): Unit = {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
@ -53,8 +56,7 @@ class SettingsFragment extends PreferenceFragment with OnPreferenceChangeListene
|
|||
override def onPreferenceChange(preference: Preference, newValue: AnyRef): Boolean = {
|
||||
if (preference.getKey == KeyUserName) {
|
||||
val service = getActivity.asInstanceOf[EnsiChatActivity].service
|
||||
service.database.getContacts
|
||||
.foreach(c => service.sendTo(c.address, new UserName(newValue.toString)))
|
||||
database.getContacts.foreach(c => service.sendTo(c.address, new UserName(newValue.toString)))
|
||||
}
|
||||
preference.setSummary(newValue.toString)
|
||||
true
|
||||
|
|
|
@ -51,7 +51,7 @@ class ChatService extends Service {
|
|||
|
||||
private val Tag = "ChatService"
|
||||
|
||||
lazy val database = new Database(this)
|
||||
private lazy val database = new Database(this)
|
||||
|
||||
private val mainHandler = new Handler()
|
||||
|
||||
|
|
|
@ -34,6 +34,9 @@ object Database {
|
|||
def onContactsUpdated()
|
||||
}
|
||||
|
||||
private var contactsUpdatedListeners =
|
||||
new mutable.WeakHashMap[OnContactsUpdatedListener, Unit].keySet
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,9 +46,6 @@ class Database(context: Context)
|
|||
extends SQLiteOpenHelper(context, Database.DatabaseName, null, Database.DatabaseVersion)
|
||||
with OnMessageReceivedListener {
|
||||
|
||||
private var contactsUpdatedListeners =
|
||||
new mutable.WeakHashMap[OnContactsUpdatedListener, Unit].keySet
|
||||
|
||||
override def onCreate(db: SQLiteDatabase): Unit = {
|
||||
db.execSQL(Database.CreateContactsTable)
|
||||
db.execSQL(Database.CreateMessagesTable)
|
||||
|
@ -133,21 +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(_.onContactsUpdated() )
|
||||
Database.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(_.onContactsUpdated())
|
||||
Database.contactsUpdatedListeners.foreach(_.onContactsUpdated())
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a callback that is called whenever a new contact is added.
|
||||
*/
|
||||
def runOnContactsUpdated(listener: OnContactsUpdatedListener) =
|
||||
contactsUpdatedListeners += listener
|
||||
Database.contactsUpdatedListeners += listener
|
||||
|
||||
override def onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int): Unit = {
|
||||
}
|
||||
|
|
Reference in a new issue