forked from VideoSpike/nativescript-web-image-cache
-
Notifications
You must be signed in to change notification settings - Fork 2
/
web-image-cache.android.ts
217 lines (183 loc) · 6.67 KB
/
web-image-cache.android.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/// <reference path="./and-ts-lib/types.d.ts" />
import { WebImageCommon, srcProperty, isLoadingProperty } from './web-image-cache.common';
import { StretchMapping as stretchMap } from './and-ts-lib/stretch-mapping';
import { Helpers as helpers } from './and-ts-lib/helpers';
import * as application from 'tns-core-modules/application';
import * as appSettings from 'tns-core-modules/application-settings';
import { Property, booleanConverter } from 'tns-core-modules/ui/core/view';
let roundedProperty = new Property<WebImageCommon, boolean>({
name: "rounded",
defaultValue: false,
valueConverter: booleanConverter,
affectsLayout: true
}),
placeholderProperty = new Property<WebImageCommon, string>({
name: "placeholder",
defaultValue: undefined,
valueConverter: function(v) {
return v;
},
affectsLayout: true
}),
placeholderStretchProperty = new Property<WebImageCommon, string>({
name: "placeholderStretch",
defaultValue: stretchMap.get('none'),
valueConverter: function(v) {
return v;
},
affectsLayout: true
}),
stretchProperty = new Property<WebImageCommon, string>({
name: "stretch",
defaultValue: stretchMap.get('none'),
valueConverter: function(v) {
return v;
},
affectsLayout: true
});
srcProperty.register(WebImageCommon);
isLoadingProperty.register(WebImageCommon);
roundedProperty.register(WebImageCommon);
placeholderStretchProperty.register(WebImageCommon);
stretchProperty.register(WebImageCommon);
placeholderProperty.register(WebImageCommon);
export class WebImage extends WebImageCommon {
public rounded: boolean;
public placeholder: string;
public placeholderStretch: string;
constructor() {
super();
}
public get android(): any {
return this.nativeView;
}
public set android(view) {
this.nativeView = view;
}
createNativeView(): any {
let simpleDraweeView = new com.facebook.drawee.view.SimpleDraweeView(this._context);
simpleDraweeView.getHierarchy().setActualImageScaleType(com.facebook.drawee.drawable.ScalingUtils.ScaleType.CENTER);
if (undefined !== this.stretch) {
helpers.setNativeStretch(simpleDraweeView.getHierarchy(), this.stretch);
}
if (undefined !== this.rounded) {
helpers.setRounded(simpleDraweeView.getHierarchy(), this.rounded);
}
if (undefined !== this.placeholder) {
helpers.setPlaceholder(simpleDraweeView.getHierarchy(), this.placeholder, this.placeholderStretch);
}
return simpleDraweeView;
}
initNativeView() {
if (undefined !== this.src) {
helpers.setSource(this, this.src);
}
}
[roundedProperty.getDefault]() {
return false;
}
[roundedProperty.setNative](value) {
let simpleDraweeView = this.nativeView;
helpers.onRoundedPropertyChanged(simpleDraweeView, value);
}
[placeholderProperty.getDefault]() {
return undefined;
}
[placeholderProperty.setNative](value) {
let simpleDraweeView = this.nativeView;
helpers.onPlaceholderPropertyChanged(simpleDraweeView, value, this.placeholderStretch);
}
[placeholderStretchProperty.getDefault]() {
return "none";
}
[placeholderStretchProperty.setNative](value) {
let simpleDraweeView = this.nativeView;
helpers.setPlaceholder(simpleDraweeView.getHierarchy(), this.src, value);
}
[stretchProperty.getDefault]() {
return stretchMap.get('none');
}
[stretchProperty.setNative](value) {
let simpleDraweeView = this.nativeView;
helpers.onStretchPropertyChanged(simpleDraweeView, value);
}
[srcProperty.getDefault]() {
return undefined;
}
[srcProperty.setNative](value) {
helpers.onSrcPropertySet(this, value);
}
[isLoadingProperty.getDefault]() {
return "";
}
[isLoadingProperty.setNative](value) {
// do nothing
}
}
export function setCacheLimit(numberOfDays) {
let noOfSecondsInAMinute = 60,
noOfMinutesInAHour = 60,
noOfHoursInADay = 24,
noOfSecondsADay = noOfSecondsInAMinute * noOfMinutesInAHour * noOfHoursInADay,
noOfSecondsInDays = noOfSecondsADay * numberOfDays,
currentSeconds = Math.round(new Date().getTime() / 1000),
referenceTime = 0;
if (true === appSettings.getBoolean("isAppOpenedFirstTime") || undefined === appSettings.getBoolean("isAppOpenedFirstTime") || null === appSettings.getBoolean("isAppOpenedFirstTime")) {
appSettings.setBoolean("isAppOpenedFirstTime", false);
com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline().clearCaches();
appSettings.setNumber("cacheTimeReference", currentSeconds);
} else {
referenceTime = appSettings.getNumber("cacheTimeReference");
if (null === referenceTime || undefined === referenceTime) {
appSettings.setNumber("cacheTimeReference", currentSeconds);
} else if ((currentSeconds - referenceTime) > noOfSecondsInDays) {
com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline().clearCaches();
appSettings.setNumber("cacheTimeReference", currentSeconds);
}
}
}
export function initialize() {
com.facebook.drawee.backends.pipeline.Fresco.initialize(application.android.context);
}
export function clearCache() {
com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline().clearCaches();
}
export function initializeOnAngular() {
throw new Error("'initializeOnAngular' has been removed from 'nativescript-web-image-cache', see its readme for details!");
}
export function preFetchImage(urls: Array<string>) : Promise<void> {
return new Promise<void>((resolve, reject) => {
if (!urls || !Array.isArray(urls) || urls.length<1) {
reject(`preFetchImage: param should be array of urls`);
} else {
let counter:number=0;
urls.forEach((url) => {
const uri = android.net.Uri.parse(url);
const prefetchSubscriber = com.facebook.datasource.BaseDataSubscriber.extend({
onNewResultImpl(dataSource) {
counter++;
if (counter===urls.length) { resolve(); }
},
onFailureImpl(dataSource) {
counter++;
if (counter===urls.length) {
reject(`preFetchImage: failed to prefetch ${uri.toString()}`);
}
}
});
/*
prefetchToDiskCache() --> load to disk slower but less CPU
prefetchToBitmapCache --> load to memory cache, faster but more CPU
*/
let dataSource = com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline().prefetchToBitmapCache(
com.facebook.imagepipeline.request.ImageRequest.fromUri(uri),
application.android.context
);
dataSource.subscribe(
new prefetchSubscriber(),
com.facebook.common.executors.UiThreadImmediateExecutorService.getInstance()
);
});
}
});
}