Skip to content

Commit 16161b7

Browse files
author
farfromrefug
committed
chore: improvement for local files handling
1 parent 359fe41 commit 16161b7

File tree

7 files changed

+46
-37
lines changed

7 files changed

+46
-37
lines changed

demo-snippets/vue/Pipeline.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { getImagePipeline } from '@nativescript-community/ui-image';
3232
export default {
3333
data() {
3434
return {
35-
url: 'https://images.pexels.com/photos/842711/pexels-photo-842711.jpeg',
35+
url: '~/assets/images/dessert.jpg',
3636
logs: [] as string[],
3737
result: ''
3838
};

packages/image/platforms/android/java/com/nativescript/image/EvictionManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public synchronized void saveKeys(final String id, final CacheKeyStore.StoredKey
100100

101101
// 2) Just save the new keys directly
102102
CacheKeyStore.StoredKeys toPersist = newStored;
103-
103+
Log.d("JS", "EvictionManager saveKeys " + id + " " + toPersist.sourceKey+ " " + toPersist.sourceKey.getClass().getName()+ " " + toPersist.signature);
104104
// 3) Save to both stores
105105
inMemoryKeyStore.put(id, toPersist);
106106
if (persistentStore != null) {
@@ -581,6 +581,8 @@ public void isInDiskCacheAsync(final String id, final DiskPresenceCallback callb
581581
try {
582582
if (hasValidStoredKeys(s)) {
583583
CustomDataCacheKey cachekey = new CustomDataCacheKey(s.sourceKey, s.signature);
584+
Log.d("JS", "EvictionManager isInDiskCacheAsync " + id + " " + s.sourceKey+ " " + s.sourceKey.getClass().getName()+ " " + s.signature);
585+
584586
sourcePresent = dc.get(cachekey) != null;
585587
byte[] transformationBytes = getTransformationBytesFromStoredKeys(s);
586588
Key resourceKey = new RecreatedResourceKey(s.sourceKey, s.signature, s.width, s.height, transformationBytes,

packages/image/platforms/android/java/com/nativescript/image/ModelSignatureMemoryCache.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,36 @@ public final class ModelSignatureMemoryCache extends LruResourceCache {
3535

3636
public ModelSignatureMemoryCache(long maxSizeBytes) {
3737
super(maxSizeBytes);
38+
Log.i("JS", "ModelSignatureMemoryCache " + maxSizeBytes);
3839
}
3940
@Override
4041
public void trimMemory(int level) {
42+
Log.i("JS", "trimMemory " + level + " " + getMaxSize());
4143
super.trimMemory(level);
4244
}
4345

4446
@Nullable
4547
@Override
4648
public Resource<?> put(@NonNull Key key, @Nullable Resource<?> resource) {
4749
// Save string representation for indexing before delegating.
50+
Log.i("JS", "MemoryCache put " + key.toString() + getSize(resource));
4851
keyStrings.put(key, key.toString());
52+
Log.i("JS", "MemoryCache put " + key.toString() + " " + keyStrings.size());
4953
return super.put(key, resource);
5054
}
5155

5256
@Nullable
5357
@Override
5458
public Resource<?> remove(@NonNull Key key) {
59+
Log.i("JS", "MemoryCache remove " + key);
5560
keyStrings.remove(key);
5661
return super.remove(key);
5762
}
5863

5964
@Override
6065
protected void onItemEvicted(@NonNull Key key, @Nullable Resource<?> item) {
6166
// Called by the LRU when an item is evicted; keep our index in sync.
67+
Log.i("JS", "MemoryCache onItemEvicted " + key + " " + getMaxSize());
6268
keyStrings.remove(key);
6369
super.onItemEvicted(key, item);
6470
}
@@ -76,6 +82,7 @@ protected void onItemEvicted(@NonNull Key key, @Nullable Resource<?> item) {
7682
public int removeByModelAndSignature(@NonNull Object model, @NonNull Key signature) {
7783
final String modelStr = model.toString();
7884
final String signatureStr = signature.toString();
85+
Log.i("JS", "ModelSignatureMemoryCache removeByModelAndSignature " + modelStr + " " + signatureStr + " " + keyStrings.size());
7986

8087
List<Key> toRemove = new ArrayList<>();
8188
for (Map.Entry<Key, String> e : keyStrings.entrySet()) {
@@ -129,8 +136,10 @@ public int removeByModelAndSignatureStrings(@NonNull String modelString, @NonNul
129136
public boolean containsByModelAndSignature(@NonNull Object model, @NonNull Key signature) {
130137
final String modelStr = model.toString();
131138
final String signatureStr = signature.toString();
139+
Log.i("JS", "ModelSignatureMemoryCache containsByModelAndSignature " + modelStr + " " + signatureStr + " " + keyStrings.size());
132140

133141
for (String s : keyStrings.values()) {
142+
Log.i("JS", "ModelSignatureMemoryCache containsByModelAndSignature1 " + s);
134143
if ((s.contains("model=" + modelStr) && s.contains("signature=" + signatureStr))
135144
|| (s.contains(modelStr) && s.contains(signatureStr))) {
136145
return true;

packages/image/platforms/android/java/com/nativescript/image/SharedPrefCacheKeyStore.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public SharedPrefCacheKeyStore(Context context) {
3232
public void put(String id, CacheKeyStore.StoredKeys keys) {
3333
try {
3434
JSONObject j = new JSONObject();
35+
Log.d("JS", "SharedPrefCacheKeyStore.put id=" + id + " sourceKey=" + keys.sourceKey);
3536

3637
// Serialize sourceKey properly - extract URL and headers
3738
if (keys.sourceKey instanceof GlideUrl) {
@@ -111,9 +112,19 @@ public CacheKeyStore.StoredKeys get(String id) {
111112
} else {
112113
sourceKey = new ObjectKey(id);
113114
}
114-
} else {
115+
} else{
115116
String source = j.optString("source", null);
116-
sourceKey = source != null && !"null".equals(source) ? new ObjectKey(source) : new ObjectKey(id);
117+
if (source != null && !"null".equals(source)) {
118+
// Extract inner value from "ObjectKey{object=value}" format
119+
String innerValue = source;
120+
if (innerValue.startsWith("ObjectKey{object=") && innerValue.endsWith("}")) {
121+
innerValue = innerValue.substring(17, innerValue.length() - 1);
122+
}
123+
sourceKey = new ObjectKey(innerValue);
124+
} else {
125+
sourceKey = new ObjectKey(id);
126+
}
127+
Log.d("JS", "SharedPrefCacheKeyStore.get id=" + id + " source=" + source + " sourceKey=" + sourceKey);
117128
}
118129

119130
String signatureStr = j.optString("signature", null);

src/image/index-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export abstract class ImageBase extends View {
324324

325325
mCanRequestImage = true;
326326
mNeedRequestImage = false;
327-
protected abstract initImage();
327+
protected abstract initImage(): Promise<void>;
328328
public onResumeNativeUpdates(): void {
329329
// {N} suspends properties update on `_suspendNativeUpdates`. So we only need to do this in onResumeNativeUpdates
330330
this.mCanRequestImage = false;

src/image/index.android.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ export class ImagePipeline {
9595
if (value instanceof android.net.Uri) {
9696
return value.toString();
9797
}
98-
return value;
98+
return getUri(value);
9999
}
100100

101-
getCacheKey(uri: string, context) {
102-
return uri;
101+
getCacheKey(uri: string | android.net.Uri) {
102+
return this.toUri(uri);
103103
}
104104

105105
isInDiskCache(uri: string | android.net.Uri): Promise<boolean> {
106-
const url = this.toUri(uri);
106+
const url = this.getCacheKey(uri);
107+
console.log('isInDiskCache', uri, url);
107108
return new Promise<boolean>((resolve, reject) => {
108109
com.nativescript.image.EvictionManager.get().isInDiskCacheAsync(
109110
url,
@@ -118,12 +119,14 @@ export class ImagePipeline {
118119

119120
isInBitmapMemoryCache(uri: string | android.net.Uri): boolean {
120121
// Still not directly accessible, but we can check if it's registered
121-
const url = this.toUri(uri);
122+
const url = this.getCacheKey(uri);
123+
console.log('isInBitmapMemoryCache', uri, url);
122124
return com.nativescript.image.EvictionManager.get().isInMemoryCache(url);
123125
}
124126

125127
evictFromMemoryCache(uri: string | android.net.Uri): Promise<void> {
126-
const url = this.toUri(uri);
128+
const url = this.getCacheKey(uri);
129+
console.log('evictFromMemoryCache', uri, url);
127130
return new Promise<void>((resolve, reject) => {
128131
com.nativescript.image.EvictionManager.get().evictMemoryForId(
129132
url,
@@ -144,7 +147,8 @@ export class ImagePipeline {
144147
}
145148

146149
evictFromDiskCache(uri: string | android.net.Uri): Promise<void> {
147-
const url = this.toUri(uri);
150+
const url = this.getCacheKey(uri);
151+
console.log('evictFromDiskCache', uri, url);
148152
return new Promise<void>((resolve, reject) => {
149153
com.nativescript.image.EvictionManager.get().evictDiskForId(
150154
url,
@@ -164,16 +168,15 @@ export class ImagePipeline {
164168
});
165169
}
166170

167-
evictFromCache(uri: string | android.net.Uri): Promise<void> {
168-
console.log('evictFromCache ', uri, new Error().stack);
169-
171+
async evictFromCache(uri: string | android.net.Uri): Promise<void> {
170172
const url = this.toUri(uri);
173+
console.log('evictFromCache ', uri, url, await this.isInDiskCache(url), this.isInBitmapMemoryCache(url));
171174
return new Promise<void>((resolve, reject) => {
172175
com.nativescript.image.EvictionManager.get().evictAllForId(
173176
url,
174177
new com.nativescript.image.EvictionManager.EvictionCallback({
175-
onComplete(success: boolean, error) {
176-
console.log('evictFromCache done', success, error);
178+
async onComplete(success: boolean, error) {
179+
console.log('evictFromCache done', uri, await this.isInDiskCache(uri), this.isInBitmapMemoryCache(uri), success, error);
177180
if (error) {
178181
if (Trace.isEnabled()) {
179182
CLog(CLogTypes.error, error);
@@ -256,6 +259,7 @@ export class ImagePipeline {
256259
private prefetchToCache(uri: string, toDiskCache: boolean, options?: PrefetchOptions): Promise<void> {
257260
return new Promise((resolve, reject) => {
258261
try {
262+
const url = this.toUri(uri);
259263
const context = Utils.android.getApplicationContext();
260264
const requestManager = com.bumptech.glide.Glide.with(context);
261265

@@ -277,14 +281,14 @@ export class ImagePipeline {
277281
}
278282

279283
loadModel = new com.nativescript.image.CustomGlideUrl(
280-
uri,
284+
url,
281285
headersMap,
282286
null, // progressCallback
283287
null // loadSourceCallback
284288
);
285289
} else {
286290
// For local files, use plain URI string
287-
loadModel = uri;
291+
loadModel = url;
288292
}
289293

290294
// Use the same model for both disk and memory to ensure key consistency
@@ -540,13 +544,6 @@ export class Img extends ImageBase {
540544
}
541545

542546
public async updateImageUri() {
543-
const imagePipeLine = getImagePipeline();
544-
const cacheKey = this.cacheKey;
545-
console.log('updateImageUri', await imagePipeLine.isInDiskCache(cacheKey), imagePipeLine.isInBitmapMemoryCache(cacheKey));
546-
if (cacheKey) {
547-
await imagePipeLine.evictFromCache(cacheKey);
548-
}
549-
console.log('updateImageUri 1', await imagePipeLine.isInDiskCache(cacheKey), imagePipeLine.isInBitmapMemoryCache(cacheKey));
550547
this.handleImageSrc(null);
551548
this.initImage();
552549
}
@@ -663,6 +660,7 @@ export class Img extends ImageBase {
663660
private loadImageWithGlide(uri: string) {
664661
const view = this.nativeViewProtected;
665662
const context = this._context;
663+
console.log('loadImageWithGlide', uri);
666664
// Cancel any prior Glide request/target for this view before starting a new one.
667665
this.cancelCurrentRequest();
668666
// Determine if this is a network request

src/image/index.ios.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,6 @@ export class Img extends ImageBase {
385385
}
386386

387387
public async updateImageUri() {
388-
const imagePipeLine = getImagePipeline();
389-
const src = this.src;
390-
const srcType = typeof src;
391-
if (src && (srcType === 'string' || src instanceof ImageAsset)) {
392-
// const isInCache = imagePipeLine.isInBitmapMemoryCache(cachekKey);
393-
// if (isInCache) {
394-
await imagePipeLine.evictFromCache(getUri(src as string | ImageAsset).absoluteString);
395-
// }
396-
}
397-
// this.src = null;
398-
// ensure we clear the image as
399388
this._setNativeImage(null, false);
400389
this.initImage();
401390
}

0 commit comments

Comments
 (0)