From bdb109b76666348a39aa337b5af1a3e938d282ca Mon Sep 17 00:00:00 2001 From: vtsybulin Date: Tue, 20 Aug 2019 17:08:43 +0300 Subject: [PATCH 1/2] Added optional fallback option to provide local image to be shown in case CacheManager fails to download the main image with, error response code different from 200. Fallback should be provided as a local asset module. --- package.json | 1 + src/Image.tsx | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9581181..307d164 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ }, "dependencies": { "crypto-js": "^3.1.9-1", + "expo-asset": "^6.0.0", "lodash": "^4.17.4" }, "peerDependencies": { diff --git a/src/Image.tsx b/src/Image.tsx index 3c7afe0..463db56 100644 --- a/src/Image.tsx +++ b/src/Image.tsx @@ -13,6 +13,7 @@ import { StyleProp } from "react-native"; import { BlurView } from "expo-blur"; +import { Asset } from 'expo-asset'; import CacheManager, { DownloadOptions } from "./CacheManager"; @@ -24,6 +25,7 @@ interface ImageProps { uri: string; transitionDuration?: number; tint?: "dark" | "light"; + fallback?: number; } interface ImageState { @@ -66,10 +68,13 @@ export default class Image extends React.Component { this.mounted = false; } - async load({ uri, options = {} }: ImageProps): Promise { + async load({ uri, options = {}, fallback }: ImageProps): Promise { if (uri) { - const path = await CacheManager.get(uri, options).getPath(); + let path = await CacheManager.get(uri, options).getPath(); if (this.mounted) { + if (!path && fallback && Number.isInteger(fallback)) { + path = Asset.fromModule(fallback).uri; + } this.setState({ uri: path }); } } From 4fe92ef8ef56463ca494cf3ae59aefb804893041 Mon Sep 17 00:00:00 2001 From: vtsybulin Date: Tue, 20 Aug 2019 17:31:56 +0300 Subject: [PATCH 2/2] Fixed linting issue --- src/Image.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Image.tsx b/src/Image.tsx index 463db56..84ad99d 100644 --- a/src/Image.tsx +++ b/src/Image.tsx @@ -13,7 +13,7 @@ import { StyleProp } from "react-native"; import { BlurView } from "expo-blur"; -import { Asset } from 'expo-asset'; +import { Asset } from "expo-asset"; import CacheManager, { DownloadOptions } from "./CacheManager";