@onekeyfe/react-native-image 6.7.0-alpha.466 → 6.8.0-10

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.
Files changed (34) hide show
  1. package/README.md +144 -86
  2. package/android/build.gradle +1 -0
  3. package/android/consumer-rules.pro +9 -0
  4. package/android/src/main/AndroidManifest.xml +12 -1
  5. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyBlockieAvatarLoader.kt +1 -1
  6. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyIcon.kt +5 -1
  7. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +240 -6
  8. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +16 -0
  9. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideModule.kt +17 -0
  10. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideRegistry.kt +19 -0
  11. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageSafety.kt +41 -0
  12. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +1106 -0
  13. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupProvider.kt +54 -0
  14. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMainThreadDecode.kt +541 -0
  15. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMediaPreviewCache.kt +199 -0
  16. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyNamedSVG.kt +758 -61
  17. package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyBlockieAvatarLoaderTest.kt +2 -1
  18. package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +600 -0
  19. package/ios/OneKeyAvatarImageLoader.swift +1 -1
  20. package/ios/OneKeyIcon.swift +6 -0
  21. package/ios/OneKeyImage.swift +85 -2
  22. package/ios/OneKeyImageCache.swift +25 -5
  23. package/ios/OneKeyImageCoderBridge.m +42 -1
  24. package/ios/OneKeyImageRequestContext.swift +16 -2
  25. package/ios/OneKeyImageStartupPrewarm.swift +602 -0
  26. package/ios/OneKeyMainThreadDecode.swift +338 -0
  27. package/ios/OneKeyMediaPreviewCache.swift +209 -0
  28. package/ios/OneKeyNamedSVG.swift +437 -9
  29. package/ios/tests/OneKeyIconTests.swift +238 -0
  30. package/ios/tests/OneKeyImageInfrastructureTests.swift +611 -0
  31. package/ios/tests/OneKeyMediaPreviewCacheTests.swift +139 -0
  32. package/lib/module/index.js +5 -1
  33. package/package.json +1 -1
  34. package/src/index.tsx +23 -2
@@ -95,14 +95,22 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
95
95
  val context = NitroModules.applicationContext
96
96
  ?: throw IllegalStateException("React application context is unavailable")
97
97
  withContext(Dispatchers.Main) {
98
+ OneKeyMediaPreviewCache.get(context).clearMemory()
98
99
  Glide.get(context).clearMemory()
99
100
  OneKeyImageMemoryVariantRegistry.clear()
101
+ OneKeyImageStartupPrewarm.clearMemoryCache()
100
102
  }
101
103
  }
102
104
 
103
105
  override fun clearDisk(): Promise<Unit> = Promise.async {
104
106
  val context = NitroModules.applicationContext
105
107
  ?: throw IllegalStateException("React application context is unavailable")
108
+ OneKeyImageStartupPrewarm.clearDiskCache(context.cacheDir)
109
+ kotlinx.coroutines.suspendCancellableCoroutine<Unit> { continuation ->
110
+ android.os.Handler(android.os.Looper.getMainLooper()).post {
111
+ OneKeyMediaPreviewCache.get(context).clearDisk { continuation.resumeWith(Result.success(Unit)) }
112
+ }
113
+ }
106
114
  Glide.get(context).clearDiskCache()
107
115
  }
108
116
 
@@ -110,8 +118,16 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
110
118
  val context = NitroModules.applicationContext
111
119
  ?: throw IllegalStateException("React application context is unavailable")
112
120
  withContext(Dispatchers.Main) {
121
+ OneKeyMediaPreviewCache.get(context).clearMemory()
113
122
  Glide.get(context).clearMemory()
114
123
  OneKeyImageMemoryVariantRegistry.clear()
124
+ OneKeyImageStartupPrewarm.clearMemoryCache()
125
+ }
126
+ OneKeyImageStartupPrewarm.clearDiskCache(context.cacheDir)
127
+ kotlinx.coroutines.suspendCancellableCoroutine<Unit> { continuation ->
128
+ android.os.Handler(android.os.Looper.getMainLooper()).post {
129
+ OneKeyMediaPreviewCache.get(context).clearDisk { continuation.resumeWith(Result.success(Unit)) }
130
+ }
115
131
  }
116
132
  Glide.get(context).clearDiskCache()
117
133
  }
