@onekeyfe/react-native-image 3.0.157 → 3.0.159
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/README.md +3 -0
- package/android/build.gradle +1 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +283 -22
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +34 -4
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageMemoryVariants.kt +119 -0
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyImageMemoryVariantsTest.kt +149 -0
- package/ios/OneKeyImage.swift +192 -31
- package/ios/OneKeyImageCache.swift +40 -3
- package/ios/OneKeyImageMemoryVariants.swift +137 -0
- package/ios/OneKeyImageRequestContext.swift +36 -0
- package/ios/tests/OneKeyAvatarImageTests.swift +4 -5
- package/ios/tests/OneKeyImageInfrastructureTests.swift +133 -0
- package/lib/module/index.js +21 -2
- package/package.json +2 -2
- package/src/index.tsx +20 -0
|
@@ -76,6 +76,7 @@ final class HybridOneKeyImageCache: HybridOneKeyImageCacheSpec {
|
|
|
76
76
|
SDImageCache.shared.clearMemory()
|
|
77
77
|
// OneKey patch: Clear the dedicated local-avatar cache with public cache operations.
|
|
78
78
|
OneKeyAvatarImageLoader.cache.clearMemory()
|
|
79
|
+
OneKeyImageMemoryVariantRegistry.shared.clear()
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -94,6 +95,7 @@ final class HybridOneKeyImageCache: HybridOneKeyImageCacheSpec {
|
|
|
94
95
|
SDImageCache.shared.clearMemory()
|
|
95
96
|
// OneKey patch: The avatar cache is shared by render and preload requests.
|
|
96
97
|
OneKeyAvatarImageLoader.cache.clearMemory()
|
|
98
|
+
OneKeyImageMemoryVariantRegistry.shared.clear()
|
|
97
99
|
return try clearDisk()
|
|
98
100
|
}
|
|
99
101
|
|
|
@@ -136,14 +138,49 @@ final class HybridOneKeyImageCache: HybridOneKeyImageCacheSpec {
|
|
|
136
138
|
source: source,
|
|
137
139
|
thumbnailPixelSize: thumbnailPixelSize
|
|
138
140
|
)
|
|
141
|
+
if result == .success {
|
|
142
|
+
recordMemoryVariant(
|
|
143
|
+
rawURL: rawURL,
|
|
144
|
+
requestURL: requestURL,
|
|
145
|
+
source: source,
|
|
146
|
+
thumbnailPixelSize: thumbnailPixelSize
|
|
147
|
+
)
|
|
148
|
+
return true
|
|
149
|
+
}
|
|
139
150
|
if case .failure = result, requestURL != rawURL {
|
|
140
|
-
|
|
151
|
+
let fallbackResult = await loadAttempt(
|
|
141
152
|
url: rawURL,
|
|
142
153
|
source: source,
|
|
143
154
|
thumbnailPixelSize: thumbnailPixelSize
|
|
144
|
-
)
|
|
155
|
+
)
|
|
156
|
+
if fallbackResult == .success {
|
|
157
|
+
recordMemoryVariant(
|
|
158
|
+
rawURL: rawURL,
|
|
159
|
+
requestURL: rawURL,
|
|
160
|
+
source: source,
|
|
161
|
+
thumbnailPixelSize: thumbnailPixelSize
|
|
162
|
+
)
|
|
163
|
+
return true
|
|
164
|
+
}
|
|
145
165
|
}
|
|
146
|
-
return
|
|
166
|
+
return false
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private static func recordMemoryVariant(
|
|
170
|
+
rawURL: URL,
|
|
171
|
+
requestURL: URL,
|
|
172
|
+
source: OneKeyImagePreloadSource,
|
|
173
|
+
thumbnailPixelSize: CGSize
|
|
174
|
+
) {
|
|
175
|
+
let policy = source.cachePolicy ?? .memoryDisk
|
|
176
|
+
guard policy == .memory || policy == .memoryDisk else { return }
|
|
177
|
+
OneKeyImageMemoryVariantRegistry.shared.record(
|
|
178
|
+
OneKeyImageMemoryVariant(
|
|
179
|
+
requestURL: requestURL,
|
|
180
|
+
thumbnailPixelSize: thumbnailPixelSize
|
|
181
|
+
),
|
|
182
|
+
for: OneKeyImageMemoryFamilyKey(rawURL: rawURL, headersJson: source.headersJson)
|
|
183
|
+
)
|
|
147
184
|
}
|
|
148
185
|
|
|
149
186
|
private static func loadAttempt(
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
struct OneKeyImageMemoryFamilyKey: Hashable, Sendable {
|
|
4
|
+
let rawURL: URL
|
|
5
|
+
let headersDigest: String?
|
|
6
|
+
|
|
7
|
+
init(rawURL: URL, headersJson: String?) {
|
|
8
|
+
self.rawURL = rawURL
|
|
9
|
+
headersDigest =
|
|
10
|
+
OneKeyImageHeadersIdentity(
|
|
11
|
+
headers: OneKeyImageRequestContext.headers(from: headersJson)
|
|
12
|
+
).cacheKeyComponent
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
struct OneKeyImageMemoryVariant: Hashable, Sendable {
|
|
17
|
+
let requestURL: URL
|
|
18
|
+
let pixelWidth: Int
|
|
19
|
+
let pixelHeight: Int
|
|
20
|
+
|
|
21
|
+
init(requestURL: URL, thumbnailPixelSize: CGSize) {
|
|
22
|
+
self.requestURL = requestURL
|
|
23
|
+
pixelWidth = max(Int(thumbnailPixelSize.width.rounded()), 1)
|
|
24
|
+
pixelHeight = max(Int(thumbnailPixelSize.height.rounded()), 1)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var thumbnailPixelSize: CGSize {
|
|
28
|
+
CGSize(width: pixelWidth, height: pixelHeight)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var area: Int64 { Int64(pixelWidth) * Int64(pixelHeight) }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// Bounded hints for reproducing size-specific SDWebImage memory-cache keys.
|
|
35
|
+
/// SDImageCache remains authoritative; stale hints are removed on probe miss.
|
|
36
|
+
final class OneKeyImageMemoryVariantRegistry: @unchecked Sendable {
|
|
37
|
+
static let shared = OneKeyImageMemoryVariantRegistry()
|
|
38
|
+
|
|
39
|
+
private static let maximumFamilies = 1024
|
|
40
|
+
private static let maximumVariantsPerFamily = 8
|
|
41
|
+
private static let maximumAspectRatioDrift = 1.1
|
|
42
|
+
|
|
43
|
+
private let lock = NSLock()
|
|
44
|
+
private var variantsByFamily: [OneKeyImageMemoryFamilyKey: [OneKeyImageMemoryVariant]] = [:]
|
|
45
|
+
private var familyAccessOrder: [OneKeyImageMemoryFamilyKey] = []
|
|
46
|
+
|
|
47
|
+
func record(_ variant: OneKeyImageMemoryVariant, for family: OneKeyImageMemoryFamilyKey) {
|
|
48
|
+
lock.lock()
|
|
49
|
+
defer { lock.unlock() }
|
|
50
|
+
var variants = variantsByFamily[family] ?? []
|
|
51
|
+
variants.removeAll { $0 == variant }
|
|
52
|
+
variants.append(variant)
|
|
53
|
+
if variants.count > Self.maximumVariantsPerFamily {
|
|
54
|
+
variants.removeFirst(variants.count - Self.maximumVariantsPerFamily)
|
|
55
|
+
}
|
|
56
|
+
variantsByFamily[family] = variants
|
|
57
|
+
touch(family)
|
|
58
|
+
while familyAccessOrder.count > Self.maximumFamilies {
|
|
59
|
+
let oldest = familyAccessOrder.removeFirst()
|
|
60
|
+
variantsByFamily.removeValue(forKey: oldest)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
func remove(_ variant: OneKeyImageMemoryVariant, for family: OneKeyImageMemoryFamilyKey) {
|
|
65
|
+
lock.lock()
|
|
66
|
+
defer { lock.unlock() }
|
|
67
|
+
guard var variants = variantsByFamily[family] else { return }
|
|
68
|
+
variants.removeAll { $0 == variant }
|
|
69
|
+
if variants.isEmpty {
|
|
70
|
+
variantsByFamily.removeValue(forKey: family)
|
|
71
|
+
familyAccessOrder.removeAll { $0 == family }
|
|
72
|
+
} else {
|
|
73
|
+
variantsByFamily[family] = variants
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
func candidates(
|
|
78
|
+
for family: OneKeyImageMemoryFamilyKey,
|
|
79
|
+
target: OneKeyImageMemoryVariant,
|
|
80
|
+
excluding exactVariants: Set<OneKeyImageMemoryVariant>
|
|
81
|
+
) -> [OneKeyImageMemoryVariant] {
|
|
82
|
+
lock.lock()
|
|
83
|
+
let variants = variantsByFamily[family] ?? []
|
|
84
|
+
if !variants.isEmpty { touch(family) }
|
|
85
|
+
lock.unlock()
|
|
86
|
+
return Self.orderedCandidates(
|
|
87
|
+
variants: variants,
|
|
88
|
+
target: target,
|
|
89
|
+
excluding: exactVariants
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func clear() {
|
|
94
|
+
lock.lock()
|
|
95
|
+
variantsByFamily.removeAll()
|
|
96
|
+
familyAccessOrder.removeAll()
|
|
97
|
+
lock.unlock()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static func orderedCandidates(
|
|
101
|
+
variants: [OneKeyImageMemoryVariant],
|
|
102
|
+
target: OneKeyImageMemoryVariant,
|
|
103
|
+
excluding exactVariants: Set<OneKeyImageMemoryVariant> = []
|
|
104
|
+
) -> [OneKeyImageMemoryVariant] {
|
|
105
|
+
var seen: Set<OneKeyImageMemoryVariant> = []
|
|
106
|
+
let compatible = variants.reversed().filter {
|
|
107
|
+
seen.insert($0).inserted && !exactVariants.contains($0) && aspectRatioIsCompatible($0, target)
|
|
108
|
+
}
|
|
109
|
+
let larger =
|
|
110
|
+
compatible
|
|
111
|
+
.filter { $0.pixelWidth >= target.pixelWidth && $0.pixelHeight >= target.pixelHeight }
|
|
112
|
+
.min { lhs, rhs in lhs.area < rhs.area }
|
|
113
|
+
let smaller =
|
|
114
|
+
compatible
|
|
115
|
+
.filter { $0.pixelWidth <= target.pixelWidth && $0.pixelHeight <= target.pixelHeight }
|
|
116
|
+
.max { lhs, rhs in lhs.area < rhs.area }
|
|
117
|
+
var result: [OneKeyImageMemoryVariant] = []
|
|
118
|
+
if let larger { result.append(larger) }
|
|
119
|
+
if let smaller, smaller != larger { result.append(smaller) }
|
|
120
|
+
return result
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private static func aspectRatioIsCompatible(
|
|
124
|
+
_ candidate: OneKeyImageMemoryVariant,
|
|
125
|
+
_ target: OneKeyImageMemoryVariant
|
|
126
|
+
) -> Bool {
|
|
127
|
+
let candidateRatio = Double(candidate.pixelWidth) / Double(candidate.pixelHeight)
|
|
128
|
+
let targetRatio = Double(target.pixelWidth) / Double(target.pixelHeight)
|
|
129
|
+
return max(candidateRatio, targetRatio) / min(candidateRatio, targetRatio)
|
|
130
|
+
<= maximumAspectRatioDrift
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private func touch(_ family: OneKeyImageMemoryFamilyKey) {
|
|
134
|
+
familyAccessOrder.removeAll { $0 == family }
|
|
135
|
+
familyAccessOrder.append(family)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -610,6 +610,23 @@ enum OneKeyImageDecodeSizing {
|
|
|
610
610
|
}
|
|
611
611
|
}
|
|
612
612
|
|
|
613
|
+
/// The logical display size the TOS rendition is picked for. A preload picks
|
|
614
|
+
/// it from the long edge of its `resizeWidth`/`resizeHeight` hint, so a view
|
|
615
|
+
/// carrying the same hint must do the same (before AND after layout) or its
|
|
616
|
+
/// request URL, and with it the cache key, diverges from what the preload
|
|
617
|
+
/// stored. Without a hint the laid-out bounds decide; nil before layout.
|
|
618
|
+
static func tosDisplaySize(
|
|
619
|
+
bounds: CGSize,
|
|
620
|
+
resizeWidth: Double?,
|
|
621
|
+
resizeHeight: Double?
|
|
622
|
+
) -> CGFloat? {
|
|
623
|
+
if let hint = logicalViewSize(width: resizeWidth, height: resizeHeight) {
|
|
624
|
+
return max(hint.width, hint.height)
|
|
625
|
+
}
|
|
626
|
+
let edge = max(bounds.width, bounds.height)
|
|
627
|
+
return edge.isFinite && edge > 0 ? edge : nil
|
|
628
|
+
}
|
|
629
|
+
|
|
613
630
|
/// Decode-thumbnail sizes worth probing the memory cache with, most specific
|
|
614
631
|
/// first. The thumbnail size is part of the SDWebImage cache key, so a probe
|
|
615
632
|
/// only hits when it reproduces the key some earlier load stored under.
|
|
@@ -768,6 +785,25 @@ enum OneKeyImageRequestContext {
|
|
|
768
785
|
return context
|
|
769
786
|
}
|
|
770
787
|
|
|
788
|
+
#if DEBUG
|
|
789
|
+
// The separate pod test module cannot import make's full signature.
|
|
790
|
+
static func testContext(
|
|
791
|
+
headersJson: String?,
|
|
792
|
+
thumbnailPixelSize: CGSize?,
|
|
793
|
+
manager: SDWebImageManager,
|
|
794
|
+
url: URL?
|
|
795
|
+
) -> [SDWebImageContextOption: Any] {
|
|
796
|
+
make(
|
|
797
|
+
headersJson: headersJson,
|
|
798
|
+
cachePolicy: .memoryDisk,
|
|
799
|
+
thumbnailPixelSize: thumbnailPixelSize,
|
|
800
|
+
safetyTracker: nil,
|
|
801
|
+
manager: manager,
|
|
802
|
+
url: url
|
|
803
|
+
)
|
|
804
|
+
}
|
|
805
|
+
#endif
|
|
806
|
+
|
|
771
807
|
static func transportGuards(
|
|
772
808
|
tracker: OneKeyImageSafetyTracker
|
|
773
809
|
) -> [SDWebImageContextOption: Any] {
|
|
@@ -11,9 +11,8 @@ final class OneKeyAvatarImageTests: XCTestCase {
|
|
|
11
11
|
let first = OneKeyImagePipeline.makeIsolatedManager()
|
|
12
12
|
let second = OneKeyImagePipeline.makeIsolatedManager()
|
|
13
13
|
func context(_ manager: SDWebImageManager, headers: String) -> [SDWebImageContextOption: Any] {
|
|
14
|
-
OneKeyImageRequestContext.
|
|
15
|
-
thumbnailPixelSize: CGSize(width: 96, height: 96),
|
|
16
|
-
manager: manager, url: url)
|
|
14
|
+
OneKeyImageRequestContext.testContext(headersJson: headers,
|
|
15
|
+
thumbnailPixelSize: CGSize(width: 96, height: 96), manager: manager, url: url)
|
|
17
16
|
}
|
|
18
17
|
let a = context(first, headers: "{\"X-Test\":\"a\"}")
|
|
19
18
|
let b = context(second, headers: "{\"X-Test\":\"b\"}")
|
|
@@ -26,8 +25,8 @@ final class OneKeyAvatarImageTests: XCTestCase {
|
|
|
26
25
|
let firstFilter = try XCTUnwrap(a[.cacheKeyFilter] as? SDWebImageCacheKeyFilter)
|
|
27
26
|
let secondFilter = try XCTUnwrap(b[.cacheKeyFilter] as? SDWebImageCacheKeyFilter)
|
|
28
27
|
XCTAssertEqual(firstFilter.cacheKey(for: url), secondFilter.cacheKey(for: url))
|
|
29
|
-
let remote = OneKeyImageRequestContext.
|
|
30
|
-
thumbnailPixelSize: nil,
|
|
28
|
+
let remote = OneKeyImageRequestContext.testContext(headersJson: nil,
|
|
29
|
+
thumbnailPixelSize: nil, manager: first,
|
|
31
30
|
url: URL(string: "https://example.com/image.png"))
|
|
32
31
|
XCTAssertNil(remote[.imageLoader])
|
|
33
32
|
XCTAssertNil(remote[.imageCache])
|
|
@@ -389,6 +389,90 @@ final class OneKeyImageInfrastructureTests: XCTestCase {
|
|
|
389
389
|
)
|
|
390
390
|
}
|
|
391
391
|
|
|
392
|
+
func testMemoryVariantsChooseNearestLargerThenNearestSmaller() {
|
|
393
|
+
let target = memoryVariant(96)
|
|
394
|
+
XCTAssertEqual(
|
|
395
|
+
OneKeyImageMemoryVariantRegistry.orderedCandidates(
|
|
396
|
+
variants: [memoryVariant(48), memoryVariant(72), memoryVariant(120), memoryVariant(144)],
|
|
397
|
+
target: target
|
|
398
|
+
),
|
|
399
|
+
[memoryVariant(120), memoryVariant(72)]
|
|
400
|
+
)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
func testMemoryVariantsIgnoreExactAndIncompatibleAspectRatios() {
|
|
404
|
+
let target = memoryVariant(96)
|
|
405
|
+
let wide = OneKeyImageMemoryVariant(
|
|
406
|
+
requestURL: URL(string: "https://example.com/wide.png")!,
|
|
407
|
+
thumbnailPixelSize: CGSize(width: 120, height: 60)
|
|
408
|
+
)
|
|
409
|
+
XCTAssertEqual(
|
|
410
|
+
OneKeyImageMemoryVariantRegistry.orderedCandidates(
|
|
411
|
+
variants: [target, wide, memoryVariant(120)],
|
|
412
|
+
target: target,
|
|
413
|
+
excluding: [target]
|
|
414
|
+
),
|
|
415
|
+
[memoryVariant(120)]
|
|
416
|
+
)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
func testCrossSizeCenterPreviewUsesTemporaryScaling() {
|
|
420
|
+
XCTAssertTrue(
|
|
421
|
+
HybridOneKeyImage.shouldScaleCenterMemoryPreview(
|
|
422
|
+
contentFit: .center,
|
|
423
|
+
candidate: memoryVariant(48),
|
|
424
|
+
target: memoryVariant(96)
|
|
425
|
+
)
|
|
426
|
+
)
|
|
427
|
+
XCTAssertTrue(
|
|
428
|
+
HybridOneKeyImage.shouldScaleCenterMemoryPreview(
|
|
429
|
+
contentFit: .center,
|
|
430
|
+
candidate: memoryVariant(144),
|
|
431
|
+
target: memoryVariant(96)
|
|
432
|
+
)
|
|
433
|
+
)
|
|
434
|
+
XCTAssertFalse(
|
|
435
|
+
HybridOneKeyImage.shouldScaleCenterMemoryPreview(
|
|
436
|
+
contentFit: .center,
|
|
437
|
+
candidate: memoryVariant(96),
|
|
438
|
+
target: memoryVariant(96)
|
|
439
|
+
)
|
|
440
|
+
)
|
|
441
|
+
XCTAssertFalse(
|
|
442
|
+
HybridOneKeyImage.shouldScaleCenterMemoryPreview(
|
|
443
|
+
contentFit: .cover,
|
|
444
|
+
candidate: memoryVariant(48),
|
|
445
|
+
target: memoryVariant(96)
|
|
446
|
+
)
|
|
447
|
+
)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
func testReplacingMemoryPreviewSkipsWholeViewFade() {
|
|
451
|
+
XCTAssertFalse(
|
|
452
|
+
HybridOneKeyImage.shouldAnimateLoadedImageTransition(
|
|
453
|
+
cacheType: .disk,
|
|
454
|
+
requestDuration: 1,
|
|
455
|
+
reduceMotionEnabled: false,
|
|
456
|
+
replacingMemoryPreview: true
|
|
457
|
+
)
|
|
458
|
+
)
|
|
459
|
+
XCTAssertTrue(
|
|
460
|
+
HybridOneKeyImage.shouldAnimateLoadedImageTransition(
|
|
461
|
+
cacheType: .disk,
|
|
462
|
+
requestDuration: 1,
|
|
463
|
+
reduceMotionEnabled: false,
|
|
464
|
+
replacingMemoryPreview: false
|
|
465
|
+
)
|
|
466
|
+
)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
private func memoryVariant(_ size: Int) -> OneKeyImageMemoryVariant {
|
|
470
|
+
OneKeyImageMemoryVariant(
|
|
471
|
+
requestURL: URL(string: "https://example.com/image-\(size).png")!,
|
|
472
|
+
thumbnailPixelSize: CGSize(width: size, height: size)
|
|
473
|
+
)
|
|
474
|
+
}
|
|
475
|
+
|
|
392
476
|
func testDisplayedImageIsPreservedOnlyWhileShowingOne() {
|
|
393
477
|
XCTAssertTrue(HybridOneKeyImage.shouldPreserveDisplayedImage(isShowingImage: true, hasImage: true))
|
|
394
478
|
XCTAssertFalse(HybridOneKeyImage.shouldPreserveDisplayedImage(isShowingImage: true, hasImage: false))
|
|
@@ -629,6 +713,55 @@ final class OneKeyImageInfrastructureTests: XCTestCase {
|
|
|
629
713
|
)
|
|
630
714
|
}
|
|
631
715
|
|
|
716
|
+
func testRecycledViewForgetsBothResizeHints() throws {
|
|
717
|
+
let image = HybridOneKeyImage()
|
|
718
|
+
image.sourceUri = "https://example.com/hinted.png"
|
|
719
|
+
image.resizeWidth = 48
|
|
720
|
+
image.resizeHeight = 64
|
|
721
|
+
image.prepareForRecycle()
|
|
722
|
+
XCTAssertNil(image.resizeWidth)
|
|
723
|
+
XCTAssertNil(image.resizeHeight)
|
|
724
|
+
XCTAssertNil(image.sourceUri)
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
func testViewAndPreloadPickTheSameTosRenditionForOneHint() throws {
|
|
728
|
+
// Preload: long edge of logicalViewSize(resizeWidth, resizeHeight).
|
|
729
|
+
// View: the same, before and after layout, so the request URL (and with it
|
|
730
|
+
// the cache key) is identical on both sides.
|
|
731
|
+
let raw = try XCTUnwrap(URL(string: "https://common.onekey-asset.com/token.png"))
|
|
732
|
+
let hint = try XCTUnwrap(OneKeyImageDecodeSizing.logicalViewSize(width: 48, height: 64))
|
|
733
|
+
let preloadDisplaySize = max(hint.width, hint.height)
|
|
734
|
+
for bounds in [CGSize.zero, CGSize(width: 48, height: 64), CGSize(width: 30, height: 30)] {
|
|
735
|
+
let viewDisplaySize = try XCTUnwrap(
|
|
736
|
+
OneKeyImageDecodeSizing.tosDisplaySize(bounds: bounds, resizeWidth: 48, resizeHeight: 64)
|
|
737
|
+
)
|
|
738
|
+
XCTAssertEqual(viewDisplaySize, preloadDisplaySize)
|
|
739
|
+
XCTAssertEqual(
|
|
740
|
+
OneKeyTosURL.optimized(
|
|
741
|
+
rawURL: raw, displaySize: viewDisplaySize, scale: 3, overscan: 1.1, hasCustomIdentity: false
|
|
742
|
+
),
|
|
743
|
+
OneKeyTosURL.optimized(
|
|
744
|
+
rawURL: raw, displaySize: preloadDisplaySize, scale: 3, overscan: 1.1, hasCustomIdentity: false
|
|
745
|
+
)
|
|
746
|
+
)
|
|
747
|
+
}
|
|
748
|
+
// Width-only hint keeps the historical behaviour (the width itself).
|
|
749
|
+
XCTAssertEqual(
|
|
750
|
+
OneKeyImageDecodeSizing.tosDisplaySize(bounds: .zero, resizeWidth: 40, resizeHeight: nil),
|
|
751
|
+
40
|
|
752
|
+
)
|
|
753
|
+
// No hint: the laid-out bounds decide; nothing before layout.
|
|
754
|
+
XCTAssertEqual(
|
|
755
|
+
OneKeyImageDecodeSizing.tosDisplaySize(
|
|
756
|
+
bounds: CGSize(width: 40, height: 64), resizeWidth: nil, resizeHeight: nil
|
|
757
|
+
),
|
|
758
|
+
64
|
|
759
|
+
)
|
|
760
|
+
XCTAssertNil(
|
|
761
|
+
OneKeyImageDecodeSizing.tosDisplaySize(bounds: .zero, resizeWidth: nil, resizeHeight: nil)
|
|
762
|
+
)
|
|
763
|
+
}
|
|
764
|
+
|
|
632
765
|
func testSupersededRequestNeverPaintsRecycledView() throws {
|
|
633
766
|
let image = HybridOneKeyImage()
|
|
634
767
|
let imageView = try XCTUnwrap(image.view as? SDAnimatedImageView)
|
package/lib/module/index.js
CHANGED
|
@@ -200,7 +200,8 @@ export function OneKeyImage({
|
|
|
200
200
|
borderStartEndRadius: flattenedStyle.borderStartEndRadius,
|
|
201
201
|
borderEndStartRadius: flattenedStyle.borderEndStartRadius,
|
|
202
202
|
borderEndEndRadius: flattenedStyle.borderEndEndRadius,
|
|
203
|
-
zIndex: 1
|
|
203
|
+
zIndex: 1,
|
|
204
|
+
...(round ? ROUND_CLIP : null)
|
|
204
205
|
} : undefined;
|
|
205
206
|
const native = /*#__PURE__*/createElement(NativeOneKeyImage, {
|
|
206
207
|
...viewProps,
|
|
@@ -223,7 +224,7 @@ export function OneKeyImage({
|
|
|
223
224
|
});
|
|
224
225
|
if (!shouldWrapNative) return native;
|
|
225
226
|
return /*#__PURE__*/_jsxs(View, {
|
|
226
|
-
style: [style, styles.overlayContainer, hasRoundedCorners ? styles.borderlessContainer : undefined],
|
|
227
|
+
style: [style, styles.overlayContainer, hasRoundedCorners ? styles.borderlessContainer : undefined, round ? ROUND_CLIP : undefined],
|
|
227
228
|
collapsable: false,
|
|
228
229
|
children: [native, effectiveState === 'loading' && placeholder != null ? /*#__PURE__*/_jsx(View, {
|
|
229
230
|
pointerEvents: "none",
|
|
@@ -259,6 +260,24 @@ export const OneKeyImageCache = {
|
|
|
259
260
|
clearDisk: () => nativeCache.clearDisk(),
|
|
260
261
|
clearAll: () => nativeCache.clearAll()
|
|
261
262
|
};
|
|
263
|
+
|
|
264
|
+
// `round` clips the native view to a circle: an oval path on Android
|
|
265
|
+
// (`OneKeyImage.kt` `addOval`), a `min(width, height) / 2` corner radius on iOS
|
|
266
|
+
// (`OneKeyImage.swift` `updateRoundMask`). The overlay container has to clip the
|
|
267
|
+
// same way, or a custom placeholder or fallback is painted square over a round
|
|
268
|
+
// image. A percentage radius says that without measuring the view, and covers
|
|
269
|
+
// every per-corner radius the caller's own style may have set.
|
|
270
|
+
const ROUND_CLIP = {
|
|
271
|
+
borderRadius: '50%',
|
|
272
|
+
borderTopLeftRadius: '50%',
|
|
273
|
+
borderTopRightRadius: '50%',
|
|
274
|
+
borderBottomLeftRadius: '50%',
|
|
275
|
+
borderBottomRightRadius: '50%',
|
|
276
|
+
borderStartStartRadius: '50%',
|
|
277
|
+
borderStartEndRadius: '50%',
|
|
278
|
+
borderEndStartRadius: '50%',
|
|
279
|
+
borderEndEndRadius: '50%'
|
|
280
|
+
};
|
|
262
281
|
const styles = StyleSheet.create({
|
|
263
282
|
overlayContainer: {
|
|
264
283
|
overflow: 'hidden'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/react-native-image",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.159",
|
|
4
4
|
"description": "High-performance native image view for OneKey",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"typescript": "^5.9.2"
|
|
82
82
|
},
|
|
83
83
|
"peerDependencies": {
|
|
84
|
-
"@onekeyfe/react-native-skeleton": "3.0.
|
|
84
|
+
"@onekeyfe/react-native-skeleton": "3.0.159",
|
|
85
85
|
"react": "*",
|
|
86
86
|
"react-native": "*",
|
|
87
87
|
"react-native-nitro-modules": "0.37.0"
|
package/src/index.tsx
CHANGED
|
@@ -355,6 +355,7 @@ export function OneKeyImage({
|
|
|
355
355
|
borderEndStartRadius: flattenedStyle.borderEndStartRadius,
|
|
356
356
|
borderEndEndRadius: flattenedStyle.borderEndEndRadius,
|
|
357
357
|
zIndex: 1,
|
|
358
|
+
...(round ? ROUND_CLIP : null),
|
|
358
359
|
}
|
|
359
360
|
: undefined;
|
|
360
361
|
|
|
@@ -389,6 +390,7 @@ export function OneKeyImage({
|
|
|
389
390
|
style,
|
|
390
391
|
styles.overlayContainer,
|
|
391
392
|
hasRoundedCorners ? styles.borderlessContainer : undefined,
|
|
393
|
+
round ? ROUND_CLIP : undefined,
|
|
392
394
|
]}
|
|
393
395
|
collapsable={false}
|
|
394
396
|
>
|
|
@@ -444,6 +446,24 @@ export const OneKeyImageCache = {
|
|
|
444
446
|
clearAll: () => nativeCache.clearAll(),
|
|
445
447
|
};
|
|
446
448
|
|
|
449
|
+
// `round` clips the native view to a circle: an oval path on Android
|
|
450
|
+
// (`OneKeyImage.kt` `addOval`), a `min(width, height) / 2` corner radius on iOS
|
|
451
|
+
// (`OneKeyImage.swift` `updateRoundMask`). The overlay container has to clip the
|
|
452
|
+
// same way, or a custom placeholder or fallback is painted square over a round
|
|
453
|
+
// image. A percentage radius says that without measuring the view, and covers
|
|
454
|
+
// every per-corner radius the caller's own style may have set.
|
|
455
|
+
const ROUND_CLIP = {
|
|
456
|
+
borderRadius: '50%',
|
|
457
|
+
borderTopLeftRadius: '50%',
|
|
458
|
+
borderTopRightRadius: '50%',
|
|
459
|
+
borderBottomLeftRadius: '50%',
|
|
460
|
+
borderBottomRightRadius: '50%',
|
|
461
|
+
borderStartStartRadius: '50%',
|
|
462
|
+
borderStartEndRadius: '50%',
|
|
463
|
+
borderEndStartRadius: '50%',
|
|
464
|
+
borderEndEndRadius: '50%',
|
|
465
|
+
} as const;
|
|
466
|
+
|
|
447
467
|
const styles = StyleSheet.create({
|
|
448
468
|
overlayContainer: {
|
|
449
469
|
overflow: 'hidden',
|