@nativescript-community/ui-image 4.4.0 → 4.5.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [4.5.0](https://github.com/nativescript-community/ui-image/compare/v4.4.0...v4.5.0) (2024-04-23)
7
+
8
+ ### Features
9
+
10
+ * **image:** headers property support. Thanks to @Logikgate ([8704ee7](https://github.com/nativescript-community/ui-image/commit/8704ee7dcd17518f1e330b57103eab23b514aac2))
11
+
6
12
  ## [4.4.0](https://github.com/nativescript-community/ui-image/compare/v4.3.41...v4.4.0) (2024-04-23)
7
13
 
8
14
  ### Features
package/README.md CHANGED
@@ -439,6 +439,13 @@ Number value used as the downsampled width of the imageModule drawable.
439
439
  <@nativescript-community/ui-image:Img decodeHeight="100"/>
440
440
  ```
441
441
 
442
+ - **headers**
443
+
444
+ Object that defines custom request headers to be sent with the image download request.
445
+
446
+ ```xml
447
+ <@nativescript-community/ui-image:Img headers="{Authorization: 'bearer abcdefghijk'}"/>
448
+ ```
442
449
 
443
450
  - **progressiveRenderingEnabled**
444
451
 
package/blueprint.md CHANGED
@@ -372,6 +372,13 @@ Number value used as the downsampled width of the imageModule drawable.
372
372
  <@nativescript-community/ui-image:Img decodeHeight="100"/>
373
373
  ```
374
374
 
375
+ - **headers**
376
+
377
+ Object that defines custom request headers to be sent with the image download request.
378
+
379
+ ```xml
380
+ <@nativescript-community/ui-image:Img headers="{Authorization: 'bearer abcdefghijk'}"/>
381
+ ```
375
382
 
376
383
  - **progressiveRenderingEnabled**
377
384
 
package/index-common.d.ts CHANGED
@@ -56,6 +56,7 @@ export declare class EventData implements IEventData {
56
56
  }
57
57
  export type Stretch = 'none' | 'fill' | 'aspectFill' | 'aspectFit';
58
58
  export declare const srcProperty: Property<ImageBase, string | ImageSource | ImageAsset>;
59
+ export declare const headersProperty: Property<ImageBase, Record<string, string>>;
59
60
  export declare const lowerResSrcProperty: Property<ImageBase, string>;
60
61
  export declare const placeholderImageUriProperty: Property<ImageBase, string>;
61
62
  export declare const failureImageUriProperty: Property<ImageBase, string>;
@@ -106,7 +107,7 @@ export declare abstract class ImageBase extends View {
106
107
  progressiveRenderingEnabled: boolean;
107
108
  localThumbnailPreviewsEnabled: boolean;
108
109
  showProgressBar: boolean;
109
- progressBarColor: string;
110
+ progressBarColor: Color;
110
111
  roundAsCircle: boolean;
111
112
  roundBottomRightRadius: number;
112
113
  roundTopLeftRadius: number;
@@ -124,6 +125,7 @@ export declare abstract class ImageBase extends View {
124
125
  alwaysFade: boolean;
125
126
  noCache: boolean;
126
127
  tintColor: Color;
128
+ headers: Record<string, string>;
127
129
  readonly isLoading: boolean;
128
130
  get nativeImageViewProtected(): any;
129
131
  mCanRequestImage: boolean;
package/index-common.js CHANGED
@@ -118,6 +118,7 @@ export class EventData {
118
118
  }
119
119
  }
120
120
  export const srcProperty = new Property({ name: 'src' });
121
+ export const headersProperty = new Property({ name: 'headers' });
121
122
  export const lowerResSrcProperty = new Property({ name: 'lowerResSrc' });
122
123
  export const placeholderImageUriProperty = new Property({ name: 'placeholderImageUri' });
123
124
  export const failureImageUriProperty = new Property({ name: 'failureImageUri' });
@@ -309,6 +310,7 @@ ImageBase.intermediateImageSetEvent = 'intermediateImageSet';
309
310
  ImageBase.releaseEvent = 'release';
310
311
  ImageBase.submitEvent = 'submit';
311
312
  srcProperty.register(ImageBase);
313
+ headersProperty.register(ImageBase);
312
314
  lowerResSrcProperty.register(ImageBase);
313
315
  placeholderImageUriProperty.register(ImageBase);
314
316
  failureImageUriProperty.register(ImageBase);
package/index.android.js CHANGED
@@ -1,9 +1,9 @@
1
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
1
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
2
2
  export * from './index-common';
3
3
  import { Application, ImageAsset, ImageSource, Trace, Utils, backgroundInternalProperty, knownFolders, path } from '@nativescript/core';
4
4
  import { isString } from '@nativescript/core/utils/types';
5
5
  import { layout } from '@nativescript/core/utils/layout-helper';
6
- import { CLog, CLogTypes, EventData, ImageBase, ScaleType, aspectRatioProperty, backgroundUriProperty, blurDownSamplingProperty, blurRadiusProperty, fadeDurationProperty, failureImageUriProperty, imageRotationProperty, lowerResSrcProperty, needRequestImage, placeholderImageUriProperty, progressBarColorProperty, roundAsCircleProperty, roundBottomLeftRadiusProperty, roundBottomRightRadiusProperty, roundTopLeftRadiusProperty, roundTopRightRadiusProperty, showProgressBarProperty, srcProperty, stretchProperty, tintColorProperty, wrapNativeException } from './index-common';
6
+ import { CLog, CLogTypes, EventData, ImageBase, ScaleType, aspectRatioProperty, backgroundUriProperty, blurDownSamplingProperty, blurRadiusProperty, fadeDurationProperty, failureImageUriProperty, headersProperty, imageRotationProperty, lowerResSrcProperty, needRequestImage, placeholderImageUriProperty, progressBarColorProperty, roundAsCircleProperty, roundBottomLeftRadiusProperty, roundBottomRightRadiusProperty, roundTopLeftRadiusProperty, roundTopRightRadiusProperty, showProgressBarProperty, srcProperty, stretchProperty, tintColorProperty, wrapNativeException } from './index-common';
7
7
  let initialized = false;
8
8
  let initializeConfig;
9
9
  export function initialize(config) {
@@ -14,23 +14,25 @@ export function initialize(config) {
14
14
  return;
15
15
  }
16
16
  let builder;
17
- const useOkhttp = config?.useOkhttp;
17
+ const useOkhttp = config?.useOkhttp !== false;
18
18
  if (useOkhttp) {
19
+ //@ts-ignore
20
+ let client;
19
21
  //@ts-ignore
20
22
  if (useOkhttp instanceof okhttp3.OkHttpClient) {
21
- builder = com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory.newBuilder(context, useOkhttp);
23
+ client = useOkhttp;
22
24
  }
23
25
  else {
24
26
  //@ts-ignore
25
- builder = com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory.newBuilder(context, new okhttp3.OkHttpClient());
27
+ client = new okhttp3.OkHttpClient();
26
28
  }
29
+ builder = com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory.newBuilder(context, client);
30
+ builder.setNetworkFetcher(new com.nativescript.image.OkHttpNetworkFetcher(client));
27
31
  }
28
32
  else {
29
33
  builder = com.facebook.imagepipeline.core.ImagePipelineConfig.newBuilder(context);
30
34
  }
31
- if (config?.isDownsampleEnabled) {
32
- builder.setDownsampleEnabled(true);
33
- }
35
+ builder.setDownsampleEnabled(config?.isDownsampleEnabled === true);
34
36
  if (config?.leakTracker) {
35
37
  builder.setCloseableReferenceLeakTracker(config.leakTracker);
36
38
  }
@@ -424,6 +426,9 @@ export class Img extends ImageBase {
424
426
  [_u = aspectRatioProperty.setNative]() {
425
427
  this.initImage();
426
428
  }
429
+ [_v = headersProperty.setNative](value) {
430
+ this.initImage();
431
+ }
427
432
  [backgroundInternalProperty.setNative](value) {
428
433
  super[backgroundInternalProperty.setNative](value);
429
434
  this.nativeViewProtected.setClipToOutline(value?.hasBorderRadius());
@@ -586,7 +591,8 @@ export class Img extends ImageBase {
586
591
  lowerResSrc: this.lowerResSrc ? getUri(this.lowerResSrc, false) : undefined,
587
592
  blurDownSampling: this.blurDownSampling,
588
593
  autoPlayAnimations: this.autoPlayAnimations,
589
- tapToRetryEnabled: this.tapToRetryEnabled
594
+ tapToRetryEnabled: this.tapToRetryEnabled,
595
+ headers: this.headers
590
596
  });
591
597
  view.setUri(uri, options, this.controllerListener);
592
598
  // const async = this.loadMode === 'async';
@@ -673,7 +679,7 @@ export class Img extends ImageBase {
673
679
  builder.setBackground(backgroundDrawable);
674
680
  }
675
681
  if (this.showProgressBar) {
676
- builder.setProgressBarImage(this.progressBarColor, this.stretch);
682
+ builder.setProgressBarImage(this.progressBarColor?.hex, this.stretch);
677
683
  }
678
684
  if (this.roundAsCircle) {
679
685
  builder.setRoundingParamsAsCircle();
@@ -813,6 +819,9 @@ __decorate([
813
819
  __decorate([
814
820
  needRequestImage
815
821
  ], Img.prototype, _u, null);
822
+ __decorate([
823
+ needRequestImage
824
+ ], Img.prototype, _v, null);
816
825
  class GenericDraweeHierarchyBuilder {
817
826
  constructor() {
818
827
  const res = Utils.android.getApplicationContext().getResources();
package/index.d.ts CHANGED
@@ -194,6 +194,11 @@ export class Img extends View {
194
194
  */
195
195
  decodeHeight: number;
196
196
 
197
+ /**
198
+ * add custom headers to the image download request
199
+ */
200
+ headers: Record<string, string>;
201
+
197
202
  /**
198
203
  * IOS: if you want to show animated images you need to set this to true
199
204
  */
package/index.ios.js CHANGED
@@ -1,9 +1,9 @@
1
- var _a, _b, _c, _d;
1
+ var _a, _b, _c, _d, _e;
2
2
  export * from './index-common';
3
- import { Color, ImageAsset, ImageSource, Screen, Trace, Utils, knownFolders, path } from '@nativescript/core';
3
+ import { ImageAsset, ImageSource, Screen, Trace, Utils, knownFolders, path } from '@nativescript/core';
4
4
  import { layout } from '@nativescript/core/utils/layout-helper';
5
5
  import { isString } from '@nativescript/core/utils/types';
6
- import { CLog, CLogTypes, ImageBase, ScaleType, failureImageUriProperty, imageRotationProperty, placeholderImageUriProperty, progressBarColorProperty, showProgressBarProperty, srcProperty, stretchProperty, wrapNativeException } from './index-common';
6
+ import { CLog, CLogTypes, ImageBase, ScaleType, failureImageUriProperty, headersProperty, imageRotationProperty, placeholderImageUriProperty, progressBarColorProperty, showProgressBarProperty, srcProperty, stretchProperty, wrapNativeException } from './index-common';
7
7
  export class ImageInfo {
8
8
  constructor(width, height) {
9
9
  this.width = width;
@@ -456,12 +456,22 @@ export class Img extends ImageBase {
456
456
  context.setValueForKey(value, k);
457
457
  });
458
458
  }
459
+ if (this.headers) {
460
+ const requestModifier = SDWebImageDownloaderRequestModifier.requestModifierWithBlock((request) => {
461
+ const newRequest = request.mutableCopy();
462
+ Object.keys(this.headers).forEach((k) => {
463
+ newRequest.addValueForHTTPHeaderField(this.headers[k], k);
464
+ });
465
+ return newRequest.copy();
466
+ });
467
+ context.setValueForKey(requestModifier, SDWebImageContextDownloadRequestModifier);
468
+ }
459
469
  this.mCacheKey = SDWebImageManager.sharedManager.cacheKeyForURLContext(uri, context);
460
470
  if (this.showProgressBar) {
461
471
  try {
462
- if (this.progressBarColor && Color.isValid(this.progressBarColor)) {
472
+ if (this.progressBarColor) {
463
473
  const indicator = new SDWebImageActivityIndicator();
464
- indicator.indicatorView.color = new Color(this.progressBarColor).ios;
474
+ indicator.indicatorView.color = this.progressBarColor.ios;
465
475
  this.nativeImageViewProtected.sd_imageIndicator = indicator;
466
476
  }
467
477
  else {
@@ -495,6 +505,8 @@ export class Img extends ImageBase {
495
505
  [progressBarColorProperty.setNative](value) {
496
506
  this.progressBarColor = value;
497
507
  }
508
+ [_e = headersProperty.setNative](value) {
509
+ }
498
510
  [failureImageUriProperty.setNative]() {
499
511
  // this.updateHierarchy();
500
512
  }
@@ -536,4 +548,7 @@ __decorate([
536
548
  __decorate([
537
549
  needRequestImage
538
550
  ], Img.prototype, _d, null);
551
+ __decorate([
552
+ needRequestImage
553
+ ], Img.prototype, _e, null);
539
554
  //# sourceMappingURL=index.ios.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativescript-community/ui-image",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "description": "Advanced and efficient image display plugin which uses Fresco (Android) and SDWebImage (iOS) to implement caching, placeholders, image effects, and much more.",
5
5
  "main": "./index",
6
6
  "sideEffects": false,
@@ -36,5 +36,5 @@
36
36
  },
37
37
  "license": "Apache-2.0",
38
38
  "readmeFilename": "README.md",
39
- "gitHead": "532c0a394b6a40a8168483f729d831707b8d50c4"
39
+ "gitHead": "c9982accf6b1ecbd0fcdfd298f70152b1e65f4c3"
40
40
  }
@@ -9,6 +9,7 @@ dependencies {
9
9
  implementation("com.facebook.fresco:imagepipeline-okhttp3:$frescoVersion") {
10
10
  exclude group: 'com.facebook.soloader', module: 'soloader'
11
11
  }
12
+ implementation("com.facebook.fresco:middleware:$frescoVersion")
12
13
  implementation("com.facebook.fresco:nativeimagetranscoder:$frescoVersion")
13
14
  implementation 'com.facebook.infer.annotation:infer-annotation:0.18.0'
14
15
  // implementation ("com.facebook.fresco:animated-gif:$frescoVersion") {
@@ -272,10 +272,12 @@ public class DraweeView extends SimpleDraweeView {
272
272
  public void setUri(android.net.Uri uri, String jsonOptions, com.facebook.drawee.controller.ControllerListener listener) {
273
273
  ImageRequestBuilder requestBuilder = ImageRequestBuilder.newBuilderWithSource(uri).setRotationOptions( com.facebook.imagepipeline.common.RotationOptions.autoRotate());
274
274
  JSONObject object = null;
275
+ JSONObject headers = null;
275
276
  if (jsonOptions.length() > 2) {
276
277
  try {
277
278
  object = new JSONObject(jsonOptions);
278
- } catch (Exception ignored) {
279
+ } catch (Exception e) {
280
+ e.printStackTrace();
279
281
  }
280
282
  }
281
283
  if (object != null) {
@@ -296,9 +298,9 @@ public class DraweeView extends SimpleDraweeView {
296
298
  int blurDownSampling = object.optInt("blurDownSampling", 1);
297
299
  requestBuilder = requestBuilder.setPostprocessor(new com.nativescript.image.ScalingBlurPostprocessor(2, blurRadius, blurDownSampling));
298
300
  }
301
+ headers = object.optJSONObject("headers");
299
302
  }
300
-
301
- ImageRequest request = requestBuilder.build();
303
+ ImageRequest request = NetworkImageRequest.fromBuilderWithHeaders(requestBuilder, headers);
302
304
 
303
305
  // if (object != null && object.optBoolean("async") == false) {
304
306
  // DataSource<CloseableReference<CloseableImage>> dataSource =
@@ -0,0 +1,55 @@
1
+ package com.nativescript.image;
2
+
3
+ import com.facebook.imagepipeline.request.ImageRequest;
4
+ import com.facebook.imagepipeline.request.ImageRequestBuilder;
5
+
6
+ import org.json.JSONObject;
7
+ import org.json.JSONException;
8
+
9
+ import java.util.HashMap;
10
+ import java.util.Map;
11
+ import java.util.Iterator;
12
+
13
+ import android.util.Log;
14
+
15
+ /** Extended ImageRequest with request headers */
16
+ public class NetworkImageRequest extends ImageRequest {
17
+
18
+ /** Headers for the request */
19
+ private Map<String, String> mHeaders = null;
20
+
21
+ static Map<String, String> toMap(JSONObject object) throws JSONException {
22
+ Map<String, String> map = new HashMap<String, String>();
23
+
24
+ Iterator<String> keysItr = object.keys();
25
+ while(keysItr.hasNext()) {
26
+ String key = keysItr.next();
27
+ Object value = object.get(key);
28
+
29
+ if(value instanceof String) {
30
+ map.put(key, (String)value);
31
+ }
32
+ }
33
+ return map;
34
+ }
35
+
36
+ public static NetworkImageRequest fromBuilderWithHeaders(
37
+ ImageRequestBuilder builder, JSONObject headers) {
38
+ return new NetworkImageRequest(builder, headers);
39
+ }
40
+
41
+ protected NetworkImageRequest(ImageRequestBuilder builder, JSONObject headers) {
42
+ super(builder);
43
+ if (headers != null) {
44
+ try {
45
+ mHeaders = toMap(headers);
46
+ } catch (Exception e) {
47
+ e.printStackTrace();
48
+ }
49
+ }
50
+ }
51
+
52
+ public Map<String, String> getHeaders() {
53
+ return mHeaders;
54
+ }
55
+ }
@@ -0,0 +1,56 @@
1
+ package com.nativescript.image;
2
+
3
+ import android.net.Uri;
4
+ import android.os.SystemClock;
5
+ import android.util.Log;
6
+ import com.facebook.imagepipeline.producers.NetworkFetcher;
7
+ import java.util.Collections;
8
+ import java.util.HashMap;
9
+ import java.util.Map;
10
+ import java.util.concurrent.Executor;
11
+ import okhttp3.CacheControl;
12
+ import okhttp3.Headers;
13
+ import okhttp3.OkHttpClient;
14
+ import okhttp3.Request;
15
+
16
+ class OkHttpNetworkFetcher extends com.facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher {
17
+
18
+ private static final String TAG = "OkHttpNetworkFetcher";
19
+
20
+ private final OkHttpClient mOkHttpClient;
21
+ private final Executor mCancellationExecutor;
22
+
23
+ /**
24
+ * @param okHttpClient client to use
25
+ */
26
+ public OkHttpNetworkFetcher(OkHttpClient okHttpClient) {
27
+ super(okHttpClient);
28
+ mOkHttpClient = okHttpClient;
29
+ mCancellationExecutor = okHttpClient.dispatcher().executorService();
30
+ }
31
+
32
+ @Override
33
+ public void fetch(
34
+ final OkHttpNetworkFetcher.OkHttpNetworkFetchState fetchState,
35
+ final NetworkFetcher.Callback callback) {
36
+ fetchState.submitTime = SystemClock.elapsedRealtime();
37
+ final Uri uri = fetchState.getUri();
38
+ Map<String, String> requestHeaders = null;
39
+ if (fetchState.getContext().getImageRequest() instanceof NetworkImageRequest) {
40
+ NetworkImageRequest networkImageRequest =
41
+ (NetworkImageRequest) fetchState.getContext().getImageRequest();
42
+ requestHeaders = networkImageRequest.getHeaders();
43
+ }
44
+ if (requestHeaders == null) {
45
+ requestHeaders = Collections.emptyMap();
46
+ }
47
+ final Request request =
48
+ new Request.Builder()
49
+ .url(uri.toString())
50
+ .headers(Headers.of(requestHeaders))
51
+ .get()
52
+ .build();
53
+
54
+ fetchWithRequest(fetchState, callback, request);
55
+ }
56
+ }
@@ -27,7 +27,11 @@
27
27
  "com.facebook.drawee.backends.pipeline.info:ImagePerfData",
28
28
  "com.facebook.drawee.controller:ControllerListener",
29
29
  "com.nativescript.image:DraweeView",
30
+ "com.nativescript.image:OkHttpNetworkFetcher",
30
31
  "com.nativescript.image:ScalingBlurPostprocessor",
32
+ "com.facebook.imagepipeline.producers:NetworkFetcher",
33
+ "com.facebook.imagepipeline.backends.okhttp3:OkHttpImagePipelineConfigFactory",
34
+ "okhttp3:OkHttpClient",
31
35
  "android.graphics.drawable:Animatable",
32
36
  "com.facebook.imagepipeline.image:ImageInfo",
33
37
  "com.facebook.common.util:UriUtil",
Binary file
@@ -20,6 +20,9 @@ declare namespace com {
20
20
  class BaseDataSubscriber extends facebook.datasource.BaseDataSubscriber<any> {
21
21
  public constructor(listener: BaseDataSubscriberListener);
22
22
  }
23
+ class OkHttpNetworkFetcher extends facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher {
24
+
25
+ }
23
26
  }
24
27
  }
25
28
  }