Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to edit structured addresses (new) #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ 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
import java.util.Locale

class EditContactActivity : ContactActivity() {
companion object {
Expand Down Expand Up @@ -609,19 +611,46 @@ class EditContactActivity : ContactActivity() {
}

private fun setupAddresses() {
if (binding.contactAddressesHolder.childCount > 0 &&
(binding.contactAddressesHolder.getChildAt(0).id == R.id.contact_structured_address_holder) !=
(config.showContactFields and SHOW_STRUCTURED_ADDRESSES_FIELD != 0)) {
// Config was changed with edit view loaded
binding.contactAddressesHolder.removeAllViews()
}
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)
var addressHolderView = binding.contactAddressesHolder.getChildAt(index)
if (config.showContactFields and SHOW_STRUCTURED_ADDRESSES_FIELD != 0) {
var structuredAddressHolder = if (addressHolderView == null) {
ItemEditStructuredAddressBinding.inflate(layoutInflater, binding.contactAddressesHolder, false).apply {
binding.contactAddressesHolder.addView(root)
}
} else {
ItemEditStructuredAddressBinding.bind(addressHolderView)
}

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 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 +857,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 +1214,44 @@ 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 ""
val addressHolderView = binding.contactAddressesHolder.getChildAt(i)
// Don't rely on config.showContactFields since that might just have changed
if (addressHolderView.id == R.id.contact_structured_address_holder) {
val structuredAddressHolder = ItemEditStructuredAddressBinding.bind(addressHolderView)
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).filter { it.isNotEmpty() }.joinToString(" ")
val lineLocality = arrayOf(postcode, city).filter { it.isNotEmpty() }.joinToString(" ")
val lines = LinkedList<String>()
if (lineStreet.isNotEmpty()) lines += lineStreet
if (lineLocality.isNotEmpty()) lines += lineLocality
if (region.isNotEmpty()) lines += region
if (country.isNotEmpty()) lines += country.uppercase(Locale.getDefault())
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(addressHolderView)
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 +1432,25 @@ 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 +1542,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
14 changes: 13 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 @@ -110,7 +110,19 @@ class VcfExporter {

contact.addresses.forEach {
val address = Address()
address.streetAddress = it.value
if (listOf(it.country, it.region, it.city, it.postcode, it.pobox, it.street, it.neighborhood)
.map{it.isNullOrEmpty()}
.fold(false){a, b -> a || b}) {
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
12 changes: 10 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,9 @@ 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
131 changes: 131 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,131 @@
<?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_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_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: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: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: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_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_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
Loading