Skip to content

Commit

Permalink
Add option to edit structured addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanritscher committed Sep 11, 2024
1 parent d98ee8a commit 30c4ffe
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import org.fossify.contacts.helpers.ADD_NEW_CONTACT_NUMBER
import org.fossify.contacts.helpers.IS_FROM_SIMPLE_CONTACTS
import org.fossify.contacts.helpers.KEY_EMAIL
import org.fossify.contacts.helpers.KEY_NAME
import java.util.LinkedList

class EditContactActivity : ContactActivity() {
companion object {
Expand Down Expand Up @@ -610,18 +611,40 @@ class EditContactActivity : ContactActivity() {

private fun setupAddresses() {
contact!!.addresses.forEachIndexed { index, address ->
val addressHolderView = binding.contactAddressesHolder.getChildAt(index)
val addressHolder = if (addressHolderView == null) {
ItemEditAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false).apply {
binding.contactAddressesHolder.addView(root)
if (config.showContactFields and SHOW_STRUCTURED_ADDRESSES_FIELD != 0) {
var structuredAddressHolderView = binding.contactAddressesHolder.getChildAt(index)
var structuredAddressHolder = if (structuredAddressHolderView == null) {
ItemEditStructuredAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false).apply {
binding.contactAddressesHolder.addView(root)
}
} else {
ItemEditStructuredAddressBinding.bind(structuredAddressHolderView)
}

structuredAddressHolder.apply {
contactStreet.setText(address.street)
contactNeighborhood.setText(address.neighborhood)
contactCity.setText(address.city)
contactPostcode.setText(address.postcode)
contactPobox.setText(address.pobox)
contactRegion.setText(address.region)
contactCountry.setText(address.country)
setupAddressTypePicker(contactStructuredAddressType, address.type, address.label)
}
} else {
ItemEditAddressBinding.bind(addressHolderView)
}
val addressHolderView = binding.contactAddressesHolder.getChildAt(index)
val addressHolder = if (addressHolderView == null) {
ItemEditAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false).apply {
binding.contactAddressesHolder.addView(root)
}
} else {
ItemEditAddressBinding.bind(addressHolderView)
}

addressHolder.apply {
contactAddress.setText(address.value)
setupAddressTypePicker(contactAddressType, address.type, address.label)
addressHolder.apply {
contactAddress.setText(address.value)
setupAddressTypePicker(contactAddressType, address.type, address.label)
}
}
}
}
Expand Down Expand Up @@ -828,10 +851,7 @@ class EditContactActivity : ContactActivity() {
}

if (contact!!.addresses.isEmpty()) {
val addressHolder = ItemEditAddressBinding.bind(binding.contactAddressesHolder.getChildAt(0))
addressHolder.contactAddressType.apply {
setupAddressTypePicker(this, DEFAULT_ADDRESS_TYPE, "")
}
addNewAddressField()
}

if (contact!!.IMs.isEmpty()) {
Expand Down Expand Up @@ -1188,13 +1208,41 @@ class EditContactActivity : ContactActivity() {
val addresses = ArrayList<Address>()
val addressesCount = binding.contactAddressesHolder.childCount
for (i in 0 until addressesCount) {
val addressHolder = ItemEditAddressBinding.bind(binding.contactAddressesHolder.getChildAt(i))
val address = addressHolder.contactAddress.value
val addressType = getAddressTypeId(addressHolder.contactAddressType.value)
val addressLabel = if (addressType == StructuredPostal.TYPE_CUSTOM) addressHolder.contactAddressType.value else ""
if (config.showContactFields and SHOW_STRUCTURED_ADDRESSES_FIELD != 0) {
val structuredAddressHolder = ItemEditStructuredAddressBinding.bind(binding.contactAddressesHolder.getChildAt(i))
val street = structuredAddressHolder.contactStreet.value
val neighborhood = structuredAddressHolder.contactNeighborhood.value
val city = structuredAddressHolder.contactCity.value
val postcode = structuredAddressHolder.contactPostcode.value
val pobox = structuredAddressHolder.contactPobox.value
val region = structuredAddressHolder.contactRegion.value
val country = structuredAddressHolder.contactCountry.value

/* from DAVdroid */
val lineStreet = arrayOf(street, pobox, neighborhood).filterNot { it.isNullOrEmpty() }.joinToString(" ")
val lineLocality = arrayOf(postcode, city).filterNot { it.isNullOrEmpty() }.joinToString(" ")
val lines = LinkedList<String>()
if (!lineStreet.isEmpty()) lines += lineStreet
if (!lineLocality.isEmpty()) lines += lineLocality
if (!region.isNullOrEmpty()) lines += region
if (!country.isNullOrEmpty()) lines += country.toUpperCase()
val address = lines.joinToString("\n")
val addressType = getAddressTypeId(structuredAddressHolder.contactStructuredAddressType.value)
val addressLabel = if (addressType == StructuredPostal.TYPE_CUSTOM) structuredAddressHolder.contactStructuredAddressType.value else ""

if (address.isNotEmpty()) {
addresses.add(Address(address, addressType, addressLabel, country, region, city, postcode, pobox,
street, neighborhood))
}
} else {
val addressHolder = ItemEditAddressBinding.bind(binding.contactAddressesHolder.getChildAt(i))
val address = addressHolder.contactAddress.value
val addressType = getAddressTypeId(addressHolder.contactAddressType.value)
val addressLabel = if (addressType == StructuredPostal.TYPE_CUSTOM) addressHolder.contactAddressType.value else ""

if (address.isNotEmpty()) {
addresses.add(Address(address, addressType, addressLabel))
if (address.isNotEmpty()) {
addresses.add(Address(address, addressType, addressLabel, "", "", "", "", "", "", ""))
}
}
}
return addresses
Expand Down Expand Up @@ -1375,13 +1423,24 @@ class EditContactActivity : ContactActivity() {
}

private fun addNewAddressField() {
val addressHolder = ItemEditAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false)
updateTextColors(addressHolder.root)
setupAddressTypePicker(addressHolder.contactAddressType, DEFAULT_ADDRESS_TYPE, "")
binding.contactAddressesHolder.addView(addressHolder.root)
binding.contactAddressesHolder.onGlobalLayout {
addressHolder.contactAddress.requestFocus()
showKeyboard(addressHolder.contactAddress)
if (config.showContactFields and SHOW_STRUCTURED_ADDRESSES_FIELD != 0) {
val structutedAddressHolder = ItemEditStructuredAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false)
updateTextColors(structutedAddressHolder.root)
setupAddressTypePicker(structutedAddressHolder.contactStructuredAddressType, DEFAULT_ADDRESS_TYPE, "")
binding.contactAddressesHolder.addView(structutedAddressHolder.root)
binding.contactAddressesHolder.onGlobalLayout {
structutedAddressHolder.contactStreet.requestFocus()
showKeyboard(structutedAddressHolder.contactStreet)
}
} else {
val addressHolder = ItemEditAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false)
updateTextColors(addressHolder.root)
setupAddressTypePicker(addressHolder.contactAddressType, DEFAULT_ADDRESS_TYPE, "")
binding.contactAddressesHolder.addView(addressHolder.root)
binding.contactAddressesHolder.onGlobalLayout {
addressHolder.contactAddress.requestFocus()
showKeyboard(addressHolder.contactAddress)
}
}
}

Expand Down Expand Up @@ -1473,8 +1532,15 @@ class EditContactActivity : ContactActivity() {
private fun parseAddress(contentValues: ContentValues) {
val type = contentValues.getAsInteger(StructuredPostal.DATA2) ?: DEFAULT_ADDRESS_TYPE
val addressValue = contentValues.getAsString(StructuredPostal.DATA4)
?: contentValues.getAsString(StructuredPostal.DATA1) ?: return
val address = Address(addressValue, type, "")
?: contentValues.getAsString(StructuredPostal.DATA1) ?: return
val country = contentValues.getAsString(StructuredPostal.COUNTRY)
val region = contentValues.getAsString(StructuredPostal.REGION)
val city = contentValues.getAsString(StructuredPostal.CITY)
val postcode = contentValues.getAsString(StructuredPostal.POSTCODE)
val pobox = contentValues.getAsString(StructuredPostal.POBOX)
val street = contentValues.getAsString(StructuredPostal.STREET)
val neighborhood = contentValues.getAsString(StructuredPostal.NEIGHBORHOOD)
val address = Address(addressValue, type, "", country, region, city, postcode, pobox, street, neighborhood)
contact!!.addresses.add(address)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ManageVisibleFieldsDialog(val activity: BaseSimpleActivity, val callback:
put(SHOW_EMAILS_FIELD, R.id.manage_visible_fields_emails)
put(SHOW_ADDRESSES_FIELD, R.id.manage_visible_fields_addresses)
put(SHOW_IMS_FIELD, R.id.manage_visible_fields_ims)
put(SHOW_STRUCTURED_ADDRESSES_FIELD, R.id.manage_visible_fields_structured_addresses)
put(SHOW_EVENTS_FIELD, R.id.manage_visible_fields_events)
put(SHOW_NOTES_FIELD, R.id.manage_visible_fields_notes)
put(SHOW_ORGANIZATION_FIELD, R.id.manage_visible_fields_organization)
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,18 @@ class VcfExporter {

contact.addresses.forEach {
val address = Address()
address.streetAddress = it.value
if (!it.country.isNullOrEmpty() || !it.region.isNullOrEmpty() || !it.city.isNullOrEmpty() || !it.postcode.isNullOrEmpty() ||
!it.pobox.isNullOrEmpty() || !it.street.isNullOrEmpty() || !it.neighborhood.isNullOrEmpty()) {
address.country = it.country
address.region = it.region
address.locality = it.city
address.postalCode = it.postcode
address.poBox = it.pobox
address.streetAddress = it.street
address.extendedAddress = it.neighborhood
} else {
address.streetAddress = it.value
}
address.parameters.addType(getAddressTypeLabel(it.type, it.label))
card.addAddress(address)
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/kotlin/org/fossify/contacts/helpers/VcfImporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class VcfImporter(val activity: SimpleActivity) {
} else {
""
}
val country = it.country ?: ""
val region = it.region ?: ""
val city = it.locality ?: ""
val postcode = it.postalCode ?: ""
val pobox = it.poBox ?: ""
val street = it.streetAddress ?: ""
val neighborhood = it.extendedAddress ?: ""

if (it.locality?.isNotEmpty() == true) {
address += " ${it.locality} "
Expand All @@ -116,8 +123,8 @@ class VcfImporter(val activity: SimpleActivity) {

address = address.trim()

if (address.isNotEmpty()) {
addresses.add(Address(address, type, label))
if (address?.isNotEmpty() == true) {
addresses.add(Address(address, type, label, country, region, city, postcode, pobox, street, neighborhood))
}
}

Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/layout/activity_edit_contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@
android:layout_toEndOf="@+id/contact_name_image"
android:orientation="vertical">

<include layout="@layout/item_edit_address" />

</LinearLayout>

<ImageView
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/dialog_manage_visible_fields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@
android:paddingBottom="@dimen/activity_margin"
android:text="@string/instant_messaging" />

<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/manage_visible_fields_structured_addresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:text="@string/structured_addresses"/>

<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/manage_visible_fields_events"
android:layout_width="match_parent"
Expand Down
141 changes: 141 additions & 0 deletions app/src/main/res/layout/item_edit_structured_address.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_structured_address_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="@dimen/activity_margin">

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_street"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="@string/street"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>

<org.fossify.commons.views.MyTextView
android:id="@+id/contact_structured_address_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/contact_street"
android:layout_alignTop="@+id/contact_street"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="?attr/selectableItemBackground"
android:paddingLeft="@dimen/medium_margin"
android:paddingRight="@dimen/medium_margin"
android:text="@string/address"
android:textSize="@dimen/bigger_text_size"/>

<LinearLayout
android:id="@+id/contact_neighborhood_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contact_street"
android:orientation="horizontal"
android:baselineAligned="false">

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_neighborhood"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toLeftOf="@+id/contact_pobox"
android:layout_toStartOf="@+id/contact_pobox"
android:hint="@string/neighborhood"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_pobox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignBottom="@+id/contact_neighborhood"
android:layout_alignTop="@+id/contact_neighborhood"
android:hint="@string/pobox"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>
</LinearLayout>

<LinearLayout
android:id="@+id/contact_city_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/contact_neighborhood_holder"
android:orientation="horizontal"
android:baselineAligned="false">

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_postcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/postcode"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignBottom="@+id/contact_postcode"
android:layout_alignTop="@+id/contact_postcode"
android:hint="@string/city"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>
</LinearLayout>

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_region"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/contact_city_holder"
android:hint="@string/region"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>

<org.fossify.commons.views.MyEditText
android:id="@+id/contact_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/contact_region"
android:hint="@string/country"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size"/>
</RelativeLayout>
Loading

0 comments on commit 30c4ffe

Please sign in to comment.