From 664dd25a26654a0db0cd1432a785402c5458ceff Mon Sep 17 00:00:00 2001 From: Stephan Ritscher Date: Tue, 23 Jan 2024 21:46:15 +0100 Subject: [PATCH 1/2] Add option to edit structured addresses --- .../activities/EditContactActivity.kt | 132 ++++++++++++++---- .../dialogs/ManageVisibleFieldsDialog.kt | 1 + .../fossify/contacts/helpers/VcfExporter.kt | 14 +- .../fossify/contacts/helpers/VcfImporter.kt | 12 +- .../main/res/layout/activity_edit_contact.xml | 2 - .../layout/dialog_manage_visible_fields.xml | 8 ++ .../layout/item_edit_structured_address.xml | 131 +++++++++++++++++ app/src/main/res/values/strings.xml | 8 ++ gradle/libs.versions.toml | 4 +- 9 files changed, 277 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..849f32734 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,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 { @@ -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) + } } } } @@ -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()) { @@ -1188,13 +1214,44 @@ 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 "" + 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() + 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 @@ -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) + } } } @@ -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) } 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 698f4161a..bda35482d 100644 --- a/app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/org/fossify/contacts/helpers/VcfExporter.kt @@ -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) } 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..338bbe916 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,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)) } } 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 7c4a7c8c5..10aba255e 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 @@ -82,6 +89,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 1fbe1887e..05561a251 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ indicatorfastscroll = "4524cd0b61" #Room room = "2.6.1" #Fossify -commons = "3788b3609c" +commons = "89147bc3d8" #Gradle gradlePlugins-agp = "8.7.1" #build @@ -43,7 +43,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:FossifyCommons", version.ref = "commons" } [bundles] room = [ "androidx-room-ktx", From 3f345b1d424cc97fa29fce4076ba4f5b0072bade Mon Sep 17 00:00:00 2001 From: Stephan Ritscher Date: Sun, 27 Oct 2024 22:42:40 +0100 Subject: [PATCH 2/2] Upgrade detekt for Java 21 compatability --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 05561a251..e2ce5e71d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,7 +4,7 @@ kotlin = "1.9.25" #KSP ksp = "1.9.25-1.0.20" #Detekt -detekt = "1.23.3" +detekt = "1.23.7" #AndroidX androidx-swiperefreshlayout = "1.1.0" #AutoFitTextView