From 24d08fba0618b480473338c26cc0de228389263f Mon Sep 17 00:00:00 2001 From: Stephan Ritscher Date: Tue, 23 Jan 2024 21:46:15 +0100 Subject: [PATCH] Add option to edit structured addresses --- .../activities/EditContactActivity.kt | 122 +++++++++++---- .../dialogs/ManageVisibleFieldsDialog.kt | 1 + .../fossify/contacts/helpers/VcfExporter.kt | 13 +- .../fossify/contacts/helpers/VcfImporter.kt | 11 +- .../main/res/layout/activity_edit_contact.xml | 2 - .../layout/dialog_manage_visible_fields.xml | 8 + .../layout/item_edit_structured_address.xml | 141 ++++++++++++++++++ app/src/main/res/values/strings.xml | 8 + gradle/libs.versions.toml | 4 +- 9 files changed, 275 insertions(+), 35 deletions(-) create mode 100644 app/src/main/res/layout/item_edit_structured_address.xml diff --git a/app/src/main/kotlin/org/fossify/contacts/activities/EditContactActivity.kt b/app/src/main/kotlin/org/fossify/contacts/activities/EditContactActivity.kt index bb0e0a60c..e903d6228 100644 --- a/app/src/main/kotlin/org/fossify/contacts/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/org/fossify/contacts/activities/EditContactActivity.kt @@ -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 { @@ -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) + } } } } @@ -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()) { @@ -1188,13 +1208,41 @@ class EditContactActivity : ContactActivity() { val addresses = ArrayList
() 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() + 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 @@ -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) + } } } @@ -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) } diff --git a/app/src/main/kotlin/org/fossify/contacts/dialogs/ManageVisibleFieldsDialog.kt b/app/src/main/kotlin/org/fossify/contacts/dialogs/ManageVisibleFieldsDialog.kt index 773fefd98..6b8f27494 100644 --- a/app/src/main/kotlin/org/fossify/contacts/dialogs/ManageVisibleFieldsDialog.kt +++ b/app/src/main/kotlin/org/fossify/contacts/dialogs/ManageVisibleFieldsDialog.kt @@ -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) diff --git a/app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt index 9e60f8c48..2e623bbad 100644 --- a/app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt @@ -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) } diff --git a/app/src/main/kotlin/org/fossify/contacts/helpers/VcfImporter.kt b/app/src/main/kotlin/org/fossify/contacts/helpers/VcfImporter.kt index dbfa1e121..d082b401a 100644 --- a/app/src/main/kotlin/org/fossify/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/org/fossify/contacts/helpers/VcfImporter.kt @@ -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} " @@ -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)) } } diff --git a/app/src/main/res/layout/activity_edit_contact.xml b/app/src/main/res/layout/activity_edit_contact.xml index 5b7b260dd..99482f3b6 100644 --- a/app/src/main/res/layout/activity_edit_contact.xml +++ b/app/src/main/res/layout/activity_edit_contact.xml @@ -278,8 +278,6 @@ android:layout_toEndOf="@+id/contact_name_image" android:orientation="vertical"> - - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c6b2021cf..c69512b29 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,6 +2,13 @@ Contacts Address + Street + Appartment or suite number + City + Postal code + Post office box + State + Country Inserting… Updating… Phone storage @@ -81,6 +88,7 @@ Phone numbers Emails Addresses + Structured Addresses (edit mode) Events (birthdays, anniversaries) Organization Websites diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c84e42974..739511b3c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,7 +14,7 @@ indicatorfastscroll = "4524cd0b61" #Room room = "2.6.1" #Fossify -commons = "1437ab513f" +commons = "556827fcc0" #Gradle gradlePlugins-agp = "8.2.1" #build @@ -41,7 +41,7 @@ androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = " androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" } androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" } #Fossify -fossify-commons = { module = "org.fossify:commons", version.ref = "commons" } +fossify-commons = { module = "com.github.stephanritscher:Simple-Commons", version.ref = "commons" } [bundles] room = [ "androidx-room-ktx",