@onekeyfe/react-native-image 3.0.158 → 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 +161 -27
- package/ios/OneKeyImageCache.swift +40 -3
- package/ios/OneKeyImageMemoryVariants.swift +137 -0
- package/ios/OneKeyImageRequestContext.swift +19 -0
- package/ios/tests/OneKeyAvatarImageTests.swift +4 -5
- package/ios/tests/OneKeyImageInfrastructureTests.swift +84 -0
- package/lib/module/index.js +21 -2
- package/package.json +2 -2
- package/src/index.tsx +20 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package com.margelo.nitro.onekeyimage
|
|
2
|
+
|
|
3
|
+
import kotlin.math.max
|
|
4
|
+
import kotlin.math.min
|
|
5
|
+
|
|
6
|
+
internal data class OneKeyImageMemoryFamilyKey(
|
|
7
|
+
val rawUrl: String,
|
|
8
|
+
val headersDigest: String?,
|
|
9
|
+
) {
|
|
10
|
+
companion object {
|
|
11
|
+
fun from(rawUrl: String, headersJson: String?): OneKeyImageMemoryFamilyKey =
|
|
12
|
+
OneKeyImageMemoryFamilyKey(
|
|
13
|
+
rawUrl = rawUrl,
|
|
14
|
+
headersDigest = OneKeyImageModel.remoteHeadersDigest(
|
|
15
|
+
OneKeyImageModel.headers(headersJson),
|
|
16
|
+
),
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
internal data class OneKeyImageMemoryVariant(
|
|
22
|
+
val requestUrl: String,
|
|
23
|
+
val width: Int,
|
|
24
|
+
val height: Int,
|
|
25
|
+
val round: Boolean,
|
|
26
|
+
) {
|
|
27
|
+
val area: Long
|
|
28
|
+
get() = width.toLong() * height.toLong()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A bounded index of size-specific Glide resources that may still be in memory.
|
|
33
|
+
*
|
|
34
|
+
* Glide remains the source of truth. Entries are only hints used to reproduce a
|
|
35
|
+
* candidate EngineKey; a synchronous cache miss removes the stale hint.
|
|
36
|
+
*/
|
|
37
|
+
internal object OneKeyImageMemoryVariantRegistry {
|
|
38
|
+
private const val MAX_FAMILIES = 1024
|
|
39
|
+
private const val MAX_VARIANTS_PER_FAMILY = 8
|
|
40
|
+
private const val MAX_ASPECT_RATIO_DRIFT = 1.1
|
|
41
|
+
|
|
42
|
+
private val variantsByFamily =
|
|
43
|
+
LinkedHashMap<OneKeyImageMemoryFamilyKey, MutableList<OneKeyImageMemoryVariant>>(
|
|
44
|
+
16,
|
|
45
|
+
0.75f,
|
|
46
|
+
true,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
@Synchronized
|
|
50
|
+
fun record(family: OneKeyImageMemoryFamilyKey, variant: OneKeyImageMemoryVariant) {
|
|
51
|
+
if (variant.width <= 0 || variant.height <= 0) return
|
|
52
|
+
val variants = variantsByFamily.getOrPut(family) { mutableListOf() }
|
|
53
|
+
variants.remove(variant)
|
|
54
|
+
variants.add(variant)
|
|
55
|
+
while (variants.size > MAX_VARIANTS_PER_FAMILY) variants.removeAt(0)
|
|
56
|
+
while (variantsByFamily.size > MAX_FAMILIES) {
|
|
57
|
+
val eldest = variantsByFamily.entries.firstOrNull()?.key ?: break
|
|
58
|
+
variantsByFamily.remove(eldest)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@Synchronized
|
|
63
|
+
fun remove(family: OneKeyImageMemoryFamilyKey, variant: OneKeyImageMemoryVariant) {
|
|
64
|
+
val variants = variantsByFamily[family] ?: return
|
|
65
|
+
variants.remove(variant)
|
|
66
|
+
if (variants.isEmpty()) variantsByFamily.remove(family)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Synchronized
|
|
70
|
+
fun candidates(
|
|
71
|
+
family: OneKeyImageMemoryFamilyKey,
|
|
72
|
+
target: OneKeyImageMemoryVariant,
|
|
73
|
+
): List<OneKeyImageMemoryVariant> = orderedCandidates(
|
|
74
|
+
variants = variantsByFamily[family].orEmpty(),
|
|
75
|
+
target = target,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
@Synchronized
|
|
79
|
+
fun clear() {
|
|
80
|
+
variantsByFamily.clear()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
internal fun orderedCandidates(
|
|
84
|
+
variants: List<OneKeyImageMemoryVariant>,
|
|
85
|
+
target: OneKeyImageMemoryVariant,
|
|
86
|
+
): List<OneKeyImageMemoryVariant> {
|
|
87
|
+
val compatible = variants
|
|
88
|
+
.asSequence()
|
|
89
|
+
.filter { it != target }
|
|
90
|
+
.filter { candidate -> target.round || !candidate.round }
|
|
91
|
+
.filter { candidate -> aspectRatioIsCompatible(candidate, target) }
|
|
92
|
+
.distinct()
|
|
93
|
+
.toList()
|
|
94
|
+
|
|
95
|
+
val larger = compatible
|
|
96
|
+
.filter { it.width >= target.width && it.height >= target.height }
|
|
97
|
+
.minWithOrNull(
|
|
98
|
+
compareBy<OneKeyImageMemoryVariant> { it.area }
|
|
99
|
+
.thenBy { if (it.round == target.round) 0 else 1 },
|
|
100
|
+
)
|
|
101
|
+
val smaller = compatible
|
|
102
|
+
.filter { it.width <= target.width && it.height <= target.height }
|
|
103
|
+
.maxWithOrNull(
|
|
104
|
+
compareBy<OneKeyImageMemoryVariant> { it.area }
|
|
105
|
+
.thenByDescending { if (it.round == target.round) 0 else 1 },
|
|
106
|
+
)
|
|
107
|
+
return listOfNotNull(larger, smaller).distinct()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private fun aspectRatioIsCompatible(
|
|
111
|
+
candidate: OneKeyImageMemoryVariant,
|
|
112
|
+
target: OneKeyImageMemoryVariant,
|
|
113
|
+
): Boolean {
|
|
114
|
+
val candidateRatio = candidate.width.toDouble() / candidate.height.toDouble()
|
|
115
|
+
val targetRatio = target.width.toDouble() / target.height.toDouble()
|
|
116
|
+
return max(candidateRatio, targetRatio) / min(candidateRatio, targetRatio) <=
|
|
117
|
+
MAX_ASPECT_RATIO_DRIFT
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
package com.margelo.nitro.onekeyimage
|
|
2
|
+
|
|
3
|
+
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
|
4
|
+
import com.bumptech.glide.request.RequestOptions
|
|
5
|
+
import org.junit.Assert.assertEquals
|
|
6
|
+
import org.junit.Assert.assertFalse
|
|
7
|
+
import org.junit.Assert.assertSame
|
|
8
|
+
import org.junit.Assert.assertTrue
|
|
9
|
+
import org.junit.Test
|
|
10
|
+
|
|
11
|
+
class OneKeyImageMemoryVariantsTest {
|
|
12
|
+
@Test
|
|
13
|
+
fun choosesNearestLargerThenNearestSmallerVariant() {
|
|
14
|
+
val target = variant(96)
|
|
15
|
+
val candidates = OneKeyImageMemoryVariantRegistry.orderedCandidates(
|
|
16
|
+
variants = listOf(variant(48), variant(72), variant(120), variant(144)),
|
|
17
|
+
target = target,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
assertEquals(listOf(variant(120), variant(72)), candidates)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Test
|
|
24
|
+
fun ignoresExactAndIncompatibleAspectRatios() {
|
|
25
|
+
val target = variant(96)
|
|
26
|
+
val candidates = OneKeyImageMemoryVariantRegistry.orderedCandidates(
|
|
27
|
+
variants = listOf(
|
|
28
|
+
target,
|
|
29
|
+
OneKeyImageMemoryVariant("wide", 120, 60, round = false),
|
|
30
|
+
variant(120),
|
|
31
|
+
),
|
|
32
|
+
target = target,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
assertEquals(listOf(variant(120)), candidates)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Test
|
|
39
|
+
fun roundTargetCanPreviewUntransformedResourceButSquareTargetCannotUseRoundResource() {
|
|
40
|
+
val untransformed = variant(120, round = false)
|
|
41
|
+
val transformed = variant(120, round = true)
|
|
42
|
+
|
|
43
|
+
assertEquals(
|
|
44
|
+
listOf(untransformed),
|
|
45
|
+
OneKeyImageMemoryVariantRegistry.orderedCandidates(
|
|
46
|
+
variants = listOf(untransformed),
|
|
47
|
+
target = variant(96, round = true),
|
|
48
|
+
),
|
|
49
|
+
)
|
|
50
|
+
assertEquals(
|
|
51
|
+
emptyList<OneKeyImageMemoryVariant>(),
|
|
52
|
+
OneKeyImageMemoryVariantRegistry.orderedCandidates(
|
|
53
|
+
variants = listOf(transformed),
|
|
54
|
+
target = variant(96, round = false),
|
|
55
|
+
),
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun crossSizeCenterPreviewUsesTemporaryScaling() {
|
|
61
|
+
assertTrue(
|
|
62
|
+
shouldScaleCenterMemoryPreview(
|
|
63
|
+
contentFit = OneKeyImageContentFit.CENTER,
|
|
64
|
+
candidate = variant(48),
|
|
65
|
+
target = variant(96),
|
|
66
|
+
),
|
|
67
|
+
)
|
|
68
|
+
assertTrue(
|
|
69
|
+
shouldScaleCenterMemoryPreview(
|
|
70
|
+
contentFit = OneKeyImageContentFit.CENTER,
|
|
71
|
+
candidate = variant(144),
|
|
72
|
+
target = variant(96),
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
assertFalse(
|
|
76
|
+
shouldScaleCenterMemoryPreview(
|
|
77
|
+
contentFit = OneKeyImageContentFit.CENTER,
|
|
78
|
+
candidate = variant(96),
|
|
79
|
+
target = variant(96),
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
assertFalse(
|
|
83
|
+
shouldScaleCenterMemoryPreview(
|
|
84
|
+
contentFit = OneKeyImageContentFit.COVER,
|
|
85
|
+
candidate = variant(48),
|
|
86
|
+
target = variant(96),
|
|
87
|
+
),
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@Test
|
|
92
|
+
fun missingPreviewDrawableAllowsSameFamilyProbeRetry() {
|
|
93
|
+
assertTrue(
|
|
94
|
+
shouldProbeMemoryPreview(
|
|
95
|
+
hasTarget = true,
|
|
96
|
+
sameFamily = true,
|
|
97
|
+
hasDrawable = false,
|
|
98
|
+
),
|
|
99
|
+
)
|
|
100
|
+
assertFalse(
|
|
101
|
+
shouldProbeMemoryPreview(
|
|
102
|
+
hasTarget = true,
|
|
103
|
+
sameFamily = true,
|
|
104
|
+
hasDrawable = true,
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@Test
|
|
110
|
+
fun replacingMemoryPreviewSkipsWholeViewFade() {
|
|
111
|
+
assertFalse(
|
|
112
|
+
shouldAnimateLoadedImageTransition(
|
|
113
|
+
cacheType = OneKeyImageCacheType.DISK,
|
|
114
|
+
fadeDelayElapsed = true,
|
|
115
|
+
animationsEnabled = true,
|
|
116
|
+
replacingMemoryPreview = true,
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
assertTrue(
|
|
120
|
+
shouldAnimateLoadedImageTransition(
|
|
121
|
+
cacheType = OneKeyImageCacheType.DISK,
|
|
122
|
+
fadeDelayElapsed = true,
|
|
123
|
+
animationsEnabled = true,
|
|
124
|
+
replacingMemoryPreview = false,
|
|
125
|
+
),
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@Test
|
|
130
|
+
fun memoryProbeKeepsMemoryEnabledAndDisablesDiskReads() {
|
|
131
|
+
val options = memoryProbeRequestOptions(
|
|
132
|
+
RequestOptions().diskCacheStrategy(DiskCacheStrategy.AUTOMATIC),
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
assertSame(DiskCacheStrategy.NONE, options.diskCacheStrategy)
|
|
136
|
+
assertTrue(options.onlyRetrieveFromCache)
|
|
137
|
+
assertTrue(options.isMemoryCacheable)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private fun variant(
|
|
141
|
+
size: Int,
|
|
142
|
+
round: Boolean = false,
|
|
143
|
+
) = OneKeyImageMemoryVariant(
|
|
144
|
+
requestUrl = "https://example.com/image-$size.png",
|
|
145
|
+
width = size,
|
|
146
|
+
height = size,
|
|
147
|
+
round = round,
|
|
148
|
+
)
|
|
149
|
+
}
|
package/ios/OneKeyImage.swift
CHANGED
|
@@ -98,6 +98,7 @@ private final class OneKeyImageSkeletonIndicator: NSObject, SDWebImageIndicator
|
|
|
98
98
|
|
|
99
99
|
final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
100
100
|
private enum DisplayState { case loading, image, error, fallback }
|
|
101
|
+
private enum MemoryProbeResult { case miss, preview, exact }
|
|
101
102
|
private static let fadeDelayThreshold: CFTimeInterval = 0.1
|
|
102
103
|
private static let fadeDuration: TimeInterval = 0.14
|
|
103
104
|
|
|
@@ -118,6 +119,8 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
118
119
|
/// `.loading` with the previous image kept on screen as the placeholder
|
|
119
120
|
/// (see `showLoading(preservedImage:)`).
|
|
120
121
|
private var isPreservingDisplayedImage = false
|
|
122
|
+
private var isShowingMemoryPreview = false
|
|
123
|
+
private var scalesCenterMemoryPreview = false
|
|
121
124
|
private var requestStartedAt: CFTimeInterval?
|
|
122
125
|
|
|
123
126
|
var view: UIView { hostView }
|
|
@@ -207,8 +210,8 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
207
210
|
// Fabric can reset `round` before clearing `sourceUri` while removing a view.
|
|
208
211
|
// Apply the shape once per committed prop batch so the outgoing frame keeps
|
|
209
212
|
// its clipping, while mounted and recycled views still receive the new value.
|
|
210
|
-
if let sourceUri, !sourceUri.isEmpty {
|
|
211
|
-
hostView.round = round
|
|
213
|
+
if let sourceUri, !sourceUri.isEmpty, let round {
|
|
214
|
+
hostView.round = round
|
|
212
215
|
}
|
|
213
216
|
scheduleLoad()
|
|
214
217
|
}
|
|
@@ -254,7 +257,7 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
254
257
|
self.cancelCurrentRequest(invalidateGeneration: true)
|
|
255
258
|
self.lastRequestSignature = nil
|
|
256
259
|
if let source = self.sourceUri, !source.isEmpty {
|
|
257
|
-
if
|
|
260
|
+
if self.showMemoryCachedImageIfAvailable(requestIsActive: false) == .miss {
|
|
258
261
|
self.showLoading(requestIsActive: false, letLibraryStartIndicator: false)
|
|
259
262
|
}
|
|
260
263
|
} else {
|
|
@@ -310,7 +313,7 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
310
313
|
requestStartedAt = CACurrentMediaTime()
|
|
311
314
|
onLoadStart?()
|
|
312
315
|
guard Self.isCurrentRequestGeneration(generation, current: requestGeneration) else { return }
|
|
313
|
-
if showMemoryCachedImageIfAvailable(requestIsActive: true) { return }
|
|
316
|
+
if showMemoryCachedImageIfAvailable(requestIsActive: true) == .exact { return }
|
|
314
317
|
// The pre-layout probe may have shown this identity from another thumbnail
|
|
315
318
|
// key (a preload's, or an earlier layout size). Keep it on screen as the
|
|
316
319
|
// placeholder while the exact rendition loads: blanking to a skeleton and
|
|
@@ -435,14 +438,32 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
435
438
|
url: rawURL,
|
|
436
439
|
rawURL: rawURL,
|
|
437
440
|
generation: generation,
|
|
438
|
-
mayFallbackToRaw: false
|
|
441
|
+
mayFallbackToRaw: false,
|
|
442
|
+
placeholderImage: self.hostView.image
|
|
439
443
|
)
|
|
440
444
|
return
|
|
441
445
|
}
|
|
442
446
|
self.finishWithError(safetyViolation ?? error, generation: generation)
|
|
443
447
|
return
|
|
444
448
|
}
|
|
449
|
+
if let image,
|
|
450
|
+
Self.canUseAsMemoryPreview(image),
|
|
451
|
+
let thumbnailPixelSize,
|
|
452
|
+
self.cachePolicy == .memory || self.cachePolicy == .memoryDisk || self.cachePolicy == nil
|
|
453
|
+
{
|
|
454
|
+
OneKeyImageMemoryVariantRegistry.shared.record(
|
|
455
|
+
OneKeyImageMemoryVariant(
|
|
456
|
+
requestURL: url,
|
|
457
|
+
thumbnailPixelSize: thumbnailPixelSize
|
|
458
|
+
),
|
|
459
|
+
for: OneKeyImageMemoryFamilyKey(
|
|
460
|
+
rawURL: rawURL,
|
|
461
|
+
headersJson: self.sourceHeadersJson
|
|
462
|
+
)
|
|
463
|
+
)
|
|
464
|
+
}
|
|
445
465
|
guard self.claimTerminal(generation) else { return }
|
|
466
|
+
let replacesMemoryPreview = self.clearMemoryPreviewPresentation()
|
|
446
467
|
self.requestActive = false
|
|
447
468
|
self.displayState = .image
|
|
448
469
|
self.isPreservingDisplayedImage = false
|
|
@@ -450,7 +471,10 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
450
471
|
self.fallbackLayer.isHidden = true
|
|
451
472
|
self.hostView.backgroundColor = .clear
|
|
452
473
|
let resolvedCacheType = Self.cacheType(cacheType)
|
|
453
|
-
self.applyLoadedImageTransition(
|
|
474
|
+
self.applyLoadedImageTransition(
|
|
475
|
+
cacheType: resolvedCacheType,
|
|
476
|
+
replacingMemoryPreview: replacesMemoryPreview
|
|
477
|
+
)
|
|
454
478
|
self.applyAutoplay()
|
|
455
479
|
let onLoad = self.onLoad
|
|
456
480
|
let onLoadEnd = self.onLoadEnd
|
|
@@ -615,6 +639,7 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
615
639
|
hostView.backgroundColor = .clear
|
|
616
640
|
return
|
|
617
641
|
}
|
|
642
|
+
clearMemoryPreviewPresentation()
|
|
618
643
|
isPreservingDisplayedImage = false
|
|
619
644
|
hostView.image = nil
|
|
620
645
|
applyLoadingAppearance(letLibraryStartIndicator: letLibraryStartIndicator)
|
|
@@ -627,7 +652,7 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
627
652
|
isShowingImage && hasImage
|
|
628
653
|
}
|
|
629
654
|
|
|
630
|
-
private func showMemoryCachedImageIfAvailable(requestIsActive: Bool) ->
|
|
655
|
+
private func showMemoryCachedImageIfAvailable(requestIsActive: Bool) -> MemoryProbeResult {
|
|
631
656
|
// JS leaves `cachePolicy` unset for the common case and Fabric then hands
|
|
632
657
|
// Nitro a nil, which the request path already treats as memory-disk; the
|
|
633
658
|
// probe must agree, otherwise a nil policy skips the memory lookup entirely
|
|
@@ -637,7 +662,7 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
637
662
|
guard effectivePolicy == .memory || effectivePolicy == .memoryDisk,
|
|
638
663
|
let sourceUri,
|
|
639
664
|
let rawURL = URL(string: sourceUri) else {
|
|
640
|
-
return
|
|
665
|
+
return .miss
|
|
641
666
|
}
|
|
642
667
|
let rawScreenScale: CGFloat = hostView.window?.screen.scale ?? UIScreen.main.scale
|
|
643
668
|
let screenScale = min(max(rawScreenScale, 1), 3)
|
|
@@ -686,20 +711,87 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
686
711
|
}
|
|
687
712
|
return cache.imageFromMemoryCache(forKey: key)
|
|
688
713
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
714
|
+
|
|
715
|
+
var exactProbes: [(variant: OneKeyImageMemoryVariant?, url: URL, size: CGSize?)] = []
|
|
716
|
+
for url in requestURL == rawURL ? [requestURL] : [requestURL, rawURL] {
|
|
717
|
+
for size in thumbnailCandidates {
|
|
718
|
+
exactProbes.append(
|
|
719
|
+
(
|
|
720
|
+
variant: size.map {
|
|
721
|
+
OneKeyImageMemoryVariant(requestURL: url, thumbnailPixelSize: $0)
|
|
722
|
+
},
|
|
723
|
+
url: url,
|
|
724
|
+
size: size
|
|
725
|
+
)
|
|
726
|
+
)
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
let family = OneKeyImageMemoryFamilyKey(rawURL: rawURL, headersJson: sourceHeadersJson)
|
|
730
|
+
let exactVariants = Set(exactProbes.compactMap(\.variant))
|
|
731
|
+
for probe in exactProbes {
|
|
732
|
+
guard let image = memoryCachedImage(for: probe.url, thumbnailPixelSize: probe.size) else {
|
|
733
|
+
if let variant = probe.variant {
|
|
734
|
+
OneKeyImageMemoryVariantRegistry.shared.remove(variant, for: family)
|
|
693
735
|
}
|
|
736
|
+
continue
|
|
694
737
|
}
|
|
695
|
-
|
|
738
|
+
if let variant = probe.variant, Self.canUseAsMemoryPreview(image) {
|
|
739
|
+
OneKeyImageMemoryVariantRegistry.shared.record(variant, for: family)
|
|
740
|
+
}
|
|
741
|
+
displayMemoryCachedImage(
|
|
742
|
+
image,
|
|
743
|
+
requestIsActive: requestIsActive,
|
|
744
|
+
terminal: true,
|
|
745
|
+
scalesCenterPreview: false
|
|
746
|
+
)
|
|
747
|
+
return .exact
|
|
696
748
|
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
749
|
+
|
|
750
|
+
if let target = exactProbes.compactMap(\.variant).first {
|
|
751
|
+
let candidates = OneKeyImageMemoryVariantRegistry.shared.candidates(
|
|
752
|
+
for: family,
|
|
753
|
+
target: target,
|
|
754
|
+
excluding: exactVariants
|
|
755
|
+
)
|
|
756
|
+
for candidate in candidates {
|
|
757
|
+
guard
|
|
758
|
+
let image = memoryCachedImage(
|
|
759
|
+
for: candidate.requestURL,
|
|
760
|
+
thumbnailPixelSize: candidate.thumbnailPixelSize
|
|
761
|
+
),
|
|
762
|
+
Self.canUseAsMemoryPreview(image)
|
|
763
|
+
else {
|
|
764
|
+
OneKeyImageMemoryVariantRegistry.shared.remove(candidate, for: family)
|
|
765
|
+
continue
|
|
766
|
+
}
|
|
767
|
+
displayMemoryCachedImage(
|
|
768
|
+
image,
|
|
769
|
+
requestIsActive: false,
|
|
770
|
+
terminal: false,
|
|
771
|
+
scalesCenterPreview: Self.shouldScaleCenterMemoryPreview(
|
|
772
|
+
contentFit: contentFit ?? .cover,
|
|
773
|
+
candidate: candidate,
|
|
774
|
+
target: target
|
|
775
|
+
)
|
|
776
|
+
)
|
|
777
|
+
OneKeyImageMemoryVariantRegistry.shared.record(candidate, for: family)
|
|
778
|
+
return .preview
|
|
779
|
+
}
|
|
700
780
|
}
|
|
781
|
+
return .miss
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
private func displayMemoryCachedImage(
|
|
785
|
+
_ image: UIImage,
|
|
786
|
+
requestIsActive: Bool,
|
|
787
|
+
terminal: Bool,
|
|
788
|
+
scalesCenterPreview: Bool
|
|
789
|
+
) {
|
|
701
790
|
let generation = requestGeneration
|
|
702
|
-
if requestIsActive, !claimTerminal(generation) { return
|
|
791
|
+
if terminal, requestIsActive, !claimTerminal(generation) { return }
|
|
792
|
+
isShowingMemoryPreview = !terminal
|
|
793
|
+
self.scalesCenterMemoryPreview = !terminal && scalesCenterPreview
|
|
794
|
+
applyContentFit()
|
|
703
795
|
displayState = .image
|
|
704
796
|
isPreservingDisplayedImage = false
|
|
705
797
|
requestActive = false
|
|
@@ -708,11 +800,11 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
708
800
|
resetImageTransition()
|
|
709
801
|
applyDisplayedImage(image, generation: generation)
|
|
710
802
|
hostView.backgroundColor = .clear
|
|
711
|
-
if requestIsActive {
|
|
712
|
-
applyLoadedImageTransition(cacheType: .memory)
|
|
803
|
+
if terminal, requestIsActive {
|
|
804
|
+
applyLoadedImageTransition(cacheType: .memory, replacingMemoryPreview: false)
|
|
713
805
|
}
|
|
714
806
|
applyAutoplay()
|
|
715
|
-
if requestIsActive {
|
|
807
|
+
if terminal, requestIsActive {
|
|
716
808
|
let onLoad = onLoad
|
|
717
809
|
let onLoadEnd = onLoadEnd
|
|
718
810
|
Self.deliverTerminalCallbacks(
|
|
@@ -721,11 +813,32 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
721
813
|
},
|
|
722
814
|
onLoadEnd: { onLoadEnd?() }
|
|
723
815
|
)
|
|
724
|
-
guard requestGeneration == generation else { return
|
|
816
|
+
guard requestGeneration == generation else { return }
|
|
725
817
|
pendingDisplayGeneration = generation
|
|
726
818
|
schedulePendingDisplayIfNeeded()
|
|
727
819
|
}
|
|
728
|
-
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
private static func canUseAsMemoryPreview(_ image: UIImage) -> Bool {
|
|
823
|
+
!(image is SDAnimatedImage) && image.images == nil
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
static func shouldScaleCenterMemoryPreview(
|
|
827
|
+
contentFit: OneKeyImageContentFit,
|
|
828
|
+
candidate: OneKeyImageMemoryVariant,
|
|
829
|
+
target: OneKeyImageMemoryVariant
|
|
830
|
+
) -> Bool {
|
|
831
|
+
guard case .center = contentFit else { return false }
|
|
832
|
+
return candidate.pixelWidth != target.pixelWidth || candidate.pixelHeight != target.pixelHeight
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
@discardableResult
|
|
836
|
+
private func clearMemoryPreviewPresentation() -> Bool {
|
|
837
|
+
let wasShowingMemoryPreview = isShowingMemoryPreview
|
|
838
|
+
isShowingMemoryPreview = false
|
|
839
|
+
scalesCenterMemoryPreview = false
|
|
840
|
+
applyContentFit()
|
|
841
|
+
return wasShowingMemoryPreview
|
|
729
842
|
}
|
|
730
843
|
|
|
731
844
|
private func showError() {
|
|
@@ -740,6 +853,7 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
740
853
|
displayState = state
|
|
741
854
|
requestActive = false
|
|
742
855
|
isPreservingDisplayedImage = false
|
|
856
|
+
clearMemoryPreviewPresentation()
|
|
743
857
|
skeletonIndicator.stopAnimatingIndicator()
|
|
744
858
|
resetImageTransition()
|
|
745
859
|
hostView.image = nil
|
|
@@ -835,12 +949,28 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
835
949
|
return [base, highlight]
|
|
836
950
|
}
|
|
837
951
|
|
|
838
|
-
|
|
952
|
+
static func shouldAnimateLoadedImageTransition(
|
|
953
|
+
cacheType: OneKeyImageCacheType,
|
|
954
|
+
requestDuration: CFTimeInterval?,
|
|
955
|
+
reduceMotionEnabled: Bool,
|
|
956
|
+
replacingMemoryPreview: Bool
|
|
957
|
+
) -> Bool {
|
|
958
|
+
!replacingMemoryPreview && cacheType != .memory
|
|
959
|
+
&& (requestDuration ?? 0) >= fadeDelayThreshold && !reduceMotionEnabled
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
private func applyLoadedImageTransition(
|
|
963
|
+
cacheType: OneKeyImageCacheType,
|
|
964
|
+
replacingMemoryPreview: Bool
|
|
965
|
+
) {
|
|
839
966
|
resetImageTransition()
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
967
|
+
let requestDuration = requestStartedAt.map { CACurrentMediaTime() - $0 }
|
|
968
|
+
guard Self.shouldAnimateLoadedImageTransition(
|
|
969
|
+
cacheType: cacheType,
|
|
970
|
+
requestDuration: requestDuration,
|
|
971
|
+
reduceMotionEnabled: UIAccessibility.isReduceMotionEnabled,
|
|
972
|
+
replacingMemoryPreview: replacingMemoryPreview
|
|
973
|
+
) else {
|
|
844
974
|
return
|
|
845
975
|
}
|
|
846
976
|
hostView.alpha = 0
|
|
@@ -885,6 +1015,10 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
885
1015
|
}
|
|
886
1016
|
|
|
887
1017
|
private func applyContentFit() {
|
|
1018
|
+
if scalesCenterMemoryPreview, case .center = contentFit ?? .cover {
|
|
1019
|
+
hostView.contentMode = .scaleAspectFit
|
|
1020
|
+
return
|
|
1021
|
+
}
|
|
888
1022
|
switch contentFit ?? .cover {
|
|
889
1023
|
case .cover: hostView.contentMode = .scaleAspectFill
|
|
890
1024
|
case .contain: hostView.contentMode = .scaleAspectFit
|
|
@@ -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(
|