add address handling at the Edit screen

This commit is contained in:
tibbi 2018-02-21 20:45:39 +01:00
parent 8459160d6e
commit ad669b0fc6
3 changed files with 156 additions and 5 deletions

View File

@ -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<Address> {
val addresses = ArrayList<Address>()
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<Event> {
val unknown = getString(R.string.unknown)
val events = ArrayList<Event>()
@ -565,6 +628,17 @@ class EditContactActivity : ContactActivity() {
}
}
private fun addNewAddressField() {
val addressHolder = layoutInflater.inflate(R.layout.item_edit_address, contact_addresses_holder, false) as ViewGroup
updateTextColors(addressHolder)
setupAddressTypePicker(addressHolder.contact_address_type)
contact_addresses_holder.addView(addressHolder)
contact_addresses_holder.onGlobalLayout {
addressHolder.contact_address.requestFocus()
showKeyboard(addressHolder.contact_address)
}
}
private fun addNewEventField() {
val eventHolder = layoutInflater.inflate(R.layout.item_event, contact_events_holder, false) as ViewGroup
updateTextColors(eventHolder)

View File

@ -213,6 +213,44 @@
android:paddingTop="@dimen/medium_margin"
android:src="@drawable/ic_plus"/>
<ImageView
android:id="@+id/contact_address_image"
android:layout_width="@dimen/contact_icons_size"
android:layout_height="@dimen/contact_icons_size"
android:layout_alignTop="@+id/contact_addresses_holder"
android:paddingBottom="@dimen/small_margin"
android:paddingEnd="@dimen/small_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/medium_margin"
android:src="@drawable/ic_place"/>
<LinearLayout
android:id="@+id/contact_addresses_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contact_email_add_new"
android:layout_marginTop="@dimen/medium_margin"
android:layout_toRightOf="@+id/contact_name_image"
android:orientation="vertical">
<include layout="@layout/item_edit_address"/>
</LinearLayout>
<ImageView
android:id="@+id/contact_address_add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/contact_addresses_holder"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/small_margin"
android:background="@drawable/button_background"
android:paddingBottom="@dimen/medium_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/medium_margin"
android:src="@drawable/ic_plus"/>
<ImageView
android:id="@+id/contact_event_image"
android:layout_width="@dimen/contact_icons_size"
@ -228,7 +266,7 @@
android:id="@+id/contact_events_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contact_email_add_new"
android:layout_below="@+id/contact_address_add_new"
android:layout_marginTop="@dimen/medium_margin"
android:layout_toRightOf="@+id/contact_name_image"
android:orientation="vertical">

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_address_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/contact_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/contact_address_type"
android:layout_toStartOf="@+id/contact_address_type"
android:hint="@string/address"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/contact_address_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/contact_address"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/contact_address"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:paddingLeft="@dimen/medium_margin"
android:paddingRight="@dimen/medium_margin"
android:text="@string/address"
android:textSize="@dimen/bigger_text_size"/>
</RelativeLayout>