Skip to content

Commit 02fc293

Browse files
author
farfromrefug
committed
fix(android): allow to configure memory size
1 parent 2321ba1 commit 02fc293

File tree

7 files changed

+94
-36
lines changed

7 files changed

+94
-36
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,29 @@ public class CustomGlideModule extends AppGlideModule {
3232
@Override
3333
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
3434

35+
// Get configuration
36+
GlideConfiguration config = GlideConfiguration.getInstance();
37+
38+
// Determine memory cache size
39+
long memoryCacheSize;
40+
if (config.getMemoryCacheSize() > 0) {
41+
// Use custom size
42+
memoryCacheSize = config.getMemoryCacheSize();
43+
// Log.i(TAG, "Using custom memory cache size: " + memoryCacheSize + " bytes");
44+
} else {
45+
// Use default calculator
46+
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
47+
.setMemoryCacheScreens(config.getMemoryCacheScreens())
48+
.build();
49+
memoryCacheSize = calculator.getMemoryCacheSize();
50+
// Log.i(TAG, "Using calculated memory cache size: " + memoryCacheSize + " bytes with memoryCacheSceens: " + config.getMemoryCacheScreens());
51+
}
52+
3553
// Use our custom memory cache wrapper
36-
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
37-
.setMemoryCacheScreens(2)
38-
.build();
39-
LruResourceCache memoryCache = new LruResourceCache(calculator.getMemoryCacheSize());
54+
LruResourceCache memoryCache = new LruResourceCache(memoryCacheSize);
4055
EvictionManager.get().setMemoryCache(memoryCache);
4156
builder.setMemoryCache(memoryCache);
57+
4258
// Set a disk cache factory that also registers the disk cache instance with
4359
// EvictionManager.
4460
builder.setDiskCache(new DiskCache.Factory() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.nativescript.image;
2+
3+
/**
4+
* Singleton to hold Glide configuration values that must be set before Glide initializes.
5+
* Call GlideConfiguration.setMemoryCacheSize() from your Application.onCreate()
6+
* or before any Glide usage.
7+
*/
8+
public class GlideConfiguration {
9+
private static GlideConfiguration instance;
10+
11+
private long memoryCacheSize = -1; // -1 means use default
12+
private float memoryCacheScreens = 2.0f;
13+
14+
private GlideConfiguration() {
15+
}
16+
17+
public static synchronized GlideConfiguration getInstance() {
18+
if (instance == null) {
19+
instance = new GlideConfiguration();
20+
}
21+
return instance;
22+
}
23+
24+
/**
25+
* Set custom memory cache size in bytes.
26+
* Must be called before Glide is initialized (before first image load).
27+
* @param bytes Size in bytes, or -1 to use default calculator
28+
*/
29+
public void setMemoryCacheSize(long bytes) {
30+
this.memoryCacheSize = bytes;
31+
}
32+
33+
public long getMemoryCacheSize() {
34+
return memoryCacheSize;
35+
}
36+
37+
/**
38+
* Set memory cache screens multiplier (default 2.0).
39+
* Only used if memoryCacheSize is -1.
40+
*/
41+
public void setMemoryCacheScreens(float screens) {
42+
this.memoryCacheScreens = screens;
43+
}
44+
45+
public float getMemoryCacheScreens() {
46+
return memoryCacheScreens;
47+
}
48+
}

packages/image/platforms/android/native-api-usage.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"com.nativescript.image:CompositeRequestListener",
1515
"com.nativescript.image:MatrixDrawableImageViewTarget",
1616
"com.nativescript.image:MatrixImageView",
17+
"com.nativescript.image:GlideConfiguration",
1718
"com.bumptech.glide:Glide",
1819
"com.bumptech.glide.signature:ObjectKey",
1920
"com.bumptech.glide.request:RequestOptions",

src/image/index-common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,13 @@ export function wrapNativeException(ex, errorType = typeof ex) {
131131
}
132132

133133
export interface ImagePipelineConfigSetting {
134+
// android only, disk cache key store persists to use evictFromDiskCache between app launches
134135
usePersistentCacheKeyStore?: boolean;
135136
// android signature key for cache. You can bump/change (v1, v2,...) to invalidate all cache
136137
globalSignatureKey?: string;
138+
139+
memoryCacheSize?: number; // in bytes
140+
memoryCacheScreens?: number; // multiplier for auto-calculation
137141
}
138142

139143
export type Stretch = 'none' | 'fill' | 'aspectFill' | 'aspectFit';

src/image/index.android.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from './index-common';
22
import { Background, Color, ImageAsset, ImageSource, Trace, Utils, backgroundInternalProperty, knownFolders, path } from '@nativescript/core';
33
import { isString } from '@nativescript/core/utils/types';
4-
import { layout } from '@nativescript/core/utils/layout-helper';
54
import {
65
AnimatedImage,
76
CLog,
@@ -50,27 +49,25 @@ const globalSignatureKey = 'v1';
5049
export function initialize(config?: ImagePipelineConfigSetting): void {
5150
if (!initialized) {
5251
const context = Utils.android.getApplicationContext();
53-
if (!context) {
54-
return;
52+
const glideConfig = com.nativescript.image.GlideConfiguration.getInstance();
53+
if (config?.memoryCacheSize > 0) {
54+
glideConfig.setMemoryCacheSize(config.memoryCacheSize);
5555
}
56-
if (config?.usePersistentCacheKeyStore) {
57-
const sharedStore = new com.nativescript.image.SharedPrefCacheKeyStore(context.getApplicationContext());
58-
com.nativescript.image.EvictionManager.get().setPersistentStore(sharedStore);
56+
57+
if (config?.memoryCacheScreens > 0) {
58+
glideConfig.setMemoryCacheScreens(config.memoryCacheScreens);
5959
}
60-
// bumping `v1` will invalidate all cache
61-
signature = new com.bumptech.glide.signature.ObjectKey(config?.globalSignatureKey ?? globalSignatureKey);
6260

63-
initialized = true;
61+
signature = new com.bumptech.glide.signature.ObjectKey(config?.globalSignatureKey || globalSignatureKey);
62+
63+
// Now initialize Glide (which will read from GlideConfiguration)
6464
glideInstance = com.bumptech.glide.Glide.get(context);
65-
com.nativescript.image.EvictionManager.get().clearAll();
6665

6766
// this is needed for further buildKey to trigger ...
68-
com.bumptech.glide.Glide.with(context)
69-
.load('toto')
70-
.apply(new com.bumptech.glide.request.RequestOptions().signature(signature))
71-
.preload();
67+
com.bumptech.glide.Glide.with(context).load('toto').apply(new com.bumptech.glide.request.RequestOptions().signature(signature)).preload();
7268
// com.nativescript.image.EngineKeyFactoryMethodDumper.dumpKeyFactoryMethods(glideInstance);
7369
// com.nativescript.image.ForcePreloadTest.forcePreloadAfterInjection(context, 'https://example.com/test-image.png');
70+
initialized = true;
7471
}
7572
}
7673

@@ -235,10 +232,7 @@ export class ImagePipeline {
235232
try {
236233
const context = Utils.android.getApplicationContext();
237234
const requestManager = com.bumptech.glide.Glide.with(context);
238-
const requestBuilder = requestManager
239-
.asBitmap()
240-
.load(uri)
241-
.apply(new com.bumptech.glide.request.RequestOptions().signature(signature));
235+
const requestBuilder = requestManager.asBitmap().load(uri).apply(new com.bumptech.glide.request.RequestOptions().signature(signature));
242236

243237
let futureTarget = null;
244238
function clearLater() {
@@ -822,10 +816,7 @@ export class Img extends ImageBase {
822816
target.setClearFirst(false);
823817
}
824818

825-
requestBuilder
826-
.apply(ro)
827-
.listener(new com.nativescript.image.CompositeRequestListener(objectArr))
828-
.into(target);
819+
requestBuilder.apply(ro).listener(new com.nativescript.image.CompositeRequestListener(objectArr)).into(target);
829820
}
830821

831822
private notifyLoadSource(source: string) {

src/image/index.d.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ImageAsset, ImageSource, View } from '@nativescript/core';
2+
import { ImagePipelineConfigSetting } from './index-common';
23

3-
export * from './index-common.ts';
4+
export * from './index-common';
45

56
/**
67
* When called, initializes the android Image library. Calling this method is required.
@@ -375,15 +376,6 @@ export enum ScaleType {
375376
focusCrop
376377
}
377378

378-
/**
379-
* Advanced Configurations used for initializing Image
380-
*/
381-
export interface ImagePipelineConfigSetting {
382-
// android only, disk cache key store persists to use evictFromDiskCache between app launches
383-
usePersistentCacheKeyStore?: boolean;
384-
// android signature key for cache. You can bump/change (v1, v2,...) to invalidate all cache
385-
globalSignatureKey?: string;
386-
}
387379
export const ImageViewTraceCategory;
388380

389381
export type GetContextFromOptionsCallback = (context: NSDictionary<string, any>, transformers: any[], options: Partial<Img>) => void;

src/image/typings/ui_image.android.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ declare namespace com {
4545
}
4646
export namespace nativescript {
4747
export namespace image {
48+
export class GlideConfiguration {
49+
setMemoryCacheScreens(memoryCacheScreens: number);
50+
getMemoryCacheSize(): number;
51+
setMemoryCacheSize(memoryCacheSize: number);
52+
static getInstance():GlideConfiguration;
53+
}
4854
export class CacheKeyStore {
4955
public static class: java.lang.Class<com.nativescript.image.CacheKeyStore>;
5056
public put(id: string, keys: com.nativescript.image.CacheKeyStore.StoredKeys): void;

0 commit comments

Comments
 (0)