diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt index 8b109b16..82ea6e14 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt @@ -22,11 +22,9 @@ import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.extensions.* import com.simplemobiletools.contacts.helpers.* -import com.simplemobiletools.contacts.models.Contact -import com.simplemobiletools.contacts.models.Email -import com.simplemobiletools.contacts.models.Event -import com.simplemobiletools.contacts.models.PhoneNumber +import com.simplemobiletools.contacts.models.* import kotlinx.android.synthetic.main.activity_edit_contact.* +import kotlinx.android.synthetic.main.item_edit_address.view.* import kotlinx.android.synthetic.main.item_edit_email.view.* import kotlinx.android.synthetic.main.item_edit_phone_number.view.* import kotlinx.android.synthetic.main.item_event.view.* @@ -164,6 +162,7 @@ class EditContactActivity : ContactActivity() { contact_name_image.applyColorFilter(textColor) contact_number_image.applyColorFilter(textColor) contact_email_image.applyColorFilter(textColor) + contact_address_image.applyColorFilter(textColor) contact_event_image.applyColorFilter(textColor) contact_source_image.applyColorFilter(textColor) @@ -172,6 +171,8 @@ class EditContactActivity : ContactActivity() { contact_number_add_new.background.applyColorFilter(textColor) contact_email_add_new.applyColorFilter(adjustedPrimaryColor) contact_email_add_new.background.applyColorFilter(textColor) + contact_address_add_new.applyColorFilter(adjustedPrimaryColor) + contact_address_add_new.background.applyColorFilter(textColor) contact_event_add_new.applyColorFilter(adjustedPrimaryColor) contact_event_add_new.background.applyColorFilter(textColor) @@ -182,6 +183,7 @@ class EditContactActivity : ContactActivity() { contact_send_email.setOnClickListener { trySendEmail() } contact_number_add_new.setOnClickListener { addNewPhoneNumberField() } contact_email_add_new.setOnClickListener { addNewEmailField() } + contact_address_add_new.setOnClickListener { addNewAddressField() } contact_event_add_new.setOnClickListener { addNewEventField() } contact_toggle_favorite.apply { @@ -232,6 +234,7 @@ class EditContactActivity : ContactActivity() { setupPhoneNumbers() setupEmails() + setupAddresses() setupEvents() } @@ -265,6 +268,21 @@ class EditContactActivity : ContactActivity() { } } + private fun setupAddresses() { + contact!!.addresses.forEachIndexed { index, address -> + var addressHolder = contact_addresses_holder.getChildAt(index) + if (addressHolder == null) { + addressHolder = layoutInflater.inflate(R.layout.item_edit_address, contact_addresses_holder, false) + contact_addresses_holder.addView(addressHolder) + } + + addressHolder!!.apply { + contact_address.setText(address.value) + setupAddressTypePicker(contact_address_type, address.type) + } + } + } + private fun setupEvents() { contact!!.events.forEachIndexed { index, event -> var eventHolder = contact_events_holder.getChildAt(index) @@ -322,6 +340,13 @@ class EditContactActivity : ContactActivity() { } } + if (contact!!.addresses.isEmpty()) { + val addressHolder = contact_addresses_holder.getChildAt(0) + (addressHolder as? ViewGroup)?.contact_address_type?.apply { + setupAddressTypePicker(this) + } + } + if (contact!!.events.isEmpty()) { val eventHolder = contact_events_holder.getChildAt(0) (eventHolder as? ViewGroup)?.apply { @@ -348,6 +373,15 @@ class EditContactActivity : ContactActivity() { } } + private fun setupAddressTypePicker(addressTypeField: TextView, type: Int = DEFAULT_ADDRESS_TYPE) { + addressTypeField.apply { + setText(getAddressTextId(type)) + setOnClickListener { + showAddressTypePicker(it as TextView) + } + } + } + private fun setupEventTypePicker(eventHolder: ViewGroup, type: Int = DEFAULT_EVENT_TYPE) { eventHolder.contact_event_type.apply { setText(getEventTextId(type)) @@ -423,6 +457,19 @@ class EditContactActivity : ContactActivity() { } } + private fun showAddressTypePicker(addressTypeField: TextView) { + val items = arrayListOf( + RadioItem(CommonDataKinds.StructuredPostal.TYPE_HOME, getString(R.string.home)), + RadioItem(CommonDataKinds.StructuredPostal.TYPE_WORK, getString(R.string.work)), + RadioItem(CommonDataKinds.StructuredPostal.TYPE_OTHER, getString(R.string.other)) + ) + + val currentAddressTypeId = getAddressTypeId(addressTypeField.value) + RadioGroupDialog(this, items, currentAddressTypeId) { + addressTypeField.setText(getAddressTextId(it as Int)) + } + } + private fun showEventTypePicker(eventTypeField: TextView) { val items = arrayListOf( RadioItem(CommonDataKinds.Event.TYPE_BIRTHDAY, getString(R.string.birthday)), @@ -450,6 +497,7 @@ class EditContactActivity : ContactActivity() { photoUri = currentContactPhotoPath phoneNumbers = getFilledPhoneNumbers() emails = getFilledEmails() + addresses = getFilledAddresses() events = getFilledEvents() source = contact!!.source starred = if (isContactStarred()) 1 else 0 @@ -496,6 +544,21 @@ class EditContactActivity : ContactActivity() { return emails } + private fun getFilledAddresses(): ArrayList
{ + val addresses = ArrayList() + val addressesCount = contact_addresses_holder.childCount + for (i in 0 until addressesCount) { + val addressHolder = contact_addresses_holder.getChildAt(i) + val address = addressHolder.contact_address.value + val addressType = getAddressTypeId(addressHolder.contact_address_type.value) + + if (address.isNotEmpty()) { + addresses.add(Address(address, addressType)) + } + } + return addresses + } + private fun getFilledEvents(): ArrayList