Skip to content

Commit

Permalink
Render global ids with correct bold ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
micbakos-rdx committed Jun 25, 2024
1 parent db55456 commit 2b46e32
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ import com.babylon.wallet.android.utils.openUrl
import com.babylon.wallet.android.utils.shareText
import com.radixdlt.sargon.AccountAddress
import com.radixdlt.sargon.Address
import com.radixdlt.sargon.AddressFormat
import com.radixdlt.sargon.ComponentAddress
import com.radixdlt.sargon.NonFungibleGlobalId
import com.radixdlt.sargon.ResourceAddress
import com.radixdlt.sargon.annotation.UsesSampleValues
import com.radixdlt.sargon.extensions.formatted
import com.radixdlt.sargon.samples.sample
import com.radixdlt.sargon.samples.sampleMainnet
import kotlinx.collections.immutable.ImmutableList
Expand Down Expand Up @@ -459,10 +457,7 @@ fun AddressDetailsDialogContentAccountAddressPreview() {
title = "My Main Account",
sections = listOf(
Section.AccountAddressQRCode(accountAddress = address),
Section.FullAddress(
rawAddress = address.formatted(format = AddressFormat.RAW),
truncatedPart = address.formatted(format = AddressFormat.MIDDLE)
),
Section.FullAddress.from(actionableAddress = actionableAddress),
Section.VisitDashboard(url = actionableAddress.dashboardUrl().orEmpty()),
Section.VerifyAddressOnLedger(accountAddress = address)
)
Expand Down Expand Up @@ -494,10 +489,7 @@ fun AddressDetailsDialogContentResourceAddressPreview() {
actionableAddress = actionableAddress,
title = "Radix (XRD)",
sections = listOf(
Section.FullAddress(
rawAddress = address.formatted(format = AddressFormat.RAW),
truncatedPart = address.formatted(format = AddressFormat.MIDDLE)
),
Section.FullAddress.from(actionableAddress = actionableAddress),
Section.VisitDashboard(url = actionableAddress.dashboardUrl().orEmpty()),
)
),
Expand Down Expand Up @@ -528,10 +520,7 @@ fun AddressDetailsDialogContentGlobalIdPreview() {
actionableAddress = actionableAddress,
title = "NFT Collection",
sections = listOf(
Section.FullAddress(
rawAddress = address.formatted(format = AddressFormat.RAW),
truncatedPart = address.formatted(format = AddressFormat.MIDDLE)
),
Section.FullAddress.from(actionableAddress = actionableAddress),
Section.VisitDashboard(url = actionableAddress.dashboardUrl()),
)
),
Expand Down Expand Up @@ -562,10 +551,7 @@ fun AddressDetailsDialogContentRandomComponentAddressPreview() {
actionableAddress = actionableAddress,
title = null,
sections = listOf(
Section.FullAddress(
rawAddress = address.formatted(format = AddressFormat.RAW),
truncatedPart = address.formatted(format = AddressFormat.MIDDLE)
),
Section.FullAddress.from(actionableAddress = actionableAddress),
Section.VisitDashboard(url = actionableAddress.dashboardUrl().orEmpty()),
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import com.babylon.wallet.android.presentation.common.UiState
import com.babylon.wallet.android.presentation.ui.composables.actionableaddress.ActionableAddress
import com.radixdlt.sargon.AccountAddress
import com.radixdlt.sargon.Address
import com.radixdlt.sargon.AddressFormat
import com.radixdlt.sargon.extensions.formatted
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import rdx.works.core.domain.resources.Resource
Expand Down Expand Up @@ -57,12 +59,7 @@ class AddressDetailsDialogViewModel @Inject constructor(
sections.add(State.Section.AccountAddressQRCode(accountAddress = actionableAddress.address.v1))
}

sections.add(
State.Section.FullAddress(
rawAddress = actionableAddress.rawAddress(),
truncatedPart = actionableAddress.truncatedPart()
)
)
sections.add(State.Section.FullAddress.from(actionableAddress = actionableAddress))

if (actionableAddress.isVisitableInDashboard) {
actionableAddress.dashboardUrl()?.let {
Expand Down Expand Up @@ -302,21 +299,67 @@ class AddressDetailsDialogViewModel @Inject constructor(
data class FullAddress(
override val order: Short = 1,
val rawAddress: String,
val truncatedPart: String
val boldRanges: ImmutableList<OpenEndRange<Int>>
) : Section {

val boldRanges: ImmutableList<OpenEndRange<Int>> = run {
val visibleCharsWhenTruncated = rawAddress.split(truncatedPart)

if (visibleCharsWhenTruncated.size != 2) return@run persistentListOf()

val startRange = 0 until visibleCharsWhenTruncated[0].length
val endRange = rawAddress.length - visibleCharsWhenTruncated[1].length until rawAddress.length
companion object {
fun from(actionableAddress: ActionableAddress): FullAddress {
val pairs = when (actionableAddress) {
is ActionableAddress.Address ->
listOf(
actionableAddress.address.formatted(
format = AddressFormat.RAW
) to actionableAddress.address.formatted(
format = AddressFormat.DEFAULT
)
)

is ActionableAddress.GlobalId -> listOf(
actionableAddress.address.resourceAddress.formatted(
format = AddressFormat.RAW
) to actionableAddress.address.resourceAddress.formatted(
format = AddressFormat.DEFAULT
),
actionableAddress.address.nonFungibleLocalId.formatted(
format = AddressFormat.RAW
) to actionableAddress.address.nonFungibleLocalId.formatted(
format = AddressFormat.DEFAULT
)
)

is ActionableAddress.TransactionId -> listOf(
actionableAddress.hash.formatted(
format = AddressFormat.RAW
) to actionableAddress.hash.formatted(
format = AddressFormat.DEFAULT
)
)
}

val raw = actionableAddress.rawAddress()

val boldRanges = mutableListOf<OpenEndRange<Int>>()
pairs.forEach { pair ->
val rawPart = pair.first
val startIndex = raw.indexOf(rawPart)
val boldChars = pair.second.split("...")

val boldStart = boldChars[0]
val firstPartStartIndex = rawPart.indexOf(boldStart) + startIndex
boldRanges.add(firstPartStartIndex until firstPartStartIndex + boldStart.length)

val boldEnd = boldChars.getOrNull(1)
if (boldEnd != null) {
val secondPartStartIndex = rawPart.lastIndexOf(boldEnd) + startIndex
boldRanges.add(secondPartStartIndex until secondPartStartIndex + boldEnd.length)
}
}

persistentListOf(
startRange,
endRange
)
return FullAddress(
rawAddress = raw,
boldRanges = boldRanges.toPersistentList()
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ sealed interface ActionableAddress {

fun rawAddress(): String

fun truncatedPart(): String

fun dashboardUrl(): String?

@Composable
Expand Down Expand Up @@ -271,8 +269,6 @@ sealed interface ActionableAddress {

override fun rawAddress(): String = address.formatted(format = AddressFormat.RAW)

override fun truncatedPart(): String = address.formatted(format = AddressFormat.MIDDLE)

override fun dashboardUrl(): String? = when (address) {
is com.radixdlt.sargon.Address.AccessController -> null
is com.radixdlt.sargon.Address.Account -> "account"
Expand Down Expand Up @@ -318,8 +314,6 @@ sealed interface ActionableAddress {

override fun rawAddress(): String = address.formatted(format = AddressFormat.RAW)

override fun truncatedPart(): String = address.formatted(format = AddressFormat.MIDDLE)

override fun dashboardUrl(): String = "${address.resourceAddress.networkId.dashboardUrl()}/nft/${address.string.encodeUtf8()}"

@Composable
Expand Down Expand Up @@ -362,8 +356,6 @@ sealed interface ActionableAddress {

override fun rawAddress(): String = hash.formatted(format = AddressFormat.RAW)

override fun truncatedPart(): String = hash.formatted(format = AddressFormat.MIDDLE)

override fun dashboardUrl(): String = "${hash.networkId.dashboardUrl()}/transaction/${hash.bech32EncodedTxId.encodeUtf8()}"

@Composable
Expand Down
Loading

0 comments on commit 2b46e32

Please sign in to comment.