@@ -0,0 +1,17 @@
1
+ package com.margelo.nitro.onekeyimage
2
+
3
+ import android.content.Context
4
+ import com.bumptech.glide.Glide
5
+ import com.bumptech.glide.GlideBuilder
6
+ import com.bumptech.glide.Registry
7
+ import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
8
+ import com.bumptech.glide.module.GlideModule
9
+
10
+ @Suppress("DEPRECATION")
11
+ class OneKeyImageGlideModule : GlideModule {
12
+ override fun applyOptions(context: Context, builder: GlideBuilder) {
13
+ builder.setDiskCache(InternalCacheDiskCacheFactory(context, 320L * 1024 * 1024))
14
+ }
15
+
16
+ override fun registerComponents(context: Context, glide: Glide, registry: Registry) {}
17
+ }
@@ -44,6 +44,7 @@ internal object OneKeyImageGlideRegistry {
44
44
  private var registered = false
45
45
 
46
46
  fun ensureRegistered(context: Context) {
47
+ OneKeyImageStartupTrigger.startIfNeeded(context)
47
48
  if (registered) return
48
49
  synchronized(this) {
49
50
  if (registered) return
@@ -80,6 +81,11 @@ internal object OneKeyImageGlideRegistry {
80
81
  InputStream::class.java,
81
82
  OneKeyImageRemoteModelLoaderFactory(imageHttpClient),
82
83
  )
84
+ registry.append(
85
+ ByteBuffer::class.java,
86
+ SVG::class.java,
87
+ OneKeyByteBufferSvgDecoder(),
88
+ )
83
89
  registry.append(
84
90
  InputStream::class.java,
85
91
  SVG::class.java,
@@ -255,6 +261,19 @@ private class OneKeyDataUriFetcher(private val dataUri: String) : DataFetcher<By
255
261
  }
256
262
  }
257
263
 
264
+ private class OneKeyByteBufferSvgDecoder : ResourceDecoder<ByteBuffer, SVG> {
265
+ private val delegate = OneKeySvgDecoder()
266
+
267
+ override fun handles(source: ByteBuffer, options: Options): Boolean = true
268
+
269
+ override fun decode(source: ByteBuffer, width: Int, height: Int, options: Options): Resource<SVG> {
270
+ val copy = source.asReadOnlyBuffer()
271
+ copy.position(0)
272
+ OneKeyImageSafety.requireEncodedLength(copy.remaining().toLong(), OneKeyImageSafety.MAX_ENCODED_BYTES)
273
+ return delegate.decode(com.bumptech.glide.util.ByteBufferUtil.toStream(copy), width, height, options)
274
+ }
275
+ }
276
+
258
277
  private class OneKeySvgDecoder : ResourceDecoder<InputStream, SVG> {
259
278
  override fun handles(source: InputStream, options: Options): Boolean = true
260
279
 
@@ -129,6 +129,47 @@ internal object OneKeyImageSafety {
129
129
  return decodedBytes.coerceAtLeast(0L)
130
130
  }
131
131
 
132
+ /**
133
+ * Decodes Base64 payloads using `android.util.Base64` (available since API 8,
134
+ * compatible with minSdkVersion 24). Falls back to a pure-Kotlin MIME Base64
135
+ * decoder only when running in JVM unit tests where `android.util.Base64.decode`
136
+ * is stubbed to return `null`.
137
+ */
138
+ fun decodeDataUriBase64(payload: String): ByteArray {
139
+ try {
140
+ android.util.Base64.decode(payload, android.util.Base64.DEFAULT)?.let { return it }
141
+ } catch (error: RuntimeException) {
142
+ if (error is IllegalArgumentException) throw error
143
+ }
144
+ return decodeBase64Fallback(payload)
145
+ }
146
+
147
+ private fun decodeBase64Fallback(payload: String): ByteArray {
148
+ val out = ByteArrayOutputStream((payload.length * 3) / 4)
149
+ var buffer = 0
150
+ var bitsCollected = 0
151
+ for (i in payload.indices) {
152
+ val c = payload[i]
153
+ if (c == '=') break
154
+ val value = when (c) {
155
+ in 'A'..'Z' -> c.code - 'A'.code
156
+ in 'a'..'z' -> c.code - 'a'.code + 26
157
+ in '0'..'9' -> c.code - '0'.code + 52
158
+ '+', '-' -> 62
159
+ '/', '_' -> 63
160
+ ' ', '\t', '\r', '\n' -> continue
161
+ else -> throw IllegalArgumentException("Invalid Base64 character")
162
+ }
163
+ buffer = (buffer shl 6) or value
164
+ bitsCollected += 6
165
+ if (bitsCollected >= 8) {
166
+ bitsCollected -= 8
167
+ out.write((buffer ushr bitsCollected) and 0xff)
168
+ }
169
+ }
170
+ return out.toByteArray()
171
+ }
172
+
132
173
  fun isSafetyFailure(error: Throwable?): Boolean {
133
174
  if (error == null) return false
134
175
  val seen = Collections.newSetFromMap(IdentityHashMap<Throwable, Boolean>())