mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-21 06:18:42 +02:00
Address-formatting-enhancements-MWEB (#2189)
* apply formatting to address book and MWEB * fix wallet type exception * Update cw_core/lib/wallet_type.dart [skip ci] * Update lib/src/screens/contact/contact_list_page.dart [skip ci] * Update lib/src/screens/contact/contact_list_page.dart [skip ci] * Update lib/utils/address_formatter.dart [skip ci] * Update lib/utils/address_formatter.dart [skip ci] * Update lib/utils/address_formatter.dart --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
@@ -5,24 +5,31 @@ import 'dart:math' as math;
|
||||
class AddressFormatter {
|
||||
static Widget buildSegmentedAddress({
|
||||
required String address,
|
||||
required WalletType walletType,
|
||||
WalletType? walletType,
|
||||
required TextStyle evenTextStyle,
|
||||
TextStyle? oddTextStyle,
|
||||
TextAlign? textAlign,
|
||||
bool shouldTruncate = false,
|
||||
}) {
|
||||
|
||||
final cleanAddress = address.replaceAll('bitcoincash:', '');
|
||||
final isMWEB = address.startsWith('ltcmweb');
|
||||
final chunkSize = walletType != null ? _getChunkSize(walletType) : 4;
|
||||
|
||||
if (shouldTruncate) {
|
||||
return _buildTruncatedAddress(
|
||||
address: address,
|
||||
walletType: walletType,
|
||||
address: cleanAddress,
|
||||
isMWEB: isMWEB,
|
||||
chunkSize: chunkSize,
|
||||
evenTextStyle: evenTextStyle,
|
||||
oddTextStyle: oddTextStyle ?? evenTextStyle.copyWith(color: evenTextStyle.color!.withAlpha(150)),
|
||||
textAlign: textAlign,
|
||||
);
|
||||
} else {
|
||||
return _buildFullSegmentedAddress(
|
||||
address: address,
|
||||
walletType: walletType,
|
||||
address: cleanAddress,
|
||||
isMWEB: isMWEB,
|
||||
chunkSize: chunkSize,
|
||||
evenTextStyle: evenTextStyle,
|
||||
oddTextStyle: oddTextStyle ?? evenTextStyle.copyWith(color: evenTextStyle.color!.withAlpha(128)),
|
||||
textAlign: textAlign,
|
||||
@@ -32,19 +39,34 @@ class AddressFormatter {
|
||||
|
||||
static Widget _buildFullSegmentedAddress({
|
||||
required String address,
|
||||
required WalletType walletType,
|
||||
required bool isMWEB,
|
||||
required int chunkSize,
|
||||
required TextStyle evenTextStyle,
|
||||
required TextStyle oddTextStyle,
|
||||
TextAlign? textAlign,
|
||||
}) {
|
||||
|
||||
final cleanAddress = address.replaceAll('bitcoincash:', '');
|
||||
final chunkSize = _getChunkSize(walletType);
|
||||
final chunks = <String>[];
|
||||
|
||||
for (int i = 0; i < cleanAddress.length; i += chunkSize) {
|
||||
final chunk = cleanAddress.substring(i, math.min(i + chunkSize, cleanAddress.length));
|
||||
chunks.add(chunk);
|
||||
if (isMWEB) {
|
||||
const mwebDisplayPrefix = 'ltcmweb';
|
||||
chunks.add(mwebDisplayPrefix);
|
||||
final startIndex = mwebDisplayPrefix.length;
|
||||
for (int i = startIndex; i < address.length; i += chunkSize) {
|
||||
final chunk = address.substring(
|
||||
i,
|
||||
math.min(i + chunkSize, address.length),
|
||||
);
|
||||
chunks.add(chunk);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < address.length; i += chunkSize) {
|
||||
final chunk = address.substring(
|
||||
i,
|
||||
math.min(i + chunkSize, address.length),
|
||||
);
|
||||
chunks.add(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
final spans = <TextSpan>[];
|
||||
@@ -62,46 +84,68 @@ class AddressFormatter {
|
||||
|
||||
static Widget _buildTruncatedAddress({
|
||||
required String address,
|
||||
required WalletType walletType,
|
||||
required bool isMWEB,
|
||||
required int chunkSize,
|
||||
required TextStyle evenTextStyle,
|
||||
required TextStyle oddTextStyle,
|
||||
TextAlign? textAlign,
|
||||
}) {
|
||||
|
||||
final cleanAddress = address.replaceAll('bitcoincash:', '');
|
||||
if (isMWEB) {
|
||||
const fixedPrefix = 'ltcmweb';
|
||||
final secondChunkStart = fixedPrefix.length;
|
||||
const chunkSize = 4;
|
||||
final secondChunk = address.substring(
|
||||
secondChunkStart,
|
||||
math.min(secondChunkStart + chunkSize, address.length),
|
||||
);
|
||||
final lastChunk = address.substring(address.length - chunkSize);
|
||||
|
||||
final int digitCount = (walletType == WalletType.monero ||
|
||||
walletType == WalletType.wownero ||
|
||||
walletType == WalletType.zano)
|
||||
? 6
|
||||
: 4;
|
||||
final spans = <TextSpan>[
|
||||
TextSpan(text: '$fixedPrefix ', style: evenTextStyle),
|
||||
TextSpan(text: '$secondChunk ', style: oddTextStyle),
|
||||
TextSpan(text: '... ', style: oddTextStyle),
|
||||
TextSpan(text: lastChunk, style: evenTextStyle),
|
||||
];
|
||||
|
||||
if (cleanAddress.length <= 2 * digitCount) {
|
||||
return _buildFullSegmentedAddress(
|
||||
address: cleanAddress,
|
||||
walletType: walletType,
|
||||
evenTextStyle: evenTextStyle,
|
||||
oddTextStyle: oddTextStyle,
|
||||
textAlign: textAlign,
|
||||
return RichText(
|
||||
text: TextSpan(children: spans),
|
||||
textAlign: textAlign ?? TextAlign.start,
|
||||
overflow: TextOverflow.visible,
|
||||
);
|
||||
} else {
|
||||
final int digitCount = chunkSize;
|
||||
|
||||
if (address.length <= 2 * digitCount) {
|
||||
return _buildFullSegmentedAddress(
|
||||
address: address,
|
||||
isMWEB: isMWEB,
|
||||
chunkSize: chunkSize,
|
||||
evenTextStyle: evenTextStyle,
|
||||
oddTextStyle: oddTextStyle,
|
||||
textAlign: textAlign,
|
||||
);
|
||||
}
|
||||
|
||||
final String firstPart = address.substring(0, digitCount);
|
||||
final String secondPart =
|
||||
address.substring(digitCount, digitCount * 2);
|
||||
final String lastPart =
|
||||
address.substring(address.length - digitCount);
|
||||
|
||||
final spans = <TextSpan>[
|
||||
TextSpan(text: '$firstPart ', style: evenTextStyle),
|
||||
TextSpan(text: '$secondPart ', style: oddTextStyle),
|
||||
TextSpan(text: '... ', style: oddTextStyle),
|
||||
TextSpan(text: lastPart, style: evenTextStyle),
|
||||
];
|
||||
|
||||
return RichText(
|
||||
text: TextSpan(children: spans),
|
||||
textAlign: textAlign ?? TextAlign.start,
|
||||
overflow: TextOverflow.visible,
|
||||
);
|
||||
}
|
||||
|
||||
final String firstPart = cleanAddress.substring(0, digitCount);
|
||||
final String secondPart = cleanAddress.substring(digitCount, digitCount * 2);
|
||||
final String lastPart = cleanAddress.substring(cleanAddress.length - digitCount);
|
||||
|
||||
final spans = <TextSpan>[
|
||||
TextSpan(text: '$firstPart ', style: evenTextStyle),
|
||||
TextSpan(text: '$secondPart ', style: oddTextStyle),
|
||||
TextSpan(text: '... ', style: oddTextStyle),
|
||||
TextSpan(text: lastPart, style: evenTextStyle),
|
||||
];
|
||||
|
||||
return RichText(
|
||||
text: TextSpan(children: spans),
|
||||
textAlign: textAlign ?? TextAlign.start,
|
||||
overflow: TextOverflow.visible,
|
||||
);
|
||||
}
|
||||
|
||||
static int _getChunkSize(WalletType walletType) {
|
||||
@@ -114,4 +158,4 @@ class AddressFormatter {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user