@onekeyfe/react-native-native-list 6.8.0-24 → 6.8.0-26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/onekey/nativelist/NativeListActivityDetailsView.kt +12 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListIdentityRenderer.kt +96 -3
- package/android/src/main/java/com/onekey/nativelist/NativeListPrimitives.kt +6 -3
- package/android/src/main/java/com/onekey/nativelist/NativeListTitleSegmentLayout.kt +71 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListTitleSegmentsView.kt +126 -0
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListTitleSegmentLayoutTest.kt +130 -0
- package/docs/ROW_TEMPLATES.md +8 -1
- package/docs/SPEC.md +156 -6
- package/ios/NativeListActivityDetailsView.swift +45 -1
- package/ios/NativeListIdentityRenderer.swift +109 -8
- package/ios/NativeListTitleSegmentLayout.swift +72 -0
- package/ios/NativeListTitleSegmentsLabel.swift +92 -0
- package/ios/tests/NativeListActivityPresentationTests.swift +137 -0
- package/ios/tests/NativeListIdentityTitleIconLayoutTests.swift +155 -0
- package/lib/module/models.js +2 -0
- package/lib/module/validation.js +77 -5
- package/lib/module/web/NativeListWebEngine.js +227 -90
- package/lib/module/web/templates/IdentityRowRenderer.js +122 -2
- package/lib/typescript/src/models.d.ts +21 -1
- package/lib/typescript/src/web/NativeListWebEngine.d.ts +9 -1
- package/package.json +1 -1
- package/src/models.ts +23 -0
- package/src/validation.ts +129 -2
- package/src/web/NativeListWebEngine.ts +309 -105
- package/src/web/templates/IdentityRowRenderer.ts +185 -2
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
|
|
3
|
+
/// Draws identity title runs on one line, each at the width that
|
|
4
|
+
/// `nativeListTitleSegmentWidths` allocates for the current bounds. Its
|
|
5
|
+
/// intrinsic width is the runs' natural width, so the title row stack can
|
|
6
|
+
/// give it less, and accessibility reads the row's `title` instead of runs.
|
|
7
|
+
final class NativeListTitleSegmentsLabel: UIView {
|
|
8
|
+
struct Run {
|
|
9
|
+
let text: String
|
|
10
|
+
let font: UIFont
|
|
11
|
+
let color: UIColor
|
|
12
|
+
let shrink: Bool
|
|
13
|
+
let maxWidthRatio: CGFloat?
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private var runs: [Run] = []
|
|
17
|
+
private var naturalWidths: [CGFloat] = []
|
|
18
|
+
var lineHeight: CGFloat = 24 {
|
|
19
|
+
didSet {
|
|
20
|
+
invalidateIntrinsicContentSize()
|
|
21
|
+
setNeedsDisplay()
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/// `style.title.offsetY`: moves the drawn runs, not the line they sit on.
|
|
25
|
+
var offsetY: CGFloat = 0 { didSet { setNeedsDisplay() } }
|
|
26
|
+
|
|
27
|
+
override init(frame: CGRect) {
|
|
28
|
+
super.init(frame: frame)
|
|
29
|
+
isOpaque = false
|
|
30
|
+
backgroundColor = .clear
|
|
31
|
+
contentMode = .redraw
|
|
32
|
+
isAccessibilityElement = true
|
|
33
|
+
accessibilityTraits = .staticText
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
37
|
+
|
|
38
|
+
func configure(_ runs: [Run], spokenText: String) {
|
|
39
|
+
self.runs = runs
|
|
40
|
+
naturalWidths = runs.map {
|
|
41
|
+
ceil(($0.text as NSString).size(withAttributes: [.font: $0.font]).width)
|
|
42
|
+
}
|
|
43
|
+
accessibilityLabel = spokenText.isEmpty ? nil : spokenText
|
|
44
|
+
invalidateIntrinsicContentSize()
|
|
45
|
+
setNeedsDisplay()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
func reset() {
|
|
49
|
+
offsetY = 0
|
|
50
|
+
configure([], spokenText: "")
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
override var intrinsicContentSize: CGSize {
|
|
54
|
+
CGSize(width: naturalWidths.reduce(0, +), height: lineHeight)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
override func layoutSubviews() {
|
|
58
|
+
super.layoutSubviews()
|
|
59
|
+
setNeedsDisplay()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override func draw(_ rect: CGRect) {
|
|
63
|
+
guard !runs.isEmpty else { return }
|
|
64
|
+
let widths = nativeListTitleSegmentWidths(
|
|
65
|
+
natural: naturalWidths, shrink: runs.map(\.shrink),
|
|
66
|
+
maxWidthRatio: runs.map(\.maxWidthRatio), available: bounds.width)
|
|
67
|
+
let overflowing = nativeListTitleSegmentOverflowIndex(
|
|
68
|
+
natural: naturalWidths, shrink: runs.map(\.shrink), widths: widths)
|
|
69
|
+
let isRightToLeft = effectiveUserInterfaceLayoutDirection == .rightToLeft
|
|
70
|
+
var x: CGFloat = isRightToLeft ? bounds.width : 0
|
|
71
|
+
for (index, (run, width)) in zip(runs, widths).enumerated() where width > 0 {
|
|
72
|
+
let paragraph = NSMutableParagraphStyle()
|
|
73
|
+
paragraph.lineBreakMode = .byTruncatingTail
|
|
74
|
+
// Text after this run is hidden: one character more than fits makes the
|
|
75
|
+
// tail truncation show it. A run too narrow for the ellipsis itself is
|
|
76
|
+
// drawn whole instead.
|
|
77
|
+
let ellipsis = "\u{2026}"
|
|
78
|
+
let showsOverflow = index == overflowing
|
|
79
|
+
&& width >= ceil((ellipsis as NSString).size(withAttributes: [.font: run.font]).width)
|
|
80
|
+
let text = NSAttributedString(
|
|
81
|
+
string: showsOverflow ? run.text + ellipsis : run.text,
|
|
82
|
+
attributes: [.font: run.font, .foregroundColor: run.color, .paragraphStyle: paragraph])
|
|
83
|
+
let height = ceil(run.font.lineHeight)
|
|
84
|
+
let originX = isRightToLeft ? x - width : x
|
|
85
|
+
text.draw(
|
|
86
|
+
with: CGRect(
|
|
87
|
+
x: originX, y: (bounds.height - height) / 2 + offsetY, width: width, height: height),
|
|
88
|
+
options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], context: nil)
|
|
89
|
+
x += isRightToLeft ? -width : width
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -519,4 +519,141 @@ final class NativeListActivityPresentationTests: XCTestCase {
|
|
|
519
519
|
rendered, accuracy: 1, name)
|
|
520
520
|
}
|
|
521
521
|
}
|
|
522
|
+
|
|
523
|
+
// OK-67497: History rows with a risk badge ("High risk") must keep the badge right
|
|
524
|
+
// after the title, like the identity title row (OK-66041). The title line used to
|
|
525
|
+
// stretch the title across the identity column, pushing badges to the amount column.
|
|
526
|
+
func testTitleBadgesFollowTheTitleTextAndSurviveRebind() throws {
|
|
527
|
+
func data(title: String, badges: Bool) -> [String: Any] {
|
|
528
|
+
var value: [String: Any] = [
|
|
529
|
+
"type": "activity", "key": "receive", "title": title, "description": "0xc168f7...62f46d",
|
|
530
|
+
"leading": ["kind": "icon", "name": "ArrowBottomOutline"],
|
|
531
|
+
"amounts": [["key": "usdt", "text": "+1,250 USDT", "secondaryText": "$1,250.13"]],
|
|
532
|
+
]
|
|
533
|
+
if badges { value["badges"] = [["key": "risk", "text": "High risk", "tone": "warning"]] }
|
|
534
|
+
return value
|
|
535
|
+
}
|
|
536
|
+
let details = NativeListActivityDetailsView()
|
|
537
|
+
let container = UIView(frame: CGRect(x: 0, y: 0, width: 390, height: 200))
|
|
538
|
+
details.translatesAutoresizingMaskIntoConstraints = false
|
|
539
|
+
container.addSubview(details)
|
|
540
|
+
NSLayoutConstraint.activate([
|
|
541
|
+
details.leadingAnchor.constraint(equalTo: container.leadingAnchor),
|
|
542
|
+
details.trailingAnchor.constraint(equalTo: container.trailingAnchor),
|
|
543
|
+
details.topAnchor.constraint(equalTo: container.topAnchor),
|
|
544
|
+
])
|
|
545
|
+
let title = try XCTUnwrap(Mirror(reflecting: details).children.first { $0.label == "title" }?.value as? UILabel)
|
|
546
|
+
let titleLine = try XCTUnwrap(Mirror(reflecting: details).children.first { $0.label == "titleLine" }?.value as? UIStackView)
|
|
547
|
+
func badge() throws -> UILabel {
|
|
548
|
+
let views = try XCTUnwrap(Mirror(reflecting: details).children.first { $0.label == "badgeViews" }?.value as? [UILabel])
|
|
549
|
+
return try XCTUnwrap(views.first)
|
|
550
|
+
}
|
|
551
|
+
// Direct bind skips NativeListRendererCell.bind, which marks the host dirty.
|
|
552
|
+
// Without setNeedsLayout a later layoutIfNeeded keeps the previous frames.
|
|
553
|
+
func layout(_ host: UIView) {
|
|
554
|
+
host.setNeedsLayout()
|
|
555
|
+
host.layoutIfNeeded()
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
details.bind(try NativeListItem(data: data(title: "Receive", badges: true)), theme: nil, fonts: NativeListFontFamilies())
|
|
559
|
+
layout(container)
|
|
560
|
+
let amountsColumn = try XCTUnwrap(details.arrangedSubviews.first as? UIStackView).arrangedSubviews[2]
|
|
561
|
+
func widestText(_ view: UIView) -> CGFloat {
|
|
562
|
+
if view.isHidden { return 0 }
|
|
563
|
+
let own = (view as? UILabel).map {
|
|
564
|
+
($0.text ?? "").isEmpty ? 0 : $0.intrinsicContentSize.width
|
|
565
|
+
} ?? 0
|
|
566
|
+
return view.subviews.reduce(own) { max($0, widestText($1)) }
|
|
567
|
+
}
|
|
568
|
+
XCTAssertEqual(amountsColumn.bounds.width, widestText(amountsColumn), accuracy: 1,
|
|
569
|
+
"stacked amounts keep their text width and leave the row's slack to the identity column")
|
|
570
|
+
XCTAssertEqual(title.bounds.width, title.intrinsicContentSize.width, accuracy: 1,
|
|
571
|
+
"title must keep its intrinsic width instead of stretching across the title line")
|
|
572
|
+
var titleFrame = title.convert(title.bounds, to: titleLine)
|
|
573
|
+
var badgeFrame = try badge().convert(try badge().bounds, to: titleLine)
|
|
574
|
+
XCTAssertEqual(badgeFrame.minX, titleFrame.maxX + 4, accuracy: 1, "badge follows the title with titleBadgeGap")
|
|
575
|
+
// Before the fix the title stretched across the line and the badge ended flush with it.
|
|
576
|
+
XCTAssertGreaterThan(titleLine.bounds.width - badgeFrame.maxX, 20,
|
|
577
|
+
"the line's slack stays after the badge instead of before it")
|
|
578
|
+
|
|
579
|
+
// A long title truncates in front of the badge; the badge stays whole inside the line.
|
|
580
|
+
details.bind(try NativeListItem(data: data(title: String(repeating: "Contract interaction ", count: 6), badges: true)),
|
|
581
|
+
theme: nil, fonts: NativeListFontFamilies())
|
|
582
|
+
layout(container)
|
|
583
|
+
titleFrame = title.convert(title.bounds, to: titleLine)
|
|
584
|
+
badgeFrame = try badge().convert(try badge().bounds, to: titleLine)
|
|
585
|
+
XCTAssertEqual(badgeFrame.minX, titleFrame.maxX + 4, accuracy: 1)
|
|
586
|
+
XCTAssertLessThanOrEqual(badgeFrame.maxX, titleLine.bounds.width + 0.5)
|
|
587
|
+
XCTAssertEqual(try badge().bounds.width, try badge().intrinsicContentSize.width, accuracy: 1)
|
|
588
|
+
|
|
589
|
+
// Rebinding without badges gives the title the whole line again.
|
|
590
|
+
details.bind(try NativeListItem(data: data(title: String(repeating: "Contract interaction ", count: 6), badges: false)),
|
|
591
|
+
theme: nil, fonts: NativeListFontFamilies())
|
|
592
|
+
layout(container)
|
|
593
|
+
XCTAssertEqual(title.convert(title.bounds, to: titleLine).maxX, titleLine.bounds.width, accuracy: 1)
|
|
594
|
+
|
|
595
|
+
// Without badges a short title still spans the line, so `alignment: center` keeps
|
|
596
|
+
// centering it on a recycled row exactly as on a fresh one.
|
|
597
|
+
var centered = data(title: "Receive", badges: false)
|
|
598
|
+
centered["style"] = ["title": ["alignment": "center"]]
|
|
599
|
+
details.bind(try NativeListItem(data: centered), theme: nil, fonts: NativeListFontFamilies())
|
|
600
|
+
layout(container)
|
|
601
|
+
XCTAssertEqual(title.bounds.width, titleLine.bounds.width, accuracy: 1)
|
|
602
|
+
XCTAssertEqual(title.textAlignment, .center)
|
|
603
|
+
let fresh = NativeListActivityDetailsView()
|
|
604
|
+
let freshContainer = UIView(frame: container.frame)
|
|
605
|
+
fresh.translatesAutoresizingMaskIntoConstraints = false
|
|
606
|
+
freshContainer.addSubview(fresh)
|
|
607
|
+
NSLayoutConstraint.activate([
|
|
608
|
+
fresh.leadingAnchor.constraint(equalTo: freshContainer.leadingAnchor),
|
|
609
|
+
fresh.trailingAnchor.constraint(equalTo: freshContainer.trailingAnchor),
|
|
610
|
+
fresh.topAnchor.constraint(equalTo: freshContainer.topAnchor),
|
|
611
|
+
])
|
|
612
|
+
fresh.bind(try NativeListItem(data: centered), theme: nil, fonts: NativeListFontFamilies())
|
|
613
|
+
layout(freshContainer)
|
|
614
|
+
let freshTitle = try XCTUnwrap(Mirror(reflecting: fresh).children.first { $0.label == "title" }?.value as? UILabel)
|
|
615
|
+
XCTAssertEqual(title.frame, freshTitle.frame, "recycled and fresh rows lay out the title the same way")
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// Amount labels share the stacked column's width. A short amount (hugging) must not
|
|
619
|
+
// squeeze a longer one (compression), whatever the order or number of lines.
|
|
620
|
+
func testStackedAmountsKeepTheLongestTextWhole() throws {
|
|
621
|
+
let details = NativeListActivityDetailsView()
|
|
622
|
+
let container = UIView(frame: CGRect(x: 0, y: 0, width: 390, height: 2000))
|
|
623
|
+
details.translatesAutoresizingMaskIntoConstraints = false
|
|
624
|
+
container.addSubview(details)
|
|
625
|
+
NSLayoutConstraint.activate([
|
|
626
|
+
details.leadingAnchor.constraint(equalTo: container.leadingAnchor),
|
|
627
|
+
details.trailingAnchor.constraint(equalTo: container.trailingAnchor),
|
|
628
|
+
details.topAnchor.constraint(equalTo: container.topAnchor),
|
|
629
|
+
])
|
|
630
|
+
func labels(_ view: UIView) -> [UILabel] {
|
|
631
|
+
if view.isHidden { return [] }
|
|
632
|
+
return ((view as? UILabel).map { [$0] } ?? []) + view.subviews.flatMap(labels)
|
|
633
|
+
}
|
|
634
|
+
let long = ["key": "long", "text": "+123,456.78 USDT", "secondaryText": "$1"]
|
|
635
|
+
let cases: [(String, [[String: Any]])] = [
|
|
636
|
+
("long first", [long, ["key": "short", "text": "1", "secondaryText": "$1"]]),
|
|
637
|
+
("long last", [["key": "short", "text": "1", "secondaryText": "$1"], long]),
|
|
638
|
+
("six lines", (0..<6).map { $0 == 3 ? long : ["key": "s\($0)", "text": "\($0)", "secondaryText": "$1"] }),
|
|
639
|
+
("32 lines", (0..<32).map { $0 == 31 ? long : ["key": "s\($0)", "text": "\($0)", "secondaryText": "$1"] }),
|
|
640
|
+
]
|
|
641
|
+
for (name, amounts) in cases {
|
|
642
|
+
details.bind(try NativeListItem(data: [
|
|
643
|
+
"type": "activity", "key": "tx", "title": "Receive", "description": "0xc168f7...62f46d",
|
|
644
|
+
"leading": ["kind": "icon", "name": "ArrowBottomOutline"], "hideLeading": true,
|
|
645
|
+
"amounts": amounts,
|
|
646
|
+
]), theme: nil, fonts: NativeListFontFamilies())
|
|
647
|
+
container.setNeedsLayout()
|
|
648
|
+
container.layoutIfNeeded()
|
|
649
|
+
let column = try XCTUnwrap(details.arrangedSubviews.first as? UIStackView).arrangedSubviews[2]
|
|
650
|
+
let texts = labels(column).filter { !($0.text ?? "").isEmpty }
|
|
651
|
+
let widest = texts.map(\.intrinsicContentSize.width).max() ?? 0
|
|
652
|
+
XCTAssertEqual(column.bounds.width, widest, accuracy: 1, name)
|
|
653
|
+
for label in texts {
|
|
654
|
+
XCTAssertGreaterThanOrEqual(label.bounds.width + 0.5, label.intrinsicContentSize.width,
|
|
655
|
+
"\(name): \(label.text ?? "") is not truncated")
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
522
659
|
}
|
|
@@ -78,4 +78,159 @@ final class NativeListIdentityTitleIconLayoutTests: XCTestCase {
|
|
|
78
78
|
XCTAssertGreaterThan(badgeFrame.minX, iconFrame.maxX)
|
|
79
79
|
XCTAssertLessThanOrEqual(badgeFrame.maxX, cell.bounds.width)
|
|
80
80
|
}
|
|
81
|
+
|
|
82
|
+
// An address book row: a plain title after the 16pt `titleLeadingIcon`. A
|
|
83
|
+
// long name truncates after the glyph instead of squeezing it to nothing.
|
|
84
|
+
func testLongPlainTitleTruncatesAfterTheLeadingIcon() throws {
|
|
85
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 320, height: 60))
|
|
86
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 200))
|
|
87
|
+
window.addSubview(cell)
|
|
88
|
+
let leadingIcon = try child(cell, "titleLeadingIconView", as: UIImageView.self)
|
|
89
|
+
let titleLabel = try child(cell, "titleLabel", as: UILabel.self)
|
|
90
|
+
let item = try NativeListItem(data: [
|
|
91
|
+
"key": "book", "type": "identity",
|
|
92
|
+
"title": "A very long address book name that cannot possibly fit on one line",
|
|
93
|
+
"height": 60,
|
|
94
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]],
|
|
95
|
+
"titleLeadingIcon": ["name": "BookOpenOutline"],
|
|
96
|
+
"subtitle": "0x1234...abcd",
|
|
97
|
+
])
|
|
98
|
+
cell.bind(item: item, theme: nil, layout: "linear", selected: false,
|
|
99
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
100
|
+
cell.layoutIfNeeded()
|
|
101
|
+
|
|
102
|
+
let iconFrame = leadingIcon.convert(leadingIcon.bounds, to: cell)
|
|
103
|
+
let labelFrame = titleLabel.convert(titleLabel.bounds, to: cell)
|
|
104
|
+
XCTAssertFalse(leadingIcon.isHidden)
|
|
105
|
+
XCTAssertEqual(leadingIcon.bounds.width, 16, accuracy: 0.5,
|
|
106
|
+
"the leading glyph keeps its 16pt width")
|
|
107
|
+
XCTAssertEqual(labelFrame.minX, iconFrame.maxX + 6, accuracy: 1,
|
|
108
|
+
"the title follows the glyph with the 6pt gap")
|
|
109
|
+
XCTAssertLessThan(titleLabel.bounds.width, titleLabel.intrinsicContentSize.width,
|
|
110
|
+
"the title truncates instead of the glyph")
|
|
111
|
+
XCTAssertLessThanOrEqual(labelFrame.maxX, cell.bounds.width)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// A wallet sidebar row keeps the plain title even when a production row
|
|
115
|
+
// carries runs and a leading glyph, also after the cell drew both for a
|
|
116
|
+
// recipient row.
|
|
117
|
+
func testWalletSidebarRowIgnoresTitleRunsAndLeadingIcon() throws {
|
|
118
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 320, height: 68))
|
|
119
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 200))
|
|
120
|
+
window.addSubview(cell)
|
|
121
|
+
let titleLabel = try child(cell, "titleLabel", as: UILabel.self)
|
|
122
|
+
let runs = try child(cell, "titleSegmentsLabel", as: UIView.self)
|
|
123
|
+
let leadingIcon = try child(cell, "titleLeadingIconView", as: UIImageView.self)
|
|
124
|
+
let decorations: [String: Any] = [
|
|
125
|
+
"titleSegments": [
|
|
126
|
+
["text": "Main", "shrink": true],
|
|
127
|
+
["text": " / ", "tone": "secondary"],
|
|
128
|
+
["text": "Account 1"],
|
|
129
|
+
],
|
|
130
|
+
"titleLeadingIcon": ["name": "BookOpenOutline"],
|
|
131
|
+
]
|
|
132
|
+
func bind(_ data: [String: Any]) throws {
|
|
133
|
+
var row: [String: Any] = [
|
|
134
|
+
"key": "wallet", "type": "identity", "title": "Main / Account 1", "height": 68,
|
|
135
|
+
]
|
|
136
|
+
data.forEach { row[$0.key] = $0.value }
|
|
137
|
+
cell.bind(item: try NativeListItem(data: row), theme: nil, layout: "linear",
|
|
138
|
+
selected: false, checkboxState: { _, _, fallback in fallback },
|
|
139
|
+
fonts: NativeListFontFamilies())
|
|
140
|
+
cell.layoutIfNeeded()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
try bind(decorations)
|
|
144
|
+
XCTAssertFalse(runs.isHidden)
|
|
145
|
+
XCTAssertFalse(leadingIcon.isHidden)
|
|
146
|
+
|
|
147
|
+
try bind(decorations.merging(["presentation": "walletSidebar"]) { $1 })
|
|
148
|
+
XCTAssertTrue(runs.isHidden, "wallet sidebar rows draw no runs")
|
|
149
|
+
XCTAssertTrue(leadingIcon.isHidden, "wallet sidebar rows draw no leading glyph")
|
|
150
|
+
XCTAssertFalse(titleLabel.isHidden)
|
|
151
|
+
XCTAssertEqual(titleLabel.text, "Main / Account 1")
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// `titleMatch` keeps the plain highlighted title on screen, so an
|
|
155
|
+
// auto-height row is measured for that title and its `titleLines`, not for
|
|
156
|
+
// one line of runs.
|
|
157
|
+
func testHighlightedTitleIsMeasuredWithItsOwnLines() throws {
|
|
158
|
+
func height(_ data: [String: Any]) throws -> CGFloat {
|
|
159
|
+
var row: [String: Any] = [
|
|
160
|
+
"key": "match", "type": "identity", "contentHeight": "auto",
|
|
161
|
+
"title": "A very long recipient name that needs a second line to show",
|
|
162
|
+
"titleLines": 2, "titleMatch": [["start": 0, "end": 6]],
|
|
163
|
+
"subtitle": "0x1234...abcd",
|
|
164
|
+
]
|
|
165
|
+
data.forEach { row[$0.key] = $0.value }
|
|
166
|
+
return try XCTUnwrap(NativeListIdentityCell.measure(
|
|
167
|
+
try NativeListItem(data: row), width: 240, theme: nil, layout: "linear",
|
|
168
|
+
fonts: NativeListFontFamilies()))
|
|
169
|
+
}
|
|
170
|
+
let runs: [[String: Any]] = [["text": "A very long", "shrink": true], ["text": " recipient"]]
|
|
171
|
+
|
|
172
|
+
let plain = try height([:])
|
|
173
|
+
XCTAssertEqual(try height(["titleSegments": runs]), plain, accuracy: 0.5)
|
|
174
|
+
XCTAssertGreaterThan(plain, try height(["titleLines": 1]))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private func autoHeight(_ data: [String: Any], width: CGFloat) throws -> CGFloat {
|
|
178
|
+
var row: [String: Any] = [
|
|
179
|
+
"key": "auto", "type": "identity", "contentHeight": "auto",
|
|
180
|
+
"title": "A very long recipient name that needs more than one line to show in full",
|
|
181
|
+
"titleLines": 3, "subtitle": "0x1234...abcd",
|
|
182
|
+
]
|
|
183
|
+
data.forEach { row[$0.key] = $0.value }
|
|
184
|
+
return try XCTUnwrap(NativeListIdentityCell.measure(
|
|
185
|
+
try NativeListItem(data: row), width: width, theme: nil, layout: "linear",
|
|
186
|
+
fonts: NativeListFontFamilies()))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// The leading glyph and its gap take 22 points from the title, so the row
|
|
190
|
+
// measures like a plain row that much narrower, at every wrapping boundary.
|
|
191
|
+
func testAutoHeightCountsTheLeadingIconInTheTitleWidth() throws {
|
|
192
|
+
let icon: [String: Any] = ["name": "BookOpenOutline"]
|
|
193
|
+
for width in stride(from: CGFloat(200), through: 320, by: 6) {
|
|
194
|
+
XCTAssertEqual(
|
|
195
|
+
try autoHeight(["titleLeadingIcon": icon], width: width),
|
|
196
|
+
try autoHeight([:], width: width - 22), accuracy: 0.5, "width \(width)")
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Runs are one line, so `style.title.lines` must not reserve more for them.
|
|
201
|
+
func testRunsAreMeasuredAsOneLineWhateverTheTitleStyleAsksFor() throws {
|
|
202
|
+
let runs: [[String: Any]] = [["text": "A very long", "shrink": true], ["text": " recipient"]]
|
|
203
|
+
let twoLines: [String: Any] = ["title": ["lines": 2]]
|
|
204
|
+
|
|
205
|
+
let oneLine = try autoHeight(["titleSegments": runs, "titleLines": 1], width: 240)
|
|
206
|
+
XCTAssertEqual(
|
|
207
|
+
try autoHeight(["titleSegments": runs, "style": twoLines], width: 240), oneLine, accuracy: 0.5)
|
|
208
|
+
XCTAssertGreaterThan(try autoHeight(["style": twoLines], width: 240), oneLine)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// `style.title.offsetY` moves the runs as it moves a plain title, and a
|
|
212
|
+
// later row without it starts from zero again.
|
|
213
|
+
func testRunsTakeTheTitleOpticalOffset() throws {
|
|
214
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 320, height: 60))
|
|
215
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 200))
|
|
216
|
+
window.addSubview(cell)
|
|
217
|
+
let runsLabel = try child(cell, "titleSegmentsLabel", as: NativeListTitleSegmentsLabel.self)
|
|
218
|
+
func bind(_ style: [String: Any]?) throws {
|
|
219
|
+
var row: [String: Any] = [
|
|
220
|
+
"key": "runs", "type": "identity", "title": "Wallet / Account", "height": 60,
|
|
221
|
+
"titleSegments": [["text": "Wallet", "shrink": true], ["text": " / Account"]],
|
|
222
|
+
]
|
|
223
|
+
if let style { row["style"] = style }
|
|
224
|
+
cell.bind(item: try NativeListItem(data: row), theme: nil, layout: "linear", selected: false,
|
|
225
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
226
|
+
cell.layoutIfNeeded()
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
try bind(["title": ["offsetY": 3]])
|
|
230
|
+
XCTAssertFalse(runsLabel.isHidden)
|
|
231
|
+
XCTAssertEqual(runsLabel.offsetY, 3, accuracy: 0.01)
|
|
232
|
+
|
|
233
|
+
try bind(nil)
|
|
234
|
+
XCTAssertEqual(runsLabel.offsetY, 0, accuracy: 0.01)
|
|
235
|
+
}
|
|
81
236
|
}
|
package/lib/module/models.js
CHANGED
|
@@ -39,6 +39,8 @@ export const ROW_BOX_STYLE_KEYS_BY_TYPE = {
|
|
|
39
39
|
|
|
40
40
|
// OneKey patch: preserve compact very-small-balance digit runs.
|
|
41
41
|
|
|
42
|
+
/** One run of an identity title line; see docs/SPEC.md. */
|
|
43
|
+
|
|
42
44
|
/** Native Market quote row. All values are already localized/formatted strings. */
|
|
43
45
|
|
|
44
46
|
/** Compact KPI card for dashboards and grids. */
|
package/lib/module/validation.js
CHANGED
|
@@ -704,6 +704,68 @@ function assertButtonGroup(row, path) {
|
|
|
704
704
|
}
|
|
705
705
|
});
|
|
706
706
|
}
|
|
707
|
+
const MAX_TITLE_SEGMENTS = 4;
|
|
708
|
+
const HEX_COLOR_PATTERN = /^#[0-9a-f]{6}([0-9a-f]{2})?$/i;
|
|
709
|
+
function assertTitleSegments(value, path) {
|
|
710
|
+
if (!Array.isArray(value) || value.length === 0 || value.length > MAX_TITLE_SEGMENTS) fail(path, `must hold 1...${MAX_TITLE_SEGMENTS} segments`);
|
|
711
|
+
let shrinking = 0;
|
|
712
|
+
// Array.from also visits holes, so a sparse list is rejected here and the
|
|
713
|
+
// production sanitizer drops it before protocol validation walks it.
|
|
714
|
+
Array.from(value).forEach((item, index) => {
|
|
715
|
+
const segmentPath = `${path}[${index}]`;
|
|
716
|
+
const segment = protocolObject(item, segmentPath);
|
|
717
|
+
assertUnknownKeys(segment, ['text', 'tone', 'fontWeight', 'shrink', 'maxWidthRatio'], segmentPath, 'title segment key');
|
|
718
|
+
const {
|
|
719
|
+
text,
|
|
720
|
+
tone,
|
|
721
|
+
fontWeight,
|
|
722
|
+
shrink,
|
|
723
|
+
maxWidthRatio
|
|
724
|
+
} = segment;
|
|
725
|
+
if (typeof text !== 'string' || !text.trim()) fail(`${segmentPath}.text`, 'must be non-blank');
|
|
726
|
+
assertText(text, `${segmentPath}.text`);
|
|
727
|
+
if (tone !== undefined && tone !== 'primary' && tone !== 'secondary') fail(`${segmentPath}.tone`, 'must be primary or secondary');
|
|
728
|
+
if (fontWeight !== undefined && fontWeight !== 'regular' && fontWeight !== 'medium') fail(`${segmentPath}.fontWeight`, 'must be regular or medium');
|
|
729
|
+
if (shrink !== undefined && typeof shrink !== 'boolean') fail(`${segmentPath}.shrink`, 'must be a boolean');
|
|
730
|
+
if (shrink) shrinking += 1;
|
|
731
|
+
if (maxWidthRatio !== undefined && (typeof maxWidthRatio !== 'number' || !Number.isFinite(maxWidthRatio) || maxWidthRatio <= 0 || maxWidthRatio > 1)) fail(`${segmentPath}.maxWidthRatio`, 'must be finite within (0, 1]');
|
|
732
|
+
});
|
|
733
|
+
if (shrinking > 1) fail(path, 'allows at most one shrinking segment');
|
|
734
|
+
}
|
|
735
|
+
function assertTitleLeadingIcon(value, path) {
|
|
736
|
+
const icon = protocolObject(value, path);
|
|
737
|
+
assertUnknownKeys(icon, ['name', 'tintColor', 'accessibilityLabel'], path, 'title leading icon key');
|
|
738
|
+
const {
|
|
739
|
+
name,
|
|
740
|
+
tintColor,
|
|
741
|
+
accessibilityLabel
|
|
742
|
+
} = icon;
|
|
743
|
+
if (typeof name !== 'string' || !name.trim()) fail(`${path}.name`, 'must be non-blank');
|
|
744
|
+
if (tintColor !== undefined && (typeof tintColor !== 'string' || !HEX_COLOR_PATTERN.test(tintColor))) fail(`${path}.tintColor`, 'must be #RRGGBB or #RRGGBBAA');
|
|
745
|
+
if (accessibilityLabel !== undefined && typeof accessibilityLabel !== 'string') fail(`${path}.accessibilityLabel`, 'must be a string');
|
|
746
|
+
}
|
|
747
|
+
function assertIdentityTitleRuns(row, path) {
|
|
748
|
+
if (row.titleSegments !== undefined) {
|
|
749
|
+
assertTitleSegments(row.titleSegments, `${path}.titleSegments`);
|
|
750
|
+
// titleMatch ranges index `title`, which the runs replace on screen.
|
|
751
|
+
if (row.titleMatch?.length) fail(`${path}.titleMatch`, 'cannot be combined with titleSegments');
|
|
752
|
+
if (row.presentation === 'walletSidebar') fail(`${path}.titleSegments`, 'is not supported with walletSidebar');
|
|
753
|
+
}
|
|
754
|
+
if (row.titleLeadingIcon !== undefined) {
|
|
755
|
+
assertTitleLeadingIcon(row.titleLeadingIcon, `${path}.titleLeadingIcon`);
|
|
756
|
+
if (row.presentation === 'walletSidebar') fail(`${path}.titleLeadingIcon`, 'is not supported with walletSidebar');
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** Production keeps decorative title runs only when they pass the rules. */
|
|
761
|
+
function passesTitleRunRules(check) {
|
|
762
|
+
try {
|
|
763
|
+
check();
|
|
764
|
+
return true;
|
|
765
|
+
} catch {
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
707
769
|
|
|
708
770
|
// A passive label: its text and two caller colors, with no action or style slot.
|
|
709
771
|
function assertWebLeadingBadge(value, path) {
|
|
@@ -954,6 +1016,7 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
954
1016
|
if (suffix.maxWidth !== undefined && (!Number.isFinite(suffix.maxWidth) || suffix.maxWidth < 0 || suffix.maxWidth > 4096)) fail(`${path}.titleSuffixes[${index}].maxWidth`, 'must be finite within 0...4096');
|
|
955
1017
|
});
|
|
956
1018
|
assertIdentityExtras(row, path);
|
|
1019
|
+
assertIdentityTitleRuns(row, path);
|
|
957
1020
|
assertText(row.subtitle, `${path}.subtitle`);
|
|
958
1021
|
// OneKey patch: selector text segments truncate independently.
|
|
959
1022
|
row.subtitleSegments?.forEach((segment, segmentIndex) => {
|
|
@@ -1251,7 +1314,7 @@ function assertSnapshotContract(snapshot) {
|
|
|
1251
1314
|
}
|
|
1252
1315
|
const COMMON_ROW_FIELDS = ['height', 'heightRounding', 'testID', 'opacity', 'backgroundColor', 'backgroundFullWidth', 'pressDisabled', 'key', 'revision', 'sectionKey', 'groupId', 'groupPosition', 'size', 'density', 'disabled', 'selected', 'accessibilityLabel', 'separator', 'type', 'style'];
|
|
1253
1316
|
const ROW_FIELDS = {
|
|
1254
|
-
identity: [...COMMON_ROW_FIELDS, 'contentHeight', 'longPressActionKey', 'titleMatch', 'titleActionKey', 'titleActionOnHover', 'subtitleSegments', 'presentation', 'leading', 'leadingAction', 'title', 'titleSuffixes', 'titleIcon', 'secondaryTitleIcon', 'titleImage', 'subtitle', 'tertiary', 'tertiaryTone', 'titleLines', 'subtitleLines', 'collapseEmptyTrailing', 'badges', 'trailing', 'draggable'],
|
|
1317
|
+
identity: [...COMMON_ROW_FIELDS, 'contentHeight', 'longPressActionKey', 'titleMatch', 'titleActionKey', 'titleActionOnHover', 'subtitleSegments', 'presentation', 'leading', 'leadingAction', 'title', 'titleSuffixes', 'titleIcon', 'secondaryTitleIcon', 'titleImage', 'titleSegments', 'titleLeadingIcon', 'subtitle', 'tertiary', 'tertiaryTone', 'titleLines', 'subtitleLines', 'collapseEmptyTrailing', 'badges', 'trailing', 'draggable'],
|
|
1255
1318
|
walletGroup: [...COMMON_ROW_FIELDS, 'parent', 'children', 'draggable'],
|
|
1256
1319
|
rail: [...COMMON_ROW_FIELDS, 'visual', 'title', 'status', 'badge', 'draggable'],
|
|
1257
1320
|
activity: [...COMMON_ROW_FIELDS, 'leading', 'hideLeading', 'secondaryLeading', 'title', 'description', 'status', 'primaryAmount', 'secondaryAmount', 'amounts', 'badges', 'presentation', 'fee', 'descriptionActionKey', 'descriptionActionStart', 'footerActions'],
|
|
@@ -1267,7 +1330,7 @@ const ROW_FIELDS = {
|
|
|
1267
1330
|
};
|
|
1268
1331
|
const COMMON_PATCH_FIELDS = ['height', 'heightRounding', 'opacity', 'pressDisabled', 'revision', 'disabled', 'selected', 'accessibilityLabel', 'separator', 'style'];
|
|
1269
1332
|
const PATCH_FIELDS = {
|
|
1270
|
-
identity: [...COMMON_PATCH_FIELDS, 'contentHeight', 'longPressActionKey', 'titleMatch', 'titleActionKey', 'titleActionOnHover', 'subtitleSegments', 'leading', 'leadingAction', 'title', 'titleSuffixes', 'titleIcon', 'secondaryTitleIcon', 'titleImage', 'subtitle', 'tertiary', 'tertiaryTone', 'subtitleLines', 'collapseEmptyTrailing', 'badges', 'trailing'],
|
|
1333
|
+
identity: [...COMMON_PATCH_FIELDS, 'contentHeight', 'longPressActionKey', 'titleMatch', 'titleActionKey', 'titleActionOnHover', 'subtitleSegments', 'leading', 'leadingAction', 'title', 'titleSuffixes', 'titleIcon', 'secondaryTitleIcon', 'titleImage', 'titleSegments', 'titleLeadingIcon', 'subtitle', 'tertiary', 'tertiaryTone', 'subtitleLines', 'collapseEmptyTrailing', 'badges', 'trailing'],
|
|
1271
1334
|
rail: [...COMMON_PATCH_FIELDS, 'title', 'visual', 'status', 'badge'],
|
|
1272
1335
|
activity: [...COMMON_PATCH_FIELDS, 'presentation', 'leading', 'title', 'badges', 'status', 'hideLeading', 'secondaryLeading', 'description', 'primaryAmount', 'secondaryAmount', 'amounts', 'fee', 'descriptionActionKey', 'descriptionActionStart', 'footerActions'],
|
|
1273
1336
|
message: [...COMMON_PATCH_FIELDS, 'presentation', 'title', 'unread', 'body', 'bodyLines', 'time', 'thumbnail'],
|
|
@@ -1287,7 +1350,7 @@ function protocolObject(value, path) {
|
|
|
1287
1350
|
function protocolKey(value, path) {
|
|
1288
1351
|
if (typeof value !== 'string' || value.trim().length === 0) fail(path, 'must be a non-blank string');
|
|
1289
1352
|
}
|
|
1290
|
-
const PROTOCOL_ARRAY_FIELDS = new Set(['columns', 'titleSuffixes', 'cells', 'points', 'images', 'overlays', 'badges', 'trailing', 'footerActions', 'buttons', 'amounts', 'metrics', 'children', 'subtitleSegments', 'titleMatch', 'selectedKeys', 'segments', 'hoverSecondary', 'hoverImages', 'secondaryBadges', 'fallbackUris', 'probeOrder', 'priceSegments', 'textSegments', 'secondaryTextSegments', 'primaryTextSegments']);
|
|
1353
|
+
const PROTOCOL_ARRAY_FIELDS = new Set(['columns', 'titleSuffixes', 'titleSegments', 'cells', 'points', 'images', 'overlays', 'badges', 'trailing', 'footerActions', 'buttons', 'amounts', 'metrics', 'children', 'subtitleSegments', 'titleMatch', 'selectedKeys', 'segments', 'hoverSecondary', 'hoverImages', 'secondaryBadges', 'fallbackUris', 'probeOrder', 'priceSegments', 'textSegments', 'secondaryTextSegments', 'primaryTextSegments']);
|
|
1291
1354
|
const PROTOCOL_NUMBER_FIELDS = new Set(['leadingContentMaxWidthRatio', 'height', 'width', 'fontSize', 'lineHeight', 'opacity', 'offsetY', 'offsetX', 'letterSpacing', 'cornerRadius', 'borderWidth', 'horizontalPadding', 'verticalPadding', 'leadingGap', 'lineGap', 'titleBadgeGap', 'trailingGap', 'start', 'end', 'padding', 'offset', 'overscan', 'minWidth', 'flex', 'paddingStart', 'paddingEnd', 'changeWidth', 'changeHeight', 'changeCornerRadius', 'contentTrailingGap', 'subtitleTrailingPadding', 'partGap', 'contentPadding', 'contentPaddingHorizontal', 'contentPaddingTop', 'contentPaddingBottom', 'itemSpacing', 'gridColumns']);
|
|
1292
1355
|
|
|
1293
1356
|
/** Validate only shapes that renderers dereference, not display budgets. */
|
|
@@ -1358,7 +1421,10 @@ function assertRowProtocol(row, path) {
|
|
|
1358
1421
|
}
|
|
1359
1422
|
if (row.height !== undefined && row.height < 0) fail(`${path}.height`, 'must be non-negative');
|
|
1360
1423
|
if (row.type === 'identity') {
|
|
1361
|
-
if (isDevelopment())
|
|
1424
|
+
if (isDevelopment()) {
|
|
1425
|
+
assertIdentityExtras(row, path);
|
|
1426
|
+
assertIdentityTitleRuns(row, path);
|
|
1427
|
+
}
|
|
1362
1428
|
if (row.titleSuffixes !== undefined && !Array.isArray(row.titleSuffixes)) fail(`${path}.titleSuffixes`, 'must be an array');
|
|
1363
1429
|
row.titleSuffixes?.forEach((suffix, index) => {
|
|
1364
1430
|
protocolObject(suffix, `${path}.titleSuffixes[${index}]`);
|
|
@@ -1617,6 +1683,8 @@ const NESTED_DECORATION_FIELDS = {
|
|
|
1617
1683
|
};
|
|
1618
1684
|
function sanitizeDecoration(value, field) {
|
|
1619
1685
|
if (value === undefined || value === null) return undefined;
|
|
1686
|
+
if (field === 'titleSegments') return passesTitleRunRules(() => assertTitleSegments(value, field)) ? value : undefined;
|
|
1687
|
+
if (field === 'titleLeadingIcon') return passesTitleRunRules(() => assertTitleLeadingIcon(value, field)) ? value : undefined;
|
|
1620
1688
|
// All or nothing: a badge without valid text and both colors is dropped, and
|
|
1621
1689
|
// only its three declared fields are kept.
|
|
1622
1690
|
if (field === 'webLeadingBadge') {
|
|
@@ -1959,6 +2027,8 @@ function assertPatchChanges(patch, index) {
|
|
|
1959
2027
|
if (suffix.maxWidth !== undefined && (!Number.isFinite(suffix.maxWidth) || suffix.maxWidth < 0 || suffix.maxWidth > 4096)) fail(`${path}.titleSuffixes[${index}].maxWidth`, 'must be finite within 0...4096');
|
|
1960
2028
|
});
|
|
1961
2029
|
assertIdentityExtras(patch.changes, path);
|
|
2030
|
+
// A null clear never reaches here; a replacement follows the row rules.
|
|
2031
|
+
assertIdentityTitleRuns(patch.changes, path);
|
|
1962
2032
|
assertText(patch.changes.subtitle, `${path}.subtitle`);
|
|
1963
2033
|
assertText(patch.changes.tertiary, `${path}.tertiary`);
|
|
1964
2034
|
if (patch.changes.tertiaryTone !== undefined && !['secondary', 'info'].includes(patch.changes.tertiaryTone)) {
|
|
@@ -2152,7 +2222,9 @@ function normalizePatch(patch, index) {
|
|
|
2152
2222
|
continue;
|
|
2153
2223
|
}
|
|
2154
2224
|
const canonical = isDevelopment() ? normalized : patch.type === 'market' && field === 'metrics' ? sanitizeMarketMetrics(normalized) : sanitizeDecoration(normalized, field);
|
|
2155
|
-
if (canonical !== undefined) changes[field] = canonical;
|
|
2225
|
+
if (canonical !== undefined) changes[field] = canonical;
|
|
2226
|
+
// A rejected title decoration clears the old one instead of leaving it drawn.
|
|
2227
|
+
else if (['titleImage', 'titleSegments', 'titleLeadingIcon'].includes(field)) changes[field] = null;
|
|
2156
2228
|
}
|
|
2157
2229
|
if (patch.type === 'market' && Array.isArray(changes.priceSegments) && changes.priceSegments.length > 0 && (typeof changes.price !== 'string' || !changes.price.trim())) fail(`${path}.changes.price`, 'is required when priceSegments is updated');
|
|
2158
2230
|
if (!isDevelopment()) {
|