Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewTighe committed May 2, 2022
1 parent 045d03b commit 7ae2b16
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 184 deletions.
74 changes: 39 additions & 35 deletions app/src/main/java/org/mozilla/fenix/settings/address/ext/Address.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,53 @@

package org.mozilla.fenix.settings.address.ext

import androidx.annotation.VisibleForTesting
import mozilla.components.concept.storage.Address

/**
* Generate an [AddressItemText] from a given [Address]. A well-formed [Address] will have
* a full name in the label and a street address as a description. If either are not present,
* the label and description will be based off lower priority properties.
* Generate a label from a given [Address]. A well-formed [Address] will have
* a full name in the label, otherwise it will find the highest priority [Address] property
* to use, as based on the desktop code found in FormAutofillUtils::getAddressLabel
* https://searchfox.org/mozilla-central/rev/87ecd21d3ca517f8d90e49b32bf042a754ed8f18/toolkit/components/formautofill/FormAutofillUtils.jsm#323
*/
fun Address.toItemText(): AddressItemText {
val label = getName().ifEmpty { iterateFields("") }
val description = iterateFields(label)
return AddressItemText(label, description)
}
fun Address.toAddressLabel(): String = getFullName().ifEmpty { iterateFields("") }

/**
* Holds item display values about an [Address].
* Generate a description from a given [Address]. A well-formed address will have a street address
* as a description. An address with missing data will use the next available property after [label]
* based on the priorities found in the desktop code in FormAutofillUtils::getAddressLabel
* https://searchfox.org/mozilla-central/rev/87ecd21d3ca517f8d90e49b32bf042a754ed8f18/toolkit/components/formautofill/FormAutofillUtils.jsm#323
*
* @property label The label for the [Address].
* @property description The subtitled description for an [Address].
* @param label: The label for an [Address]. See [toAddressLabel].
*/
data class AddressItemText(
val label: String,
val description: String
)
fun Address.toAddressDescription(label: String): String = iterateFields(label)

private fun Address.getName(): String {
val middleName = if (additionalName.isEmpty()) "" else "$additionalName "
return if (givenName.isNotEmpty()) "$givenName $middleName$familyName" else familyName
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun Address.getFullName(): String = listOf(givenName, additionalName, familyName)
.filter { it.isNotEmpty() }
.joinToString(" ")

private fun Address.iterateFields(previous: String): String =
streetAddress.joinStreetAddress()
.ifEmptyOrPrevious(previous) { addressLevel3 }
.ifEmptyOrPrevious(previous) { addressLevel2 }
.ifEmptyOrPrevious(previous) { organization }
.ifEmptyOrPrevious(previous) { addressLevel1 }
.ifEmptyOrPrevious(previous) { country }
.ifEmptyOrPrevious(previous) { postalCode }
.ifEmptyOrPrevious(previous) { tel }
.ifEmptyOrPrevious(previous) { email }
.ifEmptyOrPrevious(previous) { "" }

private fun String.joinStreetAddress(): String =
/**
* This will iterate through the available data in an [Address] based on the priority defined
* in the desktop code found in FormAutofillUtils::getAddressLabel.
* https://searchfox.org/mozilla-central/rev/87ecd21d3ca517f8d90e49b32bf042a754ed8f18/toolkit/components/formautofill/FormAutofillUtils.jsm#323
* If [previous] is not empty, this function will find the next well-formed property after
* [previous].
*
* @param [previous] The last [Address] property that was used for another text item.
*/
private fun Address.iterateFields(previous: String): String = listOf(
streetAddress.toOneLineAddress(),
addressLevel3,
addressLevel2,
organization,
addressLevel1,
country,
postalCode,
tel,
email
).filter { it.isNotEmpty() }.firstOrNull { it != previous } ?: ""

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun String.toOneLineAddress(): String =
this.split("\n").joinToString(separator = " ") { it.trim() }

private fun String.ifEmptyOrPrevious(previousValue: String, default: () -> String): String =
if (isEmpty() || this == previousValue) default() else this
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import mozilla.components.concept.storage.Address
import org.mozilla.fenix.R
import org.mozilla.fenix.compose.list.IconListItem
import org.mozilla.fenix.compose.list.TextListItem
import org.mozilla.fenix.settings.address.ext.toItemText
import org.mozilla.fenix.settings.address.ext.toAddressDescription
import org.mozilla.fenix.settings.address.ext.toAddressLabel
import org.mozilla.fenix.theme.FirefoxTheme
import org.mozilla.fenix.theme.Theme

Expand All @@ -38,11 +39,11 @@ fun AddressList(
) {
LazyColumn {
items(addresses) { address ->
val itemText = address.toItemText()
val label = address.toAddressLabel()
TextListItem(
label = itemText.label,
label = address.toAddressLabel(),
modifier = Modifier.padding(start = 56.dp),
description = itemText.description,
description = address.toAddressDescription(label),
onClick = { onAddressClick(address) },
)
}
Expand Down

This file was deleted.

Loading

0 comments on commit 7ae2b16

Please sign in to comment.