@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
package/README.md
CHANGED
|
@@ -5,6 +5,9 @@ native loading placeholders, opt-in shared-renderer skeleton, fallback states,
|
|
|
5
5
|
cache controls,
|
|
6
6
|
and conservative OneKey TOS URL resizing without depending on Expo Image.
|
|
7
7
|
|
|
8
|
+
The authoritative lifecycle, cache identity, cross-size memory-preview, safety,
|
|
9
|
+
and platform contract is defined in [docs/SPEC.md](docs/SPEC.md).
|
|
10
|
+
|
|
8
11
|
```tsx
|
|
9
12
|
<OneKeyImage
|
|
10
13
|
source={{ uri: 'https://example.com/avatar.png' }}
|
package/android/build.gradle
CHANGED
|
@@ -56,6 +56,7 @@ android {
|
|
|
56
56
|
}
|
|
57
57
|
buildFeatures { buildConfig true; prefab true }
|
|
58
58
|
buildTypes { release { minifyEnabled false } }
|
|
59
|
+
testOptions { unitTests.returnDefaultValues = true }
|
|
59
60
|
lintOptions { disable "GradleCompatible" }
|
|
60
61
|
compileOptions {
|
|
61
62
|
sourceCompatibility JavaVersion.VERSION_1_8
|
|
@@ -26,6 +26,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
|
|
|
26
26
|
import com.bumptech.glide.load.engine.GlideException
|
|
27
27
|
import com.bumptech.glide.request.RequestListener
|
|
28
28
|
import com.bumptech.glide.request.RequestOptions
|
|
29
|
+
import com.bumptech.glide.request.target.CustomTarget
|
|
29
30
|
import com.bumptech.glide.request.target.CustomViewTarget
|
|
30
31
|
import com.bumptech.glide.request.target.Target
|
|
31
32
|
import com.bumptech.glide.request.transition.Transition
|
|
@@ -46,6 +47,34 @@ private fun Context.findActivity(): Activity? {
|
|
|
46
47
|
return currentContext as? Activity
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
internal fun shouldScaleCenterMemoryPreview(
|
|
51
|
+
contentFit: OneKeyImageContentFit,
|
|
52
|
+
candidate: OneKeyImageMemoryVariant,
|
|
53
|
+
target: OneKeyImageMemoryVariant,
|
|
54
|
+
): Boolean =
|
|
55
|
+
contentFit == OneKeyImageContentFit.CENTER &&
|
|
56
|
+
(candidate.width != target.width || candidate.height != target.height)
|
|
57
|
+
|
|
58
|
+
internal fun shouldProbeMemoryPreview(
|
|
59
|
+
hasTarget: Boolean,
|
|
60
|
+
sameFamily: Boolean,
|
|
61
|
+
hasDrawable: Boolean,
|
|
62
|
+
): Boolean = !hasTarget || !sameFamily || !hasDrawable
|
|
63
|
+
|
|
64
|
+
internal fun shouldAnimateLoadedImageTransition(
|
|
65
|
+
cacheType: OneKeyImageCacheType,
|
|
66
|
+
fadeDelayElapsed: Boolean,
|
|
67
|
+
animationsEnabled: Boolean,
|
|
68
|
+
replacingMemoryPreview: Boolean,
|
|
69
|
+
): Boolean =
|
|
70
|
+
!replacingMemoryPreview && cacheType != OneKeyImageCacheType.MEMORY &&
|
|
71
|
+
fadeDelayElapsed && animationsEnabled
|
|
72
|
+
|
|
73
|
+
internal fun memoryProbeRequestOptions(base: RequestOptions): RequestOptions =
|
|
74
|
+
base
|
|
75
|
+
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
76
|
+
.onlyRetrieveFromCache(true)
|
|
77
|
+
|
|
49
78
|
private class OneKeyImageHostView(context: ThemedReactContext) : ImageView(context) {
|
|
50
79
|
private val roundClipPath = Path()
|
|
51
80
|
private val roundOutlineProvider = object : ViewOutlineProvider() {
|
|
@@ -215,12 +244,16 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
215
244
|
}
|
|
216
245
|
private var loadRunnable: Runnable? = null
|
|
217
246
|
private var currentTarget: CustomViewTarget<OneKeyImageHostView, Drawable>? = null
|
|
247
|
+
private var memoryPreviewTarget: CustomTarget<Drawable>? = null
|
|
248
|
+
private var memoryPreviewFamily: OneKeyImageMemoryFamilyKey? = null
|
|
249
|
+
private var scalesCenterMemoryPreview = false
|
|
218
250
|
private var generation = 0L
|
|
219
251
|
private var lastSignature: String? = null
|
|
220
252
|
private var disposed = false
|
|
221
253
|
private var suppressPropEffects = false
|
|
222
254
|
private var requestActive = false
|
|
223
255
|
private var displayState = DisplayState.LOADING
|
|
256
|
+
private var isPreservingDisplayedImage = false
|
|
224
257
|
private var displayRunnable: Runnable? = null
|
|
225
258
|
private var pendingDisplayGeneration: Long? = null
|
|
226
259
|
private var fallbackRunnable: Runnable? = null
|
|
@@ -306,8 +339,8 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
306
339
|
field = value
|
|
307
340
|
requestIdentityChanged()
|
|
308
341
|
}
|
|
309
|
-
// Layout hint pair with resizeWidth;
|
|
310
|
-
//
|
|
342
|
+
// Layout hint pair with resizeWidth; the pre-layout memory probe uses both
|
|
343
|
+
// values to reproduce a preload's physical decode size.
|
|
311
344
|
override var resizeHeight: Double? = null
|
|
312
345
|
set(value) {
|
|
313
346
|
if (field == value) return
|
|
@@ -395,7 +428,11 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
395
428
|
if (suppressPropEffects || disposed) return
|
|
396
429
|
lastSignature = null
|
|
397
430
|
cancelCurrent(invalidateGeneration = true)
|
|
398
|
-
if (sourceUri.isNullOrBlank())
|
|
431
|
+
if (sourceUri.isNullOrBlank()) {
|
|
432
|
+
showFallback()
|
|
433
|
+
} else if (!showMemoryCachedPreviewBeforeLayout()) {
|
|
434
|
+
showLoading(requestIsActive = false)
|
|
435
|
+
}
|
|
399
436
|
scheduleLoad()
|
|
400
437
|
}
|
|
401
438
|
|
|
@@ -425,6 +462,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
425
462
|
suppressPropEffects = false
|
|
426
463
|
requestActive = false
|
|
427
464
|
displayState = DisplayState.LOADING
|
|
465
|
+
isPreservingDisplayedImage = false
|
|
428
466
|
hostView.setImageDrawable(null)
|
|
429
467
|
hostView.drawStateSymbol = false
|
|
430
468
|
hostView.skeletonRequested = false
|
|
@@ -460,12 +498,147 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
460
498
|
}
|
|
461
499
|
}
|
|
462
500
|
|
|
501
|
+
private fun showMemoryCachedPreviewBeforeLayout(): Boolean {
|
|
502
|
+
if (Looper.myLooper() != Looper.getMainLooper()) return false
|
|
503
|
+
val rawUrl = sourceUri?.takeIf { it.isNotBlank() } ?: return false
|
|
504
|
+
val policy = cachePolicy ?: OneKeyImageCachePolicy.MEMORY_DISK
|
|
505
|
+
if (
|
|
506
|
+
policy != OneKeyImageCachePolicy.MEMORY &&
|
|
507
|
+
policy != OneKeyImageCachePolicy.MEMORY_DISK
|
|
508
|
+
) {
|
|
509
|
+
return false
|
|
510
|
+
}
|
|
511
|
+
val displayWidth = resizeWidth?.takeIf { it.isFinite() && it > 0.0 }
|
|
512
|
+
val displayHeight = resizeHeight?.takeIf { it.isFinite() && it > 0.0 }
|
|
513
|
+
if (displayWidth == null && displayHeight == null) return false
|
|
514
|
+
val density = hostView.resources.displayMetrics.density.coerceAtLeast(1f)
|
|
515
|
+
val displaySize = when {
|
|
516
|
+
displayWidth != null && displayHeight != null -> maxOf(displayWidth, displayHeight)
|
|
517
|
+
displayWidth != null -> displayWidth
|
|
518
|
+
else -> requireNotNull(displayHeight)
|
|
519
|
+
}
|
|
520
|
+
val customIdentity = OneKeyImageModel.headers(sourceHeadersJson).isNotEmpty()
|
|
521
|
+
val requestUrl = if (optimizeTos != false) {
|
|
522
|
+
OneKeyTosUrl.optimized(
|
|
523
|
+
rawUrl,
|
|
524
|
+
ceil(displaySize).toInt(),
|
|
525
|
+
density,
|
|
526
|
+
overscan ?: 1.1,
|
|
527
|
+
customIdentity,
|
|
528
|
+
)
|
|
529
|
+
} else rawUrl
|
|
530
|
+
val dimensions = OneKeyImageDecodeDimensions.forPreload(
|
|
531
|
+
displayWidth,
|
|
532
|
+
displayHeight,
|
|
533
|
+
density.toDouble(),
|
|
534
|
+
)
|
|
535
|
+
return showMemoryCachedPreview(
|
|
536
|
+
family = OneKeyImageMemoryFamilyKey.from(rawUrl, sourceHeadersJson),
|
|
537
|
+
exactVariant = OneKeyImageMemoryVariant(
|
|
538
|
+
requestUrl = requestUrl,
|
|
539
|
+
width = dimensions.width,
|
|
540
|
+
height = dimensions.height,
|
|
541
|
+
round = round == true,
|
|
542
|
+
),
|
|
543
|
+
includeExact = true,
|
|
544
|
+
policy = policy,
|
|
545
|
+
requestGeneration = generation,
|
|
546
|
+
)
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
private fun showMemoryCachedPreview(
|
|
550
|
+
family: OneKeyImageMemoryFamilyKey,
|
|
551
|
+
exactVariant: OneKeyImageMemoryVariant,
|
|
552
|
+
includeExact: Boolean,
|
|
553
|
+
policy: OneKeyImageCachePolicy,
|
|
554
|
+
requestGeneration: Long,
|
|
555
|
+
): Boolean {
|
|
556
|
+
if (
|
|
557
|
+
Looper.myLooper() != Looper.getMainLooper() ||
|
|
558
|
+
disposed ||
|
|
559
|
+
requestGeneration != generation ||
|
|
560
|
+
(policy != OneKeyImageCachePolicy.MEMORY &&
|
|
561
|
+
policy != OneKeyImageCachePolicy.MEMORY_DISK)
|
|
562
|
+
) {
|
|
563
|
+
return false
|
|
564
|
+
}
|
|
565
|
+
val candidates = buildList {
|
|
566
|
+
if (includeExact) {
|
|
567
|
+
add(exactVariant)
|
|
568
|
+
if (exactVariant.requestUrl != family.rawUrl) {
|
|
569
|
+
add(exactVariant.copy(requestUrl = family.rawUrl))
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
addAll(OneKeyImageMemoryVariantRegistry.candidates(family, exactVariant))
|
|
573
|
+
}.distinct()
|
|
574
|
+
|
|
575
|
+
for (candidate in candidates) {
|
|
576
|
+
var synchronousResource: Drawable? = null
|
|
577
|
+
val target = object : CustomTarget<Drawable>(candidate.width, candidate.height) {
|
|
578
|
+
override fun onResourceReady(
|
|
579
|
+
resource: Drawable,
|
|
580
|
+
transition: Transition<in Drawable>?,
|
|
581
|
+
) {
|
|
582
|
+
if (
|
|
583
|
+
requestGeneration == generation &&
|
|
584
|
+
!disposed &&
|
|
585
|
+
resource !is Animatable
|
|
586
|
+
) {
|
|
587
|
+
synchronousResource = resource
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
override fun onLoadCleared(placeholder: Drawable?) = Unit
|
|
592
|
+
}
|
|
593
|
+
requestManager()
|
|
594
|
+
.asDrawable()
|
|
595
|
+
.load(OneKeyImageModel.build(candidate.requestUrl, sourceHeadersJson))
|
|
596
|
+
.apply(
|
|
597
|
+
memoryProbeRequestOptions(
|
|
598
|
+
requestOptions(policy, candidate.requestUrl, candidate.round),
|
|
599
|
+
),
|
|
600
|
+
)
|
|
601
|
+
.override(candidate.width, candidate.height)
|
|
602
|
+
.into(target)
|
|
603
|
+
|
|
604
|
+
val resource = synchronousResource
|
|
605
|
+
if (resource == null) {
|
|
606
|
+
requestManager().clear(target)
|
|
607
|
+
OneKeyImageMemoryVariantRegistry.remove(family, candidate)
|
|
608
|
+
continue
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
clearMemoryPreviewTarget()
|
|
612
|
+
memoryPreviewTarget = target
|
|
613
|
+
memoryPreviewFamily = family
|
|
614
|
+
scalesCenterMemoryPreview = shouldScaleCenterMemoryPreview(
|
|
615
|
+
contentFit = contentFit ?: OneKeyImageContentFit.COVER,
|
|
616
|
+
candidate = candidate,
|
|
617
|
+
target = exactVariant,
|
|
618
|
+
)
|
|
619
|
+
applyContentFit()
|
|
620
|
+
displayState = DisplayState.IMAGE
|
|
621
|
+
requestActive = false
|
|
622
|
+
isPreservingDisplayedImage = false
|
|
623
|
+
hostView.skeletonRequested = false
|
|
624
|
+
hostView.drawStateSymbol = false
|
|
625
|
+
hostView.setBackgroundColor(Color.TRANSPARENT)
|
|
626
|
+
hostView.setImageDrawable(resource)
|
|
627
|
+
applyAutoplay()
|
|
628
|
+
OneKeyImageMemoryVariantRegistry.record(family, candidate)
|
|
629
|
+
return true
|
|
630
|
+
}
|
|
631
|
+
return false
|
|
632
|
+
}
|
|
633
|
+
|
|
463
634
|
private fun startLoad(force: Boolean) {
|
|
464
635
|
if (disposed) return
|
|
465
636
|
if (hostView.width <= 0 || hostView.height <= 0) {
|
|
466
|
-
cancelCurrent(invalidateGeneration = true)
|
|
637
|
+
cancelCurrent(invalidateGeneration = true, clearMemoryPreview = false)
|
|
467
638
|
lastSignature = null
|
|
468
|
-
|
|
639
|
+
if (memoryPreviewTarget == null || hostView.drawable == null) {
|
|
640
|
+
showLoading(requestIsActive = false)
|
|
641
|
+
}
|
|
469
642
|
return
|
|
470
643
|
}
|
|
471
644
|
val rawUrl = sourceUri?.takeIf { it.isNotBlank() }
|
|
@@ -493,7 +666,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
493
666
|
if (!force && signature == lastSignature) return
|
|
494
667
|
lastSignature = signature
|
|
495
668
|
|
|
496
|
-
cancelCurrent(invalidateGeneration = true)
|
|
669
|
+
cancelCurrent(invalidateGeneration = true, clearMemoryPreview = false)
|
|
497
670
|
val requestGeneration = generation
|
|
498
671
|
requestStartedAtMs = SystemClock.uptimeMillis()
|
|
499
672
|
onLoadStart?.invoke()
|
|
@@ -501,8 +674,14 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
501
674
|
val customIdentity = OneKeyImageModel.headers(sourceHeadersJson).isNotEmpty()
|
|
502
675
|
val optimizedUrl = if (optimizeTos != false) {
|
|
503
676
|
val density = hostView.resources.displayMetrics.density.coerceAtLeast(1f)
|
|
504
|
-
val
|
|
505
|
-
|
|
677
|
+
val displayWidth = resizeWidth?.takeIf { it.isFinite() && it > 0.0 }
|
|
678
|
+
val displayHeight = resizeHeight?.takeIf { it.isFinite() && it > 0.0 }
|
|
679
|
+
val displaySize = when {
|
|
680
|
+
displayWidth != null && displayHeight != null -> maxOf(displayWidth, displayHeight)
|
|
681
|
+
displayWidth != null -> displayWidth
|
|
682
|
+
displayHeight != null -> displayHeight
|
|
683
|
+
else -> maxOf(hostView.width, hostView.height) / density.toDouble()
|
|
684
|
+
}
|
|
506
685
|
OneKeyTosUrl.optimized(
|
|
507
686
|
rawUrl,
|
|
508
687
|
ceil(displaySize).toInt(),
|
|
@@ -512,6 +691,28 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
512
691
|
)
|
|
513
692
|
} else rawUrl
|
|
514
693
|
val decodeDimensions = OneKeyImageDecodeDimensions.forRender(hostView.width, hostView.height)
|
|
694
|
+
val memoryFamily = OneKeyImageMemoryFamilyKey.from(rawUrl, sourceHeadersJson)
|
|
695
|
+
val exactVariant = OneKeyImageMemoryVariant(
|
|
696
|
+
requestUrl = optimizedUrl,
|
|
697
|
+
width = decodeDimensions.width,
|
|
698
|
+
height = decodeDimensions.height,
|
|
699
|
+
round = round == true,
|
|
700
|
+
)
|
|
701
|
+
if (
|
|
702
|
+
shouldProbeMemoryPreview(
|
|
703
|
+
hasTarget = memoryPreviewTarget != null,
|
|
704
|
+
sameFamily = memoryPreviewFamily == memoryFamily,
|
|
705
|
+
hasDrawable = hostView.drawable != null,
|
|
706
|
+
)
|
|
707
|
+
) {
|
|
708
|
+
showMemoryCachedPreview(
|
|
709
|
+
family = memoryFamily,
|
|
710
|
+
exactVariant = exactVariant,
|
|
711
|
+
includeExact = false,
|
|
712
|
+
policy = cachePolicy ?: OneKeyImageCachePolicy.MEMORY_DISK,
|
|
713
|
+
requestGeneration = requestGeneration,
|
|
714
|
+
)
|
|
715
|
+
}
|
|
515
716
|
performRequest(
|
|
516
717
|
requestUrl = optimizedUrl,
|
|
517
718
|
rawUrl = rawUrl,
|
|
@@ -520,6 +721,8 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
520
721
|
headersJson = sourceHeadersJson,
|
|
521
722
|
policy = cachePolicy ?: OneKeyImageCachePolicy.MEMORY_DISK,
|
|
522
723
|
decodeDimensions = decodeDimensions,
|
|
724
|
+
memoryFamily = memoryFamily,
|
|
725
|
+
memoryVariant = exactVariant,
|
|
523
726
|
)
|
|
524
727
|
}
|
|
525
728
|
|
|
@@ -531,6 +734,8 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
531
734
|
headersJson: String?,
|
|
532
735
|
policy: OneKeyImageCachePolicy,
|
|
533
736
|
decodeDimensions: OneKeyImageDecodeDimensions,
|
|
737
|
+
memoryFamily: OneKeyImageMemoryFamilyKey,
|
|
738
|
+
memoryVariant: OneKeyImageMemoryVariant,
|
|
534
739
|
) {
|
|
535
740
|
if (requestGeneration != generation || disposed) return
|
|
536
741
|
clearCurrentTarget()
|
|
@@ -540,18 +745,34 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
540
745
|
val target = object : CustomViewTarget<OneKeyImageHostView, Drawable>(hostView) {
|
|
541
746
|
override fun onResourceLoading(placeholder: Drawable?) {
|
|
542
747
|
if (requestGeneration != generation || disposed) return
|
|
543
|
-
showLoading(
|
|
748
|
+
showLoading(
|
|
749
|
+
requestIsActive = true,
|
|
750
|
+
preserveDisplayedImage = memoryPreviewTarget != null && hostView.drawable != null,
|
|
751
|
+
)
|
|
544
752
|
}
|
|
545
753
|
|
|
546
754
|
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
|
|
547
755
|
if (requestGeneration != generation || disposed) return
|
|
756
|
+
val replacesMemoryPreview = memoryPreviewTarget != null && hostView.drawable != null
|
|
548
757
|
requestActive = false
|
|
549
758
|
displayState = DisplayState.IMAGE
|
|
550
759
|
hostView.skeletonRequested = false
|
|
551
760
|
hostView.drawStateSymbol = false
|
|
552
761
|
hostView.setBackgroundColor(Color.TRANSPARENT)
|
|
553
762
|
hostView.setImageDrawable(resource)
|
|
554
|
-
|
|
763
|
+
isPreservingDisplayedImage = false
|
|
764
|
+
if (
|
|
765
|
+
resource !is Animatable &&
|
|
766
|
+
(policy == OneKeyImageCachePolicy.MEMORY ||
|
|
767
|
+
policy == OneKeyImageCachePolicy.MEMORY_DISK)
|
|
768
|
+
) {
|
|
769
|
+
OneKeyImageMemoryVariantRegistry.record(memoryFamily, memoryVariant)
|
|
770
|
+
}
|
|
771
|
+
clearMemoryPreviewTarget()
|
|
772
|
+
applyLoadedImageTransition(
|
|
773
|
+
cacheType = resolvedCacheType,
|
|
774
|
+
replacingMemoryPreview = replacesMemoryPreview,
|
|
775
|
+
)
|
|
555
776
|
applyAutoplay()
|
|
556
777
|
val loadCallback = onLoad
|
|
557
778
|
val loadEndCallback = onLoadEnd
|
|
@@ -603,6 +824,8 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
603
824
|
headersJson = headersJson,
|
|
604
825
|
policy = policy,
|
|
605
826
|
decodeDimensions = decodeDimensions,
|
|
827
|
+
memoryFamily = memoryFamily,
|
|
828
|
+
memoryVariant = memoryVariant.copy(requestUrl = rawUrl),
|
|
606
829
|
)
|
|
607
830
|
}
|
|
608
831
|
}
|
|
@@ -618,7 +841,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
618
841
|
requestManager()
|
|
619
842
|
.asDrawable()
|
|
620
843
|
.load(OneKeyImageModel.build(requestUrl, headersJson))
|
|
621
|
-
.apply(requestOptions(policy, requestUrl, round
|
|
844
|
+
.apply(requestOptions(policy, requestUrl, memoryVariant.round))
|
|
622
845
|
.override(decodeDimensions.width, decodeDimensions.height)
|
|
623
846
|
.listener(object : RequestListener<Drawable> {
|
|
624
847
|
override fun onLoadFailed(
|
|
@@ -678,7 +901,10 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
678
901
|
)
|
|
679
902
|
}
|
|
680
903
|
|
|
681
|
-
private fun cancelCurrent(
|
|
904
|
+
private fun cancelCurrent(
|
|
905
|
+
invalidateGeneration: Boolean,
|
|
906
|
+
clearMemoryPreview: Boolean = true,
|
|
907
|
+
) {
|
|
682
908
|
loadRunnable?.let(hostView::removeCallbacks)
|
|
683
909
|
loadRunnable = null
|
|
684
910
|
displayRunnable?.let(hostView::removeCallbacks)
|
|
@@ -687,7 +913,9 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
687
913
|
fallbackRunnable?.let(hostView::removeCallbacks)
|
|
688
914
|
fallbackRunnable = null
|
|
689
915
|
clearCurrentTarget()
|
|
916
|
+
if (clearMemoryPreview) clearMemoryPreviewTarget()
|
|
690
917
|
requestActive = false
|
|
918
|
+
isPreservingDisplayedImage = false
|
|
691
919
|
hostView.skeletonRequested = false
|
|
692
920
|
requestStartedAtMs = null
|
|
693
921
|
resetImageTransition()
|
|
@@ -702,6 +930,15 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
702
930
|
requestManager().clear(target)
|
|
703
931
|
}
|
|
704
932
|
|
|
933
|
+
private fun clearMemoryPreviewTarget() {
|
|
934
|
+
val target = memoryPreviewTarget
|
|
935
|
+
memoryPreviewTarget = null
|
|
936
|
+
memoryPreviewFamily = null
|
|
937
|
+
scalesCenterMemoryPreview = false
|
|
938
|
+
applyContentFit()
|
|
939
|
+
if (target != null) requestManager().clear(target)
|
|
940
|
+
}
|
|
941
|
+
|
|
705
942
|
private fun scheduleOnDisplay(requestGeneration: Long) {
|
|
706
943
|
pendingDisplayGeneration = requestGeneration
|
|
707
944
|
schedulePendingDisplayIfNeeded()
|
|
@@ -730,10 +967,22 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
730
967
|
hostView.postOnAnimation(runnable)
|
|
731
968
|
}
|
|
732
969
|
|
|
733
|
-
private fun showLoading(
|
|
970
|
+
private fun showLoading(
|
|
971
|
+
requestIsActive: Boolean,
|
|
972
|
+
preserveDisplayedImage: Boolean = false,
|
|
973
|
+
) {
|
|
734
974
|
displayState = DisplayState.LOADING
|
|
735
975
|
requestActive = requestIsActive
|
|
736
976
|
resetImageTransition()
|
|
977
|
+
if (preserveDisplayedImage && hostView.drawable != null) {
|
|
978
|
+
isPreservingDisplayedImage = true
|
|
979
|
+
hostView.skeletonRequested = false
|
|
980
|
+
hostView.drawStateSymbol = false
|
|
981
|
+
hostView.setBackgroundColor(Color.TRANSPARENT)
|
|
982
|
+
hostView.invalidate()
|
|
983
|
+
return
|
|
984
|
+
}
|
|
985
|
+
isPreservingDisplayedImage = false
|
|
737
986
|
hostView.setImageDrawable(null)
|
|
738
987
|
hostView.drawStateSymbol = false
|
|
739
988
|
applyLoadingAppearance()
|
|
@@ -750,7 +999,9 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
750
999
|
private fun showTerminalState(state: DisplayState) {
|
|
751
1000
|
displayState = state
|
|
752
1001
|
requestActive = false
|
|
1002
|
+
isPreservingDisplayedImage = false
|
|
753
1003
|
resetImageTransition()
|
|
1004
|
+
clearMemoryPreviewTarget()
|
|
754
1005
|
hostView.setImageDrawable(null)
|
|
755
1006
|
hostView.skeletonRequested = false
|
|
756
1007
|
val hidesTerminalState = loadingStrategy == OneKeyImageLoadingStrategy.NONE
|
|
@@ -819,7 +1070,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
819
1070
|
private fun applyVariant() {
|
|
820
1071
|
when (displayState) {
|
|
821
1072
|
DisplayState.IMAGE -> Unit
|
|
822
|
-
DisplayState.LOADING -> applyLoadingAppearance()
|
|
1073
|
+
DisplayState.LOADING -> if (!isPreservingDisplayedImage) applyLoadingAppearance()
|
|
823
1074
|
DisplayState.ERROR, DisplayState.FALLBACK -> {
|
|
824
1075
|
val hidesTerminalState = loadingStrategy == OneKeyImageLoadingStrategy.NONE
|
|
825
1076
|
hostView.setBackgroundColor(
|
|
@@ -851,16 +1102,19 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
851
1102
|
OneKeyImageVariant.AVATAR -> "●"
|
|
852
1103
|
}
|
|
853
1104
|
|
|
854
|
-
private fun applyLoadedImageTransition(
|
|
1105
|
+
private fun applyLoadedImageTransition(
|
|
1106
|
+
cacheType: OneKeyImageCacheType,
|
|
1107
|
+
replacingMemoryPreview: Boolean,
|
|
1108
|
+
) {
|
|
855
1109
|
resetImageTransition()
|
|
856
1110
|
val startedAt = requestStartedAtMs ?: return
|
|
857
|
-
|
|
858
|
-
cacheType
|
|
859
|
-
SystemClock.uptimeMillis() - startedAt
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
1111
|
+
val shouldAnimate = shouldAnimateLoadedImageTransition(
|
|
1112
|
+
cacheType = cacheType,
|
|
1113
|
+
fadeDelayElapsed = SystemClock.uptimeMillis() - startedAt >= FADE_DELAY_THRESHOLD_MS,
|
|
1114
|
+
animationsEnabled = animationsEnabled(),
|
|
1115
|
+
replacingMemoryPreview = replacingMemoryPreview,
|
|
1116
|
+
)
|
|
1117
|
+
if (!shouldAnimate) return
|
|
864
1118
|
hostView.alpha = 0f
|
|
865
1119
|
hostView.animate()
|
|
866
1120
|
.alpha(1f)
|
|
@@ -914,6 +1168,13 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
914
1168
|
}
|
|
915
1169
|
|
|
916
1170
|
private fun applyContentFit() {
|
|
1171
|
+
if (
|
|
1172
|
+
scalesCenterMemoryPreview &&
|
|
1173
|
+
(contentFit ?: OneKeyImageContentFit.COVER) == OneKeyImageContentFit.CENTER
|
|
1174
|
+
) {
|
|
1175
|
+
hostView.scaleType = ImageView.ScaleType.FIT_CENTER
|
|
1176
|
+
return
|
|
1177
|
+
}
|
|
917
1178
|
hostView.scaleType = when (contentFit ?: OneKeyImageContentFit.COVER) {
|
|
918
1179
|
OneKeyImageContentFit.COVER -> ImageView.ScaleType.CENTER_CROP
|
|
919
1180
|
OneKeyImageContentFit.CONTAIN -> ImageView.ScaleType.FIT_CENTER
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
package com.margelo.nitro.onekeyimage
|
|
2
2
|
|
|
3
|
+
import android.graphics.drawable.Animatable
|
|
4
|
+
import android.graphics.drawable.Drawable
|
|
3
5
|
import com.bumptech.glide.Glide
|
|
4
6
|
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
|
5
7
|
import com.bumptech.glide.request.RequestOptions
|
|
@@ -65,7 +67,8 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
65
67
|
} else source.uri
|
|
66
68
|
|
|
67
69
|
val succeeded = try {
|
|
68
|
-
load(context, requestUrl, source, options, decodeDimensions)
|
|
70
|
+
val resource = load(context, requestUrl, source, options, decodeDimensions)
|
|
71
|
+
recordMemoryVariant(source, requestUrl, decodeDimensions, resource)
|
|
69
72
|
true
|
|
70
73
|
} catch (optimizedError: Exception) {
|
|
71
74
|
if (
|
|
@@ -75,7 +78,8 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
75
78
|
false
|
|
76
79
|
} else {
|
|
77
80
|
try {
|
|
78
|
-
load(context, source.uri, source, options, decodeDimensions)
|
|
81
|
+
val resource = load(context, source.uri, source, options, decodeDimensions)
|
|
82
|
+
recordMemoryVariant(source, source.uri, decodeDimensions, resource)
|
|
79
83
|
true
|
|
80
84
|
} catch (_: Exception) {
|
|
81
85
|
false
|
|
@@ -92,6 +96,7 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
92
96
|
?: throw IllegalStateException("React application context is unavailable")
|
|
93
97
|
withContext(Dispatchers.Main) {
|
|
94
98
|
Glide.get(context).clearMemory()
|
|
99
|
+
OneKeyImageMemoryVariantRegistry.clear()
|
|
95
100
|
}
|
|
96
101
|
}
|
|
97
102
|
|
|
@@ -106,6 +111,7 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
106
111
|
?: throw IllegalStateException("React application context is unavailable")
|
|
107
112
|
withContext(Dispatchers.Main) {
|
|
108
113
|
Glide.get(context).clearMemory()
|
|
114
|
+
OneKeyImageMemoryVariantRegistry.clear()
|
|
109
115
|
}
|
|
110
116
|
Glide.get(context).clearDiskCache()
|
|
111
117
|
}
|
|
@@ -116,7 +122,7 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
116
122
|
source: OneKeyImagePreloadSource,
|
|
117
123
|
options: RequestOptions,
|
|
118
124
|
decodeDimensions: OneKeyImageDecodeDimensions,
|
|
119
|
-
) {
|
|
125
|
+
): Drawable {
|
|
120
126
|
val request = Glide.with(context)
|
|
121
127
|
.asDrawable()
|
|
122
128
|
.load(OneKeyImageModel.build(url, source.headersJson))
|
|
@@ -124,9 +130,33 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
124
130
|
.override(decodeDimensions.width, decodeDimensions.height)
|
|
125
131
|
val future = request.submit()
|
|
126
132
|
try {
|
|
127
|
-
future.get()
|
|
133
|
+
return future.get()
|
|
128
134
|
} finally {
|
|
129
135
|
Glide.with(context).clear(future)
|
|
130
136
|
}
|
|
131
137
|
}
|
|
138
|
+
|
|
139
|
+
private fun recordMemoryVariant(
|
|
140
|
+
source: OneKeyImagePreloadSource,
|
|
141
|
+
requestUrl: String,
|
|
142
|
+
decodeDimensions: OneKeyImageDecodeDimensions,
|
|
143
|
+
resource: Drawable,
|
|
144
|
+
) {
|
|
145
|
+
val policy = source.cachePolicy ?: OneKeyImageCachePolicy.MEMORY_DISK
|
|
146
|
+
if (
|
|
147
|
+
resource is Animatable ||
|
|
148
|
+
(policy != OneKeyImageCachePolicy.MEMORY && policy != OneKeyImageCachePolicy.MEMORY_DISK)
|
|
149
|
+
) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
OneKeyImageMemoryVariantRegistry.record(
|
|
153
|
+
family = OneKeyImageMemoryFamilyKey.from(source.uri, source.headersJson),
|
|
154
|
+
variant = OneKeyImageMemoryVariant(
|
|
155
|
+
requestUrl = requestUrl,
|
|
156
|
+
width = decodeDimensions.width,
|
|
157
|
+
height = decodeDimensions.height,
|
|
158
|
+
round = false,
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
}
|
|
132
162
|
}
|