forked from VideoSpike/nativescript-web-image-cache
-
Notifications
You must be signed in to change notification settings - Fork 2
/
web-image-cache.ios.ts
221 lines (173 loc) · 6.76 KB
/
web-image-cache.ios.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
218
219
220
221
/// <reference path='./ios-ts-lib/types.d.ts'/>
import { WebImageCommon, srcProperty, isLoadingProperty } from './web-image-cache.common';
import { Property } from 'tns-core-modules/ui/core/view';
import * as appSettings from 'tns-core-modules/application-settings';
import { Helpers as helpers } from './ios-ts-lib/helpers';
import * as enums from 'tns-core-modules/ui/enums';
declare class UIImageView {
public contentMode: any;
public clipsToBounds: any;
public userInteractionEnabled: any;
}
declare class UIViewContentMode {
static UIViewContentModeScaleToFill: any;
static UIViewContentModeTopLeft: any;
static UIViewContentModeScaleAspectFit: any;
static UIViewContentModeScaleAspectFill: any;
}
declare class SDWebImagePrefetcher {
static sharedImagePrefetcher: any;
}
let placeholderProperty = new Property<WebImageCommon, string>({
name: "placeholder",
defaultValue: undefined,
affectsLayout: true
}),
stretchProperty = new Property<WebImageCommon, string>({
name: "stretch",
defaultValue: "none",
affectsLayout: true
});
srcProperty.register(WebImageCommon);
isLoadingProperty.register(WebImageCommon);
placeholderProperty.register(WebImageCommon);
stretchProperty.register(WebImageCommon);
export class WebImage extends WebImageCommon {
constructor() {
super();
}
createNativeView(): any {
let imageView = new UIImageView();
imageView.contentMode = UIViewContentMode.UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = true;
imageView.userInteractionEnabled = true;
return imageView;
}
public get ios(): any {
return this.nativeView;
}
public set ios(view) {
this.nativeView = view;
}
onMeasure(widthMeasureSpec, heightMeasureSpec) {
let utils = require("utils/utils"),
width = utils.layout.getMeasureSpecSize(widthMeasureSpec),
widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec),
height = utils.layout.getMeasureSpecSize(heightMeasureSpec),
heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec),
nativeWidth = this.nativeView ? (this.nativeView.image ? this.nativeView.image.size.width : 0) : 0,
nativeHeight = this.nativeView ? (this.nativeView.image ? this.nativeView.image.size.height : 0) : 0,
measureWidth = Math.max(nativeWidth, this.minWidth as number),
measureHeight = Math.max(nativeHeight, this.minHeight as number),
finiteWidth = widthMode !== utils.layout.UNSPECIFIED,
finiteHeight = heightMode !== utils.layout.UNSPECIFIED;
if (nativeWidth !== 0 && nativeHeight !== 0 && (finiteWidth || finiteHeight)) {
let scale = this.computeScaleFactor(width, height, finiteWidth, finiteHeight, nativeWidth, nativeHeight, this.stretch),
resultW = Math.floor(nativeWidth * scale.width),
resultH = Math.floor(nativeHeight * scale.height);
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;
let trace = require("trace");
trace.write("Image stretch: " + this.stretch +
", nativeWidth: " + nativeWidth +
", nativeHeight: " + nativeHeight, trace.categories.Layout);
}
let view = require("ui/core/view");
let widthAndState = view.View.resolveSizeAndState(measureWidth, width, widthMode, 0);
let heightAndState = view.View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
computeScaleFactor(measureWidth, measureHeight, widthIsFinite, heightIsFinite, nativeWidth, nativeHeight, imageStretch) {
let scaleW = 1,
scaleH = 1;
if ((imageStretch === enums.Stretch.aspectFill || imageStretch === enums.Stretch.aspectFit || imageStretch === enums.Stretch.fill) &&
(widthIsFinite || heightIsFinite)) {
scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0;
scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0;
if (!widthIsFinite) {
scaleW = scaleH;
} else if (!heightIsFinite) {
scaleH = scaleW;
} else {
switch (imageStretch) {
case enums.Stretch.aspectFit:
scaleH = scaleW < scaleH ? scaleW : scaleH;
scaleW = scaleH;
break;
case enums.Stretch.aspectFill:
scaleH = scaleW > scaleH ? scaleW : scaleH;
scaleW = scaleH;
break;
}
}
}
return {
width: scaleW,
height: scaleH
};
}
[placeholderProperty.getDefault]() {
return undefined;
}
[placeholderProperty.setNative](value) {
// do nothing
}
[stretchProperty.getDefault]() {
return "none";
}
[stretchProperty.setNative](value) {
helpers.onStretchPropertyChanged(this.nativeView, value);
}
[isLoadingProperty.getDefault]() {
return false;
}
[isLoadingProperty.setNative](value) {
// do nothing
}
[srcProperty.getDefault]() {
}
[srcProperty.setNative](value) {
helpers.onSrcPropertySet(this, value);
}
}
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);
this.clearCache();
appSettings.setNumber("cacheTimeReference", currentSeconds);
} else {
referenceTime = appSettings.getNumber("cacheTimeReference");
if (null == referenceTime || undefined === referenceTime) {
appSettings.setNumber("cacheTimeReference", currentSeconds);
} else if ((currentSeconds - referenceTime) > noOfSecondsInDays) {
this.clearCache();
appSettings.setNumber("cacheTimeReference", currentSeconds);
}
}
}
export function clearCache() {
let imageCache = SDImageCache.sharedImageCache();
imageCache.clearMemory();
imageCache.clearDiskOnCompletion(() => {});
}
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 {
SDWebImagePrefetcher.sharedImagePrefetcher.prefetchURLsProgressCompleted(urls, null, (finishedCount, skippedCount) => {
resolve();
});
}
});
}