differentiate between creating a new contact and editing one
This commit is contained in:
parent
aeaf2bdf99
commit
5ffe57251a
|
@ -2,35 +2,113 @@ package com.simplemobiletools.contacts.activities
|
|||
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.commons.extensions.applyColorFilter
|
||||
import com.simplemobiletools.commons.extensions.getContrastColor
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
|
||||
import com.simplemobiletools.contacts.R
|
||||
import com.simplemobiletools.contacts.extensions.config
|
||||
import com.simplemobiletools.contacts.helpers.CONTACT_ID
|
||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||
import com.simplemobiletools.contacts.models.Contact
|
||||
import kotlinx.android.synthetic.main.activity_contact.*
|
||||
|
||||
class ContactActivity : SimpleActivity() {
|
||||
private var wasActivityInitialized = false
|
||||
private var contact: Contact? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_contact)
|
||||
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_cross)
|
||||
|
||||
handlePermission(PERMISSION_WRITE_CONTACTS) {
|
||||
if (it) {
|
||||
initContact()
|
||||
} else {
|
||||
toast(R.string.no_contacts_permission)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun initContact() {
|
||||
val contactId = intent.getIntExtra(CONTACT_ID, 0)
|
||||
if (contactId != 0) {
|
||||
contact = ContactsHelper(this).getContactWithId(contactId)
|
||||
if (contact == null) {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
finish()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (contact != null) {
|
||||
setupEditContact()
|
||||
} else {
|
||||
setupNewContact()
|
||||
}
|
||||
|
||||
contact_photo.applyColorFilter(config.primaryColor.getContrastColor())
|
||||
contact_photo.background = ColorDrawable(config.primaryColor)
|
||||
|
||||
val textColor = config.textColor
|
||||
contact_sms.applyColorFilter(textColor)
|
||||
contact_call.applyColorFilter(textColor)
|
||||
contact_email.applyColorFilter(textColor)
|
||||
contact_send_sms.applyColorFilter(textColor)
|
||||
contact_start_call.applyColorFilter(textColor)
|
||||
contact_send_email.applyColorFilter(textColor)
|
||||
contact_name_image.applyColorFilter(textColor)
|
||||
contact_number_image.applyColorFilter(textColor)
|
||||
contact_email_image.applyColorFilter(textColor)
|
||||
|
||||
contact_photo.setOnClickListener { }
|
||||
contact_sms.setOnClickListener { }
|
||||
contact_call.setOnClickListener { }
|
||||
contact_send_sms.setOnClickListener { }
|
||||
contact_start_call.setOnClickListener { }
|
||||
contact_email.setOnClickListener { }
|
||||
|
||||
updateTextColors(contact_scrollview)
|
||||
wasActivityInitialized = true
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.menu_contact, menu)
|
||||
if (wasActivityInitialized) {
|
||||
menu.findItem(R.id.delete).isVisible = contact?.id != 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.save -> saveContact()
|
||||
R.id.delete -> deleteContact()
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun setupEditContact() {
|
||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
||||
supportActionBar?.title = resources.getString(R.string.edit_contact)
|
||||
contact_name.setText(contact!!.name)
|
||||
contact_number.setText(contact!!.number)
|
||||
contact_email.setText(contact!!.email)
|
||||
}
|
||||
|
||||
private fun setupNewContact() {
|
||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
|
||||
supportActionBar?.title = resources.getString(R.string.new_contact)
|
||||
}
|
||||
|
||||
private fun saveContact() {
|
||||
|
||||
}
|
||||
|
||||
private fun deleteContact() {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import com.simplemobiletools.contacts.R
|
|||
import com.simplemobiletools.contacts.adapters.ContactsAdapter
|
||||
import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog
|
||||
import com.simplemobiletools.contacts.extensions.config
|
||||
import com.simplemobiletools.contacts.helpers.CONTACT_ID
|
||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||
import com.simplemobiletools.contacts.models.Contact
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
|
@ -138,7 +139,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||
}
|
||||
|
||||
private fun itemClicked(contact: Contact) {
|
||||
|
||||
Intent(applicationContext, ContactActivity::class.java).apply {
|
||||
putExtra(CONTACT_ID, contact.id)
|
||||
startActivity(this)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addNewContact() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
package com.simplemobiletools.contacts.helpers
|
||||
|
||||
val SORTING = "sorting"
|
||||
val CONTACT_ID = "contact_id"
|
||||
|
|
|
@ -13,13 +13,7 @@ class ContactsHelper(val activity: SimpleActivity) {
|
|||
val contacts = ArrayList<Contact>()
|
||||
Thread {
|
||||
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
|
||||
ContactsContract.CommonDataKinds.Phone.NUMBER,
|
||||
ContactsContract.CommonDataKinds.Phone.PHOTO_URI
|
||||
)
|
||||
|
||||
val projection = getContactProjection()
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, null, null, null)
|
||||
|
@ -27,9 +21,9 @@ class ContactsHelper(val activity: SimpleActivity) {
|
|||
do {
|
||||
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
|
||||
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: continue
|
||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: continue
|
||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: ""
|
||||
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
|
||||
val contact = Contact(id, name, number, photoUri)
|
||||
val contact = Contact(id, name, number, photoUri, "")
|
||||
contacts.add(contact)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
|
@ -41,4 +35,36 @@ class ContactsHelper(val activity: SimpleActivity) {
|
|||
callback(contacts)
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun getContactWithId(id: Int): Contact? {
|
||||
if (id == 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||
val projection = getContactProjection()
|
||||
val selection = "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?"
|
||||
val selectionArgs = arrayOf(id.toString())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: return null
|
||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: ""
|
||||
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
|
||||
return Contact(id, name, number, photoUri, "")
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getContactProjection() = arrayOf(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
|
||||
ContactsContract.CommonDataKinds.Phone.NUMBER,
|
||||
ContactsContract.CommonDataKinds.Phone.PHOTO_URI
|
||||
)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.simplemobiletools.contacts.models
|
|||
import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER
|
||||
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
||||
|
||||
data class Contact(val id: Int, var name: String, var number: String, var photoUri: String) : Comparable<Contact> {
|
||||
data class Contact(val id: Int, var name: String, var number: String, var photoUri: String, var email: String) : Comparable<Contact> {
|
||||
companion object {
|
||||
var sorting: Int = 0
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
android:src="@drawable/ic_person"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/contact_sms"
|
||||
android:id="@+id/contact_send_sms"
|
||||
style="@style/MyBorderlessBackgroundStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
|
@ -34,14 +34,14 @@
|
|||
android:src="@drawable/ic_sms"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/contact_call"
|
||||
android:id="@+id/contact_start_call"
|
||||
style="@style/MyBorderlessBackgroundStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@id/contact_photo"
|
||||
android:layout_alignTop="@+id/contact_photo"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_toLeftOf="@+id/contact_sms"
|
||||
android:layout_toLeftOf="@+id/contact_send_sms"
|
||||
android:adjustViewBounds="true"
|
||||
android:paddingLeft="@dimen/activity_margin"
|
||||
android:paddingRight="@dimen/activity_margin"
|
||||
|
@ -49,14 +49,14 @@
|
|||
android:src="@drawable/ic_phone"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/contact_email"
|
||||
android:id="@+id/contact_send_email"
|
||||
style="@style/MyBorderlessBackgroundStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@id/contact_photo"
|
||||
android:layout_alignTop="@+id/contact_photo"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_toLeftOf="@+id/contact_call"
|
||||
android:layout_toLeftOf="@+id/contact_start_call"
|
||||
android:adjustViewBounds="true"
|
||||
android:paddingLeft="@dimen/activity_margin"
|
||||
android:paddingRight="@dimen/activity_margin"
|
||||
|
@ -67,14 +67,14 @@
|
|||
android:id="@+id/contact_name_image"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignBottom="@+id/contact_name_value"
|
||||
android:layout_alignTop="@+id/contact_name_value"
|
||||
android:layout_alignBottom="@+id/contact_name"
|
||||
android:layout_alignTop="@+id/contact_name"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:src="@drawable/ic_person"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyEditText
|
||||
android:id="@+id/contact_name_value"
|
||||
android:id="@+id/contact_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_photo"
|
||||
|
@ -93,17 +93,17 @@
|
|||
android:id="@+id/contact_number_image"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignBottom="@+id/contact_number_value"
|
||||
android:layout_alignTop="@+id/contact_number_value"
|
||||
android:layout_alignBottom="@+id/contact_number"
|
||||
android:layout_alignTop="@+id/contact_number"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:src="@drawable/ic_phone"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyEditText
|
||||
android:id="@+id/contact_number_value"
|
||||
android:id="@+id/contact_number"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_name_value"
|
||||
android:layout_below="@+id/contact_name"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginStart="@dimen/medium_margin"
|
||||
android:layout_marginTop="@dimen/activity_margin"
|
||||
|
@ -119,17 +119,17 @@
|
|||
android:id="@+id/contact_email_image"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignBottom="@+id/contact_email_value"
|
||||
android:layout_alignTop="@+id/contact_email_value"
|
||||
android:layout_alignBottom="@+id/contact_email"
|
||||
android:layout_alignTop="@+id/contact_email"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:src="@drawable/ic_email"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyEditText
|
||||
android:id="@+id/contact_email_value"
|
||||
android:id="@+id/contact_email"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_number_value"
|
||||
android:layout_below="@+id/contact_number"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginStart="@dimen/medium_margin"
|
||||
android:layout_marginTop="@dimen/activity_margin"
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/save"
|
||||
android:icon="@drawable/ic_check"
|
||||
android:title="@string/save"
|
||||
app:showAsAction="ifRoom"/>
|
||||
<item
|
||||
android:id="@+id/delete"
|
||||
android:icon="@drawable/ic_delete"
|
||||
android:title="@string/delete"
|
||||
app:showAsAction="ifRoom"/>
|
||||
</menu>
|
Loading…
Reference in New Issue